Prometheus容器部署结合grafana画图
1. 拉取prometheus官方镜像
// 拉取镜像
[root@master ~]# docker pull prom/prometheus
Using default tag: latest
latest: Pulling from prom/prometheus
3cb635b06aa2: Already exists
34f699df6fe0: Pull complete
33d6c9635e0f: Pull complete
f2af7323bed8: Pull complete
c16675a6a294: Pull complete
827843f6afe6: Pull complete
3d272942eeaf: Pull complete
7e785cfa34da: Pull complete
05e324559e3b: Pull complete
170620261a59: Pull complete
ec35f5996032: Pull complete
5509173eb708: Pull complete
Digest: sha256:cb9817249c346d6cfadebe383ed3b3cd4c540f623db40c4ca00da2ada45259bb
Status: Downloaded newer image for prom/prometheus:latest
docker.io/prom/prometheus:latest
// 解压一个prometheus软件包获取配置文件
[root@master src]# pwd
/usr/src
[root@master src]# ls
debug kernels prometheus-2.31.1.linux-amd64.tar.gz
[root@master src]# tar xf prometheus-2.31.1.linux-amd64.tar.gz
// 为了方便操作做一个软链接
[root@master src]# ln -s prometheus-2.31.1.linux-amd64 prometheus
2. 官方镜像运行Prometheus容器并做端口映射和挂载卷
[root@master prometheus]# docker run -itd --name prometheus --restart always -p 9090:9090 -v /usr/src/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
e32d6acf5d97964f24d4ec925c31288b4b6f4fda526ef94b8c23a1360fa71482
// 浏览器访问测试
3. 下载包并解压
[root@node1 src]# pwd
/usr/src
[root@node1 src]# wget https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz
[root@node1 src]# tar xf node_exporter-1.3.1.linux-amd64.tar.gz -C /usr/local/
// 做软链接为了方便操作
[root@node1 local]# ln -s node_exporter-1.3.1.linux-amd64 node_exporter
4. 写service使用systemd控制node_exporter
[root@node1 local]# vim /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=Node Export server daemon
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.targe
[root@node1 local]# systemctl daemon-reload
[root@node1 local]# systemctl enable --now node_exporter.service
[root@node1 local]# systemctl status node_exporter.service
● node_exporter.service - Node Export server daemon
Loaded: loaded (/usr/lib/systemd/system/node_exporter.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2021-12-30 20:30:55 CST; 5s ago
[root@node1 local]# ss -anlt | grep 9100
LISTEN 0 128 *:9100 *:*
5. 修改配置文件
// 到master修改Prometheus.yml的配置文件
[root@master prometheus]# vim prometheus.yml
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "prometheus"
- job_name: "linux" //添加此行,这个是监控的任务名
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ["localhost:9090"]
- targets: ["192.168.182.151:9100"] //添加这一行,写node1上的IP和端口号
// 重启容器
[root@master prometheus]# docker restart prometheus
prometheus
6. 运行grafana容器
[root@master ~]# docker pull grafana/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
[root@master ~]# docker run -itd --name grafana -p 3000:3000 grafana/grafana
ef4b5c97f5e20b852e8bd804d634d51e3639d51c7abe9a819165178b173efdc4
// 用浏览器访问IP加3000端口号
用户名和密码均为admin,登录之后,系统会让你重新设置密码
// 点击这个
// 因为Prometheus和grafana在同一台主机,所以直接写localhost加Prometheus端口号就可以了
// 点击保存并测试
// 导入模板,我们可以去官网找想要的模板
grafana官网
// 点击load