小哥之哥 小哥之哥
首页
    • Prometheus
    • Kubertenes
    • Docker
    • MySQL
  • Go
  • Python
  • Vue
  • Jenkins
  • ELK
  • LDAP
  • 随笔
  • 最佳实践
  • 博客搭建
  • 问题杂谈
关于
友链
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

小哥之哥

运维扫地僧
首页
    • Prometheus
    • Kubertenes
    • Docker
    • MySQL
  • Go
  • Python
  • Vue
  • Jenkins
  • ELK
  • LDAP
  • 随笔
  • 最佳实践
  • 博客搭建
  • 问题杂谈
关于
友链
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Kubertenes

  • Prometheus

    • Prometheus介绍
    • Prometheus安装部署
    • PromQL学习
    • kube-prometheus 实战指南
    • Prometheus高可用-Thanos
    • Prometheus监控JVM实战
    • Alertmanager报警历史持久化
    • Kubernetes监控的自定义部署实践【未完整版】
      • Prometheus远程存储之VictoriaMetrics
      • Prometheus数据迁移至VMstorage
    • Docker

    • 数据库

    • 运维利器

    • 运维
    • Prometheus
    tchua
    2024-03-22
    目录

    Kubernetes监控的自定义部署实践【未完整版】

    # 一、概述

    在Kubernetes生态系统中,Prometheus已成为监控服务的首选工具,它提供了强大的数据采集和查询能力。虽然Prometheus Operator极大地简化了Prometheus监控栈的部署与管理,但在某些场景下,如资源受限的环境、特定的安全要求或是为了减少监控系统的复杂性,我们可能需要选择非Prometheus Operator的方式来部署监控系统。

    如果想使用Prometheus Operator的方式,可以参考kube-prometheus 实战指南 (opens new window)

    # 二、核心服务
    • Prometheus Server
      • Prometheus Server是整个监控系统的核心,负责抓取时间序列数据、存储数据、执行PromQL查询以及触发警报。
    • Node Exporter
      • Node Exporter用于收集Kubernetes节点的硬件和操作系统层面的指标,如CPU、内存、磁盘和网络统计。它通常以DaemonSet的形式部署在每个节点上,以确保全面覆盖。
    • kube-state-metrics
      • kube-state-metrics从Kubernetes API服务器中提取状态信息,并将其转换为Prometheus可抓取的格式。它提供关于Pods、Services、Deployments等Kubernetes资源的指标。
    • cAdvisor
      • cAdvisor是一个容器分析器,可以收集容器和节点的资源使用情况。cAdvisor作为kubelet内置的一部分程序可以直接使用,也就是我们可以直接使用cadvisor采集数据,可以采集到和容器运行相关的所有指标,数据路径为/api/v1/nodes/[节点名称]/proxy/metrics/cadvisor

    注意

    对于Kubernetes集群本身的状态监控,我们可以利用系统组件如etcd、kubelet、kube-apiserver、kube-controller-manager和kube-scheduler等自带的监控能力。这些组件通常会在/metrics路径下暴露其内部状态和性能指标,涵盖集群的运行状况、资源分配、调度决策等多方面信息。

    # 三、监控组件部署

    为了方便管理,我们把所有涉及到Prometheus相关的服务都部署至monitor命名空间,因此在创建下面的资源之前,先确保集群中已创建命名空间monitor,否则执行如下命令:

    kubectl create ns monitor
    
    1
    # 3.1 Node-exporter

    通过DaemonSet方式部署,采集 k8s 集群中各个节点的物理指标。

    • 资源清单文件, node-exporter-dep.yaml
    apiVersion: apps/v1
    kind: DaemonSet
    metadata:
      namespace: monitor
      name: node-exporter
      labels:
        name: node-exporter
    spec:
      selector:
        matchLabels:
          name: node-exporter
      template:
        metadata:
          labels:
            name: node-exporter
        spec:
          hostPID: true
          hostIPC: true
          hostNetwork: true
          containers:
          - name: node-exporter
            image: prom/node-exporter:latest
            ports:
            - containerPort: 9100
            resources:
              requests:
                cpu: 1
            securityContext:
              privileged: true
            args:
            - --path.procfs
            - /host/proc
            - --path.sysfs
            - /host/sys
            - --collector.filesystem.ignored-mount-points
            - '"^/(sys|proc|dev|host|etc)($|/)"'
            volumeMounts:
            - name: dev
              mountPath: /host/dev
            - name: proc
              mountPath: /host/proc
            - name: sys
              mountPath: /host/sys
            - name: rootfs
              mountPath: /rootfs
          tolerations:
          - key: "node-role.kubernetes.io/control-plane"
            operator: "Exists"
            effect: "NoSchedule"
          volumes:
            - name: proc
              hostPath:
                path: /proc
            - name: dev
              hostPath:
                path: /dev
            - name: sys
              hostPath:
                path: /sys
            - name: rootfs
              hostPath:
                path: /
    
    ---
    kind: Service
    apiVersion: v1
    metadata:
      labels:
        app: node-exporter
      name: node-exporter-service
      namespace: monitor
    spec:
      ports:
        - name:	http
          port: 9100
          nodePort: 31672
          protocol: TCP
      type: NodePort
      selector:
        app: node-exporter
    
    
    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

    注意

    从v1.20开始,Kubernetes为了更准确地描述master节点的角色和职责,将原有的node-role.kubernetes.io/master标签改为了node-role.kubernetes.io/control-plane。这一改变是为了更好地反映这些节点作为集群控制平面的功能,而不仅仅是"master"的概念。同时,这一改动也有助于提高Kubernetes架构的可理解性和一致性。因此tolerations这里需要自己根据集群标签情况,进行修改,确保即使在控制平面节点上也能运行。

    # 3.2 kube-state-metrics

    kube-state-metrics部署时我们需要特别注意集群兼容版本,点击查看 (opens new window)

    kube-state-metrics主要用于收集Kubernetes API Server中的资源状态并将其转换为Prometheus可抓取的指标格式。为了能够访问Kubernetes API Server中的各种资源信息,kube-state-metrics需要一定的权限,这通过RBAC(Role-Based Access Control)机制来实现。

    • 定义RBAC,清单文件kube-state-metrics-rbac.yaml
    点击查看完整`kube-state-metrics-rbac.yaml`文件
    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRole
    metadata:
      labels:
        app.kubernetes.io/name: kube-state-metrics
        app.kubernetes.io/version: v2.11.0
      name: kube-state-metrics
    rules:
    - apiGroups:
      - ""
      resources:
      - configmaps
      - secrets
      - nodes
      - pods
      - services
      - resourcequotas
      - replicationcontrollers
      - limitranges
      - persistentvolumeclaims
      - persistentvolumes
      - namespaces
      - endpoints
      verbs:
      - list
      - watch
    - apiGroups:
      - extensions
      resources:
      - daemonsets
      - deployments
      - replicasets
      - ingresses
      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:
      - authentication.k8s.io
      resources:
      - tokenreviews
      verbs:
      - create
    - apiGroups:
      - authorization.k8s.io
      resources:
      - subjectaccessreviews
      verbs:
      - create
    - apiGroups:
      - policy
      resources:
      - poddisruptionbudgets
      verbs:
      - list
      - watch
    - apiGroups:
      - certificates.k8s.io
      resources:
      - certificatesigningrequests
      verbs:
      - list
      - watch
    - apiGroups:
      - storage.k8s.io
      resources:
      - storageclasses
      - volumeattachments
      verbs:
      - list
      - watch
    - apiGroups:
      - admissionregistration.k8s.io
      resources:
      - mutatingwebhookconfigurations
      - validatingwebhookconfigurations
      verbs:
      - list
      - watch
    - apiGroups:
      - networking.k8s.io
      resources:
      - networkpolicies
      verbs:
      - list
      - watch
    - apiGroups:
      - coordination.k8s.io
      resources:
      - leases
      verbs:
      - list
      - watch
      
    ---
    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRoleBinding
    metadata:
      labels:
        app.kubernetes.io/name: kube-state-metrics
        app.kubernetes.io/version: v2.11.0
      name: kube-state-metrics
    roleRef:
      apiGroup: rbac.authorization.k8s.io
      kind: ClusterRole
      name: kube-state-metrics
    subjects:
    - kind: ServiceAccount
      name: kube-state-metrics
      namespace: monitor
      
    ---
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      labels:
        app.kubernetes.io/name: kube-state-metrics
        app.kubernetes.io/version: v2.11.0
      name: kube-state-metrics
      namespace: monitor
    
    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

    参数说明

    这段配置定义了Kubernetes集群中用于kube-state-metrics的RBAC(Role-Based Access Control)权限设置。具体包含了三个部分:ClusterRole、ClusterRoleBinding 和 ServiceAccount。

    • ClusterRole :定义了kube-state-metrics服务在整个集群范围内需要的权限。这里的权限非常广泛,几乎涵盖了Kubernetes API中所有的资源类型。当然这些资源的权限都集中在list和watch动词上,意味着kube-state-metrics只能列出和观察这些资源,但不能修改或删除它们。
    • ClusterRoleBinding:将ClusterRole与一个ServiceAccount关联起来,使得该ServiceAccount能够使用ClusterRole中定义的权限。这里的ServiceAccount是kube-state-metrics,位于kube-system命名空间中。
    • ServiceAccount :定义了kube-state-metrics运行时使用的身份。通过这个ServiceAccount,kube-state-metrics可以以一种安全且受控的方式访问Kubernetes API。
    • deployment资源清单
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        app.kubernetes.io/name: kube-state-metrics
        app.kubernetes.io/version: v2.11.0
      name: kube-state-metrics
      namespace: monitor
    spec:
      replicas: 1
      selector:
        matchLabels:
          app.kubernetes.io/name: kube-state-metrics
      template:
        metadata:
          labels:
            app.kubernetes.io/name: kube-state-metrics
            app.kubernetes.io/version: v2.11.0
        spec:
          containers:
          - image: registry.k8s.io/kube-state-metrics/kube-state-metrics:v2.11.0
            livenessProbe:
              httpGet:
                path: /healthz
                port: 8080
              initialDelaySeconds: 5
              timeoutSeconds: 5
            name: kube-state-metrics
            ports:
            - containerPort: 8080
              name: http-metrics
            - containerPort: 8081
              name: telemetry
            args:
            - --metric-labels-allowlist=pods=[*]
            readinessProbe:
              httpGet:
                path: /
                port: 8081
              initialDelaySeconds: 5
              timeoutSeconds: 5
            securityContext:
              runAsUser: 65534
          serviceAccountName: 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

    注意

    如果 本地无法获取registry.k8s.io地址镜像,可参考kube-state-metrics自定义镜像 (opens new window)

    # 3.1 Prometheus

    在部署Prometheus时,必须创建和配置适当的RBAC资源,如Roles、ClusterRoles、RoleBindings和ClusterRoleBindings,以确保Prometheus Server及其相关组件能够安全地访问和监控Kubernetes集群内的所有必要资源

    • 定义RBAC,文件吗prometeus-rbac.yaml
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: prometheus
    ---
    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRole
    metadata:
      name: prometheus
    rules:
      - apiGroups: [""]
        resources:
          - nodes
          - nodes/proxy
          - services
          - endpoints
          - pods
        verbs:
          - get
          - watch
          - list
      - apiGroups:
          - extensions
        resources:
          - ingresses
        verbs:
          - get
          - watch
          - list
      - nonResourceURLs: ["/metrics"]
        verbs:
          - get
    ---
    kind: ClusterRoleBinding
    apiVersion: rbac.authorization.k8s.io/v1
    metadata:
      name: prometheus
      namespace: default
      labels:
        app: prometheus
    subjects:
    - kind: ServiceAccount
      name: prometheus
      namespace: default
    roleRef:
      kind: ClusterRole
      name: prometheus
      apiGroup: ""
    
    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
    文件说明
    • ServiceAccount: 定义了一个名为prometheus的服务账户,这是Prometheus服务在Kubernetes中运行的身份。
    • ClusterRole: 定义了一个名为prometheus的集群范围的角色,允许执行以下操作:
      • 访问服务(services)、Pods、节点(nodes)及其代理(nodes/proxy)、端点(endpoints),权限包括获取(get)、列出(list)和观察(watch)。
      • 创建、获取、更新和删除名为prometheus-config的配置映射(configmaps)。
      • 访问非资源URL/metrics,用于获取Prometheus自身的监控数据。
    • ClusterRoleBinding: 将prometheus角色绑定到prometheus服务账户,使该服务账户能够执行ClusterRole中定义的所有权限。
    • 定义

    https://www.cnblogs.com/liugp/p/16704650.html

    https://www.cnblogs.com/hujinzhong/p/15000626.html

    编辑 (opens new window)
    #Prometheus
    上次更新: 2024/09/24, 11:04:43
    Alertmanager报警历史持久化
    Prometheus远程存储之VictoriaMetrics

    ← Alertmanager报警历史持久化 Prometheus远程存储之VictoriaMetrics→

    最近更新
    01
    cert-manager自动签发Lets Encrypt
    09-05
    02
    Docker构建多架构镜像
    08-02
    03
    Prometheus数据迁移至VMstorage
    08-01
    更多文章>
    Theme by Vdoing | Copyright © 2023-2024 |豫ICP备2021026650号
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式