【云原生 Prometheus篇】Prometheus的动态服务发现机制与认证配置

news/2024/5/19 1:19:37 标签: 云原生, prometheus, 服务发现, kubernetes, 架构, 运维

目录

  • 一、Prometheus服务发现的方式
  • 二、实例一:部署基于文件的服务发现
    • 2.1 创建用于服务发现的文件
    • 2.2 修改Prometheus的配置文件
    • 2.3 浏览器访问测试
  • 三、实例二:部署基于consul的服务发现
    • 3.1 部署Consul服务
    • 3.2 在Consul 上注册 Services
    • 3.3 修改 prometheus 配置文件

一、Prometheus服务发现的方式

1.1 基于文件的服务发现

基于文件的服务发现是仅仅略优于静态配置的服务发现方式,它不依赖于任何平台或第三方服务,因而也是最为简单和通用的实现方式。

Prometheus Server 会定期从文件中加载 Target 信息,文件可使用 YAML 和 JSON 格式,它含有定义的 Target 列表,以及可选的标签信息。

1.2 基于consul的服务发现

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

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

1.3 基于 Kubernetes API 的服务发现

1.3.1 简介


//基于 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 节点的入口


#基于 Kubernetes 发现机制的部分配置参数
# The API server addresses. If left empty, Prometheus is assumed to run inside of the cluster and will discover API servers automatically
and use the pod's
# CA certificate and bearer token file at /var/run/secrets/kubernetes.io/serviceaccount/.
[ api_server: <host> ]

# The Kubernetes role of entities that should be discovered. One of endpoints, service, pod, node, or ingress.
role: <string>

# Optional authentication information used to authenticate to the API server.
# Note that 'basic_auth', 'bearer_token'和'bearer_token_file' 等认证方式互斥;
[ bearer_token: <secret> ]
[ bearer_token_file: <filename> ]

# TLS configuration.
tls_config:
# CA certificate to validate API server certificate with.
[ ca_file: <filename> ]

# Certificate and key files for client cert authentication to the server.
[ cert_file: <filename> ]
[ key_file: <filename> ]

# ServerName extension to indicate the name of the server.
[ server_name: <string> ]

# Optional namespace discovery. If omitted, all namespaces are used.
namespaces:
names:
[ - <string> ]

1.3.2 基于Kurbernetes发现机制的部分配置参数

# The API server addresses. If left empty, Prometheus is assumed to run inside of the cluster and will discover API servers automatically
and use the pod's
# CA certificate and bearer token file at /var/run/secrets/kubernetes.io/serviceaccount/.
[ api_server: <host> ]

# The Kubernetes role of entities that should be discovered. One of endpoints, service, pod, node, or ingress.
role: <string>

# Optional authentication information used to authenticate to the API server.
# Note that 'basic_auth', 'bearer_token'和'bearer_token_file' 等认证方式互斥;
[ bearer_token: <secret> ]
[ bearer_token_file: <filename> ]

# TLS configuration.
tls_config:
# CA certificate to validate API server certificate with.
[ ca_file: <filename> ]

# Certificate and key files for client cert authentication to the server.
[ cert_file: <filename> ]
[ key_file: <filename> ]

# ServerName extension to indicate the name of the server.
[ server_name: <string> ]

# Optional namespace discovery. If omitted, all namespaces are used.
namespaces:
names:
[ - <string> ]

二、实例一:部署基于文件的服务发现

2.1 创建用于服务发现的文件

新建工作目录

cd /usr/local/prometheus

mkdir targets

在文件中配置所需的 target

vim targets/node-exporter.yaml
- targets:
  - 192.168.2.108:9100
  - 192.168.2.106:9100
  labels:
    app: node-exporter
    job: node


vim targets/mysqld-exporter.yaml
- targets:
  - 192.168.2.108:9104
  - 192.168.2.106:9104
  labels:
    app: mysqld-exporter
    job: mysqld

2.2 修改Prometheus的配置文件

修改 prometheus 配置文件,发现 target 的配置,定义在配置文件的 job 之中。

vim /usr/local/prometheus/prometheus.yml
......
scrape_configs:
  - job_name: nodes
    file_sd_configs:                  #指定使用文件服务发现
    - files:                          #指定要加载的文件列表
      - targets/node*.yaml            #文件加载支持通配符
      refresh_interval: 2m            #每隔 2 分钟重新加载一次文件中定义的 Targets,默认为 5m
  
  - job_name: mysqld
    file_sd_configs:
    - files:
      - targets/mysqld*.yaml
      refresh_interval: 2m

在这里插入图片描述

2.3 浏览器访问测试

#先重启服务
systemctl reload prometheus
#然后
浏览器查看 Prometheus 页面的 Status -> Targets

在这里插入图片描述

三、实例二:部署基于consul的服务发现

3.1 部署Consul服务

cd /opt/
unzip consul_1.9.2_linux_amd64.zip
mv consul /usr/local/bin/
#创建 Consul 服务的数据目录和配置目录
mkdir /var/lib/consul-data
mkdir /etc/consul/
#使用 server 模式启动 Consul 服务
consul agent \
-server \
-bootstrap \
-ui \
-data-dir=/var/lib/consul-data \
-config-dir=/etc/consul/ \
-bind=192.168.2.108 \
-client=0.0.0.0 \
-node=consul-server01 &> /var/log/consul.log &
#查看 consul 集群成员
consul members

