Prometheus容器化部署

本文介绍了如何从官方镜像拉取Prometheus,配置prometheus.yml以监控节点,添加node_exporter并部署Grafana展示指标。还展示了如何在Grafana中部署并映射服务端口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//拉取Prometheus官方镜像
[root@master ~]# docker pull prom/prometheus  

[root@master ~]# docker images
REPOSITORY                                                        TAG             IMAGE ID       CREATED        SIZE
prom/prometheus                                                   latest          a3d385fc29f9   11 days ago    201MB

//在client上获取prometheus.yml配置文件
[root@client ~]# tar xf prometheus-2.31.1.linux-amd64.tar.gz
[root@client ~]# cd prometheus-2.31.1
[root@client prometheus-2.31.1]# scp /root/prometheus-2.31.1/prometheus.yml 192.168.58.131:/opt/prometheus.yml
root@192.168.8.131's password: 
prometheus.yml      

//使用镜像运行容器
[root@master ~]# cat /opt/prometheus.yml 
# my global config
global:
  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # 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=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]

# 查看镜像
[root@master ~]# docker images
REPOSITORY                                                        TAG             IMAGE ID       CREATED        SIZE
prom/prometheus                                                   latest          a3d385fc29f9   11 days ago    201MB

# 映射端口和配置文件到主机上且设置随docker启动而启动容器
[root@master opt]# docker run -d --name prometheus --restart always -p 9090:9090 -v /opt/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
cb748d375af075241ea835c14a00896a8d94a3e05f911f8b88c155be9ae35980

[root@master opt]# docker ps | grep prometheus
cb748d375af0   prom/prometheus                                     "/bin/prometheus --c…"   7 seconds ago    Up 7 seconds    0.0.0.0:9090->9090/tcp, :::9090->9090/tcp   prometheus


# 查看容器运行状态
[root@master ~]# docker ps | grep prometheus
933b88601ed6   prom/prometheus                                     "/bin/prometheus --c…"   10 minutes ago   Up 10 minutes   0.0.0.0:9090->9090/tcp, :::9090->9090/tcp   prometheus

//在客户端部署node-exporter
[root@client ~]# ls
anaconda-ks.cfg  node_exporter-1.3.0.linux-amd64.tar.gz  
[root@client ~]# tar xf node_exporter-1.3.0.linux-amd64.tar.gz -C /usr/local/
[root@client ~]# cd /usr/local/

[root@client local]# ls
bin  etc  games  include  lib  lib64  libexec  node_exporter-1.3.0.linux-amd64  prometheus  sbin  share  src
[root@client local]# mv node_exporter-1.3.0.linux-amd64/ node_exporter
[root@client local]# ls
bin  etc  games  include  lib  lib64  libexec  node_exporter  prometheus  sbin  share  src

#配置service文件
[root@client ~]# vim /usr/lib/systemd/system/node_exporter.service
[unit]
Description=The node_exporter Server
After=network.target

[Service]
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure
RestartSec=15s
SyslogIdentifier=node_exporter

[Install]
WantedBy=multi-user.target

# 设置自启node_exporter
[root@client local]# systemctl daemon-reload && systemctl enable node_exporter && systemctl restart node_exporter
Created symlink /etc/systemd/system/multi-user.target.wants/node_exporter.service → /usr/lib/systemd/system/node_exporter.service.

[root@client ~]# ss -anlt
State               Recv-Q               Send-Q                             Local Address:Port                             Peer Address:Port              
LISTEN              0                    128                                      0.0.0.0:22                                    0.0.0.0:*                 
LISTEN              0                    128                                         [::]:22                                       [::]:*                 
LISTEN              0                    128                                            *:9100                                        *:*                 

//在master 主机上修改prometheus.yaml配置文件,添加节点
[root@master ~]#  vi /opt/prometheus.yml 
# my global config
global:
  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # 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=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]
  - job_name: "Linux Server"  //添加此处
    static_configs:                    //添加此处
      - targets: ["192.168.58.20:9100"]   //添加此处,将node_exporter所在的宿主机ip+9100



在这里插入图片描述

//Grafan部署
#拉取grafan/grafan官方镜像
[root@master ~]# docker pull grafana/grafana
Using default tag: latest
latest: Pulling from grafana/grafana
97518928ae5f: Pull complete 
5b58818b7f48: Pull complete 
d9a64d9fd162: Pull complete 
4e368e1b924c: Pull complete 
867f7fdd92d9: Pull complete 
387c55415012: Pull complete 
07f94c8f51cd: Pull complete 
ce8cf00ff6aa: Pull complete 
e44858b5f948: Pull complete 
4000fdbdd2a3: Pull complete 
Digest: sha256:18d94ae734accd66bccf22daed7bdb20c6b99aa0f2c687eea3ce4275fe275062
Status: Downloaded newer image for grafana/grafana:latest
docker.io/grafana/grafana:latest

#使用镜像运行grafan容器,并映射端口提供服务
[root@master ~]# docker images
REPOSITORY                                                        TAG             IMAGE ID       CREATED        SIZE
prom/prometheus                                                   latest          a3d385fc29f9   11 days ago    201MB
grafana/grafana                                                   latest          9b957e098315   2 weeks ago    275MB

[root@master ~]# docker run -dit --name grafan -p 3000:3000 grafana/grafana
2a068867c04d57aa67ece4d35f28e2a77f188c248de6a43bc071a9bb21aae417
[root@master ~]# docker ps | grep grafan
2a068867c04d   grafana/grafana                                     "/run.sh"                11 seconds ago   Up 8 seconds   0.0.0.0:3000->3000/tcp, :::3000->3000/tcp   grafan



在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

彭宇栋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值