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权限
如下:
https://ithelp.ithome.com.tw/upload/images/20210610/20111603E5fa6465MB.png

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

第二步骤

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

kind: ConfigMap
apiVersion: v1
metadata:
  name: mysql-config
  namespace: yc
data:
  .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: Secret
apiVersion: v1
metadata:
  name: mysql-config
  namespace: yc
data:
  .56.cnf: |-
    XXXXXXXXXXXXXji3g4base64XXXXXXXXXX
  .57.cnf: |-
    XXXXXXXXXXXji3g4base64XXXXXXXXXXXXX
  .80.cnf: |-
    XXXXXXXXXXXXXji3g4base64XXXX

第三步骤

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

kind: Deployment
apiVersion: apps/v1
metadata:
  name: mysql-exporter
  namespace: yc
spec:
  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: Service
apiVersion: v1
metadata:
  name: mysql-exporter
  namespace: yc
  labels:
    app: mysql-exporter
spec:
  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/v1
kind: ServiceMonitor
metadata:
  name: mysql-exporter
  namespace: hktv-monitoring
spec:
  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都写好了 可以去看看。


<<:  message too long for rsa [solution]

>>:  k8s prometheus 监控多个MySql -盖完後的新增

Day 22:开心SQL,SQLDelight

Keyword: SQLDelight,SQL语法 到23日,引入SQLDelight,到在Andr...

Day33 | 赛後中场休息 X 复刊时间

赛後中场休息 X 复刊时间 大家好,我是韦恩,今天开始将会暂缓完赛後的系列的发文,复刊时间约在下下礼...

快来让电脑变顺并提升 Mac 的速度吧

磁碟空间快满了 好多朋友胡乱将各种档案存储在 MacBook 里,但又没有定期整理资料的习惯。时间...

Day-2 我也...可以跟电脑娘说话吗

我也...可以跟电脑娘说话吗 tags: IT铁人 首篇不会给大家太多压力,简单介绍我们写出来的程序...

Android Studio初学笔记-Day5-TextView

基本元件介绍-TextView 接下来会开始介绍android studio一些基本的元件,这些元件...