[root@\ k8s-m-01~]# mkdir redis
[root@\ k8s-m-01~]# cd redis/
[root@\ k8s-m-01~/redis]# vim deploymeng_redis.yaml
kind: Deployment
apiVersion: apps/v1
metadata:
name: redis
spec:
selector:
matchLabels:
k8s: redis-exporter
template:
metadata:
labels:
k8s: redis-exporter
spec:
containers:
- name: redis
imagePullPolicy: IfNotPresent
image: redis
kind: Service
apiVersion: v1
metadata:
name: redis-svc
labels:
k8s: redis-exporter
spec:
ports:
- port: 6379
targetPort: 6379
name: redis
selector:
k8s: redis-exporter
[root@\ k8s-m-01~/redis]# kubectl get pods
NAME READY STATUS RESTARTS AGE
redis-6b9dd9c556-2dllt 1/1 Running 0 49s
[root@\ k8s-m-01~/redis]# kubectl get deployments.apps
NAME READY UP-TO-DATE AVAILABLE AGE
redis 1/1 1 1 64s
2,部署expertor,从而创建一个metrics接口
[root@\ k8s-m-01~/redis]# vim exprter_redis.yaml
kind: Deployment
apiVersion: apps/v1
metadata:
name: redis-exporter-server
spec:
selector:
matchLabels:
k8s: redis-exporter-server
template:
metadata:
annotations:
prometheus.io/scrape: “true”
prometheus.io/port: “9121”
labels:
k8s: redis-exporter-server
spec:
containers:
- name: redis
image: oliver006/redis_exporter
args:
- “–redis.addr=redis://redis-svc.default.svc.cluster.local:6379”
ports:
- containerPort: 9121
kind: Service
apiVersion: v1
metadata:
name: redis-exporter-server
labels:
k8s: redis-exporter-server-svc
spec:
ports:
- port: 9121
targetPort: 9121
name: redis-exporter
selector:
k8s: redis-exporter-server
[root@\ k8s-m-01~/redis]# kubectl apply -f exprter_redis.yaml
deployment.apps/redis-exporter-server created
service/redis-exporter-server created
[root@\ k8s-m-01~/redis]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
redis-exporter-server ClusterIP 10.111.0.77 9121/TCP 11s
redis-svc ClusterIP 10.111.17.45 6379/TCP 3m37s
[root@\ k8s-m-01~/redis]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-prometheus-exporter-86bc575bb-2mgh8 1/1 Running 0 44m
redis-6b9dd9c556-2dllt 1/1 Running 0 3m43s
redis-exporter-server-6f6476f86f-dwbtq 1/1 Running 0 17s
3,创建ServiceMonitor,注入promethu
[root@\ k8s-m-01~/redis]# vim servicemonitor_redis.yaml
kind: ServiceMonitor
apiVersion: monitoring.coreos.com/v1
metadata:
labels:
app: redis-prometheus-exporter-servicemonitor
deploy: redis-exporter-servicemonitor
name: redis-monitor
namespace: monitoring
spec:
endpoints:
- interval: 3s
port: redis-exporter
selector: