Prometheus-operator对Mysql的监控

1,508次阅读
没有评论

共计 5408 个字符,预计需要花费 14 分钟才能阅读完成。

Prometheus-operator对Mysql的监控

使用mysql-exporter监控mysql

快速创建mysql样例

[root@k8s-master standalone]# kubectl create secret generic mysql-root-password --from-literal=password=abcd123456
[root@k8s-master standalone]# cat >mysql-deploy.yaml<<-'EOF'
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mysql-deploy-pvc
spec:
  storageClassName: nfs-storage
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi

---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: mysql
    tier: standalone
  name: mysql-standalone
spec:
  selector:
    matchLabels:
      app: mysql
      tier: standalone
  template:
    metadata:
      labels:
        app: mysql
        tier: standalone
    spec:
      containers:
      - image: mysql:8.0
        name: mysql-standalone
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-root-password
              key: password
          # 不使用secret则可以用下面方式指定
          #value: "123456"
        ports:
        - containerPort: 3306
        volumeMounts:
        - name: mysqlvolume
          mountPath: /var/lib/mysql
      volumes:
      - name: mysqlvolume
        # 使用pvc
        persistentVolumeClaim:
          claimName: mysql-deploy-pvc
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: mysql
    tier: standalone
  name: mysql-standalone-deploy
spec:
  selector:
    app: mysql
    tier: standalone
  type: NodePort
  ports:
  - name: mysql
    port: 3306
    protocol: TCP
    targetPort: 3306
EOF

[root@k8s-master standalone]# kubectl apply -f mysql-deploy.yaml
[root@k8s-master standalone]# kubectl get -f mysql-deploy.yaml
NAME                                     STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
persistentvolumeclaim/mysql-deploy-pvc   Bound    pvc-7065898b-e28e-4b67-9d01-18837c4ceb37   5Gi        RWO            nfs-storage    11m

NAME                               READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/mysql-standalone   1/1     1            1           11m

NAME                              TYPE       CLUSTER-IP    EXTERNAL-IP   PORT(S)          AGE
service/mysql-standalone-deploy   NodePort   10.96.160.8   <none>        3306:30684/TCP   11m

deploy创建mysql-exporter

[root@k8s-master mysql-exporter]# cat >mysql-exporter.yaml<<-'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysqld-exporter
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysqld-exporter
  template:
    metadata:
      labels:
        app: mysqld-exporter
    spec:
      containers:
      - name: mysqld-exporter
        image: prom/mysqld-exporter:v0.12.1
        args:
        - --collect.info_schema.tables
        - --collect.info_schema.innodb_tablespaces
        - --collect.info_schema.innodb_metrics
        - --collect.global_status
        - --collect.global_variables
        - --collect.slave_status
        - --collect.info_schema.processlist
        - --collect.perf_schema.tablelocks
        - --collect.perf_schema.eventsstatements
        - --collect.perf_schema.eventsstatementssum
        - --collect.perf_schema.eventswaits
        - --collect.auto_increment.columns
        - --collect.binlog_size
        - --collect.perf_schema.tableiowaits
        - --collect.perf_schema.indexiowaits
        - --collect.info_schema.userstats
        - --collect.info_schema.clientstats
        - --collect.info_schema.tablestats
        - --collect.info_schema.schemastats
        - --collect.perf_schema.file_events
        - --collect.perf_schema.file_instances
        - --collect.perf_schema.replication_group_member_stats
        - --collect.perf_schema.replication_applier_status_by_worker
        - --collect.slave_hosts
        - --collect.info_schema.innodb_cmp
        - --collect.info_schema.innodb_cmpmem
        - --collect.info_schema.query_response_time
        - --collect.engine_tokudb_status
        - --collect.engine_innodb_status
        ports:
        - containerPort: 9104
          protocol: TCP
        env:
        - name: DATA_SOURCE_NAME
          value: "mysqld-exporter:123456@(mysql-standalone-deploy.default.svc.cluster.local:3306)/"
---
apiVersion: v1
kind: Service
metadata:
  name: mysqld-exporter
  labels:
    app: mysqld-exporter
spec:
  type: ClusterIP
  ports:
  - port: 9104
    protocol: TCP
    name: http
  selector:
    app: mysqld-exporter
EOF

[root@k8s-master mysql-exporter]# kubectl apply -f mysql-exporter.yaml
[root@k8s-master mysql-exporter]# kubectl get -f mysql-exporter.yaml
NAME                              READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/mysqld-exporter   1/1     1            1           9m30s

NAME                      TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
service/mysqld-exporter   ClusterIP   10.96.115.101   <none>        9104/TCP   9m30s

测试mysql-expoter获取数据

[root@k8s-master mysql-exporter]# curl -s 10.96.115.101:9104/metrics  | head -n 30
# HELP go_gc_duration_seconds A summary of the GC invocation durations.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 1.3012e-05
go_gc_duration_seconds{quantile="0.25"} 2.234e-05
go_gc_duration_seconds{quantile="0.5"} 0.000186935
go_gc_duration_seconds{quantile="0.75"} 0.000324783
go_gc_duration_seconds{quantile="1"} 0.000843768
go_gc_duration_seconds_sum 0.005051619
go_gc_duration_seconds_count 21
# HELP go_goroutines Number of goroutines that currently exist.
# TYPE go_goroutines gauge
go_goroutines 7
# HELP go_info Information about the Go environment.
# TYPE go_info gauge
go_info{version="go1.12.7"} 1
# HELP go_memstats_alloc_bytes Number of bytes allocated and still in use.
# TYPE go_memstats_alloc_bytes gauge
go_memstats_alloc_bytes 2.906528e+06
# HELP go_memstats_alloc_bytes_total Total number of bytes allocated, even if freed.
# TYPE go_memstats_alloc_bytes_total counter
go_memstats_alloc_bytes_total 4.4115728e+07
# HELP go_memstats_buck_hash_sys_bytes Number of bytes used by the profiling bucket hash table.
# TYPE go_memstats_buck_hash_sys_bytes gauge
go_memstats_buck_hash_sys_bytes 1.455961e+06
# HELP go_memstats_frees_total Total number of frees.
# TYPE go_memstats_frees_total counter
go_memstats_frees_total 706242
# HELP go_memstats_gc_cpu_fraction The fraction of this program's available CPU time used by the GC since the program started.
# TYPE go_memstats_gc_cpu_fraction gauge
go_memstats_gc_cpu_fraction 1.4876387211155109e-05

准备servicemonitor

[root@k8s-master mysql-exporter]# cat >mysql-exporter-servicemonitor.yaml<<-'EOF'
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: mysqld-exporter
  namespace: monitoring
spec:
  jobLabel: mysqld-exporter
  endpoints:
  - port: http
    interval: 30s
    scheme: http
  namespaceSelector:
    matchNames:
    - default
  selector:
    matchLabels:
      app: mysqld-exporter
EOF

[root@k8s-master mysql-exporter]# kubectl apply -f mysql-exporter-servicemonitor.yaml

此时登录prometheus查看target

Prometheus-operator对Mysql的监控

在grafana上导入dashboard ID: 140577362

正文完
 
xadocker
版权声明:本站原创文章,由 xadocker 2020-09-08发表,共计5408字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)