Hiển thị dữ liệu từ Prometheus trong Kubernetes

Thiết lập giám sát với Prometheus và Grafana trên Kubernetes

Để theo dõi hiệu năng hệ thống và trạng thái các tài nguyên trong cụm Kubernetes, ta có thể triển khai bộ công cụ giám sát gồm Prometheus và Grafana. Dưới đây là cách cấu hình và triển khai các thành phần này.

1. Triển khai Grafana qua file YAML

Dưới đây là định nghĩa deployment cho Grafana, sử dụng PersistentVolumeClaim để lưu trữ dữ liệu:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: grafana
  namespace: ops
spec:
  replicas: 1
  selector:
    matchLabels:
      app: grafana
  template:
    metadata:
      labels:
        app: grafana
    spec:
      containers:
      - name: grafana
        image: grafana/grafana:7.1.0
        ports:
        - containerPort: 3000
          protocol: TCP
        resources:
          limits:
            cpu: 100m
            memory: 256Mi
          requests:
            cpu: 100m
            memory: 256Mi
        volumeMounts:
        - name: grafana-storage
          mountPath: /var/lib/grafana
          subPath: grafana-data
      securityContext:
        fsGroup: 472
        runAsUser: 472
      volumes:
      - name: grafana-storage
        persistentVolumeClaim:
          claimName: grafana-pvc

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: grafana-pvc
  namespace: ops
spec:
  storageClassName: "managed-nfs-storage"
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi

---
apiVersion: v1
kind: Service
metadata:
  name: grafana
  namespace: ops
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 3000
    nodePort: 30030
  selector:
    app: grafana

2. Triển khai và kiểm tra hoạt động

Sau khi áp dụng cấu hình:

[root@k8s-master prometheus]# kubectl apply -f grafana.yaml
deployment.apps/grafana unchanged
persistentvolumeclaim/grafana-pvc unchanged
service/grafana unchanged

Kiểm tra trạng thái pod và service:

[root@k8s-master prometheus]# kubectl get pods,svc -n ops
NAME                              READY   STATUS    RESTARTS   AGE
pod/grafana-757fcd5f7c-twbmj      1/1     Running   0          40m
pod/prometheus-859dbbc5f7-rlsqp   2/2     Running   0          22h

NAME                 TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
service/grafana      NodePort   10.108.238.42   <none>        80:30030/TCP     40m
service/prometheus   NodePort   10.108.53.165   <none>        9090:30090/TCP   22h

Truy cập giao diện Grafana qua trình duyệt: http://<ip-máy-chủ>:30030. Tài khoản mặc định là admin/admin.

3. Cấu hình nguồn dữ liệu (Data Source)

Trong Grafana, truy cập Configuration → Data Sources, thêm Prometheus làm nguồn dữ liệu với endpoint: http://prometheus.ops.svc.cluster.local:9090. Kiểm tra kết nối để đảm bảo hoạt động bình thường.

4. Giám sát nút (Node Exporter)

Triển khai Node Exporter để thu thập thông tin từ các node trong cụm:

[root@k8s-master prometheus]# kubectl apply -f node-exporter.yml
daemonset.apps/node-exporter created
service/node-exporter created

Quan sát kết quả sau triển khai:

[root@k8s-master prometheus]# kubectl get pods,svc -n ops
NAME                              READY   STATUS    RESTARTS   AGE
pod/grafana-757fcd5f7c-twbmj      1/1     Running   0          62m
pod/node-exporter-27zdq           1/1     Running   0          2m6s
pod/node-exporter-ftrhc           1/1     Running   0          2m6s
pod/prometheus-859dbbc5f7-rlsqp   2/2     Running   0          23h

NAME                    TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
service/grafana         NodePort    10.108.238.42   <none>        80:30030/TCP     62m
service/node-exporter   ClusterIP   None            <none>        9100/TCP         2m6s
service/prometheus      NodePort    10.108.53.165   <none>        9090:30090/TCP   23h

5. Giám sát tài nguyên Kubernetes (kube-state-metrics)

Để theo dõi trạng thái các đối tượng như Pod, Node, Service, Deployment…, cần triển khai kube-state-metrics:

[root@k8s-master prometheus]# kubectl apply -f kube-state-metrics.yaml
deployment.apps/kube-state-metrics created
configmap/kube-state-metrics-config created
service/kube-state-metrics created
serviceaccount/kube-state-metrics created
clusterrole.rbac.authorization.k8s.io/kube-state-metrics created
role.rbac.authorization.k8s.io/kube-state-metrics-resizer created
clusterrolebinding.rbac.authorization.k8s.io/kube-state-metrics created
rolebinding.rbac.authorization.k8s.io/kube-state-metrics created

Kiểm tra trạng thái sau khi triển khai:

[root@k8s-master prometheus]# kubectl get pods -n ops
NAME                                  READY   STATUS    RESTARTS   AGE
grafana-757fcd5f7c-twbmj              1/1     Running   0          3h8m
kube-state-metrics-667bc48f47-cwg8p   2/2     Running   0          116s
node-exporter-27zdq                   1/1     Running   0          128m
node-exporter-ftrhc                   1/1     Running   0          128m
prometheus-859dbbc5f7-rlsqp           2/2     Running   0          25h

Bây giờ, Grafana có thể hiển thị biểu đồ chi tiết về trạng thái tài nguyên Kubernetes, bao gồm số lượng Pod đang chạy, tỷ lệ sử dụng CPU/Memory, trạng thái của các Deployment, v.v.

Thẻ: prometheus Grafana Kubernetes node-exporter kube-state-metrics

Đăng vào ngày 16 tháng 9 lúc 16:50