Kubernetes 之 Prometheus 服务发现 consul_sd_configs 、file_sd_configs (三)
consul_sd_configs:consul服务发现
#consul_sd_configs
#实验环境,192.168.40.101/111/112
unzip consul_1.13.1_linux_amd64.zip
cp consul /usr/local/bin
scp consul 192.168.40.111:/usr/local/bin/
scp consul 192.168.40.112:/usr/local/bin/
consul -h
mkdir /data/consul/ -p #3台都要创建
#参数
consul agent -server #使用 server 模式运行 consul 服务
-bootstrap #首次部署使用初始化模式
-bind #设置群集通信的监听地址
-client #设置客户端访问的监听地址
-data-dir #指定数据保存路径
-ui#启用内置静态 web Ul 服务器
-node #此节点的名称,群集中必须唯
-datacenter=dc1 #集群名称,默认是 dc1
-join #加入到已有 consul 环境
#启动服务
nohup consul agent -server -bootstrap -bind=192.168.40.101 -client=192.168.40.101 -data-dir=/data/consul -ui -node=192.168.40.101 &
nohup consul agent -bind=192.168.40.111 -client=192.168.40.111 -data-dir=/data/consul -node=192.168.40.111 -join=192.168.40.101 &
nohup consul agent -bind=192.168.40.112 -client=192.168.40.112 -data-dir=/data/consul -node=192.168.40.112 -join=192.168.40.101 &
#验证集群查看日志
[root@k8s-master01 Prometheus]# tail nohup.out
2023-12-19T16:40:37.406+0800 [INFO] agent.leader: started routine: routine="federation state pruning"
2023-12-19T16:40:37.406+0800 [INFO] agent.server: New leader elected: payload=192.168.40.101
2023-12-19T16:40:37.406+0800 [INFO] agent.server: member joined, marking health alive: member=192.168.40.101 partition=default
2023-12-19T16:40:37.408+0800 [INFO] agent.server: federation state anti-entropy synced
2023-12-19T16:40:38.299+0800 [ERROR] agent.server.autopilot: Failed to reconcile current state with the desired state
2023-12-19T16:40:38.784+0800 [INFO] agent: Synced node info
2023-12-19T16:46:12.162+0800 [INFO] agent.server.serf.lan: serf: EventMemberJoin: 192.168.40.111 192.168.40.111
2023-12-19T16:46:12.162+0800 [INFO] agent.server: member joined, marking health alive: member=192.168.40.111 partition=default
2023-12-19T16:46:25.552+0800 [INFO] agent.server.serf.lan: serf: EventMemberJoin: 192.168.40.112 192.168.40.112
2023-12-19T16:46:25.552+0800 [INFO] agent.server: member joined, marking health alive: member=192.168.40.112 partition=default
#访问UI
http://192.168.40.101:8500/ui/dc1/services
#测试注册写入数据 通过consul的API写入数据
curl -X PUT -d '{"id":"node-exporter101","name":"node-exporter101","address":"192.168.40.101","port":9100,"tags":["node-exporter"],"checks":[{"http":"http://192.168.40.101:9100/","interval":"5s"}]}' "http://192.168.40.101:8500/v1/agent/service/register"
curl -X PUT -d '{"id":"node-exporter111","name":"node-exporter111","address":"192.168.40.111","port":9100,"tags":["node-exporter"],"checks":[{"http":"http://192.168.40.111:9100/","interval":"5s"}]}' "http://192.168.40.101:8500/v1/agent/service/register"
curl -X PUT -d '{"id":"node-exporter112","name":"node-exporter112","address":"192.168.40.112","port":9100,"tags":["node-exporter"],"checks":[{"http":"http://192.168.40.112:9100/","interval":"5s"}]}' "http://192.168.40.101:8500/v1/agent/service/register"
#访问UI验证数据
http://192.168.40.101:8500/ui/dc1/services
#主要配置字段
static_configs: #配置数据源
consul sd configs: #指定基于 consul 服务发现的配置
rebel_configs: #重新标记
services: []#表示匹配 consul 中所有的 service
#添加配置
vim case3-1-prometheus-cfg.yaml
- job_name: 'consul'
honor_labels: true
metrics_path: /metrics
scheme: http
consul_sd_configs:
- server: 192.168.40.101:8500
services: [] #发现的目标服务器,空为所有服务器,可以写servicea,serviceb,servicec
- server: 192.168.40.111:8500
services: []
- server: 192.168.40.112:8500
services: []
relabel_configs:
- source_labels: ['_meta_consul_tags']
target_label: 'product'
- source_labels: ['_meta_consul_dc']
target_label: 'idc'
- source_labels: ['_meta_consul_service']
regex: "consul"
action: drop
kubectl apply -f case3-1-prometheus-cfg.yaml
kubectl delete -f case3-2-prometheus-deployment.yaml
kubectl apply -f case3-2-prometheus-deployment.yaml
kubectl get pods -n monitoring
#查看url consul-job
#consul服务删除
curl --request PUT http://192.168.40.101:8500/v1/agent/service/deregister/node-exporter112
file_sd_configs : 文件发现
#因为部署在k8s中 需要配置 这里对应有点奇怪 sd_my_server.json文件没有对应到file_sd文件中 只在/prometheus这一层
vim case3-1-prometheus-cfg.yaml
- job_name: 'file_sd_config_CAserver'
file_sd_configs:
- files:
- /prometheus/sd_my_server.json
refresh_interval: 10s
#增加配置段
vim case3-2-prometheus-deployment.yaml
volumeMounts:
- mountPath: /prometheus/file_sd/
name: file-sd-config
volumes:
- name: file-sd-config
hostPath:
path: /root/file_sd/
type: DirectoryOrCreate
vim /root/file_sd/sd_my_server.json
[
{
"targets": ["192.168.40.101:9100","192.168.40.111:9100","192.168.40.112:9100"]
}
]
[root@k8s-master01 file_sd]# kubectl apply -f case3-1-prometheus-cfg.yaml
[root@k8s-master01 file_sd]# kubectl delete -f case3-2-prometheus-deployment.yaml
[root@k8s-master01 file_sd]# kubectl apply -f case3-2-prometheus-deployment.yaml
[root@k8s-master01 file_sd]# kubectl get pods -n monitoring