一、实验环境

主机:虚拟机(不做介绍)

IP地址:192.168.60.130

硬件配置:2vCPU、4G 内存

操作系统:Ubuntu 22.04.4 LTS

Docker版本:24.0.7

docker-compose版本:v2.29.7

Prometheus联邦集群:

  • Prometheus:采集、存储数据。
  • alertmanager:用于接收 Prometheus 发送的告警信息。
  • node-reporter: 用于收集操作系统和硬件信息的metrics。
  • grafana:用于图形化展示。

二、安装Docker

2.1、配置Ubuntu 22.04国内源

将/etc/apt/sources.list文件内容改为国内源,这里使用的是阿里云镜像源。

deb https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse

保存并退出编辑器。

然后,更新软件包列表:

sudo apt update

Docker搭建Prometheus监控系统_docker-compose

2.2、安装docker

在安装 Docker 之前,确保安装一些必要的依赖项:

sudo apt install apt-transport-https ca-certificates curl software-properties-common

使用以下命令添加 Docker 的官方 GPG 密钥:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

安装docker

# 安装docker
apt-get -y install docker.io
# 查看docker版本
root@ubuntu:~# docker --version
Docker version 24.0.7, build 24.0.7-0ubuntu2~22.04.1
# 配置开机启动
 systemctl enable docker --now

2.3、配置docker镜像加速器

执行命令:

# 创建docker配置文件目录
sudo mkdir -p /etc/docker
# 配置docker镜像加速器。加速器可能被限制。
sudo tee /etc/docker/daemon.json << EOF
{
  "registry-mirrors": ["https://docker.lmirror.top","https://docker.m.daocloud.io","https://hub.uuuadc.top","https://docker.anyhub.us.kg","https://dockerhub.jobcher.com","https://dockerhub.icu","https://docker.ckyl.me","https://docker.awsl9527.cn","https://docker.laoex.link"]
}
EOF
# 重启docker,使加速器生效
systemctl restart docker

2.4、安装docker-compose

下载页面:docker-compose

我下载的是:V2.29.7

将下载的docker-compose上传到服务器上。

安装docker-compose

Docker搭建Prometheus监控系统_docker_02

三、Docker-compose安装Prometheus

3.1、创建Prometheus项目目录

用于存放Prometheus项目所有的文件。

Docker搭建Prometheus监控系统_docker-compose_03

3.2、创建Prometheus配置文件及告警规则文件

3.2.1、Prometheus配置文件

在项目目录/usr/local/prometheus/prometheus下创建prometheus.yml

root@ubuntu:/usr/local/prometheus# cat prometheus/prometheus.yml 
global:
  scrape_interval: 15s 
  evaluation_interval: 15s 
alerting:
  alertmanagers:
    - static_configs:
      - targets: ['alertmanager:9093']	# alertmanager这个名字是docker-compose.yaml中service的各个服务的名字,可在容器间相互通信,下面的其他服务的名字也是一样。
rule_files:
  - "alert.yml"	# 指定告警规则文件,也是触发文件。一般与Prometheus的配置文件放在同一目录。
scrape_configs:
  - job_name: 'prometheus'
    scrape_interval: 15s
    static_configs:
      - targets: ['prometheus:9090']
  - job_name: 'alertmanager'
    scrape_interval: 15s
    static_configs:
      - targets: ['alertmanager:9093']
  - job_name: 'cadvisor'	# 监控容器
    scrape_interval: 15s
    static_configs:
      - targets: ['cadvisor:8080']
        labels:
          instance: Prometheus服务器
  - job_name: 'node-exporter'
    scrape_interval: 15s
    static_configs:
      - targets: ['node_exporter:9100']
        labels:
          instance: Prometheus服务器
root@ubuntu:/usr/local/prometheus#

注意:在使用上面配置文件时,需要将注释内容给删除掉,不然启动容器可能报错。

3.2.2、创建告警规则文件/触发文件

在项目目录/usr/local/prometheus/prometheus/下创建alert.yml(和prometheus.yml文件中的规则文件名称保持一致,且放在同一目录)

root@ubuntu:/usr/local/prometheus# cat prometheus/alert.yml 
groups:
- name: Prometheus alert rules
  rules:
  - alert: 服务告警
    expr: up == 0
    for: 30s
    labels:
      severity: critical
    annotations:
      summary: "服务异常,实例:{{ $labels.instance }}"
      description: "{{ $labels.job }} 服务已关闭"
root@ubuntu:/usr/local/prometheus#

3.3、创建Alertmanager配置文件

在项目目录/usr/local/prometheus/alertmanager/下创建config.yml

root@ubuntu:/usr/local/prometheus# cat alertmanager/config.yml
global:
  # 配置邮件发送者,可以改为自己的邮箱信息
  smtp_smarthost: 'smtp.163.com:465'
  smtp_from: 'example@163.com'
  smtp_auth_username: 'example@163.com'
  smtp_auth_password: 'your-password'
  smtp_require_tls: false
route:
  group_by: ['alertname']
  group_wait: 10s
  group_interval: 10s
  repeat_interval: 10m
  receiver: email
receivers:
- name: 'email'
  email_configs:
  # 邮件接收者
  - to: 'example@163.com'
inhibit_rules:
  - source_match:
      severity: 'critical'
    target_match:
      severity: 'warning'
    equal: ['alertname', 'instance']
root@ubuntu:/usr/local/prometheus#

注意:在使用上面配置文件时,需要将注释内容给删除掉,不然启动容器可能报错。

