大家好,我是小悟。

Kubernetes是一个强大的容器编排平台,能大幅简化大规模、容器化应用的部署和管理。通过它搭建性能监控系统,可以实现高度的自动化和弹性。

下面是详细的步骤和说明,帮你从零开始,在Kubernetes上建立一个基于Prometheus、Grafana和Alertmanager的现代化性能监控系统。

第一步:部署 Prometheus 和 Alertmanager

Prometheus 是核心的监控数据抓取、存储和告警引擎。

1. 创建命名空间 将监控相关组件隔离到一个独立的命名空间中。

# 01-namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
  name: monitoring

2. 部署 Prometheus 这里使用一个简化配置的Deployment示例。实际生产应使用StatefulSet管理数据持久化。

# 02-prometheus-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: prometheus-server
  namespace: monitoring
spec:
  replicas: 1
  selector:
    matchLabels:
      app: prometheus
  template:
    metadata:
      labels:
        app: prometheus
    spec:
      containers:
      - name: prometheus
        image: prom/prometheus:latest
        args:
          - "--config.file=/etc/prometheus/prometheus.yml"
          - "--storage.tsdb.path=/prometheus/data"
        ports:
        - containerPort: 9090
        volumeMounts:
        - name: prometheus-config-volume
          mountPath: /etc/prometheus
        - name: prometheus-storage-volume
          mountPath: /prometheus/data
      volumes:
      - name: prometheus-config-volume
        configMap:
          name: prometheus-config
      - name: prometheus-storage-volume
        emptyDir: {}
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-config
  namespace: monitoring
data:
  prometheus.yml: |
    global:
      scrape_interval: 15s
    scrape_configs:
      - job_name: 'kubernetes-pods'
        kubernetes_sd_configs:
        - role: pod
        relabel_configs:
        - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
          action: keep
          regex: true

3. 部署 Alertmanager Alertmanager负责处理Prometheus发送的告警,并进行分组、抑制和路由。

# 03-alertmanager-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: alertmanager
  namespace: monitoring
spec:
  replicas: 1
  selector:
    matchLabels:
      app: alertmanager
  template:
    metadata:
      labels:
        app: alertmanager
    spec:
      containers:
      - name: alertmanager
        image: prom/alertmanager:latest
        ports:
        - containerPort: 9093

4. 暴露服务 为Prometheus和Alertmanager创建Service,以便在集群内访问。

# 04-prometheus-alertmanager-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: prometheus-service
  namespace: monitoring
spec:
  selector:
    app: prometheus
  ports:
  - port: 80
    targetPort: 9090

---
apiVersion: v1
kind: Service
metadata:
  name: alertmanager-service
  namespace: monitoring
spec:
  selector:
    app: alertmanager
  ports:
  - port: 80
    targetPort: 9093

应用以上配置:

kubectl apply -f 01-namespace.yaml
kubectl apply -f 02-prometheus-deployment.yaml
kubectl apply -f 03-alertmanager-deployment.yaml
kubectl apply -f 04-prometheus-alertmanager-service.yaml

第二步:部署 Grafana 数据可视化

Grafana 是强大的数据可视化工具,用于展示从 Prometheus 查询到的监控图表。

1. 部署 Grafana 同样以Deployment形式部署,并通过ConfigMap预设数据源。

# 05-grafana-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: grafana
  namespace: monitoring
spec:
  replicas: 1
  selector:
    matchLabels:
      app: grafana
  template:
    metadata:
      labels:
        app: grafana
    spec:
      containers:
      - name: grafana
        image: grafana/grafana:latest
        ports:
        - containerPort: 3000
        env:
        - name: GF_SECURITY_ADMIN_PASSWORD
          value: "admin" # 生产环境请使用Secret管理密码
        volumeMounts:
        - name: grafana-datasources-volume
          mountPath: /etc/grafana/provisioning/datasources
      volumes:
      - name: grafana-datasources-volume
        configMap:
          name: grafana-datasources
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: grafana-datasources
  namespace: monitoring
data:
  prometheus.yaml: |
    apiVersion: 1
    datasources:
    - name: Prometheus
      type: prometheus
      url: 
      access: proxy
      isDefault: true

