k8s之pushgateway-v1.4.2部署

本文介绍了Prometheus PushGateway在监控场景中的应用,如何作为中间件解决跨网络数据获取问题,以及如何配置Prometheus和PushGateway来接收和汇总业务数据。重点讲解了部署、数据发送与Prometheus的整合过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PushGateway简介

  • Pushgateway 是 prometheus 的一个组件, prometheus server默认是通过exporter主动获取数据(默认采取 pull 拉取数据) pushgateway 则是通过被动方式推送数据到 prometheus server,用户可以写一些自定义的监控脚本把需要监控的数据发送给 pushgateway 然后pushgateway 再把数据发送给 Prometheus server
  • Prometheus默认采用定时 pull 模式拉取 targets 数据,但是如果不在一个子网或者防火墙,prometheus就拉取不到 targets 数据,所以可以采用各个 target 往 pushgateway 上 push 数据,然后 prometheus 去 pushgateway 上定时 pull 数据
  • 在监控业务数据的时候,需要将不同数据汇总, 汇总之后的数据可以由 pushgateway 统一收集,然后由 Prometheus 统一拉取
  • Prometheus拉取状态只针对 pushgateway, 不能对每个节点都有效;Pushg ateway 出现问题,整个采集到的数据都会出现问题,监控下线,prometheus 还会拉取到旧的监控数据,需要手动清理 pushgateway 不要的数据

Github链接:https://github.com/prometheus/pushgateway

PushGateway-v1.4.2部署

主机IP
k8s-master-1(Prometheus-server)192.168.0.10
k8s-node-1(Push-Gateway)192.168.0.11

Prometheus-Server

[root@k8s-master-1 pushgateway]# cat prometheus-server.yaml 
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: prometheus
rules:
- apiGroups: [""]
  resources: ["nodes","nodes/proxy","services","endpoints","pods"]
  verbs: ["get", "list", "watch"]
- apiGroups: ["extensions"]
  resources: ["ingresses"]
  verbs: ["get", "list", "watch"]
- nonResourceURLs: ["/metrics"]
  verbs: ["get"]
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: prometheus
  namespace: kube-prometheus
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: prometheus
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: prometheus
subjects:
- kind: ServiceAccount
  name: prometheus
  namespace: kube-prometheus
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-config
  namespace: kube-prometheus
data:
  prometheus.yml: |-
    global:
      scrape_interval: 15s 
      scrape_timeout: 10s
      evaluation_interval: 1m
    scrape_configs:
    - job_name: 'PushGateway'
      honor_labels: true                  # 必须要加一个参数honor_labels: true,如果不加,会造成推送给Pushgateway的instance和job全部加了"exported_"前缀
      static_configs:
      - targets: ['192.168.0.11:9091']    # Work节点
        labels:
          instance: 'PushGateway'
---
apiVersion: v1
kind: Service
metadata:
  name: prometheus
  namespace: kube-prometheus
  labels:
    name: prometheus-server
spec:
  ports:
  - name: prometheus
    protocol: TCP
    port: 9090
    targetPort: 9090
    nodePort: 40000       # 将其在宿主机暴露端口固定成40000
  selector:
    name: prometheus-server
  type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: prometheus-server
  namespace: kube-prometheus
  labels:
    name: prometheus-server
spec:
  selector:
    matchLabels:
      name: prometheus-server
  template:
    metadata:
      labels:
        name: prometheus-server
    spec:
      nodeName: k8s-master-1                    # 指定在Master节点运行
      serviceAccountName: prometheus
      containers:
      - name: prometheus
        image: prom/prometheus:v2.35.0
        imagePullPolicy: IfNotPresent
        command:
        - prometheus
        - --config.file=/etc/prometheus/prometheus.yml
        - --storage.tsdb.path=/prometheus       # 旧数据存储目录
        - --storage.tsdb.retention=720h         # 何时删除旧数据,默认为15天。
        - --web.enable-lifecycle                # 开启热加载
        ports:
        - containerPort: 9090
          protocol: TCP
        volumeMounts:
        - name: prometheus-storage-volume
          mountPath: /prometheus/
        - name: prometheus-config
          mountPath: /etc/prometheus/prometheus.yml
          subPath: prometheus.yml
        readinessProbe:
          httpGet:
            path: /-/ready
            port: 9090
          failureThreshold: 3
          successThreshold: 1
          periodSeconds: 5
          initialDelaySeconds: 5
          timeoutSeconds: 5
        livenessProbe:
          httpGet:
            path: /-/healthy
            port: 9090
          failureThreshold: 3
          successThreshold: 1
          periodSeconds: 5
          initialDelaySeconds: 15
          timeoutSeconds: 5
      volumes:
      - name: prometheus-config
        configMap:
          name: prometheus-config
      - name: prometheus-storage-volume
        emptyDir: {}

