在CentOS上部署Kubernetes(k8s)集群并設置監控告警,可以采用多種工具和方案。以下是一個基于Prometheus和Grafana的常見解決方案:
Prometheus是一個開源的監控和告警工具,可以通過kube-state-metrics等exporter收集Kubernetes集群的各類指標數據。
wget https://get.helm.sh/helm-v3.8.1-linux-amd64.tar.gz
tar zxvf helm-v3.8.1-linux-amd64.tar.gz
sudo mv linux-amd64/helm /usr/local/bin/
helm repo add grafana https://grafana.github.io/helm-charts
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
helm install prometheus stable/prometheus -n monitoring
helm install grafana stable/grafana -n monitoring
prometheus/values.yaml
文件,配置抓取的目標(包括kube-state-metrics、node等)。Alertmanager用于處理Prometheus生成的告警,并通過多種方式(如郵件、Slack等)發送通知。
helm install alertmanager stable/alertmanager -n monitoring
alertmanager/values.yaml
文件,配置告警接收方式。在Prometheus中,告警規則通常存儲在prometheus.yml
或單獨的規則文件中。
groups:
- name: kubernetes-alerts
rules:
- alert: HighCpuUsage
expr: sum(rate(container_cpu_usage_seconds_total{job="kubelet", cluster="", container!="POD", container!=""}[5m])) by (container) > 0.85 for: 2m
labels:
severity: critical
annotations:
summary: "CPU usage is too high"
description: "CPU usage of container {{ $labels.container }} in pod {{ $labels.pod }} is above 85% for the last 2 minutes."
- alert: PodCrashLoopBackOff
expr: kube_pod_container_status_restarts_total{job="kubelet", cluster":""} > 5 for: 10m
labels:
severity: warning
annotations:
summary: "Pod CrashLoopBackOff detected"
description: "Pod {{ $labels.pod }} in namespace {{ $labels.namespace }} has restarted more than 5 times in the last 10 minutes."
使用Grafana可以創建自定義的儀表盤,并查看告警狀態。
kubectl port-forward -n monitoring grafana-prometheus-operator-grafana-0 3000
然后在瀏覽器中訪問http://<node-ip>:3000
,使用配置的Grafana賬號登錄。
除了Prometheus+Grafana,還可以考慮以下方案:
通過上述步驟,可以在CentOS上的Kubernetes集群中實現有效的監控和告警。選擇合適的工具和方案,可以顯著提升集群的可管理性和穩定性。根據具體需求,可以選擇Prometheus+Grafana或kube-prometheus-stack等方案進行部署。