centos

centos k8s集群监控与告警设置

小樊
44
2025-07-02 21:00:52
栏目: 智能运维

在CentOS上部署Kubernetes(k8s)集群并设置监控告警,可以采用多种工具和方案。以下是一个基于Prometheus和Grafana的常见解决方案:

1. 部署Prometheus

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

2. 配置Alertmanager

Alertmanager用于处理Prometheus生成的告警,并通过多种方式(如邮件、Slack等)发送通知。

安装步骤:

helm install alertmanager stable/alertmanager -n monitoring

3. 监控和告警规则配置

在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 in 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 in namespace has restarted more than 5 times in the last 10 minutes."

4. 可视化和告警管理

使用Grafana可以创建自定义的仪表盘,并查看告警状态。

访问Grafana

kubectl port-forward -n monitoring grafana-prometheus-operator-grafana-0 3000

然后在浏览器中访问 http://<node-ip>:3000,使用配置的Grafana账号登录。

5. 其他监控工具和方案

除了Prometheus和Grafana,还可以考虑以下方案:

通过上述步骤,可以在CentOS上的Kubernetes集群中实现有效的监控和告警。选择合适的工具和方案,可以显著提升集群的可管理性和稳定性。

0
看了该问题的人还看了