【云原生 | Kubernetes 系列】---Prometheus监控Redis

1. Prometheus监控Redis

通过redis_exporter连接当前ip的6379端口监控redis服务状态,
如果是node节点上运行redis,那么就在node节点上运行redis_exporter,让他监控本机的6379端口

git clone https://github.com/oliver006/redis_exporter.git
cd redis_exporter
go build .
./redis_exporter --version

2. 配置yaml

1个pod运行2个container,通过redis-exporter去收集redis的信息.
再通过nodeport将redis-exporter暴露给prometheus,redis如果是提供给k8s内部使用就不必用nodeport方式暴露.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
  namespace: wework 
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
      - name: redis
        image: redis:4.0.14 
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        ports:
        - containerPort: 6379
      - name: redis-exporter
        image: oliver006/redis_exporter:latest
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        ports:
        - containerPort: 9121
---
kind: Service
apiVersion: v1
metadata:
  name: redis-redis-service
  namespace: wework 
spec:
  selector:
    app: redis
  ports:
  - nodePort: 31081
    name: redis
    port: 6379
    protocol: TCP
    targetPort: 6379
  type: NodePort
---
kind: Service
apiVersion: v1
metadata:
  annotations:
    prometheus.io/scrape: 'true'
    prometheus.io/port: "9121"
  name: redis-exporter-service
  namespace: wework
spec:
  selector:
    app: redis
  ports:
  - nodePort: 31082
    name: prom
    port: 9121
    protocol: TCP
    targetPort: 9121
  type: NodePort

部署redis-deployment,创建一个包含2个container的redis pod
通过http://192.168.31.111:31082/metrics 向外提供给prometheus

# kubectl apply -f redis-deployment.yaml 
deployment.apps/redis created
service/redis-redis-service created
service/redis-exporter-service created
# kubectl get svc -n wework|grep redis
redis-exporter-service       NodePort    10.200.72.101    <none>        9121:31082/TCP                                 71s
redis-redis-service          NodePort    10.200.149.112   <none>        6379:31081/TCP                                 71s
# kubectl get deploy -n wework|grep redis
redis                           1/1     1            1           90s

【云原生 | Kubernetes 系列】---Prometheus监控Redis_第1张图片

3. 修改prometheus的配置

  - job_name: "k8s-redis-31082"
    metrics_path: /metrics
    static_configs:
      - targets: ["192.168.31.113:31082"]

重启服务后

systemctl restart prometheus.service

在prometheus页面上发现k8s-redis-31082

【云原生 | Kubernetes 系列】---Prometheus监控Redis_第2张图片

4. grafna导入

11835

【云原生 | Kubernetes 系列】---Prometheus监控Redis_第3张图片

你可能感兴趣的:(Docker,prometheus,K8s,kubernetes,云原生,prometheus)