先建立一个命名空间: prometheus-exporter 用来存放本环境的所有exporter
kubectl create ns prometheus-exporter
如果需要监控多个mysql,可以用一下部分再建一个mysql-exporter
CREATE USER 'exporter'@'%' IDENTIFIED BY 'XXXXXXXX';
GRANT PROCESS, REPLICATION CLIENT ON *.* TO 'exporter'@'%';
GRANT SELECT ON performance_schema.* TO '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)]
使用下面的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 服务创建完成
访问
curl localhost:30018/metrics
使用下面的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创建完成
使用下面的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 服务创建完成
访问
curl localhost:30019/metrics
返回如下
使用下面的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创建完成
使用下面的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 服务创建完成
访问
curl localhost:30020/metrics
返回如下
使用下面的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创建完成
使用下面的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 服务创建完成
访问
curl localhost:30021/metrics
使用下面的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创建完成
使用下面的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 服务创建完成
访问
curl localhost:30022/metrics
修改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
成功