当前位置: 首页 > news >正文

Prometheus 服务发现

目录

1.基于文件的服务发现

2.基于 Consul 的服务发现

3.基于 Kubernetes API 的服务发现


1.基于文件的服务发现

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

创建用于服务发现的文件,在文件中配置所需的 target 
cd /usr/local/prometheus
mkdir file_sd
cd file_sd/
vim node-exporter.yaml
- targets:- 192.168.9.210:9100labels:svc: node              vim prometheus.yml
- job_name: "node_exporter"scheme: httpmetrics_path: /metricsfile_sd_configs:                    #指定使用文件服务发现- files:                            #指定要加载的文件列表- file_sd/node-exporter.yaml      #文件加载
systemctl restart prometheus.service


如何实现不重载普罗米修斯重载配置文件

vim prometheus.yml
refresh_interval: 1m   #添加配置systemctl restart prometheus.servicecd file_sd/
vim node-exporter.yaml
- 192.168.9.116:9100添加监控的ip



2.基于 Consul 的服务发现

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

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

cd /opt
上传consul_1.9.2_linux_amd64.zip
unzip consul_1.9.2_linux_amd64.zip
mv consul /usr/local/bin/
consul version

cd /usr/local/
mkdir consul
cd consul/
mkdir data conf logs
consul agent \
> -server \
> -bootstrap \
> -ui \
> -data-dir=/usr/local/consul/data \
> -config-dir=/usr/local/consul/conf \
> -bind=192.168.9.114 \
> -client=0.0.0.0 \
> -node=consul-server01 &> /usr/local/consul/logs/consul.log &netstat -lntp | grep consul


使用consul发现目标主机

cd /usr/local/consul/conf
vim nodes.json
{"services": [{"id": "node_exporter-node01","name": "node01","address": "192.168.9.210","port": 9100,"tags": ["nodes"],"checks": [{"http": "http://192.168.9.210:9100/metrics","interval": "5s"}]},{"id": "node_exporter-node02","name": "node01","address": "192.168.9.116","port": 9100,"tags": ["nodes"],"checks": [{"http": "http://192.168.9.116:9100/metrics","interval": "5s"}]}]
}consul reload


关联普罗米修斯

vim prometheus.yml- job_name: "node_exporter"scheme: httpmetrics_path: /metricsconsul_sd_configs:- server: 192.168.9.114:8500tags:- nodesrefresh_interval: 1m              每隔1分钟重新加载一次文件中定义的 Targets,默认为 5m
systemctl restart prometheus.service


如何剔除/添加consul中的主机

consul services deregister -id "node_exporter-node01"

consul services register ./nodes.json


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

k8s主节点操作vim rbac.yaml
apiVersion: v1
kind: Namespace
metadata:name: monitoring---
apiVersion: v1
kind: ServiceAccount
metadata:name: outside-prometheusnamespace: monitoring---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:name: outside-prometheus
rules:
- apiGroups:- ""resources:- nodes- services- endpoints- pods- nodes/proxyverbs:- get- list- watch
- apiGroups:- "networking.k8s.io"resources:- ingressesverbs:- get- list- watch
- apiGroups:- ""resources:- configmaps- nodes/metricsverbs:- get
- nonResourceURLs:- /metricsverbs:- get---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:name: outside-prometheus
roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: outside-prometheus
subjects:
- kind: ServiceAccountname: outside-prometheusnamespace: monitoring
kubectl apply -f rbac.yaml获取ServiceAccount对应Secret资源对象中保存的token,然后将token保存到Prometheus节点上的文件里。TOKEN=`kubectl get secret/$(kubectl -n monitoring get secret | awk '/outside-prometheus/{print $1}') -n monitoring -o jsonpath={.data.token} | base64 -d`echo $TOKENcd /etc/kubernetes/pki/
scp ca.crt 192.168.9.113:/usr/local/prometheus


prometheus服务器
cd /usr/local/prometheus
echo eyJhbGciOiJSUzI1NiIsImtpZCI6IlVlekNjZThYQ1lGNHVUVzFkeXdVbjhtaGUzbFJuMzZSeXV4VWExMnJVUzAifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJtb25pdG9yaW5nIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZWNyZXQubmFtZSI6Im91dHNpZGUtcHJvbWV0aGV1cy10b2tlbi12OGJtOSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50Lm5hbWUiOiJvdXRzaWRlLXByb21ldGhldXMiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiJmYWEwNjA1NC0zZWRhLTQ2MmQtODE0NS0yNjYzYTU0ZTM3MmIiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6bW9uaXRvcmluZzpvdXRzaWRlLXByb21ldGhldXMifQ.jzBcGYgiADGQ1MxR-9xJ7dCIAFI2aCkuX64w6AzDDIwV_XMRUNNSIoX6867Mu3ebuFPChBX6B0bIJ-SrxspjSNIOBiuYSuCKXNqwwxfpVSNNq_v40JFBxn7NpzxrLDHm5eq8scwWMEy6HPVdN83uhU4Zrd4GVenArirwKScsl_bphjrFf-vp1Z9aMWH241BzJtvv-aQ2m0-dQ13ZljZ-YP_FO-lz0EGuECfyiIozFkDPwooMowRxy68m0liwGo-McrKttv3BC8KJcSmSmFZyGAzQuNoHa8j5FIOHMJxlhfIxXlrTDSUEUgBSjsOWMKXEN_mMBzi4lLHIwfMJDv4Zmw > k8s-api-token
cat k8s-api-token