2. 暴露 Grafana 服务 创建Service,并为了方便访问,可额外创建一个NodePort或LoadBalancer类型的服务。

# 06-grafana-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: grafana-service
  namespace: monitoring
spec:
  selector:
    app: grafana
  ports:
  - port: 80
    targetPort: 3000

应用配置:

kubectl apply -f 05-grafana-deployment.yaml
kubectl apply -f 06-grafana-service.yaml

第三步:部署 Node Exporter

要监控Kubernetes节点的资源使用情况(CPU、内存、磁盘等),需要在每个节点上部署Node Exporter。

# 07-node-exporter-daemonset.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: node-exporter
  namespace: monitoring
spec:
  selector:
    matchLabels:
      app: node-exporter
  template:
    metadata:
      labels:
        app: node-exporter
    spec:
      hostNetwork: true
      hostPID: true
      containers:
      - name: node-exporter
        image: prom/node-exporter:latest
        args:
        - "--path.rootfs=/host"
        ports:
        - containerPort: 9100
        volumeMounts:
        - name: host-root
          mountPath: /host
          readOnly: true
      volumes:
      - name: host-root
        hostPath:
          path: /

应用配置:

kubectl apply -f 07-node-exporter-daemonset.yaml

第四步:集成与验证

所有组件部署完成后,需要进行验证和集成。

  1. 检查Pod状态:确保所有Pod都处于Running状态。

    kubectl get pods -n monitoring
    
  2. 访问 Grafana 仪表盘

    • 获取访问地址:kubectl get svc grafana-service -n monitoring -o wide
    • 使用默认用户名 admin 和你在YAML中设置的密码登录。
    • 在Grafana中导入官方或社区的Prometheus监控仪表盘(如ID为 1860 的“Node Exporter Full”仪表盘),即可看到丰富的节点监控图表。
  3. 配置告警规则:在Prometheus配置中添加自定义告警规则,当指标(如节点内存使用率 > 90%)触发阈值时,告警会被发送到Alertmanager进行处理和通知。

总结与进阶

通过以上步骤,已经成功在Kubernetes上搭建了一个具备监控数据采集(Prometheus)、可视化展示(Grafana)和告警管理(Alertmanager) 核心能力的系统。

这个系统充分利用了Kubernetes的声明式API和自动化管理能力,只需描述组件的期望状态,Kubernetes就会负责调度、部署和维护。

生产环境注意事项

这个示例是入门级配置,面向生产环境时,必须考虑以下几点:

考虑方面生产环境建议
数据持久化为Prometheus和Grafana配置PersistentVolume (PV)PersistentVolumeClaim (PVC) ,确保监控数据在Pod重启后不丢失。
高可用性部署多个Prometheus、Alertmanager和Grafana副本,并结合反亲和性策略,将它们调度到不同的节点上,避免单点故障。
安全性为Pod配置合理的资源请求和限制;使用RBAC 角色严格控制访问权限;敏感信息(如密码)务必使用Secret 对象管理。
可扩展性遵循微服务模式,可以通过配置让Prometheus自动发现Kubernetes中的Pod、Service等资源进行监控。对于更复杂的监控需求,可以研究使用 Prometheus Operator 来简化整个监控栈的声明式管理。
外部访问使用Ingress Controller 或云服务商的LoadBalancer 来安全、统一地暴露Grafana等服务,而不是简单的NodePort。

这套基于Kubernetes的监控方案性能强大、扩展灵活、与云原生生态紧密结合。可以根据实际业务需求,逐步添加对特定应用、中间件(如MySQL、Redis)或自定义业务指标的监控,构建起全方位的可观测性体系。

谢谢你看我的文章,既然看到这里了,如果觉得不错,随手点个赞、转发、在看三连吧,感谢感谢。那我们,下次再见。

您的一键三连,是我更新的最大动力,谢谢

山水有相逢,来日皆可期,谢谢阅读,我们再会

我手中的金箍棒,上能通天,下能探海

本站提供的所有下载资源均来自互联网,仅提供学习交流使用,版权归原作者所有。如需商业使用,请联系原作者获得授权。 如您发现有涉嫌侵权的内容,请联系我们 邮箱:alixiixcom@163.com