• [云原生] Prometheus自动服务发现部署


    一、部署服务发现

    1.1 基于文件的服务发现

    基于文件的服务发现是仅仅略优于静态配置的服务发现方式,它不依赖于任何平台或第三方服务,因而也是最为简单和通用的实现方式。
    Prometheus Server 会定期从文件中加载 Target 信息,文件可使用 YAML 和 JSON 格式,它含有定义的 Target 列表,以及可选的标签信息。

    (1)创建用于服务发现的文件,在文件中配置所需的 target  
    1. cd /usr/local/prometheus
    2. mkdir targets
    3. vim targets/node-exporter.yaml
    4. - targets:
    5. - 192.168.80.130:9100
    6. labels:
    7. from: node-exporter
    8. vim targets/mysqld-exporter.yaml
    9. - targets:
    10. - 192.168.80.160:9104
    11. labels:
    12. from: mysqld-exporter

    (2)修改 prometheus 配置文件,发现 target 的配置,定义在配置文件的 job 之中 
    1. vim /usr/local/prometheus/prometheus.yml
    2. ......
    3. scrape_configs:
    4. - job_name: nodes
    5. file_sd_configs: #指定使用文件服务发现
    6. - files: #指定要加载的文件列表
    7. - targets/node*.yaml #文件加载支持通配符
    8. refresh_interval: 2m #每隔 2 分钟重新加载一次文件中定义的 Targets,默认为 5m
    9. - job_name: mysqld
    10. file_sd_configs:
    11. - files:
    12. - targets/mysqld*.yaml
    13. refresh_interval: 2m
    14. systemctl reload prometheus
    15. 浏览器查看 Prometheus 页面的 Status -> Targets

    1.2  基于 Consul 的服务发现

    Consul 是一款基于 golang 开发的开源工具,主要面向分布式,服务化的系统提供服务注册、服务发现和配置管理的功能。
           提供服务注册/发现、健康检查、Key/Value存储、多数据中心和分布式一致性保证等功能。

    下载地址:https://www.consul.io/downloads/

    (1)部署 Consul 服务 
    1. cd /opt/
    2. mkdir cnsul
    3. unzip consul_1.9.2_linux_amd64.zip
    4. mv consul /usr/local/bin/
    5. #创建 Consul 服务的数据目录和配置目录
    6. mkdir data logs conf
    7. #使用 server 模式启动 Consul 服务
    8. consul agent -server -bootstrap -ui -data-dir=./data -config-dir=./conf -bind=192.168.136.160 -client=0.0.0.0 -node=consul-server01 &> ./logs/consul.log &
    9. #查看 consul 集群成员
    10. consul members

    (2)在 Consul 上注册 Services 
    1. #在配置目录中添加文件
    2. cd conf/
    3. vim nodes.json
    4. {
    5. "services": [
    6. {
    7. "id": "node_exporter-node01",
    8. "name": "node01",
    9. "address": "192.168.136.130",
    10. "port": 9100,
    11. "tags": ["nodes"],
    12. "checks": [{
    13. "http": "http://192.168.136.130:9100/metrics",
    14. "interval": "5s"
    15. }]
    16. }
    17. ]
    18. }
    19. vim mysqld.json
    20. {
    21. "services": [
    22. {
    23. "id": "mysqld_exporter-node01",
    24. "name": "node02",
    25. "address": "192.168.136.160",
    26. "port": 9104,
    27. "tags": ["mysqld"],
    28. "checks": [{
    29. "http": "http://192.168.136.160:9104/metrics",
    30. "interval": "5s"
    31. }]
    32. }
    33. ]
    34. }
    35. vim nginx.json
    36. {
    37. "services": [
    38. {
    39. "id": "nginx_exporter-node01",
    40. "name": "node03",
    41. "address": "192.168.136.160",
    42. "port": 9913,
    43. "tags": ["nginx"],
    44. "checks": [{
    45. "http": "http://192.168.136.160:9913/metrics",
    46. "interval": "5s"
    47. }]
    48. }
    49. ]
    50. }


    (3)修改 prometheus 配置文件 
    1. vim /usr/local/prometheus/prometheus.yml
    2. ......
    3. - job_name: nodes
    4. consul_sd_configs: #指定使用 consul 服务发现
    5. - server: 192.168.136.160:8500 #指定 consul 服务的端点列表
    6. tags: #指定 consul 服务发现的 services 中哪些 service 能够加入到 prometheus 监控的标签
    7. - nodes
    8. refresh_interval: 1m
    9. ........
    10. systemctl reload prometheus
    11. 浏览器查看 Prometheus 页面的 Status -> Targets
    12. #让 consul 注销 Service
    13. consul services deregister -id="node_exporter-node02"
    14. #重新注册
    15. consul services register /etc/consul/nodes.json

    1.3 基于 Kubernetes API 的服务发现 

    基于 Kubernetes API 的服务发现机制,支持将API Server 中 Node、Service、Endpoint、Pod 和 Ingress 等资源类型下相应的各资源对象视作 target, 并持续监视相关资源的变动

    ●Node、Service、Endpoint、Pod 和 Ingress 资源分别由各自的发现机制进行定义

    ●负责发现每种类型资源对象的组件,在 Prometheus 中称为一个 role

    ●支持在集群上基于 DaemonSet 控制器部署 node-exporter 后发现各 Node 节点,也可以通过 kubelet 来作为 Prometheus 发现各 Node 节点的入口

    1. #基于 Kubernetes 发现机制的部分配置参数
    2. # The API server addresses. If left empty, Prometheus is assumed to run inside of the cluster and will discover API servers automatically
    3. and use the pod's
    4. # CA certificate and bearer token file at /var/run/secrets/kubernetes.io/serviceaccount/.
    5. [ api_server: ]
    6. # The Kubernetes role of entities that should be discovered. One of endpoints, service, pod, node, or ingress.
    7. role: <string>
    8. # Optional authentication information used to authenticate to the API server.
    9. # Note that 'basic_auth', 'bearer_token'和'bearer_token_file' 等认证方式互斥;
    10. [ bearer_token: ]
    11. [ bearer_token_file: ]
    12. # TLS configuration.
    13. tls_config:
    14. # CA certificate to validate API server certificate with.
    15. [ ca_file: ]
    16. # Certificate and key files for client cert authentication to the server.
    17. [ cert_file: ]
    18. [ key_file: ]
    19. # ServerName extension to indicate the name of the server.
    20. [ server_name: <string> ]
    21. # Optional namespace discovery. If omitted, all namespaces are used.
    22. namespaces:
    23. names:
    24. [ - <string> ]
    在K8S节点上 

    如果Prometheus是部署在k8s集群内部的,可以直接和apiserver通信。如果是集群外部的Prometheus可以通过以下步骤配置通过API Server实现服务发现。

    RBAC授权
    集群外部的Prometheus访问apiserver需要认证,还需要一定权限查询对应的资源对象。所以,这里先创建一个ServiceAccount和一个ClusterRole,并将它们绑定,之后集群外的Prometheus会以此ServiceAccount的身份访问apiserver。yaml文件内容如下:

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

    1. 获取ServiceAccount对应Secret资源对象中保存的token,然后将token保存到Prometheus节点上的文件里。
    2. TOKEN=`kubectl get secret/$(kubectl -n monitoring get secret | awk '/outside-prometheus/{print $1}') -n monitoring -o jsonpath={.data.token} | base64 -d`

    scp /etc/kubernetes/pki/ca.crt <prometheus_node>:/usr/local/prometheus/
    在Prometheus 节点上 

     

    1. echo <token-value> > /usr/local/prometheus/kubernetes-api-token
    2. cat /usr/local/prometheus/kubernetes-api-token #和上面获取的对比是否一致
    3. 修改Prometheus配置,添加job
    4. #集群api-server自动发现job
    5. - job_name: kubenetes-apiserver
    6. kubernetes_sd_configs:
    7. - role: endpoints
    8. api_server: https://192.168.80.10:6443 #指定API Server地址
    9. #这里的配置证书和token是连接API Server做服务发现时使用
    10. tls_config:
    11. ca_file: /usr/local/prometheus/pki/ca.crt #指定kubernetes ca根证书,用于验证api-server证书
    12. # insecure_skip_verify: true #也可以使用此选项跳过证书验证
    13. authorization:
    14. credentials_file: /usr/local/prometheus/kubernetes-api-token #指定访问api-server时使用的token文件
    15. scheme: https
    16. #这里的配置证书和token是连接从api-server抓取数据时使用
    17. tls_config:
    18. ca_file: /usr/local/prometheus/pki/ca.crt
    19. authorization:
    20. credentials_file: /usr/local/prometheus/kubernetes-api-token
    21. relabel_configs:
    22. - source_labels: ["__meta_kubernetes_namespace", "__meta_kubernetes_endpoints_name", "__meta_kubernetes_endpoint_port_name"]
    23. regex: default;kubernetes;https
    24. action: keep
    25. #集群节点自动发现job
    26. - job_name: "kubernetes-nodes"
    27. kubernetes_sd_configs:
    28. - role: node #指定发现类型为node
    29. api_server: https://192.168.80.10:6443
    30. tls_config:
    31. ca_file: /usr/local/prometheus/pki/ca.crt
    32. authorization:
    33. credentials_file: /usr/local/prometheus/kubernetes-api-token
    34. relabel_configs:
    35. - source_labels: ["__address__"] #重写target地址,默认端口是kubelet端口10250,修改为node-exporter端口9100
    36. regex: (.*):10250
    37. action: replace
    38. target_label: __address__
    39. replacement: $1:9100
    40. - action: labelmap #保留之前存在的__meta_kubernetes_node_label开头的标签
    41. regex: __meta_kubernetes_node_label_(.+)
    42. 验证
    43. 配置修改完成后,重载Prometheus,然后在界面查看target状态。
    44. 其实除了需要额外配置访问 API Server的证书外,其余配置和集群内的Prometheus服务发现配置基本一致。
    45. 另外,没有配置关于Pod的服务发现job,因为集群外的Prometheus无法访问集群内的Pod,需要添加路由规则才能实现互通。
  • 相关阅读:
    VMWare Ubuntu压缩虚拟磁盘
    ABAP 调用HTTP上传附件(二)之中文乱码
    学完就干的Linux常用命令
    linux7上powerpath卸载
    快乐数(C++解法)
    Spring
    nginx反向代理实例
    期中考Web复现
    Pycharm中使用远程JupyterLab以及JupyterHub登录问题
    一些流程图(自用)
  • 原文地址:https://blog.csdn.net/Cnm_147258/article/details/136702934