温馨提示×

k8s集群centos如何监控

小樊
71
2025-07-04 21:20:33
栏目: 智能运维

在CentOS上监控Kubernetes(k8s)集群有多种方法和工具可以使用。以下是一些常用的监控工具和步骤:

使用kubectl命令行工具

kubectl 是 Kubernetes 的默认命令行工具,可以用来查询和管理集群中的资源。

  • 查看节点状态
    kubectl get nodes
    
  • 查看Pod状态
    kubectl get pods --all-namespaces
    
  • 查看Deployment状态
    kubectl get deployments --all-namespaces
    
  • 查看服务状态
    kubectl get services --all-namespaces
    
  • 查看事件日志
    kubectl get events --all-namespaces
    

使用Kubernetes Dashboard

Kubernetes Dashboard是一个基于Web的UI,可以提供集群的实时监控和管理功能。

  • 安装Dashboard
    kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.5.1/aio/deploy/recommended.yaml
    
  • 访问Dashboard: 打开浏览器,访问 https://<node-ip>:30000,使用默认用户名和密码(通常是 admin/admin)登录。

使用Prometheus和Grafana

Prometheus是一个开源的监控系统和时间序列数据库,Grafana是一个开源的分析和监测平台。两者结合可以提供强大的监控和可视化功能。

安装Prometheus和Grafana

  • 安装Helm
    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/
    
  • 添加Prometheus和Grafana的Helm仓库
    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
    
  • 安装Prometheus和Grafana
    helm install prometheus prometheus-community/kube-prometheus-stack
    helm install grafana grafana/grafana
    

配置Prometheus

编辑 prometheus/values.yaml 文件,配置抓取的目标(包括 kube-state-metricsnode 等)。

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

在Grafana界面中添加Prometheus数据源,填入Prometheus的服务地址(如 http://my-prometheus.monitoring.svc.cluster.local)。创建一个新的Dashboard,并添加各种Kubernetes监控面板,如节点状态、Pod状态、资源使用情况等。

使用kube-state-metrics

kube-state-metrics 收集 Kubernetes 集群内资源对象的数据,如Deployment、StatefulSet 和 DaemonSet 的状态。

  • 安装kube-state-metrics
    kubectl apply -f https://github.com/kubernetes-sigs/kube-state-metrics/releases/latest/download/components.yaml
    

使用第三方监控工具

还有一些第三方监控工具,如 Datadog、New Relic、Dynatrace 等,它们提供了更高级的监控和报警功能。

通过以上方法,你可以全面监控CentOS上的Kubernetes集群的运行情况。根据你的需求选择合适的工具和方法。

0