Prometheus监控系列二 | Docker容器化部署实战
文章目录
1 部署 Docker 服务
curl https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker.repo
yum list docker-ce --showduplicates | sort -r # 显示所有版本
yum install -y docker-ce-20.10.5 # 指定 docker 版本
systemctl start docker # 启动 docker 服务
systemctl status docker # 查看 docker 服务状态
systemctl enable docker # 设置 docker 开机自启动
2 部署 Prometheus 服务
创建 mon 用户,创建目录:
groupadd -g 2000 mon
useradd -u 2000 -g mon mon
mkdir -p /home/mon/prometheus/{
etc,data,rules}
创建配置文件:
vim /home/mon/prometheus/etc/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']
启动容器服务:
docker pull prom/prometheus
cd /home/mon/
chown mon. -R prometheus
docker run -d --user root -p 9090:9090 --name prometheus \
-v /home/mon/prometheus/etc/prometheus.yml:/etc/prometheus/prometheus.yml \
-v /home/mon/prometheus/rules:/etc/prometheus/rules \
-v /home/mon/prometheus/data:/data/prometheus \
prom/prometheus \
--config.file="/etc/prometheus/prometheus.yml" \
--storage.tsdb.path="/data/prometheus" \
--web.listen-address="0.0.0.0:9090"
3 部署 Grafana 服务
创建数据目录:
mkdir -p /home/mon/grafana/plugins
安装插件:下载Grafana插件
tar zxf /tmp/grafana-plugins.tar.gz -C /home/mon/grafana/plugins/
chown -R mon. /home/mon/grafana
chmod 777 -R /home/mon/grafana
启动容器服务:
docker pull grafana/grafana:latest
docker run -d -p 3000:3000 -v /home/mon/grafana:/var/lib/grafana --name=grafana grafana/grafana:latest
4 配置 Grafana 对接 Prometheus
访问 http://ip:3000,初始账号密码为 admin/admin,会要求更改密码。
按照如下截图顺序配置 Prometheus Dashboard:







5 部署 Node_Exporter 服务
我以监控一台阿里云ECS为例。
安装配置Node_Exporter:
curl https://github.com/prometheus/node_exporter/releases/download/v1.1.1/node_exporter-1.1.1.linux-amd64.tar.gz > /opt/node_exporter-1.1.1.linux-amd64.tar.gz
cd /opt
tar zxf node_exporter-1.1.1.linux-amd64.tar.gz
mv node_exporter-1.1.1.linux-amd64 node_exporter
配置服务启动脚本:
vim /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=node_exporter service
[Service]
User=root
ExecStart=/opt/node_exporter/node_exporter
TimeoutStopSec=10
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start node_exporter
systemctl status node_exporter
systemctl enable node_exporter
在这台ECS服务器的Nginx下配置反向代理:
vim /opt/nginx/conf/conf.d/blog.conf
# prometheus monitor node exporter
location /node/service {
proxy_pass http://127.0.0.1:9100/metrics;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
/opt/nginx/sbin/nginx -s reload
在Prometheus服务器端修改 配置文件:
vim /home/mon/prometheus/etc/prometheus.yml
- job_name: 'node-service'
static_configs:
- targets: ['blog.iuskye.com']

最低0.47元/天 解锁文章
994

被折叠的 条评论
为什么被折叠?