Prometheus-PushGateway

[root@k8s-master-1 pushgateway]# cat prometheus-pushgateway.yaml 
apiVersion: v1
kind: Service
metadata:
  name: prometheus-pushgateway
  namespace: kube-prometheus
spec:
  selector: 
    name: prometheus-pushgateway 
  type: NodePort
  ports:
  - port: 9091
    targetPort: 9091
    nodePort: 42000
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: prometheus-pushgateway
  namespace: kube-prometheus
  labels:
    name: prometheus-pushgateway 
spec:
  selector:
    matchLabels:
      name: prometheus-pushgateway 
  template:
    metadata:
      labels:
        name: prometheus-pushgateway 
    spec:
      nodeName: k8s-node-1                      # 指定在Work节点运行
      hostNetwork: true                         # 将容器端口在宿主机内暴露出来
      containers:
      - name: pushgateway
        image: prom/pushgateway:v1.4.2
        ports:
          - containerPort: 9091

在这里插入图片描述

在这里插入图片描述

发送数据给PushGateway

# 将k8s-master-1 的 node_exporter metrics 发送给pushgateway
[root@k8s-node-1 ~]# curl k8s-master-1:9100/metrics | curl --data-binary @- http://192.168.0.11:9091/metrics/job/k8s_master_1_node_exporter/label1/value1/label2/value2
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 84411    0 84411    0     0  4132k      0 --:--:-- --:--:-- --:--:-- 4338k


# 将k8s-node-1 的 node_exporter metrics 发送给pushgateway
[root@k8s-node-1 ~]# curl k8s-node-1:9100/metrics | curl --data-binary @- http://192.168.0.11:9091/metrics/job/k8s_node_1_node_exporter/label1/value1/label2/value2
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 74858    0 74858    0     0  4897k      0 --:--:-- --:--:-- --:--:-- 5221k

在这里插入图片描述
可以发现,二次上传的数据均传到prometheus-pushgateway
在这里插入图片描述
可以发现prometheus-server通过pushgateway拿到我们手动发送的数据了

KubeSphere是一个开源的企业级 Kubernetes 容器平台,它提供了一个全面的管理界面,用于简化对 Kubernetes 集群的运维部署Prometheus Pushgateway通常是用来解决Prometheus远程写入(remote write)的需求,因为Prometheus默认不支持直接从工作节点收集指标。 要在KubeSphere环境中部署Pushgateway,你可以按照以下步骤操作: 1. **安装依赖**:首先需要确保你的集群已经配置了Prometheus Operator,并且你有权限通过Operator管理资源。 2. **创建Pushgateway Deployment**:使用KubeSphere的UI或者命令行工具(如kubectl),创建一个新的Deployment资源。示例YAML文件可能类似这样: ```yaml apiVersion: monitoring.coreos.com/v1 kind: ServiceMonitor metadata: name: pushgateway labels: app: pushgateway spec: jobName: pushgateway selector: matchLabels: app: pushgateway endpoints: - port: web interval: 15s --- apiVersion: apps/v1 kind: Deployment metadata: name: pushgateway namespace: monitoring spec: replicas: 1 selector: matchLabels: app: pushgateway template: metadata: labels: app: pushgateway spec: containers: - name: pushgateway image: prom/pushgateway:latest ports: - containerPort: 9091 protocol: TCP ``` 3. **配置Service**:为了允许Prometheus访问Pushgateway,还需要创建一个Service,将Pod暴露给外部网络。 ```yaml apiVersion: v1 kind: Service metadata: name: pushgateway labels: app: pushgateway spec: type: ClusterIP ports: - name: http port: 9090 targetPort: 9091 selector: app: pushgateway ``` 4. **验证部署**:运行上述操作后,检查Pod是否成功启动并在Prometheus中添加相应规则来指向Pushgateway
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旺仔_牛奶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值