• 外部 prometheus监控k8s集群资源


    二进制安装的prometheus,监控k8s集群信息。

    监控指标实现方式举例
    Pod资源利用率cAdvisor容器CPU、内存利用率
    K8s资源状态kube-state-metricscontroller控制器、Node、Namespace、Pod、ReplicaSet、service等

    一,通过CADvisior 监控pod的资源状态

    1.1 授权外边用户可以访问prometheus接口。

    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: prometheus
      namespace: kube-system
    ---
    apiVersion: rbac.authorization.k8s.io/v1beta1
    kind: ClusterRole
    metadata:
      name: prometheus
    rules:
    - apiGroups:
      - ""
      resources:
      - nodes
      - services
      - endpoints
      - pods
      - nodes/proxy
      verbs:
      - get
      - list
      - watch
    - apiGroups:
      - "extensions"
      resources:
        - ingresses
      verbs:
      - get
      - list
      - watch
    - apiGroups:
      - ""
      resources:
      - configmaps
      - nodes/metrics
      verbs:
      - get
    - nonResourceURLs:
      - /metrics
      verbs:
      - get
    ---
    apiVersion: rbac.authorization.k8s.io/v1beta1
    kind: ClusterRoleBinding
    metadata:
      name: prometheus
    roleRef:
      apiGroup: rbac.authorization.k8s.io
      kind: ClusterRole
      name: prometheus
    subjects:
    - kind: ServiceAccount
      name: prometheus
      namespace: kube-system
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    kubectl apply -f rbac.yaml
    
    • 1

    1.2 获取token保存

    kubectl get secrets -n kube-system |grep prometheus #查看toekn name
    name:prometheus-token-vgxhc
    
    kubectl describe secret prometheus-token-vgxhc -n kube-system > token.k8s
    #kubectl get secrets -n kube-system -o yaml prometheus-token-vgxhc |grep token
    scp token.k8s prometheus #拷贝到prometheus服务器prometheus的目录下
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    我的token放在 /opt/monitor/prometheus/token.k8s

    1.3 配置prometheus.yml 启动并查看状态

    vim prometheus.yml

      - job_name: kubernetes-nodes-cadvisor
        metrics_path: /metrics
        scheme: https
        kubernetes_sd_configs:
        - role: node
          api_server: https://172.18.0.0:6443
          bearer_token_file: /opt/monitor/prometheus/token.k8s 
          tls_config:
            insecure_skip_verify: true
        bearer_token_file: /opt/monitor/prometheus/token.k8s 
        tls_config:
          insecure_skip_verify: true
        relabel_configs:
        # 将标签(.*)作为新标签名,原有值不变
        - action: labelmap
          regex: __meta_kubernetes_node_label_(.*)
        # 修改NodeIP:10250为APIServerIP:6443
        - action: replace
          regex: (.*)
          source_labels: ["__address__"]
          target_label: __address__
          replacement: 172.18.0.0:6443
        # 实际访问指标接口 https://NodeIP:10250/metrics/cadvisor 这个接口只能APISERVER访问,故此重新标记标签使用APISERVER代理访问
        - action: replace
          source_labels: [__meta_kubernetes_node_name]
          target_label: __metrics_path__
          regex: (.*)
          replacement: /api/v1/nodes/${1}/proxy/metrics/cadvisor 
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    ./promtool check config prometheus.yml 
    重启prometheus 或 kill -HUP PrometheusPid
    
    • 1
    • 2

    在prometheus的target页面查看
    http://172.18.0.0:9090
    在这里插入图片描述

    1.4 Grafana 导入仪表盘

    导入3119 仪表盘
    在这里插入图片描述
    在这里插入图片描述完成pod资源监控

    二,通过kube-state-metrics 监控k8s资源状态

    2.1 部署 kube-state-metrics

    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: kube-state-metrics
      namespace: kube-system
      labels:
        kubernetes.io/cluster-service: "true"
        addonmanager.kubernetes.io/mode: Reconcile
    ---
    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRole
    metadata:
      name: kube-state-metrics
      labels:
        kubernetes.io/cluster-service: "true"
        addonmanager.kubernetes.io/mode: Reconcile
    rules:
    - apiGroups: [""]
      resources:
      - configmaps
      - secrets
      - nodes
      - pods
      - services
      - resourcequotas
      - replicationcontrollers
      - limitranges
      - persistentvolumeclaims
      - persistentvolumes
      - namespaces
      - endpoints
      verbs: ["list", "watch"]
    - apiGroups: ["apps"]
      resources:
      - statefulsets
      - daemonsets
      - deployments
      - replicasets
      verbs: ["list", "watch"]
    - apiGroups: ["batch"]
      resources:
      - cronjobs
      - jobs
      verbs: ["list", "watch"]
    - apiGroups: ["autoscaling"]
      resources:
      - horizontalpodautoscalers
      verbs: ["list", "watch"]
    - apiGroups: ["networking.k8s.io", "extensions"]
      resources:
      - ingresses 
      verbs: ["list", "watch"]
    - apiGroups: ["storage.k8s.io"]
      resources:
      - storageclasses 
      verbs: ["list", "watch"]
    - apiGroups: ["certificates.k8s.io"]
      resources:
      - certificatesigningrequests
      verbs: ["list", "watch"]
    - apiGroups: ["policy"]
      resources:
      - poddisruptionbudgets 
      verbs: ["list", "watch"]
    
    
    
    ---
    apiVersion: rbac.authorization.k8s.io/v1
    kind: Role
    metadata:
      name: kube-state-metrics-resizer
      namespace: kube-system
      labels:
        kubernetes.io/cluster-service: "true"
        addonmanager.kubernetes.io/mode: Reconcile
    rules:
    - apiGroups: [""]
      resources:
      - pods
      verbs: ["get"]
    - apiGroups: ["extensions","apps"]
      resources:
      - deployments
      resourceNames: ["kube-state-metrics"]
      verbs: ["get", "update"]
    ---
    apiVersion: rbac.authorization.k8s.io/v1 
    kind: ClusterRoleBinding
    metadata:
      name: kube-state-metrics
      labels:
        kubernetes.io/cluster-service: "true"
        addonmanager.kubernetes.io/mode: Reconcile
    roleRef:
      apiGroup: rbac.authorization.k8s.io
      kind: ClusterRole
      name: kube-state-metrics
    subjects:
    - kind: ServiceAccount
      name: kube-state-metrics
      namespace: kube-system
    ---
    apiVersion: rbac.authorization.k8s.io/v1
    kind: RoleBinding
    metadata:
      name: kube-state-metrics
      namespace: kube-system
      labels:
        kubernetes.io/cluster-service: "true"
        addonmanager.kubernetes.io/mode: Reconcile
    roleRef:
      apiGroup: rbac.authorization.k8s.io
      kind: Role
      name: kube-state-metrics-resizer
    subjects:
    - kind: ServiceAccount
      name: kube-state-metrics
      namespace: kube-system
    
    ---
    
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: kube-state-metrics
      namespace: kube-system
      labels:
        k8s-app: kube-state-metrics
        kubernetes.io/cluster-service: "true"
        addonmanager.kubernetes.io/mode: Reconcile
        version: v1.3.0
    spec:
      selector:
        matchLabels:
          k8s-app: kube-state-metrics
          version: v1.3.0
      replicas: 1
      template:
        metadata:
          labels:
            k8s-app: kube-state-metrics
            version: v1.3.0
          annotations:
            scheduler.alpha.kubernetes.io/critical-pod: ''
        spec:
          priorityClassName: system-cluster-critical
          serviceAccountName: kube-state-metrics
          containers:
          - name: kube-state-metrics
            image: harbor.cpit.com.cn/monitor/kube-state-metrics:v1.8.0
            ports:
            - name: http-metrics
              containerPort: 8080
            - name: telemetry
              containerPort: 8081
            readinessProbe:
              httpGet:
                path: /healthz
                port: 8080
              initialDelaySeconds: 5
              timeoutSeconds: 5
          - name: addon-resizer
            image: harbor.cpit.com.cn/monitor/addon-resizer:1.8.6
            resources:
              limits:
                cpu: 1000m
                memory: 500Mi
              requests:
                cpu: 1000m
                memory: 500Mi
            env:
              - name: MY_POD_NAME
                valueFrom:
                  fieldRef:
                    fieldPath: metadata.name
              - name: MY_POD_NAMESPACE
                valueFrom:
                  fieldRef:
                    fieldPath: metadata.namespace
            volumeMounts:
              - name: config-volume
                mountPath: /etc/config
            command:
              - /pod_nanny
              - --config-dir=/etc/config
              - --container=kube-state-metrics
              - --cpu=100m
              - --extra-cpu=1m
              - --memory=100Mi
              - --extra-memory=2Mi
              - --threshold=5
              - --deployment=kube-state-metrics
          volumes:
            - name: config-volume
              configMap:
                name: kube-state-metrics-config
    ---
    # Config map for resource configuration.
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: kube-state-metrics-config
      namespace: kube-system
      labels:
        k8s-app: kube-state-metrics
        kubernetes.io/cluster-service: "true"
        addonmanager.kubernetes.io/mode: Reconcile
    data:
      NannyConfiguration: |-
        apiVersion: nannyconfig/v1alpha1
        kind: NannyConfiguration
    
    ---
    
    apiVersion: v1
    kind: Service
    metadata:
      name: kube-state-metrics
      namespace: kube-system
      labels:
        kubernetes.io/cluster-service: "true"
        addonmanager.kubernetes.io/mode: Reconcile
        kubernetes.io/name: "kube-state-metrics"
      annotations:
        prometheus.io/scrape: 'true'
    spec:
      ports:
      - name: http-metrics
        port: 8080
        targetPort: http-metrics
        protocol: TCP
      - name: telemetry
        port: 8081
        targetPort: telemetry
        protocol: TCP
      selector:
        k8s-app: kube-state-metrics
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238

    部署

    kubectl apply -f kube-state-metrics.yaml
    kubectl get pods -n kube-system
    
    • 1
    • 2

    在这里插入图片描述
    pod的正常运行

    2.2 配置prometheus.yml

    
      - job_name: kubernetes-service-endpoints
        kubernetes_sd_configs:
        - role: endpoints
          api_server: https://192.168.0.0:6443
          bearer_token_file: /opt/monitor/prometheus/token.k8s
          tls_config:
            insecure_skip_verify: true
        bearer_token_file: /opt/monitor/prometheus/token.k8s
        tls_config:
          insecure_skip_verify: true
         Service没配置注解prometheus.io/scrape的不采集
        relabel_configs:
        - action: keep
          regex: true
          source_labels:
          - __meta_kubernetes_service_annotation_prometheus_io_scrape
      重命名采集目标协议
        - action: replace
          regex: (https?)
          source_labels:
          - __meta_kubernetes_service_annotation_prometheus_io_scheme
          target_label: __scheme__
      重命名采集目标指标URL路径
        - action: replace
          regex: (.+)
          source_labels:
          - __meta_kubernetes_service_annotation_prometheus_io_path
          target_label: __metrics_path__
      重命名采集目标地址
        - action: replace
          regex: ([^:]+)(?::\d+)?;(\d+)
          replacement: $1:$2
          source_labels:
          - __address__
          - __meta_kubernetes_service_annotation_prometheus_io_port
          target_label: __address__
      将K8s标签(.*)作为新标签名,原有值不变
        - action: labelmap
          regex: __meta_kubernetes_service_label_(.+)
      生成命名空间标签
        - action: replace
          source_labels:
          - __meta_kubernetes_namespace
          target_label: kubernetes_namespace
      生成Service名称标签
        - action: replace
          source_labels:
          - __meta_kubernetes_service_name
          target_label: kubernetes_service_name
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    ./promtool check config prometheus.yml 
    重启prometheus 或 kill -HUP PrometheusPid
    
    • 1
    • 2

    在prometheus的target页面查看
    http://172.18.0.0:9090
    在这里插入图片描述

    2.3 Grafana 导入仪表盘

    Grafana导入k8s集群资源对象监控仪表盘 6417

    在这里插入图片描述
    完成k8s集群资源对象监控仪表盘监控

    2.4 Grafana没有数据,添加路由转发

    ip route
    ip route add 172.40.0.0/16 via 172.18.2.30 dev eth0
    ip route
    
    • 1
    • 2
    • 3

    #172.40.1.208:kube-state-metrics pod 集群内部ip
    #172.18.2.30:k8s master 节点ip

    然后在查看Grafana仪表盘。

  • 相关阅读:
    苍穹外卖—解决前端时间属性显示问题
    TypeScript系列之类型 string
    nginx代理参数proxy_pass
    golang中的Mutex原理解析
    python基于PHP+MySQL的连锁商店仓库管理系统
    ML XGBoost详细原理及公式推导讲解+面试必考知识点
    document.body.clientHeight获取可视区域高度为0问题解决
    pyotrch学习笔记——模型与数据放到GPU上
    美国每月1-2千美元的工资怎么生活?
    AutoIt的使用(Selenium 上传文件)
  • 原文地址:https://blog.csdn.net/MrFDd/article/details/134535787