prometheus自动发现pod配置

prometheus自动发现pod配置

按照传统的方法来部署prometheus监控实际发现,新增某个东西的时候都要去一个个配置job很麻烦,不如自动发现pod和svc,把新生成的pod和svc等资源,自动加入到系统的pod和svc监控中去。实际上我们使用的官方k8s部署prometheus已经帮我们完成。我们在部署pod和svc的时候增加配置即可。

部署自动发现redis

要想自动发现集群中的 Service,就需要在 Service 的annotation区域添加:prometheus.io/scrape=true的声明,要自动发现集群中的 pod,也需要我们在 pod 的annotation区域添加:prometheus.io/scrape=true的声明

部署redis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
cat > redis-deploy.yaml <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
namespace:
spec:
selector:
matchLabels:
app: redis
template:
metadata:
annotations:
prometheus.io/scrape: "true"
prometheus.io/port: "9121"
labels:
app: redis
spec:
containers:
- name: redis
image: redis:latest
imagePullPolicy: IfNotPresent
resources:
requests:
cpu: 100m
memory: 100Mi
env:
- name: TZ
value: Asia/Shanghai
ports:
- containerPort: 6379
- name: redis-exporter
image: oliver006/redis_exporter:latest
resources:
requests:
cpu: 100m
memory: 100Mi
ports:
- containerPort: 9121
EOF

# 创建redis
kubectl apply -f redis-deploy.yaml

创建redis 服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
cat >redis-service.yml <<EOF
apiVersion: v1
kind: Service
metadata:
namespace:
name: redis
annotations:
prometheus.io/scrape: "true"
prometheus.io/port: "9121"
labels:
app: redis
spec:
type: ClusterIP
ports:
- name: redis
port: 6379
targetPort: 6379
- name: prom
port: 9121
targetPort: 9121
selector:
app: redis
EOF

# 创建svc
kubectl apply -f redis-service.yml

部署nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
cat >nginx-deploy.yaml <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
namespace:
spec:
selector:
matchLabels:
app: nginx
replicas: 1
template:
metadata:
annotations:
prometheus.io/scrape: "true"
prometheus.io/port: "9113"
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
- name: nginx-exporter
image: fish/nginx-exporter
resources:
requests:
cpu: 100m
memory: 100Mi
ports:
- containerPort: 9113
EOF

# 创建nginx
kubectl apply -f nginx-deploy.yaml

创建nginx 服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
cat >nginx-service.yaml <<EOF
apiVersion: v1
kind: Service
metadata:
namespace:
name: nginx
annotations:
prometheus.io/scrape: "true"
prometheus.io/port: "9113"
labels:
app: nginx
spec:
type: ClusterIP
ports:
- name: nginx
port: 80
targetPort: 80
- name: fish
port: 9113
targetPort: 9113
selector:
app: nginx
EOF

# 创建nginx 服务
kubectl apply -f nginx-service.yaml

创建完成后,在prometheusui界面的status——>Service Discovery可以看到。

坚持原创技术分享,您的支持将鼓励我继续创作!
0%