k8s部署mysql-exporter,mongo-exporter,redis-exporter,zookeeper-exporter,kafka-exporter

k8s部署mysql-exporter,mongo-exporter,redis-exporter监控

命名空间创建

先建立一个命名空间: prometheus-exporter 用来存放本环境的所有exporter

kubectl create ns prometheus-exporter

mysql监控

如果需要监控多个mysql,可以用一下部分再建一个mysql-exporter

1 创建mysql-exporter用户

CREATE USER 'exporter'@'%' IDENTIFIED BY 'XXXXXXXX';
GRANT PROCESS, REPLICATION CLIENT ON *.* TO 'exporter'@'%';
GRANT SELECT ON performance_schema.* TO 'exporter'@'%';

2 创建mysql-exporter

使用下面的yaml文件创建一个redis-exporter的Deployment‘

vi   01-deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysqld-exporter
  namespace: prometheus-exporter
  labels:
    app: mysqld-exporter
spec:
  selector:
    matchLabels:
      app: mysqld-exporter
  template:
    metadata:
      labels:
        app: mysqld-exporter
    spec:
      containers:
      - name: mysqld-exporter
        image: prom/mysqld-exporter
        env:
        - name: DATA_SOURCE_NAME
          value: exporter:password@(mysql:3306)/  #数据库连接: 用户:密码@(主机:端口)
        ports:
        - containerPort: 9104
          name: http

创建资源

kubectl apply -f  01-deployment.yaml

此时mysql-exporter创建完成

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xQ9mhoQ3-1626414896608)(…/img/中间件监控/image-20210716112334408.png)]

3 创建mysql-exporter服务

使用下面的yaml文件创建redis-exporter服务

vi 02-service.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    app: mysqld-exporter
  name: mysqld-exporter
  namespace: prometheus-exporter
spec:
  type: NodePort
  ports:
  - name: http
    port: 9104
    nodePort: 30018
    targetPort: http
  selector:
    app: mysqld-exporter

创建资源

kubectl apply -f  02-service.yaml

此时mysql-exporter 服务创建完成

4 测试

访问

curl localhost:30018/metrics

返回如下
k8s部署mysql-exporter,mongo-exporter,redis-exporter,zookeeper-exporter,kafka-exporter_第1张图片

redis监控

1 创建redis-exporter

使用下面的yaml文件创建一个redis-exporter的Deployment‘

vi   01-deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis-exporter
  namespace: prometheus-exporter
  labels:
    app: redis-exporter
spec:
  selector:
    matchLabels:
      app: redis-exporter
  template:
    metadata:
      labels:
        app: redis-exporter
    spec:
      containers:
      - name: redis-exporter
        image: oliver006/redis_exporter:latest
        args: ["-redis.addr", "redis://10.20.20.155:6379"]  #这里填写redis地址
        ports:
        - containerPort: 9121
          name: http

创建资源

kubectl apply -f  01-deployment.yaml

此时redis-exporter创建完成

在这里插入图片描述

2 创建redis-exporter服务

使用下面的yaml文件创建redis-exporter服务

vi 02-service.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    app: redis-exporter
  name: redis-exporter
  namespace: prometheus-exporter
spec:
  type: NodePort
  ports:
  - name: http
    port: 9121
    nodePort: 30019
    targetPort: http
  selector:
    app: redis-exporter

创建资源

kubectl apply -f  02-service.yaml

此时redis-exporter 服务创建完成

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qpjqETzA-1626414896620)(../img/中间件监控/image-20210716104555324.png)]

3 测试

访问

 curl localhost:30019/metrics

返回如下

k8s部署mysql-exporter,mongo-exporter,redis-exporter,zookeeper-exporter,kafka-exporter_第2张图片

mongo监控

1 创建mongodb-exporter

使用下面的yaml文件创建一个mongodb-exporter的Deployment‘

vi   01-deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongodb-exporter
  namespace: prometheus-exporter
  labels:
    app: mongodb-exporter
spec:
  selector:
    matchLabels:
      app: mongodb-exporter
  template:
    metadata:
      labels:
        app: mongodb-exporter
    spec:
      containers:
      - name: mongodb-exporter
        image: ssheehy/mongodb-exporter
        env:
        - name: MONGODB_URI
          value: mongodb://localhost:27017   #mongo地址
        ports:
        - containerPort: 9216
          name: http