在这里插入图片描述

3.2 在Consul 上注册 Services

#在配置目录中添加文件
vim /etc/consul/nodes.json
{
  "services": [
    {
      "id": "node_exporter-node01",
      "name": "node01",
      "address": "192.168.2.108",
      "port": 9100,
      "tags": ["nodes"],
      "checks": [{
        "http": "http://192.168.2.108:9100/metrics",
        "interval": "5s"
      }]
    },
    {
      "id": "node_exporter-node02",
      "name": "node02",
      "address": "192.168.2.106",
      "port": 9100,
      "tags": ["nodes"],
      "checks": [{
        "http": "http://192.168.2.106:9100/metrics",
        "interval": "5s"
      }]
    }
  ]
}
#让 consul 重新加载配置信息
consul reload		

浏览器访问:http://192.168.2.108:8500

在这里插入图片描述

prometheus__257">3.3 修改 prometheus 配置文件

vim /usr/local/prometheus/prometheus.yml
......
  - job_name: nodes
    consul_sd_configs:                  #指定使用 consul 服务发现
    - server: 192.168.2.108:8500        #指定 consul 服务的端点列表
      tags:                             #指定 consul 服务发现的 services 中哪些 service 能够加入到 prometheus 监控的标签
      - nodes
      refresh_interval: 2m

在这里插入图片描述

systemctl reload prometheus
浏览器查看 Prometheus 页面的 Status -> Targets

在这里插入图片描述

#让 consul 注销 Service
consul services deregister -id="node_exporter-node02"

#重新注册
consul services register /etc/consul/nodes.json

http://www.niftyadmin.cn/n/5208422.html

相关文章

26. 删除有序数组中的重复项(remove-duplicates-from-sorted-array)

26. 删除有序数组中的重复项(remove-duplicates-from-sorted-array) 给你一个 非严格递增排列 的数组 nums &#xff0c;请你** 原地** 删除重复出现的元素&#xff0c;使每个元素 只出现一次 &#xff0c;返回删除后数组的新长度。元素的 相对顺序 应该保持 一致 。然后返回 …

C# 执行Excel VBA宏工具类

写在前面 在Excel文档的自动化处理流程中&#xff0c;有部分值需要通过已定义的宏来求解&#xff0c;所以延伸出了用C# 调用Excel中的宏代码的需求。 首先要从NuGet中引入Microsoft.Office.Interop.Excel 类库 using Excel Microsoft.Office.Interop.Excel; 代码实现 /// &l…

如何使用ArcGIS Pro制作一张陆地和海洋对调后图

你是否有想过如果海洋和陆地对调一下——陆地变海洋&#xff0c;海洋变陆地后的世界地图会是什么样子&#xff0c;对于这个大胆的想法&#xff0c;我们可以通过ArcGIS Pro来制作地图&#xff0c;这里为大家介绍一下制作方法&#xff0c;希望能对你有所帮助。 数据来源 本教程…

使用Pytorch从零开始构建CGAN (conditional GAN)

GAN和DCGAN生成随机图像。因此&#xff0c;我们几乎无法控制生成哪些图像。然而&#xff0c;CGAN 可以让我们指定一个条件&#xff0c;以便我们可以告诉它要生成哪些图像。诀窍是使用可学习层将标签值转换为特征向量&#xff0c;以便生成器可以学习要生成什么图像。鉴别器还利用…

vue2.6源码分析

vue相关文档 vue-cli官方文档 vuex官方文档 vue-router 官方文档 vue2.6源码地址 如何调试源码 package.json 添加了--sourcemap "scripts": {"dev": "rollup -w -c scripts/config.js --environment TARGET:web-full-dev --sourcemap" }新增…

PMP 考试的含金量怎么样?

这里可以三个思考题和三个价值点帮你认识PMP考试。 三个思维题 1.工作环境 PMP证书含金量的一个很大因素&#xff0c;就是考证的人是否对PMP证书有比较强的实际需求。相反&#xff0c;如果只是听别人说&#xff0c;PMP证书很好&#xff0c;不管工作中是否有需要&#xff0c;…

Diffie-Hellman 函数

gpt: Diffie-Hellman&#xff08;迪菲-赫尔曼&#xff09;密钥交换是一种通过非安全信道安全地共享密钥的方法。它是由Whitfield Diffie和Martin Hellman于1976年提出的&#xff0c;是公钥密码学的里程碑之一。该协议允许两个通信方在不共享密钥的情况下协商出一个共享的密钥&…

深度学习常见激活函数:ReLU,sigmoid,Tanh,softmax,Leaky ReLU,PReLU,ELU整理集合,应用场景选择

文章目录 1、ReLU 函数&#xff08;隐藏层中是一个常用的默认选择&#xff09;1.1 优点1.2 缺点 2、sigmoid 函数2.1 优点2.2 缺点 3、Tanh 函数3.1 优点3.2 缺点 4、softmax 函数&#xff08;多分类任务最后一层都会使用&#xff09;5、Leaky ReLU 函数5.1 优点5.2 缺点 6、PR…