k8s主节点
kubectl create clusterrolebinding cluster-system-anonymous --clusterrole=cluster-admin --user=system:anonymous

prometheus服务器
vim prometheus.yml
- job_name: "k8s-apiserver"scheme: httpskubernetes_sd_configs:- role: endpointsapi_server: https://192.168.9.210:6443tls_config:ca_file: /usr/local/prometheus/ca.crtauthorization:credentials_file: /usr/local/prometheus/k8s-api-tokentls_config:ca_file: /usr/local/prometheus/ca.crtauthorization:credentials_file: /usr/local/prometheus/k8s-api-tokenrelabel_configs:- source_labels: ["__meta_kubernetes_namespace", "__meta_kubernetes_endpoints_name", "__meta_kubernetes_endpoint_port_name"]regex: default;kubernetes;httpsaction: keep- job_name: "kubernetes-nodes"kubernetes_sd_configs:- role: nodeapi_server: https://192.168.9.210:6443tls_config:ca_file: /usr/local/prometheus/ca.crtauthorization:credentials_file: /usr/local/prometheus/k8s-api-tokenrelabel_configs:- source_labels: ["__address__"]regex: (.*):10250action: replacetarget_label: __address__replacement: $1:9100- action: labelmapregex: __meta_kubernetes_node_label_(.+)systemctl restart prometheus.service


相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • k8s Unable to fetch container log stats failed to get fsstats for
  • 常规方法调用Mapper对象bean,批量插入数据
  • API接口安全101:基础概念与最佳实践
  • 企业高性能web服务器【Nginx详解】
  • jmeter简单发送接口
  • 龙门吊(天车)防撞方案
  • HarmonyOS开发实战:应用权限/通知设置跳转方案
  • ZooKeeper入门及核心知识点整理
  • 在Windows Server上安装typecho博客程序(基于IIS)
  • [CLIP-VIT-L + Qwen] 多模态大模型源码阅读 - 语言模型篇(4)
  • 阿里云ubuntu系统安装jdk + tomcat
  • 初识C++以及安装C++学习工具
  • VirtualBox和VMware的虚拟机ip配置为同一网段不使用wlan的网卡(vulnhub打靶前期准备)
  • Chainlit接入DifyAI知识库接口快速实现自定义用户聊天界面
  • AI自动生成PPT怎么用?5大AI生成PPT技巧教会你
  • [rust! #004] [译] Rust 的内置 Traits, 使用场景, 方式, 和原因
  • [译]CSS 居中(Center)方法大合集
  • 2017届校招提前批面试回顾
  • go append函数以及写入
  • java第三方包学习之lombok
  • JS进阶 - JS 、JS-Web-API与DOM、BOM
  • Lucene解析 - 基本概念
  • python docx文档转html页面
  • Python代码面试必读 - Data Structures and Algorithms in Python
  • UMLCHINA 首席专家潘加宇鼎力推荐
  • 服务器之间,相同帐号,实现免密钥登录
  • 关于Android中设置闹钟的相对比较完善的解决方案
  • 如何使用 OAuth 2.0 将 LinkedIn 集成入 iOS 应用
  • 少走弯路,给Java 1~5 年程序员的建议
  • 时间复杂度与空间复杂度分析
  • 使用putty远程连接linux
  • 数组的操作
  • 突破自己的技术思维
  • 原生js练习题---第五课
  • 在Mac OS X上安装 Ruby运行环境
  • 湖北分布式智能数据采集方法有哪些?
  • ​queue --- 一个同步的队列类​
  • ​猴子吃桃问题:每天都吃了前一天剩下的一半多一个。
  • ​数据链路层——流量控制可靠传输机制 ​
  • !!java web学习笔记(一到五)
  • # 透过事物看本质的能力怎么培养?
  • (ISPRS,2021)具有遥感知识图谱的鲁棒深度对齐网络用于零样本和广义零样本遥感图像场景分类
  • (二)延时任务篇——通过redis的key监听,实现延迟任务实战
  • (附源码)springboot太原学院贫困生申请管理系统 毕业设计 101517
  • (实测可用)(3)Git的使用——RT Thread Stdio添加的软件包,github与gitee冲突造成无法上传文件到gitee
  • (一)spring cloud微服务分布式云架构 - Spring Cloud简介
  • (转)Android学习系列(31)--App自动化之使用Ant编译项目多渠道打包
  • ******IT公司面试题汇总+优秀技术博客汇总
  • ***汇编语言 实验16 编写包含多个功能子程序的中断例程
  • .gitignore文件忽略的内容不生效问题解决
  • .net websocket 获取http登录的用户_如何解密浏览器的登录密码?获取浏览器内用户信息?...
  • .NET 解决重复提交问题
  • .NET 快速重构概要1
  • .NET 使用 JustAssembly 比较两个不同版本程序集的 API 变化
  • .net和php怎么连接,php和apache之间如何连接