在CentOS上监控Kubernetes(k8s)集群有多种方法和工具可以使用。以下是一些常用的监控工具和步骤:
kubectl
是 Kubernetes 的默认命令行工具,可以用来查询和管理集群中的资源。
kubectl get nodes
kubectl get pods --all-namespaces
kubectl get deployments --all-namespaces
kubectl get services --all-namespaces
kubectl get events --all-namespaces
Kubernetes Dashboard是一个基于Web的UI,可以提供集群的实时监控和管理功能。
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.5.1/aio/deploy/recommended.yaml
https://<node-ip>:30000
,使用默认用户名和密码(通常是 admin/admin
)登录。Prometheus是一个开源的监控系统和时间序列数据库,Grafana是一个开源的分析和监测平台。两者结合可以提供强大的监控和可视化功能。
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 prometheus-community/kube-prometheus-stack
helm install grafana grafana/grafana
编辑 prometheus/values.yaml
文件,配置抓取的目标(包括 kube-state-metrics
、node
等)。
apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
name: my-prometheus
spec:
replicas: 2
serviceAccountName: prometheus-k8s
serviceMonitorSelector: {}
resources:
requests:
memory: "400Mi"
ruleSelector:
matchLabels:
prometheus: k8s
alerting:
alertmanagers:
- namespace: monitoring
name: alertmanager-main
port: web
storage:
storageClassName: default
volumeClaimTemplate:
spec:
resources:
requests:
storage: "10Gi"
ingress:
enabled: false
kubectl apply -f prometheus.yaml
在Grafana界面中添加Prometheus数据源,填入Prometheus的服务地址(如 http://my-prometheus.monitoring.svc.cluster.local
)。创建一个新的Dashboard,并添加各种Kubernetes监控面板,如节点状态、Pod状态、资源使用情况等。
kube-state-metrics
收集 Kubernetes 集群内资源对象的数据,如Deployment、StatefulSet 和 DaemonSet 的状态。
kubectl apply -f https://github.com/kubernetes-sigs/kube-state-metrics/releases/latest/download/components.yaml
还有一些第三方监控工具,如 Datadog、New Relic、Dynatrace 等,它们提供了更高级的监控和报警功能。
通过以上方法,你可以全面监控CentOS上的Kubernetes集群的运行情况。根据你的需求选择合适的工具和方法。