创建资源

kubectl apply -f  01-deployment.yaml

此时mongodb-exporter创建完成

2 创建mongodb-exporter服务

使用下面的yaml文件创建mongodb-exporter服务

vi 02-service.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    app: mongodb-exporter
  name: mongodb-exporter
  namespace: prometheus-exporter
spec:
  type: NodePort
  ports:
  - name: http
    port: 9216
    nodePort: 30020
    targetPort: http
  selector:
    app: mongodb-exporter

创建资源

kubectl apply -f  02-service.yaml

此时mongodb-exporter 服务创建完成

3 测试

访问

 curl localhost:30020/metrics

返回如下

k8s部署mysql-exporter,mongo-exporter,redis-exporter,zookeeper-exporter,kafka-exporter_第3张图片

zookeeper监控

1 创建zookeeper-exporter

使用下面的yaml文件创建一个redis-exporter的Deployment‘

vi   01-deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: zookeeper-exporter
  namespace: prometheus-exporter
  labels:
    app: zookeeper-exporter
spec:
  selector:
    matchLabels:
      app: zookeeper-exporter
  template:
    metadata:
      labels:
        app: zookeeper-exporter
    spec:
      containers:
      - name: zookeeper-exporter
        image: josdotso/zookeeper-exporter
        args: ["-zookeeper", "10.20.20.155:30458"]  #这里填写zookeeper地址
        ports:
        - containerPort: 9141
          name: http

创建资源

kubectl apply -f  01-deployment.yaml

此时zookeeper-exporter创建完成

2 创建zookeeper-exporter服务

使用下面的yaml文件创建zookeeper-exporter服务

vi 02-service.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    app: zookeeper-exporter
  name: zookeeper-exporter
  namespace: prometheus-exporter
spec:
  type: NodePort
  ports:
  - name: http
    port: 9141
    nodePort: 30021
    targetPort: http
  selector:
    app: zookeeper-exporter

创建资源

kubectl apply -f  02-service.yaml

此时zookeeper-exporter 服务创建完成

3 测试

访问

 curl localhost:30021/metrics

kafka监控

1 创建kafka-exporter

使用下面的yaml文件创建一个redis-exporter的Deployment‘

vi   01-deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: kafka-exporter
  namespace: prometheus-exporter
  labels:
    app: kafka-exporter
spec:
  selector:
    matchLabels:
      app: kafka-exporter
  template:
    metadata:
      labels:
        app: kafka-exporter
    spec:
      containers:
      - name: kafka-exporter
        image: danielqsj/kafka-exporter
        args: ["--kafka.server=kafka:9092"]  #这里填写kafka地址
        ports:
        - containerPort: 9308
          name: http

创建资源

kubectl apply -f  01-deployment.yaml

此时kafka-exporter创建完成

2 创建kafka-exporter服务

使用下面的yaml文件创建kafka-exporter服务

vi 02-service.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    app: kafka-exporter
  name: kafka-exporter
  namespace: prometheus-exporter
spec:
  type: NodePort
  ports:
  - name: http
    port: 9308
    nodePort: 30022
    targetPort: http
  selector:
    app: kafka-exporter

创建资源

kubectl apply -f  02-service.yaml

此时kafka-exporter 服务创建完成

3 测试

访问

 curl localhost:30022/metrics

prometheus添加target

修改Prometheus配置文件

添加target

scrape_configs:
  - job_name: 'mysql'
    scrape_interval: 30s
    static_configs:
      - targets: ['10.20.20.153:30018']
  - job_name: 'redis'
    scrape_interval: 30s
    static_configs:
      - targets: ['10.20.20.153:30019']
  - job_name: 'mongodb'
    scrape_interval: 30s
    static_configs:
      - targets: ['10.20.20.153:30020']
  - job_name: 'zookeeper'
    scrape_interval: 30s
    static_configs:
      - targets: ['10.20.20.153:30021']
  - job_name: 'kafka'
    scrape_interval: 30s
    static_configs:
      - targets: ['10.20.20.153:30022']

重启Prometheus

访问Prometheus查看target

k8s部署mysql-exporter,mongo-exporter,redis-exporter,zookeeper-exporter,kafka-exporter_第4张图片

成功

你可能感兴趣的:(Prometheus)