在 CentOS 上部署 Kubernetes(k8s)后,监控通常从“集群层面 + 节点层面 + 应用层面”三块入手。下面给你一套生产常用、可落地的方案,从简单到完整。
在 Kubernetes 之前,先保证 CentOS 节点是被监控的。
# 在每台 CentOS 节点上
docker run -d \
--net="host" \
--pid="host" \
-v "/:/host:ro,rslave" \
quay.io/prometheus/node-exporter:latest \
--path.rootfs=/host
采集指标:
✅ 适合监控 CentOS 系统本身
✅ kube-prometheus-stack(Helm 部署,最省事)
# 添加 Helm 仓库
helm repo add prometheus-community \
https://prometheus-community.github.io/helm-charts
helm repo update
# 安装
helm install kube-prometheus prometheus-community/kube-prometheus-stack \
-n monitoring --create-namespace
包含内容:
kubectl get pods -n monitoring
kubectl get svc -n monitoring
访问 Grafana:
kubectl port-forward svc/kube-prometheus-grafana 3000:80 -n monitoring
浏览器访问:
http://localhost:3000
默认账号:
# CPU 使用率
sum(rate(container_cpu_usage_seconds_total[5m])) by (pod)
# 内存使用
sum(container_memory_usage_bytes) by (pod)
# Pod 重启次数
increase(kube_pod_container_status_restarts_total[10m])
Promtail + Loki + Grafana
helm install loki grafana/loki-stack -n logging --create-namespace
功能:
常见告警:
示例:
- alert: NodeNotReady
expr: kube_node_status_condition{condition="Ready",status="true"} == 0
for: 5m
支持:
kubectl apply -f \
https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml
适合:
❌ 不替代 Prometheus
systemctl stop firewalld
systemctl disable firewalld
yum install -y chrony
systemctl enable chronyd
cat >> /etc/sysctl.conf <<EOF
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
sysctl -p
✅ 生产推荐
kube-prometheus-stack + Loki + Grafana
✅ 监控覆盖
如果你愿意,可以告诉我:
我可以直接给你 一套完整部署 YAML + 告警规则。