k8s prometheus 监控多个MySql

【YC的寻路青春】

这边我们用的是接线生prometheus-operator的版本

namespace 我们这边的惯例就是用 yc /或是不写就是预设

监控单个MySql,用mysql-exporter实现对资料库性能以及资源利用率的监控

方便阅读 这边叫要监控三个的资料库
YCdata1:3306
YCdata2:3306
YCdata3:3306

第一步骤

请去你要监控的sql里面 创建帐号密码

第二步骤

config - 每组连线用的帐号密码

第三步骤

Deployment - mysql-exporter
多个资料库需要执行 --web.listen-address=XXXX

第四步骤

service - 开port号

第五步骤

Servicemonitor mysql-exporter-prometheus连线


第一步骤

请去你要监控的sql里面 创建帐号密码
然后赋予他process以及replication client权限
如下:
http://img2.58codes.com/2024/20111603E5fa6465MB.png

CREATE USER 'yc'@'%' IDENTIFIED BY 'yc';grant process, replication client on *.* to 'yc'@'%';                  

第二步骤

写一个连线用的config
大概如下

kind: ConfigMapapiVersion: v1metadata:  name: mysql-config  namespace: ycdata:  .56.cnf: |-    [client]    user=yc@yc-1    password=yc    port=3306    host=YCdata1  .57.cnf: |-    [client]    user=yc@yc-2    password=yc    port=3306    host=YCdata2  .80.cnf: |-    [client]    user=yc@yc-3    password=yc    port=3306    host=YCdata3

但是连线的东西如果没有加密有点危险
写config当然可以 但推荐测试完之后改用秘密(Secret)
写法一样
kind改成Secret 然后转base 64即可

kind: SecretapiVersion: v1metadata:  name: mysql-config  namespace: ycdata:  .56.cnf: |-    XXXXXXXXXXXXXji3g4base64XXXXXXXXXX  .57.cnf: |-    XXXXXXXXXXXji3g4base64XXXXXXXXXXXXX  .80.cnf: |-    XXXXXXXXXXXXXji3g4base64XXXX

第三步骤

写Deployment啦
这里的labels是 app: mysql-exporter (等下要接service用)

kind: DeploymentapiVersion: apps/v1metadata:  name: mysql-exporter  namespace: ycspec:  replicas: 1  selector:    matchLabels:      app: mysql-exporter  template:    metadata:      creationTimestamp: null      labels:        app: mysql-exporter    spec:      volumes:        - name: config          secret:            secretName: mysql-config            defaultMode: 420      containers:        - name: mysql-exporter          image: 'prom/mysqld-exporter:latest'          ports:            - containerPort: 9104              protocol: TCP          env:            - name: DATA_SOURCE_NAME              value: >-                yc@yc-1:yc@(YCdata1:3306)/            - name: podIP              valueFrom:                fieldRef:                  apiVersion: v1                  fieldPath: status.podIP          resources: {}          volumeMounts:            - name: config              mountPath: /etc/.56.cnf              subPath: .56.cnf            - name: config              mountPath: /etc/.57.cnf              subPath: .57.cnf            - name: config              mountPath: /etc/.80.cnf              subPath: .80.cnf          livenessProbe:            exec:              command:                - /bin/sh                - '-c'                - >-                  nohup mysqld_exporter --web.listen-address=$podIP:9105                  --config.my-cnf=/etc/.57.cnf & nohup mysqld_exporter                  --web.listen-address=$podIP:9106 --config.my-cnf=/etc/.80.cnf                  & exit            timeoutSeconds: 1            periodSeconds: 10            successThreshold: 1            failureThreshold: 3

这边的细节就是livenessProbe
如果没有这段的话连不到另外两个资料库
你也可以试着先不要
然后手动key
kubectl exec -it mysql-exporter-xxxxx /bin/sh -n yc
进去之后打
mysqld_exporter就可以看到连线的内容
这时候就打
nohup mysqld_exporter --web.listen-address=$podIP:9105
--config.my-cnf=/etc/.57.cnf
就可以多连到一个资料库了
这边我们都弄完再回头看

第四步骤

盖service
这边的labels是app: mysql-exporter 有这个等下接service monitor用的
这里的selector是app: mysql-exporter 这样才接的到pod 这好像是一句没啥用的话

这边可以开port号出来 提供大家使用 开了9104 9105 9106 三个 ,如果要增加/减少的话在这边跟servicemonitor里面更改

kind: ServiceapiVersion: v1metadata:  name: mysql-exporter  namespace: yc  labels:    app: mysql-exporterspec:  ports:    - name: YCdata1      protocol: TCP      port: 9104      targetPort: 9104    - name: YCdata2      protocol: TCP      port: 9105      targetPort: 9105    - name: YCdata3      protocol: TCP      port: 9106      targetPort: 9106  selector:    app: mysql-exporter  type: ClusterIP  sessionAffinity: None

第五步骤

写Servicemonitor啦
这个是拿来让exporter <-> prometheus 互动的
Servicemonitor不能直接用新增yaml档的方式
请vi Servicemonitor.yaml
kubectl apply -f Servicemonitor.yaml

relabelings把instance的名字改成自己喜欢的样子 是很重要的 这样在发警报通知的时候才会知道谁是谁
matchLabels写app: mysql-exporter 如果你的prometheus是空的 表示你没连到service 大概是这边错了

apiVersion: monitoring.coreos.com/v1kind: ServiceMonitormetadata:  name: mysql-exporter  namespace: hktv-monitoringspec:  endpoints:    - interval: 15s      port: YCdata1      relabelings:        - targetLabel: instance          replacement: YCdata1    - interval: 15s      port: YCdata2      relabelings:        - targetLabel: instance          replacement: YCdata2    - interval: 15s      port: YCdata3      relabelings:        - targetLabel: instance          replacement: YCdata3  namespaceSelector:    matchNames:      - hktv-monitoring  selector:    matchLabels:      app: mysql-exporter

完工。

至于要怎么看有没有连到 如果你是看我上一篇架prometheus的话 我们rule都写好了 可以去看看。


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章