Kubernetes1.22 安装 prometheus 及自定义暴露指标 grafana

添加prometheus-community到repo

helm repo add prometheus-community https://prometheus-community.github.io/helm-charts

 更新repo

helm repo update

创建命名空间

kubectl create ns prometheus-stack

安装

helm -n prometheus-stack install  kube-prometheus-stack prometheus-community/kube-prometheus-stack

 安装完成效果如下图:

什么是服务发现?

我们在每个节点上面都运行了 node-exporter,如果我们通过一个 Service 来将数据收集到一起用静态配置的方式配置到 Prometheus 去中,就只会显示一条数据,我们得自己在指标数据中去过滤每个节点的数据,当然我们也可以手动的把所有节点用静态的方式配置到 Prometheus 中去,但是以后要新增或者去掉节点的时候就还得手动去配置,那么有没有一种方式可以让 Prometheus 去自动发现我们节点的 node-exporter 程序,并且按节点进行分组呢?这就是 Prometheus 里面非常重要的「服务发现」功能。

配置参考链接:https://prometheus.io/docs/prometheus/latest/configuration/configuration/

创建自动发现规则

我们定义的 Prometheus 的配置如下 prometheus-additional.yaml

  • endpoints role 从每个服务监听的 endpoints 发现,每个 endpoint 都会发现一个port,
  • 如果 endpoint 是一个pod,所有包含的容器不被绑定到一个 endpoint port,也会被 targets 被发现。
  • 要想自动发现集群中的 Service,就需要我们在 Service 的 annotation 区域添加 prometheus.io/scrape=true 的声明
- job_name: 'kubernetes-endpoints'
  kubernetes_sd_configs:
    - role: endpoints
  relabel_configs:
    - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scrape]
      action: keep
      regex: true
    - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scheme]
      action: replace
      target_label: __scheme__
      regex: (https?)
    - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_path]
      action: replace
      target_label: __metrics_path__
      regex: (.+)
    - source_labels: [__address__, __meta_kubernetes_service_annotation_prometheus_io_port]
      action: replace
      target_label: __address__
      regex: ([^:]+)(?::\d+)?;(\d+)
      replacement: $1:$2
    - action: labelmap
      regex: __meta_kubernetes_service_label_(.+)
    - source_labels: [__meta_kubernetes_namespace]
      action: replace
      target_label: kubernetes_namespace
    - source_labels: [__meta_kubernetes_service_name]
      action: replace
      target_label: kubernetes_name
    - source_labels: [__meta_kubernetes_pod_name]
      action: replace
      target_label: kubernetes_pod_name

创建新的 secret additional-configs 从文件 prometheus-additional.yaml

kubectl create secret generic additional-configs --from-file=prometheus-additional.yaml -n  prometheus-stack

注入Prometheus

然后我们需要在声明 prometheus 的资源对象文件中通过 additionalScrapeConfigs 属性添加上这个额外的配置:

kubectl edit prometheuses -n prometheus-stack

additionalScrapeConfigs:

      key: prometheus-additional.yaml

      name: additional-configs

创建rbac 

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: prometheus-k8s
rules:
  - apiGroups:
      - ""
    resources:
      - nodes
      - services
      - endpoints
      - pods
      - nodes/proxy
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - ""
    resources:
      - configmaps
      - nodes/metrics
    verbs:
      - get
  - nonResourceURLs:
      - /metrics
    verbs:
      - get

在svc上打上annoation

kind: Service
apiVersion: v1
metadata:
  name: myapp-svc-nodeport
  annotations:
    prometheus.io/port: "8080"
    prometheus.io/scrape: "true"
spec:
  type: NodePort
  selector:
    app: myweb-nginx
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080

deployment文件配置如下:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myweb-deploy
  labels:
    app: myweb-deploy
spec:
  selector:
    matchLabels:
      app: myweb-nginx # 关联template下的label的pod
  replicas: 2
  template:
    metadata:
      labels:
        app: myweb-nginx
    spec:
      volumes:
      - name: data
        hostPath:
          path: /tmp
          type: Directory
      containers:
      - name: go-httpserver
        image: itxiaoq733/httpserver:0.2
        ports:
          - containerPort: 8080
        volumeMounts:
          - name: data
            mountPath: /data
        readinessProbe: # 探活指针-是否pod已就绪
          failureThreshold: 3
          httpGet:
            path: /healthz
            port: 8080
          initialDelaySeconds: 30 #延迟检测时间
          periodSeconds: 5 #检测时间间隔
        lifecycle:
          postStart:
            exec:
              command: ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]
          preStop:
            exec:
              command: ["/bin/sh","-c","echo Hello from the preStop handler > /usr/share/message"]
        resources: # 资源需求
          limits: # 最高 cgroup 控制
            memory: 512Mi
            cpu: 1
          requests: # 最小 调度要求
            memory: 256Mi
            cpu: 100m

安装完成后效果如下:

kubectl get svc -n prometheus-stack

 

查看Prometheus原生监控图

kubectl edit svc kube-prometheus-stack-grafana -n prometheus-stack -oyaml

kubectl edit svc kube-prometheus-stack-prometheus -n prometheus-stack -oyaml

把service的type改为NodePort,访问测试(注意:测试完成之后要改回为ClusterIP)

 

访问界面方式:服务器公网Ip:9090对应的nodeport端口

效果图如下:

 该项与prometheus_additional.yaml文件的job_name一致,则自定义暴露指标设置成功。

然后通过PromQL基本语法输出折线图

sum(rate(web_request_duration_seconds_bucket[60m])) 

Grafana

默认账号密码 admin/prom-operator

 完整效果如下:

把刚刚那俩个service改回为ClusterIP

完美收工!!! 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值