K8S Canal基於Prometheus進行實時指標監控

itread01 2020-11-06 20:52:00
k8s 技术开发 canal Prometheus itread01


[文章來源於本人的印象筆記,如出現格式問題可訪問該連結檢視原文](https://app.yinxiang.com/fx/c1c8b0bd-1d81-4b3b-ac75-e05bd0955e6f) ## 部署canal的prometheus監控到k8s中 1、grafana的docker部署方式;https://grafana.com/grafana/download?platform=docker 2、prometheus的docker部署方式: https://github.com/prometheus/prometheus 有了現成的docker映象後,直接部署即可; ## k8s中部署prometheus yml編排檔案如下: ``` --- apiVersion: apps/v1 kind: Deployment metadata: annotations: deployment.kubernetes.io/revision: '16' k8s.kuboard.cn/displayName: canal-prometheus k8s.kuboard.cn/ingress: 'false' k8s.kuboard.cn/service: NodePort k8s.kuboard.cn/workload: svc-canal-prometheus creationTimestamp: '2020-11-06T03:09:55Z' generation: 16 labels: k8s.kuboard.cn/layer: svc k8s.kuboard.cn/name: svc-canal-prometheus name: svc-canal-prometheus namespace: canal-ns resourceVersion: '22246892' selfLink: /apis/apps/v1/namespaces/canal-ns/deployments/svc-canal-prometheus uid: 4ad37eec-3b36-4107-8ed9-07456abba5ba spec: progressDeadlineSeconds: 600 replicas: 1 revisionHistoryLimit: 10 selector: matchLabels: k8s.kuboard.cn/layer: svc k8s.kuboard.cn/name: svc-canal-prometheus strategy: rollingUpdate: maxSurge: 25% maxUnavailable: 25% type: RollingUpdate template: metadata: annotations: kubectl.kubernetes.io/restartedAt: '2020-11-06T14:07:33+08:00' labels: k8s.kuboard.cn/layer: svc k8s.kuboard.cn/name: svc-canal-prometheus spec: containers: - image: prom/prometheus imagePullPolicy: Always name: canal-prometheus terminationMessagePath: /dev/termination-log terminationMessagePolicy: File volumeMounts: - mountPath: /etc/prometheus/prometheus.yml name: canal-prometheus-volume subPath: etc/prometheus/prometheus.yml dnsPolicy: ClusterFirst restartPolicy: Always schedulerName: default-scheduler terminationGracePeriodSeconds: 30 volumes: - configMap: defaultMode: 420 items: - key: prometheus.yml path: etc/prometheus/prometheus.yml name: canal-prometheus name: canal-prometheus-volume status: availableReplicas: 1 conditions: - lastTransitionTime: '2020-11-06T05:30:16Z' lastUpdateTime: '2020-11-06T05:30:16Z' message: Deployment has minimum availability. reason: MinimumReplicasAvailable status: 'True' type: Available - lastTransitionTime: '2020-11-06T05:55:49Z' lastUpdateTime: '2020-11-06T06:07:43Z' message: >- ReplicaSet "svc-canal-prometheus-6f7d7b66c5" has successfully progressed. reason: NewReplicaSetAvailable status: 'True' type: Progressing observedGeneration: 16 readyReplicas: 1 replicas: 1 updatedReplicas: 1 ``` 此處有一個有趣的點是,k8s中使用configMap進行目錄掛載時,一般情況下我們直接掛載到對應的容器目錄後,此時目錄將會被覆蓋,而此時在配置普羅米修斯的檔案對映時,則只是掛載到了具體的檔案中;主要的配置則是:subPath: etc/prometheus/prometheus.yml,通過subPath的方式可以直接掛載到具體的檔案中; [原創宣告:作者:Arnold.zhao 部落格園地址:https://www.cnblogs.com/zh94](https://www.cnblogs.com/zh94) 所掛載的具體配置檔案的內容如下: ``` # my global config test global: scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute. evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute. # scrape_timeout is set to the global default (10s). # Alertmanager configuration alerting: alertmanagers: - static_configs: - targets: # - alertmanager:9093 # Load rules once and periodically evaluate them according to the global 'evaluation_interval'. rule_files: # - "first_rules.yml" # - "second_rules.yml" # A scrape configuration containing exactly one endpoint to scrape: # Here it's Prometheus itself. scrape_configs: # The job name is added as a label `job= ` to any timeseries scraped from this config. - job_name: 'canal' # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ['svc-canal-deployer:11112'] ``` 主要是配置下對應的canal-deployer的地址即可; - targets: ['svc-canal-deployer:11112'] 預設情況下canal-deployer的監控埠就是11112,當然如果你修改過該埠,另當別論了 。 svc-canal-deployer是canal-deployer的server名稱,由於canal-deployer此處也是已經部署在k8s了所以直接使用服務名進行訪問,由k8s service自動做轉發即可,如果此處不是在k8s的話,則直接配置canal-deployer的ip地址即可,一樣的。 對應的configMap的建立yml如下: ``` --- apiVersion: v1 data: prometheus.yml: >- # my global config test global: scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute. evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute. # scrape_timeout is set to the global default (10s). # Alertmanager configuration alerting: alertmanagers: - static_configs: - targets: # - alertmanager:9093 # Load rules once and periodically evaluate them according to the global 'evaluation_interval'. rule_files: # - "first_rules.yml" # - "second_rules.yml" # A scrape configuration containing exactly one endpoint to scrape: # Here it's Prometheus itself. scrape_configs: # The job name is added as a label `job= ` to any timeseries scraped from this config. - job_name: 'canal' # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ['svc-canal-deployer:11112'] kind: ConfigMap metadata: creationTimestamp: '2020-11-06T03:15:04Z' name: canal-prometheus namespace: canal-ns resourceVersion: '22246778' selfLink: /api/v1/namespaces/canal-ns/configmaps/canal-prometheus uid: 2918cb4e-acd6-4c82-9a2e-19e575ba6cea ``` 建立一個專用的Service進行埠對映,此處對映埠為9090 ``` --- apiVersion: v1 kind: Service metadata: annotations: k8s.kuboard.cn/displayName: canal-prometheus k8s.kuboard.cn/workload: svc-canal-prometheus creationTimestamp: '2020-11-06T03:11:34Z' labels: k8s.kuboard.cn/layer: svc k8s.kuboard.cn/name: svc-canal-prometheus name: svc-canal-prometheus namespace: canal-ns resourceVersion: '22204370' selfLink: /api/v1/namespaces/canal-ns/services/svc-canal-prometheus uid: 6246dafe-f8fd-42ec-8e27-caf38539d35c spec: clusterIP: 10.204.71.228 externalTrafficPolicy: Cluster ports: - name: prometheus-9090 nodePort: 30018 port: 9090 protocol: TCP targetPort: 9090 selector: k8s.kuboard.cn/layer: svc k8s.kuboard.cn/name: svc-canal-prometheus sessionAffinity: None type: NodePort ``` 好了,部署完成,此時訪問埠結果如下: ![](https://img2020.cnblogs.com/blog/1104472/202011/1104472-20201106162822540-2081171840.png) ## 部署grafana Deployment的yml如下 ``` --- apiVersion: apps/v1 kind: Deployment metadata: annotations: deployment.kubernetes.io/revision: '1' k8s.kuboard.cn/ingress: 'false' k8s.kuboard.cn/service: NodePort k8s.kuboard.cn/workload: web-canal-grafana creationTimestamp: '2020-11-06T06:10:27Z' generation: 1 labels: k8s.kuboard.cn/layer: web k8s.kuboard.cn/name: web-canal-grafana name: web-canal-grafana namespace: canal-ns resourceVersion: '22247823' selfLink: /apis/apps/v1/namespaces/canal-ns/deployments/web-canal-grafana uid: 484350e4-b408-4361-ac27-633f8d815468 spec: progressDeadlineSeconds: 600 replicas: 1 revisionHistoryLimit: 10 selector: matchLabels: k8s.kuboard.cn/layer: web k8s.kuboard.cn/name: web-canal-grafana strategy: rollingUpdate: maxSurge: 25% maxUnavailable: 25% type: RollingUpdate template: metadata: creationTimestamp: null labels: k8s.kuboard.cn/layer: web k8s.kuboard.cn/name: web-canal-grafana spec: containers: - image: grafana/grafana imagePullPolicy: Always name: canal-grafana resources: {} terminationMessagePath: /dev/termination-log terminationMessagePolicy: File dnsConfig: {} dnsPolicy: ClusterFirst restartPolicy: Always schedulerName: default-scheduler securityContext: seLinuxOptions: {} terminationGracePeriodSeconds: 30 ``` 再進行一下server代理,yml如下;對映埠為3000埠 ``` --- apiVersion: v1 kind: Service metadata: annotations: k8s.kuboard.cn/workload: web-canal-grafana creationTimestamp: '2020-11-06T06:10:27Z' labels: k8s.kuboard.cn/layer: web k8s.kuboard.cn/name: web-canal-grafana name: web-canal-grafana namespace: canal-ns resourceVersion: '22247699' selfLink: /api/v1/namespaces/canal-ns/services/web-canal-grafana uid: 8cbde138-6855-4eaf-b9cd-8dae72a2efeb spec: clusterIP: 10.204.195.124 externalTrafficPolicy: Cluster ports: - name: canal-grafana-3000 nodePort: 31010 port: 3000 protocol: TCP targetPort: 3000 selector: k8s.kuboard.cn/layer: web k8s.kuboard.cn/name: web-canal-grafana sessionAffinity: None type: NodePort ``` OK,啟動後,訪問對應的應用後,效果如圖所示: ![](https://img2020.cnblogs.com/blog/1104472/202011/1104472-20201106162836466-246527803.png) 此時還沒有進行grefana的配置,所以無法獲取canal的監控資訊 ## Greafana配置canal監控資訊 很簡單,基本按照官方的說明即可, 新建一個dataSource,選擇prometheus,然後填寫對應的prometheus的url地址即可。 ![](https://img2020.cnblogs.com/blog/1104472/202011/1104472-20201106162851539-1562379402.png) ![](https://img2020.cnblogs.com/blog/1104472/202011/1104472-20201106162906287-392654020.png) 填寫完成後,最後一步則是,grafana中匯入 canal的監控指標 ![](https://img2020.cnblogs.com/blog/1104472/202011/1104472-20201106162916615-95201631.png) 此時按照URL的匯入方式:https://raw.githubusercontent.com/alibaba/canal/master/deployer/src/main/resources/metrics/Canal_instances_tmpl.json 匯入該json檔案即可; 不過我在處理的時候,該url匯入不可用,所以,如果你也是不可用的話,可以直接 wget https://raw.githubusercontent.com/alibaba/canal/master/deployer/src/main/resources/metrics/Canal_instances_tmpl.json 獲取對應的檔案內容後,再填寫到第二個框中即可; ## 最終的監控效果圖 [原創宣告:作者:Arnold.zhao 部落格園地址:https://www.cnblogs.com/zh94](https://www.cnblogs.com/zh94) ![](https://img2020.cnblogs.com/blog/1104472/202011/1104472-20201106162925338-1996296800.png) 具體各監控指標所表示的含義,直接看github canal文件即可; 由於此處我的canal client並沒有直接通過tcp的方式和canal -deployer進行互動,而是直接接入的canal-deployer所吐出來的kakfa資料,所以此處所展示的client指標則為空; 參考連結: [關於普羅米修斯(prometheus)的基本概念](https://www.cnblogs.com/chenqionghe/p/10494868.html) [關於Grafana的基本概念](https://blog.csdn.net/freeking101/article/details/105844927/) [關於canal安裝Grafana&prometheus進行監控的說明](https://github.com/alibaba/canal/wiki/Prometheus-QuickStart) [關於k8s中目錄掛載時不覆蓋容器原目錄的用法](https://www.cnblogs.com/weifeng1463/p/1133231
版权声明
本文为[itread01]所创,转载请带上原文链接,感谢
https://www.itread01.com/content/1604666703.html

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云