云原生 | K8s中安装Prometheus和grafana并监控ETCD

发布于:2025-04-07 ⋅ 阅读:(24) ⋅ 点赞:(0)

Prometheus 简介

Prometheus起源

  • Prometheus受启发于Google的Brogmon监控系统(相似的Kubernetes是从Google的Brog系统演变而来),从2012年开始由前Google工程师在Soundcloud以开源软件的形式进行研发,并且于2015年早期对外发布早期版本。2016年5月继Kubernetes之后成为第二个正式加入CNCF基金会的项目,同年6月正式发布1.0版本。2017年底发布了基于全新存储层的2.0版本,能更好地与容器平台、云平台配合。

Prometheus 原理架构图

image-20240526212854062

 

Prometheus 基本原理

Prometheus是一个开源的系统监控和警报工具,它的基本原理包括:

  • 数据抓取: Prometheus Server 主动通过HTTP协议周期性地抓取被监控组件的状态数据。

  • Exporters: 被监控组件通过Exporter暴露其监控数据。Exporter是一个轻量级的程序,它运行在被监控的服务器上,将被监控的数据转换为Prometheus能够理解的格式。

  • 多维数据模型: Prometheus使用多维数据模型,数据由指标名和键值对标签组成,这使得它能够提供灵活的数据查询和聚合。

  • 数据存储: Prometheus Server将采集到的数据存储在本地的时序数据库中。

  • 查询语言: Prometheus提供了强大的PromQL查询语言,用于查询和分析监控数据。

  • 可视化与警报: 通过Grafana等工具进行数据可视化,以及通过Alertmanager进行警报管理。

 K8S中安装Prometheus

 

grafana-deployment.yaml

[root@k8s-master apply]# cat grafana-deployment.yaml
# Deployment
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:9.5.2
        ports:
        - containerPort: 3000
        readinessProbe:
          httpGet:
            path: /api/health
            port: 3000
          initialDelaySeconds: 30
          timeoutSeconds: 5
        volumeMounts:
        - name: grafana-storage
          mountPath: /var/lib/grafana
        - name: grafana-datasources
          mountPath: /etc/grafana/provisioning/datasources
      volumes:
      - name: grafana-storage
        emptyDir: {}
      - name: grafana-datasources
        configMap:
          name: grafana-datasources

---
# DataSource ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
  name: grafana-datasources
  namespace: monitoring
data:
  datasources.yaml: |
    apiVersion: 1
    datasources:
    - name: Prometheus
      type: prometheus
      access: proxy
      url: http://prometheus.monitoring.svc.cluster.local:9090
      isDefault: true

---
# Service
apiVersion: v1
kind: Service
metadata:
  name: grafana
  namespace: monitoring
spec:
  type: NodePort
  selector:
    app: grafana
  ports:
  - port: 3000
    targetPort: 3000
    nodePort: 30300

 node_exporter_daemonset.yaml

[root@k8s-master apply]# cat node_exporter_daemonset.yaml
# DaemonSet + Service
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: node-exporter
  namespace: monitoring
  labels:
    app: node-exporter
spec:
  selector:
    matchLabels:
      app: node-exporter
  template:
    metadata:
      labels:
        app: node-exporter
    spec:
      hostNetwork: true
      hostPID: true
      tolerations:
      - operator: Exists
      containers:
      - name: node-exporter
        image: prom/node-exporter:v1.6.1
        args:
        - --path.procfs=/host/proc
        - --path.sysfs=/host/sys
        - --no-collector.netdev
        ports:
        - containerPort: 9100
          name: http
        volumeMounts:
        - name: proc
          mountPath: /host/proc
        - name: sys
          mountPath: /host/sys
        - name: root
          mountPath: /rootfs
      volumes:
      - name: proc
        hostPath:
          path: /proc
      - name: sys
        hostPath:
          path: /sys
      - name: root
        hostPath:
          path: /

---
apiVersion: v1
kind: Service
metadata:
  name: node-exporter
  namespace: monitoring
  labels:
    app: node-exporter
spec:
  clusterIP: None
  selector:
    app: node-expo

网站公告

今日签到

点亮在社区的每一天
去签到