3.4、创建Grafana配置文件

在项目目录/usr/local/prometheus/grafana/下创建config.monitoring

root@ubuntu:/usr/local/prometheus# cat grafana/config.monitoring 
GF_SECURITY_ADMIN_PASSWORD=admin	# 配置管理员密码为admin
GF_USERS_ALLOW_SIGN_UP=false	# 设置不允许注册
root@ubuntu:/usr/local/prometheus#

注意:在使用上面配置文件时,需要将注释内容给删除掉,不然启动容器可能报错。

3.5、创建docker-compose.yml文件

在项目目录/usr/local/prometheus创建docker-compose.yml文件。

root@ubuntu:/usr/local/prometheus# cat docker-compose.yml
# yaml 配置实例
version: '3'
services:
  prometheus:
    image: prom/prometheus:v2.37.6
    container_name: prometheus
    restart: always
    volumes:
      - ./prometheus:/etc/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/usr/share/prometheus/console_libraries'
      - '--web.console.templates=/usr/share/prometheus/consoles'
      #热加载配置
      - '--web.enable-lifecycle'
      #api配置
      #- '--web.enable-admin-api'
      #历史数据最大保留时间,默认15天
      - '--storage.tsdb.retention.time=30d'
    networks:
      - monitoring
    links:
      - alertmanager
      - cadvisor
      - node_exporter
    expose:
      - '9090'
    ports:
      - 9090:9090
    depends_on:
      - cadvisor
  alertmanager:
    image: prom/alertmanager:v0.25.0
    container_name: alertmanager
    restart: always
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - ./alertmanager/:/etc/alertmanager/
    command:
      - '--config.file=/etc/alertmanager/config.yml'
      - '--storage.path=/alertmanager'
    networks:
      - monitoring
    expose:
      - '9093'
    ports:
      - 9093:9093
  cadvisor:
    image: lagoudocker/cadvisor:v0.37.0
    container_name: cadvisor
    restart: always
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /:/rootfs:ro
      - /var/run:/var/run:rw
      - /sys:/sys:ro
      - /var/lib/docker/:/var/lib/docker:ro
    networks:
      - monitoring
    expose:
      - '8080'
  node_exporter:
    image: prom/node-exporter:v1.5.0
    container_name: node-exporter
    restart: always
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /proc:/host/proc:ro
      - /sys:/host/sys:ro
      - /:/rootfs:ro
    command:
      - '--path.procfs=/host/proc'
      - '--path.sysfs=/host/sys'
      - '--collector.filesystem.ignored-mount-points=^/(sys|proc|dev|host|etc|rootfs/var/lib/docker)(915|/)'
    networks:
      - monitoring
    ports:
      - '9100:9100'
  grafana:
    image: grafana/grafana:9.4.3
    container_name: grafana
    restart: always
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - grafana_data:/var/lib/grafana
      - ./grafana/provisioning/:/etc/grafana/provisioning/
    env_file:
      - ./grafana/config.monitoring
    networks:
      - monitoring
    links:
      - prometheus
    ports:
      - 3000:3000
    depends_on:
      - prometheus
volumes:
  prometheus_data: {}
  grafana_data: {}
networks:
  monitoring:
    driver: bridge
root@ubuntu:/usr/local/prometheus#

三、管理Prometheus集群

运行:

在项目目录/usr/local/prometheus执行以下命令运行Prometheus集群。

cd /usr/local/prometheus	# 切换到项目目录
docker-compose up -d	# 启动Prometheus集群

Docker搭建Prometheus监控系统_Prometheus_04

删除:

在项目目录/usr/local/prometheus执行以下命令移除Prometheus集群。

cd /usr/local/prometheus	# 切换到项目目录
docker-compose down	# 删除Prometheus集群

Docker搭建Prometheus监控系统_docker_05

停止:

在项目目录/usr/local/prometheus执行以下命令停止Prometheus集群。

cd /usr/local/prometheus	# 切换到项目目录
docker-compose stop	# 停止Prometheus集群

Docker搭建Prometheus监控系统_Prometheus_06

启动:

在项目目录/usr/local/prometheus执行以下命令启动Prometheus集群。

cd /usr/local/prometheus	# 切换到项目目录
docker-compose 	# 启动Prometheus集群

Docker搭建Prometheus监控系统_Ubuntu22国内源_07

状态:

在项目目录/usr/local/prometheus执行以下命令查看Prometheus集群状态。

cd /usr/local/prometheus	# 切换到项目目录
docker-compose 	# 启动Prometheus集群

Docker搭建Prometheus监控系统_docker-compose_08

四、查看Prometheus各组件

4.1、查看Prometheus

网页访问:http://192.168.60.130:9090 ,无用户和密码

Docker搭建Prometheus监控系统_docker-compose_09

4.2、查看alertmanager

网页访问地址:http://192.168.60.130:9093 ,无用户和密码。

Docker搭建Prometheus监控系统_docker_10

4.3、查看node_exporter

网页访问地址:http://192.168.60.130:9100 ,无用户和密码。

Docker搭建Prometheus监控系统_Ubuntu22国内源_11



4.4、查看grafana

网页访问地址:http://192.168.60.130:3000 ,用户名:admin,密码:admin(这个密码是在配置文件中自己配置的,上面的grafana配置文件中指定的)

Docker搭建Prometheus监控系统_docker_12

首次登录需要修改密码

Docker搭建Prometheus监控系统_docker_13

登录后的页面

Docker搭建Prometheus监控系统_Ubuntu22国内源_14