Kubernetes

Tài liệu này hướng dẫn cấu hình để thu thập Metrics, Logs, Events từ Kubernetes

1. Chuẩn bị

  • Tham khảo cài đặt kube-prometheus-stack theo tài liệu:

     Hướng dẫn cài đặt Prometheus

  • Thực hiện tải helm chart k8s-monitoring bằng lệnh sau: 
curl -L -o k8s-monitoring-3.7.1.tgz https://s3hn.smartcloud.vn/agent/helm-charts/k8s-monitoring-3.7.1.tgz

Các helm chart cần thiết bao gồm:

STT

Helm Chart

Chart Version

1

kube-prometheus-stack 

80.3.0

2

k8s-monitoring

3.7.1

2. Cài đặt

2.1 Cài đặt helm chart thu thập metrics kubernetes

  • Tạo namespaces monitoring
kubectl create namespace monitoring 
  •  Tạo secret cấu hình xác thực endpoint dịch vụ APM
kubectl create secret generic secret-token \
-n monitoring \
--from-literal=token=<token_dịch_vụ_APM>
  • Thực hiện cài đặt Prometheus bằng lệnh sau:
helm install kube-prometheus-stack prometheus-community/kube-prometheus-stack \
  --namespace monitoring \
  --create-namespace \
  --version 80.3.0 \
  --set prometheus.prometheusSpec.retentionSize=5Gi \
  --set prometheus.prometheusSpec.retention=1d \
  --set prometheus.prometheusSpec.storageSpec.volumeClaimTemplate.spec.resources.requests.storage=10Gi \
  --set alertmanager.alertmanagerSpec.storage.volumeClaimTemplate.spec.resources.requests.storage=2Gi \
  --set grafana.ingress.enabled=true \
  --set grafana.ingress.ingressClassName=<class_name> \
  --set grafana.ingress.hosts[0]=<host_domain> \
  --set prometheus.prometheusSpec.remoteWrite[0].url=<endpoint_dịch_vụ_APM> \
  --set prometheus.prometheusSpec.remoteWrite[0].authorization.type=Bearer \
  --set prometheus.prometheusSpec.remoteWrite[0].authorization.credentials.name=secret-token \
  --set prometheus.prometheusSpec.remoteWrite[0].authorization.credentials.key=token \
  --set prometheus.prometheusSpec.remoteWrite[0].writeRelabelConfigs[0].sourceLabels[0]=job \
  --set prometheus.prometheusSpec.remoteWrite[0].writeRelabelConfigs[0].targetLabel=job \
  --set-string prometheus.prometheusSpec.remoteWrite[0].writeRelabelConfigs[0].regex="(integrations/(node_exporter|unix))|.*node-exporter.*" \
  --set prometheus.prometheusSpec.remoteWrite[0].writeRelabelConfigs[0].replacement=integrations/unix

Thay các trường ở file cấu hình sau, trong đó:

TrườngÝ nghĩaGiá trị mẫu
endpoint_dịch_vụ_APMEndpoint của dịch vụ APM trên giao diện Consolehttps://metrics-0qcs3dsvvva.apm.vnptplatform.vn/api/v1/push
token_dịch_vụ_APMToken của dịch vụ APM trên giao diện Consolea5130b91-ff9b-41ef-8722-9d923a574123
class_nameClass của Ingress Controllernginx
host_domainDomain truy cập Prometheusmonitor-apm01.obs.vnptplatform.vn

2.2 Cài đặt helm chart thu thập Kubernetes logs, events

helm install -n monitoring kube-prometheus-stack . 

Thay các trường ở file cấu hình trên, trong đó:

endpoint_dịch_vụ_APMEndpoint của dịch vụ APM trên giao diện Consolehttps://logs-w3fl36b8ok.obs.vnptplatform.vn/loki/api/v1/push
token_dịch_vụ_APMToken của dịch vụ APM trên giao diện Consolea5130b91-ff9b-41ef-8722-9d923a574123
destinations_nameXác định điểm đích tiếp nhận logloki
destinations_name Danh sách namespace cần thu thập logs pod

- kube-system

- monitoring

Thực hiện thay đổi các cấu hình sau để thu thập kubernetes events, logs.

  • clusterEvents:
clusterEvents:
 enabled: true
 destinations:
   - <destinations_name>
 collector: alloy-singleton
  • nodeLogs:
nodeLogs:
  enabled: true
  destinations: 
    - <destinations_name>
  collector: alloy-logs
  • podLogs:
podLogs:
 enabled: true
 destinations:
   - <destinations_name>
 collector: alloy-logs
  • destinations:
destinations: 
  - name: loki
    type: <destinations_name>
    url: <endpoint_dịch_vụ_APM>
    auth:
      type: bearerToken
      bearerToken: "<token_dịch_vụ_APM>"
    secret:
      embed: true
  • namespace:  Giám sát logs pod theo namespace chỉ định
podLogs:
 namespace: [<namespace_list>]
  • resources: Giới hạn tài nguyên theo collector (ví dụ với alloy-logs):
alloy-logs:
  enabled: true

  resources:
    limits:
      cpu: 500m
      memory: 1024Mi
    requests:
      cpu: 1m
      memory: 5Mi
  • Cấu hình bật collector thu thập logs, events kubernetes và syslog:
alloy-singleton:
 enabled: true

alloy-logs:
 enabled: true
 alloy:
   podSecurityContext:
     runAsUser: 0
     runAsGroup: 0
     runAsNonRoot: false
   securityContext:
     privileged: true
     allowPrivilegeEscalation: true
   extraVolumes:
     - name: varlog
       hostPath:
         path: /var/log
         type: Directory
   extraVolumeMounts:
     - name: varlog
       mountPath: /var/log
       readOnly: true
   extraConfig: |
     loki.source.file "syslog" {
       path_targets = [
         {
           __path__ = "/var/log/syslog*",
           job      = "syslog"
         }]
       labels = {
         job = "integrations/node_exporter",
         instance = sys.env("HOSTNAME"),
       }
       max_age = "30m"
       forward_to = [loki.write.loki.receiver] 
  • Thực hiện cài đặt k8s-monitoring bằng câu lệnh sau:
helm install -n monitoring k8s-monitoring .

3. Kiểm tra

3.1 Kiểm tra dịch vụ

Thực hiện lệnh sau:

helm list -n monitoring
kubectl get po,svc -n monitoring

Kết quả helm release, pod running:

Một số dashboard mẫu khi tích hợp thành công: