在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集群的運行情況。根據你的需求選擇合適的工具和方法。