本文已参与「新人创作礼」活动,一起开启掘金创作之路。
如未指定,下述命令在所有节点执行!
一、系统资源规划
节点名称 | 系统名称 | CPU/ 内存 | 网卡 | 磁盘 | IP 地址 | OS |
---|---|---|---|---|---|---|
Prometheus | prometheus | 2C/4G | ens33 | 128G | 192.168.0.10 | CentOS7 |
Grafana | grafana | 2C/4G | ens33 | 128G | 192.168.0.20 | CentOS7 |
Exporter | exporter | 2C/4G | ens33 | 128G | 192.168.0.21 | CentOS7 |
cAdvisor | cadvisor | 2C/4G | ens33 | 128G | 192.168.0.22 | CentOS7 |
二、系统软件安装与设置
1、安装基本软件
yum -y install vim lrzsz bash-completion
2、设置名称解析
echo 192.168.0.10 prometheus >> /etc/hosts
echo 192.168.0.20 grafana >> /etc/hosts
echo 192.168.0.21 exporter >> /etc/hosts
echo 192.168.0.22 cadvisor >> /etc/hosts
3、设置NTP
yum -y install chrony
systemctl start chronyd
systemctl enable chronyd
systemctl status chronyd
chronyc sources
4、设置防火墙、SELinux
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
三、部署Prometheus监控平台
1、部署Prometheus
在Prometheus节点上下载Prometheus二进制文件:
在Prometheus节点上解压Prometheus二进制文件至系统目录:
tar -xf /root/prometheus-2.30.0-rc.0.linux-amd64.tar.gz -C /usr/local/
mv /usr/local/prometheus-2.30.0-rc.0.linux-amd64/ /usr/local/prometheus/
在Prometheus节点上创建数据目录:
mkdir -p /usr/local/prometheus/data/
在Prometheus节点上配置systemd管理prometheus:
cat > /etc/systemd/system/prometheus.service << EOF
[Unit]
Description=Prometheus
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/usr/local/prometheus/data/
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
在Prometheus节点上启动prometheus,并设置自启动:
systemctl start prometheus
systemctl enable prometheus
systemctl status prometheus
登录Prometheus界面:
http://192.168.0.10:9090/graph
2、部署Grafana
在Grafana节点上下载Grafana二进制文件:
下载地址:dl.grafana.com/oss/release…
在Grafana节点上解压Grafana二进制文件至系统目录:
tar -xf /root/grafana-8.1.3.linux-amd64.tar.gz -C /usr/local/
mv /usr/local/grafana-8.1.3/ /usr/local/grafana/
在Grafana节点上创建数据目录:
mkdir -p /usr/local/grafana/data/
在Grafana节点上修改配置文件/usr/local/grafana/conf/defaults.ini:
[paths]
data = /usr/local/grafana/data
temp_data_lifetime = 24h
logs = /usr/local/grafana/log
plugins = /usr/local/grafana/plugins
provisioning = /usr/local/grafana/conf/provisioning
在Grafana节点上配置systemd管理grafana-server:
cat > /etc/systemd/system/grafana-server.service << EOF
[Unit]
Description=Grafana
After=network.target
[Service]
Type=notify
ExecStart=/usr/local/grafana/bin/grafana-server -homepath /usr/local/grafana/
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
在Grafana节点上启动grafana-server,并设置自启动:
systemctl start grafana-server
systemctl enable grafana-server
systemctl status grafana-server
登录Grafana界面:
http://192.168.0.20:3000,初始用户名密码:admin/admin
在Grafana节点上添加数据源,http://192.168.0.10:9090:
Configuration > Data sources > Add data source > Prometheus
3、部署Node Exporter
在Exporter节点上下载Node Exporter二进制文件:
在Exporter节点上解压Node Exporter二进制文件至系统目录:
tar -xf /root/node_exporter-1.2.2.linux-amd64.tar.gz -C /usr/local/
mv /usr/local/node_exporter-1.2.2.linux-amd64/ /usr/local/node_exporter/
在Exporter节点上配置systemd管理node_exporter:
cat > /etc/systemd/system/node_exporter.service << EOF
[Unit]
Description=Prometheus Node Exporter
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
在Exporter节点上启动node_exporter,并设置自启动:
systemctl start node_exporter
systemctl enable node_exporter
systemctl status node_exporter
在Prometheus节点上添加mode_exporter配置:
修改/usr/local/prometheus/prometheus.yml配置文件,在scrape_configs下增加配置:
- job_name: 'node_exporter'
static_configs:
- targets: ['192.168.0.21:9100']
在Prometheus节点上重启prometheus:
systemctl restart prometheus
systemctl status prometheus
在Prometheus节点上查看数据采集状态:
http://192.168.0.10:9090/graph,States > Targets
在Grafana界面导入Node监控模板,编号为9276:
http://192.168.0.20:3000,Create > Import
4、部署MySQL Exporter
在Exporter节点上部署MySQL,过程省略。
在Exporter节点上下载MySQL Exporter二进制文件:
在Exporter节点上解压MySQL Exporter二进制文件至系统目录:
tar -xf /root/mysqld_exporter-0.13.0.linux-amd64.tar.gz -C /usr/local/
mv /usr/local/mysqld_exporter-0.13.0.linux-amd64/ /usr/local/mysqld_exporter/
在Exporter节点上创建MySQL配置文件:
cat > /etc/.mysqld_exporter.cnf << EOF
[client]
user=root
password=PassWord5.7!
EOF
在Exporter节点上配置systemd管理mysqld_exporter:
cat > /etc/systemd/system/mysqld_exporter.service << EOF
[Unit]
Description=Prometheus MySQL Exporter
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf /etc/.mysqld_exporter.cnf --collect.auto_increment.columns --collect.binlog_size --collect.engine_innodb_status --collect.engine_tokudb_status --collect.global_status --web.listen-address=0.0.0.0:9104
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
在Exporter节点上启动mysqld_exporter,并设置自启动:
systemctl start mysqld_exporter
systemctl enable mysqld_exporter
systemctl status mysqld_exporter
在Prometheus节点上添加mysqld_exporter配置:
修改/usr/local/prometheus/prometheus.yml配置文件,在scrape_configs下增加配置:
- job_name: 'mysqld_exporter'
static_configs:
- targets: ['192.168.0.21:9104']
在Prometheus节点上重启prometheus:
systemctl restart prometheus
systemctl status prometheus
在Prometheus节点上查看数据采集状态:
http://192.168.0.10:9090/graph,States > Targets
在Grafana界面导入MySQL监控模板,编号为7362:
http://192.168.0.20:3000,Create > Import
5、部署Redis Exporter
在Exporter节点上部署Redis,过程省略。
在Exporter节点上下载Redis Exporter二进制文件:
在Exporter节点上解压Redis Exporter二进制文件至系统目录:
tar -xf /root/redis_exporter-v1.27.0.linux-amd64.tar.gz -C /usr/local/
mv /usr/local/redis_exporter-v1.27.0.linux-amd64/ /usr/local/redis_exporter/
在Exporter节点上配置systemd管理redis_exporter:
cat > /etc/systemd/system/redis_exporter.service << EOF
[Unit]
Description=Prometheus Redis Exporter
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/redis_exporter/redis_exporter -redis.addr 127.0.0.1:6379 -redis.password 123456
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
在Exporter节点上启动redis_exporter,并设置自启动:
systemctl start redis_exporter
systemctl enable redis_exporter
systemctl status redis_exporter
在Prometheus节点上添加redis_exporter配置:
修改/usr/local/prometheus/prometheus.yml配置文件,在scrape_configs下增加配置:
- job_name: 'redis_exporter'
static_configs:
- targets: ['192.168.0.21:9121']
在Prometheus节点上重启prometheus:
systemctl restart prometheus
systemctl status prometheus
在Prometheus节点上查看数据采集状态:
http://192.168.0.10:9090/graph,States > Targets
在Grafana界面导入Redis监控模板,编号为763:
http://192.168.0.20:3000,Create > Import
6、部署Memcache Exporter
在Exporter节点上部署Memcache,过程省略。
在Exporter节点上下载Memcache Exporter二进制文件:
在Exporter节点上解压Memcache Exporter二进制文件至系统目录:
tar -xf /root/memcached_exporter-0.9.0.linux-amd64.tar.gz -C /usr/local/
mv /usr/local/memcached_exporter-0.9.0.linux-amd64/ /usr/local/memcached_exporter/
在Exporter节点上配置systemd管理memcached_exporter:
cat > /etc/systemd/system/memcached_exporter.service << EOF
[Unit]
Description=Prometheus Memcached Exporter
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/memcached_exporter/memcached_exporter --memcached.address=127.0.0.1:11211
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
在Exporter节点上启动memcached_exporter,并设置自启动:
systemctl start memcached_exporter
systemctl enable memcached_exporter
systemctl status memcached_exporter
在Prometheus节点上添加memcached_exporter配置:
修改/usr/local/prometheus/prometheus.yml配置文件,在scrape_configs下增加配置:
- job_name: 'memcached_exporter'
static_configs:
- targets: ['192.168.0.21:9150']
在Prometheus节点上重启prometheus:
systemctl restart prometheus
systemctl status prometheus
在Prometheus节点上查看数据采集状态:
http://192.168.0.10:9090/graph,States > Targets
在Grafana界面导入Memcache监控模板,编号为37:
http://192.168.0.20:3000,Create > Import
7、部署cAdvisor
在Exporter节点上部署Docker,过程省略。
在cAdvisor节点上启动cAdvisor容器:
docker run --volume /:/rootfs:ro --volume /var/run:/var/run:rw --volume /sys:/sys:ro --volume /var/lib/docker:/var/lib/docker:ro --network host -d --name cadvisor google/cadvisor
在cAdvisor节点上启动10个业务容器:
for (( i = 0; i < 10; i++ )); do docker run -itd busybox; done
在cAdvisor节点上查看容器运行状态:
在Prometheus节点上添加cadvisor配置:
修改/usr/local/prometheus/prometheus.yml配置文件,在scrape_configs下增加配置:
- job_name: 'cadvisor'
static_configs:
- targets: ['192.168.0.22:8080']
在Prometheus节点上重启prometheus:
systemctl restart prometheus
systemctl status prometheus
在Prometheus节点上查看数据采集状态:
http://192.168.0.10:9090/graph,States > Targets
在Grafana界面导入cAdvisor监控模板,编号为11277:
http://192.168.0.20:3000,Create > Import