• Centos7 部署Prometheus(Kubernetes)


            最近项目中通过Kubernetes部署Prometheus完成可视化大屏数据采集,特此记录便于日后查阅。

    一、Prometheus部署

            1、deploy.yaml

    1. apiVersion: apps/v1
    2. kind: Deployment
    3. metadata:
    4. labels:
    5. name: prometheus-deployment
    6. name: prometheus
    7. namespace: monitoring
    8. spec:
    9. replicas: 1
    10. selector:
    11. matchLabels:
    12. app: prometheus
    13. template:
    14. metadata:
    15. labels:
    16. app: prometheus
    17. spec:
    18. containers:
    19. - image: prom/prometheus:v2.7.1
    20. name: prometheus
    21. command:
    22. - "/bin/prometheus"
    23. args:
    24. - "--config.file=/etc/prometheus/prometheus.yml"
    25. - "--storage.tsdb.path=/prometheus"
    26. - "--storage.tsdb.retention=72h"
    27. ports:
    28. - containerPort: 9090
    29. protocol: TCP
    30. volumeMounts:
    31. - mountPath: "/prometheus"
    32. name: data
    33. - mountPath: "/etc/prometheus"
    34. name: prometheus-config-volume
    35. resources:
    36. requests:
    37. cpu: 100m
    38. memory: 100Mi
    39. limits:
    40. cpu: 500m
    41. memory: 2500Mi
    42. serviceAccountName: prometheus
    43. volumes:
    44. - name: data
    45. emptyDir: {}
    46. - name: prometheus-config-volume
    47. configMap:
    48. name: prometheus-config
    49. ---
    50. kind: Service
    51. apiVersion: v1
    52. metadata:
    53. labels:
    54. app: prometheus
    55. name: prometheus
    56. namespace: monitoring
    57. spec:
    58. type: NodePort
    59. ports:
    60. - port: 9090
    61. targetPort: 9090
    62. nodePort: 30003
    63. selector:
    64. app: prometheus

            2、configmap.yaml

    1. apiVersion: v1
    2. kind: ConfigMap
    3. metadata:
    4. name: prometheus-config
    5. namespace: monitoring
    6. data:
    7. prometheus.yml: |
    8. global:
    9. scrape_interval: 15s
    10. evaluation_interval: 15s
    11. scrape_configs:
    12. - job_name: 'k8s-cadvisor'
    13. metrics_path: /metrics/cadvisor
    14. kubernetes_sd_configs:
    15. - role: node
    16. relabel_configs:
    17. - source_labels: [__address__]
    18. regex: '(.*):10250'
    19. replacement: '${1}:10255'
    20. target_label: __address__
    21. action: replace
    22. - action: labelmap
    23. regex: __meta_kubernetes_node_label_(.+)
    24. metric_relabel_configs:
    25. - source_labels: [instance]
    26. separator: ;
    27. regex: (.+)
    28. target_label: node
    29. replacement: $1
    30. action: replace
    31. - source_labels: [pod_name]
    32. separator: ;
    33. regex: (.+)
    34. target_label: pod
    35. replacement: $1
    36. action: replace
    37. - source_labels: [container_name]
    38. separator: ;
    39. regex: (.+)
    40. target_label: container
    41. replacement: $1
    42. action: replace
    43. - job_name: kube-state-metrics
    44. kubernetes_sd_configs:
    45. - role: endpoints
    46. namespaces:
    47. names:
    48. - ops-monit
    49. relabel_configs:
    50. - source_labels: [__meta_kubernetes_service_label_app_kubernetes_io_name]
    51. regex: kube-state-metrics
    52. replacement: $1
    53. action: keep
    54. - action: labelmap
    55. regex: __meta_kubernetes_service_label_(.+)
    56. - source_labels: [__meta_kubernetes_namespace]
    57. action: replace
    58. target_label: k8s_namespace
    59. - source_labels: [__meta_kubernetes_service_name]
    60. action: replace
    61. target_label: k8s_sname
    62. - job_name: spring-boot-metrics
    63. kubernetes_sd_configs:
    64. - role: endpoints
    65. relabel_configs:
    66. - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_spring]
    67. action: keep
    68. regex: true
    69. - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_path]
    70. action: replace
    71. target_label: __metrics_path__
    72. regex: (.+)
    73. - source_labels: [__address__, __meta_kubernetes_service_annotation_prometheus_io_port]
    74. action: replace
    75. target_label: __address__
    76. regex: ([^:]+)(?::\d+)?;(\d+)
    77. replacement: $1:$2
    78. - action: labelmap
    79. regex: __meta_kubernetes_service_label_(.+)
    80. - source_labels: [__meta_kubernetes_namespace]
    81. action: replace
    82. target_label: spring_namespace
    83. - source_labels: [__meta_kubernetes_service_name]
    84. action: replace
    85. target_label: spring_name

            3、kube-state-metrics.yaml

    1. apiVersion: rbac.authorization.k8s.io/v1
    2. kind: ClusterRoleBinding
    3. metadata:
    4. labels:
    5. app.kubernetes.io/name: kube-state-metrics
    6. app.kubernetes.io/version: v1.9.7
    7. name: kube-state-metrics
    8. roleRef:
    9. apiGroup: rbac.authorization.k8s.io
    10. kind: ClusterRole
    11. name: kube-state-metrics
    12. subjects:
    13. - kind: ServiceAccount
    14. name: kube-state-metrics
    15. namespace: ops-monit
    16. ---
    17. apiVersion: rbac.authorization.k8s.io/v1
    18. kind: ClusterRole
    19. metadata:
    20. labels:
    21. app.kubernetes.io/name: kube-state-metrics
    22. app.kubernetes.io/version: v1.9.7
    23. name: kube-state-metrics
    24. rules:
    25. - apiGroups:
    26. - ""
    27. resources:
    28. - configmaps
    29. - secrets
    30. - nodes
    31. - pods
    32. - services
    33. - resourcequotas
    34. - replicationcontrollers
    35. - limitranges
    36. - persistentvolumeclaims
    37. - persistentvolumes
    38. - namespaces
    39. - endpoints
    40. verbs:
    41. - list
    42. - watch
    43. - apiGroups:
    44. - extensions
    45. resources:
    46. - daemonsets
    47. - deployments
    48. - replicasets
    49. - ingresses
    50. verbs:
    51. - list
    52. - watch
    53. - apiGroups:
    54. - apps
    55. resources:
    56. - statefulsets
    57. - daemonsets
    58. - deployments
    59. - replicasets
    60. verbs:
    61. - list
    62. - watch
    63. - apiGroups:
    64. - batch
    65. resources:
    66. - cronjobs
    67. - jobs
    68. verbs:
    69. - list
    70. - watch
    71. - apiGroups:
    72. - autoscaling
    73. resources:
    74. - horizontalpodautoscalers
    75. verbs:
    76. - list
    77. - watch
    78. - apiGroups:
    79. - authentication.k8s.io
    80. resources:
    81. - tokenreviews
    82. verbs:
    83. - create
    84. - apiGroups:
    85. - authorization.k8s.io
    86. resources:
    87. - subjectaccessreviews
    88. verbs:
    89. - create
    90. - apiGroups:
    91. - policy
    92. resources:
    93. - poddisruptionbudgets
    94. verbs:
    95. - list
    96. - watch
    97. - apiGroups:
    98. - certificates.k8s.io
    99. resources:
    100. - certificatesigningrequests
    101. verbs:
    102. - list
    103. - watch
    104. - apiGroups:
    105. - storage.k8s.io
    106. resources:
    107. - storageclasses
    108. - volumeattachments
    109. verbs:
    110. - list
    111. - watch
    112. - apiGroups:
    113. - admissionregistration.k8s.io
    114. resources:
    115. - mutatingwebhookconfigurations
    116. - validatingwebhookconfigurations
    117. verbs:
    118. - list
    119. - watch
    120. - apiGroups:
    121. - networking.k8s.io
    122. resources:
    123. - networkpolicies
    124. verbs:
    125. - list
    126. - watch
    127. ---
    128. apiVersion: v1
    129. kind: ServiceAccount
    130. metadata:
    131. labels:
    132. app.kubernetes.io/name: kube-state-metrics
    133. app.kubernetes.io/version: v1.9.7
    134. name: kube-state-metrics
    135. namespace: ops-monit
    136. ---
    137. apiVersion: v1
    138. kind: Service
    139. metadata:
    140. # annotations:
    141. # prometheus.io/scrape: 'true'
    142. labels:
    143. app.kubernetes.io/name: kube-state-metrics
    144. app.kubernetes.io/version: v1.9.7
    145. name: kube-state-metrics
    146. namespace: ops-monit
    147. spec:
    148. clusterIP: None
    149. ports:
    150. - name: http-metrics
    151. port: 8080
    152. targetPort: http-metrics
    153. - name: telemetry
    154. port: 8081
    155. targetPort: telemetry
    156. selector:
    157. app.kubernetes.io/name: kube-state-metrics
    158. ---
    159. apiVersion: apps/v1
    160. kind: Deployment
    161. metadata:
    162. labels:
    163. app.kubernetes.io/name: kube-state-metrics
    164. app.kubernetes.io/version: v1.9.7
    165. name: kube-state-metrics
    166. namespace: ops-monit
    167. spec:
    168. replicas: 1
    169. selector:
    170. matchLabels:
    171. app.kubernetes.io/name: kube-state-metrics
    172. template:
    173. metadata:
    174. labels:
    175. app.kubernetes.io/name: kube-state-metrics
    176. app.kubernetes.io/version: v1.9.7
    177. spec:
    178. containers:
    179. - image: quay.mirrors.ustc.edu.cn/coreos/kube-state-metrics:v1.9.7
    180. livenessProbe:
    181. httpGet:
    182. path: /healthz
    183. port: 8080
    184. initialDelaySeconds: 5
    185. timeoutSeconds: 5
    186. name: kube-state-metrics
    187. ports:
    188. - containerPort: 8080
    189. name: http-metrics
    190. - containerPort: 8081
    191. name: telemetry
    192. readinessProbe:
    193. httpGet:
    194. path: /
    195. port: 8081
    196. initialDelaySeconds: 5
    197. timeoutSeconds: 5
    198. nodeSelector:
    199. beta.kubernetes.io/os: linux
    200. serviceAccountName: kube-state-metrics

            4、node-exporter.yaml

    1. apiVersion: apps/v1
    2. kind: DaemonSet
    3. metadata:
    4. name: node-exporter
    5. namespace: monitoring
    6. labels:
    7. k8s-app: node-exporter
    8. spec:
    9. selector:
    10. matchLabels:
    11. k8s-app: node-exporter
    12. template:
    13. metadata:
    14. labels:
    15. k8s-app: node-exporter
    16. spec:
    17. containers:
    18. - image: prom/node-exporter:v1.0.1
    19. name: node-exporter
    20. ports:
    21. - containerPort: 9100
    22. protocol: TCP
    23. name: http
    24. ---
    25. apiVersion: v1
    26. kind: Service
    27. metadata:
    28. labels:
    29. k8s-app: node-exporter
    30. name: node-exporter
    31. namespace: monitoring
    32. spec:
    33. ports:
    34. - name: http
    35. port: 9100
    36. nodePort: 31672
    37. protocol: TCP
    38. type: NodePort
    39. selector:
    40. k8s-app: node-exporter

            5、sa.yaml

    1. apiVersion: rbac.authorization.k8s.io/v1
    2. kind: ClusterRole
    3. metadata:
    4. name: prometheus
    5. rules:
    6. - apiGroups: [""]
    7. resources:
    8. - nodes
    9. - nodes/proxy
    10. - services
    11. - endpoints
    12. - pods
    13. verbs: ["get", "list", "watch"]
    14. - apiGroups:
    15. - extensions
    16. resources:
    17. - ingresses
    18. verbs: ["get", "list", "watch"]
    19. - nonResourceURLs: ["/metrics"]
    20. verbs: ["get"]
    21. ---
    22. apiVersion: v1
    23. kind: ServiceAccount
    24. metadata:
    25. name: prometheus
    26. namespace: monitoring
    27. ---
    28. apiVersion: rbac.authorization.k8s.io/v1
    29. kind: ClusterRoleBinding
    30. metadata:
    31. name: prometheus
    32. roleRef:
    33. apiGroup: rbac.authorization.k8s.io
    34. kind: ClusterRole
    35. name: prometheus
    36. subjects:
    37. - kind: ServiceAccount
    38. name: prometheus
    39. namespace: monitoring

            到此Centos7 部署Prometheus(Kubernetes)介绍完成。

  • 相关阅读:
    【Java面试小短文】Spring Bean生命周期的执行流程
    linux基础
    java设计模式5,接口隔离原则
    Android日志猫的使用
    【Flyway 介绍】
    STM32使用PWM控制舵机
    2022年软件评测师考试大纲
    线性表--->算法总结
    JS奇淫技巧:alert有几种写法?
    uboot学习:(四)顶层makefile分析
  • 原文地址:https://blog.csdn.net/qq_19734597/article/details/127962462