容器化部署告警监控系统
# 部署微服务告警监控系统
# 📌容器化部署Node-Exporter
在master节点上编写 /root/Monitor/Dockerfile-exporter 文件构建 monitor-exporter:v1.0 镜像具体要求如下:(需要用到的软件包:http://10.24.1.46/Monitor.tar.gz)
(1)基础镜像:centos:centos7.9.2009 ;
(2)使用二进制包 node_exporter-0.18.1.linux-amd64.tar.gz
安装node-exporter
服务;
(3)开放端口:9100
;
(4)设置服务开机自启。
当前所有的文件如下:
[root@master Monitor]# ll
total 369616
-rw-r--r--. 1 root root 25880024 May 17 08:18 alertmanager-0.24.0.linux-amd64.tar.gz
-rw-------. 1 root root 211696640 Jan 12 08:49 CentOS_7.9.2009.tar
-rw-r--r--. 1 root root 952 May 18 22:16 docker-compose.yaml
-rw-r--r--. 1 root root 633 May 17 16:55 Dockerfile-alert
-rw-r--r--. 1 root root 243 May 17 15:47 Dockerfile-exporter
-rw-r--r--. 1 root root 191 May 18 05:33 Dockerfile-grafana
-rw-r--r--. 1 root root 519 May 18 22:17 Dockerfile-prometheus
-rw-r--r--. 1 root root 59635417 May 17 08:18 grafana-6.4.1.linux-amd64.tar.gz
-rw-r--r--. 1 root root 8083296 May 17 08:17 node_exporter-0.18.1.linux-amd64.tar.gz
drwxr-xr-x. 4 3434 3434 132 May 18 06:42 prometheus-2.29.1.linux-amd64
-rw-r--r--. 1 root root 73156341 May 17 08:18 prometheus-2.29.1.linux-amd64.tar.gz
-rw-r--r--. 1 root root 1122 May 18 21:37 prometheus.yml
FROM centos:centos7.9.2009
MAINTAINER Chinaskill
ADD node_exporter-0.18.1.linux-amd64.tar.gz /opt
RUN mv /opt/node_exporter-0.18.1.linux-amd64/node_exporter /bin/node_exporter
EXPOSE 9100
ENTRYPOINT [ "/bin/node_exporter" ]
[root@master ~]# docker build -t monitor-exporter:v1.0 -f Dockerfile-exporter .
检测命令1:
$ cd /rootMonitor && docker build -t monitor-exporterv1.0 -f Dockerfile-exporter .
返回
node_exporter-0.18.1.linux-amd64.tar.gz || 9100 || Successfully tagged monitor-exporterv1.0
检测命令2:
$ docker run -d --name exporter-exam-jiance monitor-exporter:v1.0 && sleep 5 && docker exec exporter-exam-jiance ps -aux && docker rm -f exporter-exam-jiance
返回
node_exporter
# 📌容器化部署Alertmanager
在master节点上编写 /root/Monitor/Dockerfile-alert 文件构建 monitor-alert:v1.0 镜像具体要求如下:(需要用到的软件包:http://10.24.1.46/Monitor.tar.gz)
(1)基础镜像:centos:centos7.9.2009 ;
(2)使用提供的二进制包 alertmanager-0.19.0.linux-amd64.tar.gz
安装 Alertmanager
服务;
(3)开放端口:9093, 9094
;
(4)设置服务开机自启。
FROM centos:centos7.9.2009
MAINTAINER Chinaskill
ADD alertmanager-0.24.0.linux-amd64.tar.gz /bin/
RUN mv /bin/alertmanager-0.24.0.linux-amd64/ /bin/alertmanager
EXPOSE 9093 9094
ENTRYPOINT ["/bin/alertmanager/alertmanager"]
CMD ["--config.file=/bin/alertmanager/alertmanager.yml"]
[root@master ~]# docker build -t monitor-alert:v1.0 -f Dockerfile-alert .
检测命令1:
$ cd /root/Monitor/ && docker build -t monitor-alert:v1.0 -f Dockerfile-alert .
返回
alertmanager-0.19.0.linux-amd64.tar.gz || 9093 || Successfully tagged monitor-alert:v1.0
检测命令2:
$ docker run -d --name alert-exam-jiance monitor-alert:v1.0 && sleep 5 && docker exec alert-exam-jiance ps -aux && docker rm -f alert-exam-jiance
返回
alertmanager
# 📌容器化部署Grafana
在master节点上编写 /root/Monitor/Dockerfile-grafana 文件构建 monitor-grafana:v1.0 镜像具体要求如下:(需要用到的软件包:http://10.24.1.46/Monitor.tar.gz)
(1)基础镜像:centos:centos7.9.2009 ;
(2)使用提供的二进制包 grafana-6.4.1.linux-amd64.tar.gz
安装 grafana
服务;
(3)开放端口:3000 ;
(4)设置 nacos 服务开机自启。
FROM centos:centos7.9.2009
MAINTAINER Chinaskill
ADD grafana-6.4.1.linux-amd64.tar.gz /usr/
WORKDIR /usr/grafana-6.4.1/
EXPOSE 3000
ENTRYPOINT ["/usr/grafana-6.4.1/bin/grafana-server"]
[root@master ~]# docker build -t monitor-grafana:v1.0 -f Dockerfile-grafana .
检测命令1:
$ cd /root/Monitor/ && docker build -t monitor-grafana:v1.0 -f Dockerfile-grafana .
返回
grafana-6.4.1.linux-amd64.tar.gz || 3000 || successfully tagged monitor-grafana:v1.0
检测命令2:
$ docker run -d --name grafana-exam-jiance monitor-grafana:v1.0 && sleep 5 && docker exec grafana-exam-jiance ps -aux && docker rm -f grafana-exam-jiance
返回
grafana-server
# 📌容器化部署Prometheus
在master节点上编写 /rootMonitor/Dockerfile-prometheus 文件构建 monitor-prometheus:v1.0 镜像具体要求如下:(需要用到的软件包:http://10.24.1.46/Monitor.tar.gz)
(1)基础镜像:centos:centos7.9.2009 ;
(2)使用提供的二进制包 monitor-2.13.0.linux-amd64.tar.gz
安装promethues
服务;
(3)编写 prometheus.yml
文件,创建3个任务模板:prometheus
、node
和 alertmanager
,并将该文件拷贝到/data/prometheus/
目录下;
(4)开放端口:9090;
(5)设置服务开机自启。
注意:
Dockerfile注释的三行是配置地区时间,也可以在docker-compose中配置,我是在docker-compose配置
FROM centos:centos7.9.2009
MAINTAINER Chinaskill
RUN mkdir /data
ADD prometheus-2.29.1.linux-amd64.tar.gz /data/
RUN mv /data/prometheus-2.29.1.linux-amd64 /data/prometheus && \
rm -rf /data/prometheus/prometheus.yml
COPY prometheus.yml /data/prometheus/
WORKDIR /data/prometheus
# ENV TZ "Asia/Shanghai"
# RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# RUN echo $TZ > /etc/timezone
EXPOSE 9090
ENTRYPOINT ["/data/prometheus/prometheus"]
CMD [ "--config.file=/data/prometheus/prometheus.yml"]
[root@master ~]# docker build -t monitor-prometheus:v1.0 -f Dockerfile-prometheus .
查看prometheus.yml
monitor-alertmanager
是容器名称monitor-node
是容器名称monitor-alertmanager
是容器名称
# 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:
- monitor-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: "node"
static_configs:
- targets: ["monitor-node:9100"]
- job_name: "alertmanager"
static_configs:
- targets: ["monitor-alertmanager:9093"]
检测命令1:
$ cd /root/Monitorl && docker build -t monitor-prometheus:v1.0 -f Dockerfile-prometheus .
返回
prometheus-2.13.0.linux-amd64.tar.gz || 9090 || Successfully tagged monitor-prometheus:v1.0
检测命令2:
$ docker run -d --name monitor-exam-jiance monitor-prometheus:v1.0 && sleep 5 && docker exec monitor-exam-jiance ps -aux && docker rm -f monitor-exam-jiance
返回
prometheus.yml
# 📌编排部署监控系统
在master节点上编写/root/Monitor/docker-compose.yaml文件部署,具体要求如下:
(1)容器 1 名称:monitor-node;
镜像:monitor-exporter:v1.0;
端口映射:9100:9100
;
(2)容器 2 名称:monitor-alertmanager;
镜像:monitor-alert:v1.0;
端口映射:9093:9093
、9094:9094
;
(3)容器 3 名称:monitor-grafana;
镜像:monitor-grafana:v1.0;
端口映射:3000
:3000
;
(4)容器 4 名称:monitor-prometheus;
镜像:monitor-prometheus:v1.0;
端口映射:9090
:9090
;
完成后编排部署监控系统,将Prometheus
设置为Grafana
的数据源,并命名为Prometheus
。
[root@master Monitor]# docker images | grep monitor
monitor-grafana v1.0 d924b97d967c 54 minutes ago 376MB
monitor-alert v1.0 1cae6c12be14 55 minutes ago 318MB
monitor-exporter v1.0 61356c46a125 55 minutes ago 238MB
monitor-prometheus v1.0 773023b3fc23 58 minutes ago 590MB
编写docker—compose:
- 先部署
monitor-node
- 再部署
monitor-node
- 部署
Prometheus
需要依赖monitor-node
和monitor-node
- 部署
Grafana
需要依赖Prometheus
- 所有容器配置时间地区和时间同步,保证不会出现时间差
version: "3.7"
services:
monitor-node:
image: monitor-exporter:v1.0
container_name: monitor-node
ports:
- "9100:9100"
restart: always
volumes:
- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro
- /proc:/host/proc:ro
- /sys:/host/sys:ro
- /:/rootfs:ro
monitor-alertmanager:
image: monitor-alert:v1.0
container_name: monitor-alertmanager
ports:
- "9093:9093"
- "9094:9094"
restart: always
volumes:
- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro
monitor-prometheus:
image: monitor-prometheus:v1.0
container_name: monitor-prometheus
ports:
- "9090:9090"
restart: always
depends_on:
- monitor-alertmanager
volumes:
- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro
monitor-grafana:
image: monitor-grafana:v1.0
container_name: monitor-grafana
ports:
- "3000:3000"
restart: always
volumes:
- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro
depends_on:
- monitor-prometheus
执行docker-compose测试:
[root@host-192-168-100-186 opt]# docker-compose up -d
[+] Running 5/5
⠿ Network opt_default Created 0.1s
⠿ Container monitor-alertmanager Started 0.6s
⠿ Container monitor-node Started 0.6s
⠿ Container monitor-prometheus Started 1.4s
⠿ Container monitor-grafana Started 2.3s
[root@host-192-168-100-186 opt]# docker-compose ps
NAME COMMAND SERVICE STATUS PORTS
monitor-alertmanager "/bin/alertmanager/a…" monitor-alertmanager running 0.0.0.0:9093-9094->9093-9094/tcp, :::9093-9094->9093-9094/tcp
monitor-grafana "/usr/grafana-6.4.1/…" monitor-grafana running 0.0.0.0:3000->3000/tcp, :::3000->3000/tcp
monitor-node "/bin/node_exporter" monitor-node running 0.0.0.0:9100->9100/tcp, :::9100->9100/tcp
monitor-prometheus "/usr/prometheus-2.2…" monitor-prometheus running 0.0.0.0:9090->9090/tcp, :::9090->9090/tcp
检测命令1:
$ docker-compose -f /root/Monitor/docker-compose.yaml ps | grep Up
返回
monitor-alertmanager || 9093->9093 || monitor-grafana || 3000->3000 || monitor-node || 9100->9100 || monitor-prometheus || 9090->9090
检测命令2:
$ curl-L http://$(hostname -i):9090/targets
返回
alertmanager (1/1 up) || node(1/1 up) || prometheus (1/1 up)
检测命令3:
$ curl -u admin:admin http://$(hostname -i):3000/datasources
返回
9090
检测命令4:
$ docker exec monitor-prometheus cat ldata/prometheus/prometheus.yml
返回
node-exporter || alertmanager
检测命令5:
$ curl -L http://$(hostname -i):9093
返回
Alertmanager
# 📌配置Grafana
查看当前的Prometheus
监控的targets
查看当前的alertmanager
的状态
查看当前的node_exporter
监控指标
登录Grafana
配置prometheus
的数据源
账号: admin
密码: admin
导入我们下载好的离线json文件