专栏名称: 院长技术
《院长技术》专注于DevOps技术栈研究,云原生、Istio,CICD、Docker、Prometheus、K8s、ServiceMesh、Jenkins、Linux、微服务、分布式存储、监控、日志收集、安全渗透、虚拟化等技术栈。
目录
相关文章推荐
奔腾融媒 新闻天天看  ·  【26-27日·天气预报】内蒙古晴天大风在线 ... ·  12 小时前  
内蒙古掌上12348  ·  关于《内蒙古自治区节约用水条例 ... ·  昨天  
内蒙古教育发布  ·  新学期 “新” 引力!叮!校园生活重启键已启动~ ·  2 天前  
内蒙古教育发布  ·  新学期 “新” 引力!叮!校园生活重启键已启动~ ·  2 天前  
51好读  ›  专栏  ›  院长技术

CentOS7安装Prometheus及consul服务发现

院长技术  · 公众号  ·  · 2024-03-03 10:49

正文

院长简介

作者:院长

职位:运维开发工程师

官网:https://deanit.cn

博客:https://blog.deanit.cn

擅长:

【虚拟化,容器化,自动化运维,CICD,监控,日志,中间件,双机热备,分布式存储,数据库,认证,组网隧道,Kali 安全渗透,网络攻击,负载均衡,Html,Css,Js,Jq,Vue3,Shell,Python,Go,前后端框架】

环境准备

ubuntu服务器:
master:172.19.11.50
node:172.19.10.3
规格:4C 8G

下载版本包

下载地址:https://prometheus.io/download/ 下载各个组件的tar包,下载完毕后如下图:

image.png

部署master节点

上传版本包至服务器

image.png

解压 【prometheus-2.45.3.linux-amd64.tar.gz】 进入目录,执行命令

cd prometheus-2.45.3.linux-amd64 && ./prometheus

检查部署master节点是否成功

查看9090端口是否存在

image.png

浏览器输入: http://172.19.11.50:9090,访问成功即为部署成功

设置开机自启

编写开机自启配置: /etc/systemd/system/prometheus.service

