docker

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > 云和虚拟化 > docker > Docker部署Prometheus

Docker部署Prometheus的实现示例

作者:不甘平凡※

Prometheus是一款开源的监控和告警工具,它可以收集系统的各种指标数据,如CPU使用率、内存使用量等,本文主要介绍了Docker部署Prometheus的实现示例,感兴趣的可以了解一下

环境:阿里云CentOS7.9

docker部署

1、确定是CentOS7及以上版本

cat /etc/redhat-release

2、如有旧版本,需要先卸载旧版本

 yum remove docker

3、yum安装gcc相关以及需要的软件包

yum -y install gcc gcc-c++ yum-utils

4、配置国内docker-ce的yum源(采用的是阿里云)

yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

5、配置完yum源后可以切换到/etc/yum.repos.d/目录下,查看下是否存在docker-ce.repo,存在的话说明配置成功

 6、更新yum软件包索引

yum makecache fast

 7、安装docker

yum -y install docker-ce docker-ce-cli containerd.io

8、启动docker

systemctl start docker
systemctl status docker

9、查看docker版本

docker --version
Docker version 26.1.4, build 5650f9b

10、配置镜像加速器

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://cz1numin.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

11、docker info命令查看一下是否新增了阿里云的地址,确保配置成功

Prometheus部署

1、下载镜像包

docker pull prom/node-exporter
docker pull prom/mysqld-exporter
docker pull oliver006/redis_exporter 
docker pull prom/prometheus
docker pull grafana/grafana

2、启动容器

2.1启动node-exporter

docker run -d --name node-exporter \
--restart=always -p 9100:9100 \
-v /proc:/host/proc:ro \
-v /sys:/host/sys:ro \
-v /:/rootfs:ro prom/node-exporter

访问地址:http://47.119.27.111:9100/metrics

2.2启动 mysqld-exporter

docker run -d --name mysqld_exporter \
--restart=always -p 9104:9104 \
-e DATA_SOURCE_NAME="root:Password123@(192.168.0.230:3306)/"  \
prom/mysqld-exporter

访问地址:http://47.119.27.111:9104/metrics

2.3启动 redis_exporter

#不带密码
docker run -d --name redis_exporter -p 9121:9121 oliver006/redis_exporter --redis.addr redis://192.168.0.230:6379

# 绑定集群中一个redis实例就可以了
# 带密码
docker run -d --name redis_exporter -p 9121:9121 oliver006/redis_exporter --redis.addr redis://192.168.0.230:6379 --redis.password '123123'

访问地址:http://47.119.27.111:9121/metrics

2.4查看容器IP

查看当前运行的网络列表

docker network ls

NETWORK ID     NAME      DRIVER    SCOPE
16dab27fe490   bridge    bridge    local
05a7e6db54e6   host      host      local
f2da59c2d4f9   none      null      local

查看指定网络中的容器和其IP地址

docker network inspect <网络ID或网络名称>

            "0f8f21d3d4cb8a892e3915430a56569c5182346d79a7dd6434718b7dfa59ab0a": {
                "Name": "redis_exporter",
                "EndpointID": "597b8133bfc51af671889a8786000fc27b4c53a74edda683b2be4272ceac0310",
                "MacAddress": "02:42:ac:11:00:04",
                "IPv4Address": "172.17.0.4/16",
                "IPv6Address": ""
            },
            "940f4c0ca99bc9af06dd17f405bb5fb5f52bcd147149270f07ebe5af72128d9f": {
                "Name": "node-exporter",
                "EndpointID": "111ecdc84bd9682018f09d8e5010802dcb5c6d4052c126b931abbc0e539dff90",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16",
                "IPv6Address": ""
            },
            "cdc09d537f36572104e001d071cbc4642b0b9a528019f37149de3727f96827e1": {
                "Name": "mysqld_exporter",
                "EndpointID": "63029028def1861190829aec2a4d2f75048bd8e3acfe0fee0c628c169562e998",
                "MacAddress": "02:42:ac:11:00:03",
                "IPv4Address": "172.17.0.3/16",
                "IPv6Address": ""
            }

2.5启动Prometheus

新建目录prometheus,编辑配置文件prometheus.yml

mkdir /opt/prometheus
cd /opt/prometheus/
vim prometheus.yml
global:
  scrape_interval:     60s
  evaluation_interval: 60s

scrape_configs:

  - job_name: prometheus
    static_configs:
      - targets: ['localhost:9090']
        labels:
          instance: prometheus

  - job_name: node
    static_configs:
      - targets: ['172.17.0.2:9100']
        labels:
          instance: node-exporter

  - job_name: redis
    static_configs:
      - targets: ['172.17.0.4:9121']
        labels:
          instance: redis_exporter

  - job_name: mysqld
    static_configs:
      - targets: ['172.17.0.3:9104']
        labels:
          instance: mysql-exporter

启动(注意:宿主机的路径必须是绝对路径:/opt/prometheus/prometheus.yml,否则会报错)

docker run  -d --name prometheus --restart=always -p 9090:9090 -v /opt/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus

访问地址:http://47.119.27.111:9090/graph

访问targets地址:http://47.119.27.111:9090/targets

2.6启动grafana

新建空文件夹grafana-storage,用来存储数据

mkdir /opt/grafana-storage

添加权限

chmod 777 -R /opt/grafana-storage

启动

docker run -d --name grafana --restart=always  \
 -p 3000:3000 --name=grafana  \
 -v /opt/grafana-storage:/var/lib/grafana grafana/grafana

访问地址:http://47.119.27.111:3000/login

配置Grafana

默认登录:admin/admin

设置----》Data sources----》Add data source

选择Prometheus

Name:Prometheus

URL【Prometheus的ip(docker容器私网ip)+端口】:http://172.17.0.5:9090

点击下面的Save & Test,如果出现绿色的,说明ok了

监控mysql

导入grafana监控模板,mysql监控模板id为7362,填入如下输入框内

 

监控主机

导入主机的监控模板,模板id为8919,以相同方式导入即可

监控容器

导入容器的监控模板,模板id为179,以相同方式导入即可

监控redis

导入容器的监控模板,模板id为 763,以相同方式导入即可

到此这篇关于Docker部署Prometheus的实现示例的文章就介绍到这了,更多相关Docker部署Prometheus内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

您可能感兴趣的文章:
阅读全文