iT邦幫忙

1

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都寫好了 可以去看看。


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
smallwhitetw
iT邦新手 5 級 ‧ 2021-07-01 22:57:20

版主牛逼,一點精美的圖片,一小段簡潔的說明,即可讓人融會貫通。你要確定欸

我要留言

立即登入留言