[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target

[Service]
Type=simple
User=root
ExecStart=/usr/local/prometheus/prometheus-2.45.3.linux-amd64/prometheus --config.file=/usr/local/prometheus/prometheus-2.45.3.linux-amd64/prometheus.yml --storage.tsdb.path=data/ --web.enable-admin-api --web.enable-lifecycle
Restart=on-failure

[Install]
WantedBy=multi-user.target

保存并退出上述.service文件,下面的命令可以启动服务并将其添加到系统自启动项中:

sudo systemctl start prometheus.service
sudo systemctl enable prometheus.service

执行完该命令后,可以使用systemctl命令来查看该服务的状态:

sudo systemctl status prometheus.service

启动服务后,它将在系统启动时自动启动。也可以在任何时候使用以下命令停止服务并从自启动项中删除:

sudo systemctl stop prometheus.service
sudo systemctl disable prometheus.service

其中: --web.enable-admin-api --web.enable-lifecycle 用于 【Prometheus】可通过Post请求热加载配置文件

部署node节点

下载组件node_exporter到node服务器

从master节点中,将【node_exporter-*.tar.gz】包上传至node节点,【172.19.10.3】

image.png

启动node

【172.19.10.3】服务器,解压tar包,并启动node

image.png

查看端口9100

image.png

在Prometheus中添加node节点

修改配置

修改master节点中,**/usr/local/prometheus/prometheus-2.45.3.linux-amd64/prometheus.yml 配置文件** 修改static_configs文件,用于添加node节点

image.png





    
my global config
global:
  scrape_interval:     10s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 10s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093

Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # "first_rules.yml"
  # "second_rules.yml"

A scrape configuration containing exactly one endpoint to scrape:
Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=` to any timeseries scraped from this config.
    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
  - job_name: "prometheus"
    static_configs:
      - targets: ["172.19.10.3:9100"]

重新启动master节点

重新启动master节点,查看页面

image.png
image.png

查看节点状态是否正常

若正常,应与localhost:9090一致,无报错信息

image.png

若提示以上错误,检查当前节点 ①:是否启动成功 ②:端口是否开启 经检查,node节点【172.19.10.3】防火墙未关闭,并且9100端口未开启,因此开启9100端口即可

image.png
image.png

回到master节点页面,查看状态,可看到节点【172.19.10.3】已正常

image.png

查看指标

访问地址:http://172.19.10.3:9100/metrics ,可查看node_exporter提供给prometheus可以拉取的指标

热加载

使用Post请求:

curl -X POST http://172.19.11.50:9090/-/reload

基于consul的服务发现(自动发现主机)

下载Consul

下载链接:https://releases.hashicorp.com/consul ,选择版本号,下载zip包 consul简介: 一款基于golang开发的开源工具,面向分布式,服务化的系统提供服务注册,服务发现和配置管理的功能 提供服务注册/发现、健康检查、key/value存储,多数据中心和分布式一致性保证等功能

image.png

下载并解压

image.png

将consul拷贝到bin目录

image.png

创建数据存储目录和文件加载目录

mkdir -p /usr/local/prometheus/consul/data
mkdir -p /usr/local/prometheus/consul/etc

启动Consul

设置数据目录为:/usr/local/prometheus/consul/data 设置配置目录为:/usr/local/prometheus/consul/etc

consul agent -dev -ui -data-dir=/usr/local/prometheus/consul/data -config-dir=/usr/local/prometheus/consul/etc -client=0.0.0.0
image.png

访问页面

部署成功,访问地址:http://172.19.11.50:8500/ui

image.png

修改Prometheus配置,设置基于Consul发现节点

my global config
global:
  scrape_interval:     10s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 10s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093

Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # "first_rules.yml"
  # "second_rules.yml"

A scrape configuration containing exactly one endpoint to scrape:
Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=` to any timeseries scraped from this config.
    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
    consul_sd_configs:
    - server: "172.19.11.50:8500"
      tags: #consul中的节点,若标签存在标签名"prometheus" ,prometheus则会通过该tags去自动发现
      - "prometheus"
      refresh_interval: 15s
  - job_name: 'nodes'
    consul_sd_configs: #由file变为consul
    - server: "172.19.11.50:8500"
      tags: #consul中的节点,若标签存在nodes ,prometheus则会通过该tags去自动发现
      - "nodes"
      refresh_interval: 15s

执行命令以下,热加载配置文件:

curl -X POST http://172.19.11.50:9090/-/reload

新增consul节点配置

consul启动时,设置启动的配置文件目录为:/usr/local/prometheus/consul/etc/ 因此在该目录创建文件:nodes.json

{
  "services": [
   {
     "id": "node-exporter-node_6_9",
     "name": "node_6_9",
     "address": "172.19.6.9",
     "port": 9100,
     "tags": ["nodes", "linux"],
      "check": [{
     "http":"http://172.19.6.9:9100/metrics",
         "interval": "5s"  
    }]     
  },
  {
     "id": "node-exporter-node_6_10",
     "name": "node_6_9",
     "address": "172.19.6.10",
     "port": 9100,
     "tags": ["nodes", "linux"],
      "check": [{
         "http":"http://172.19.6.10:9100/metrics",
         "interval": "5s"  
    }]     
  },    
  {
     "id": "node-exporter-node_4_72",
     "name": "node_4_72",
     "address": "172.19.4.72",
     "port": 9100,
     "tags": ["nodes", "linux"],
      "check": [{
         "http":"http://172.19.4.72:9100/metrics",
         "interval": "5s"  
    }]     
  },      
 ]
}

重新加载consul

执行命令

consul reload
image.png

查看Prometheus界面

image.png

基于文件发现主机

创建自动发现主机的文件: /usr/local/prometheus/prometheus-2.45.3.linux-amd64/targets/hosts.json 新增2台主机:

mkdir -p /usr/local/prometheus/prometheus-2.45.3.linux-amd64/targets
vi hosts.json
gongjianjun@localhost:/usr/local/prometheus/prometheus-2.45.3.linux-amd64/targets$ cat hosts.json
[
        {
                "targets": [ "172.19.10.3:9100" ],
                "labels": {
                  "job": "file_nodes",
                  "service": "node_10_3"
                }
        },
        {
                "targets": [ "172.19.4.72:9100" ],
                "labels": {
                  "job": "file_nodes",
                  "service": "node_4_72"
                }
        }

]

新增Prometheus配置:

  - job_name: "file_nodes"
    file_sd_configs:
      - files:
        - "/usr/local/prometheus/prometheus-2.45.3.linux-amd64/targets/hosts.json"
        refresh_interval: 6s
image.png

重启或Prometheus热加载配置

curl -X POST http://172.19.11.50:9090/-/reload

查看Prometheus界面信息,即可看到两个刚配置的主机: file_nodes

image.png

后续若需新增主机,则只修改 【/usr/local/prometheus/prometheus-2.45.3.linux-amd64/targets/hosts.json】文件即可,无需重启Prometheus

招贤纳士

欢迎👏关注《院长技术》,只做原创,干货~~~

更多技术干货持续输出中......







请到「今天看啥」查看全文