在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等方案进行部署。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:centos k8s集群如何搭建