通过上一篇(一)prometheus 监控系统 的学习,我们已经对 prometheus 有了一个全面的了解,本篇详细部署安装 prometheus 监控系统。
准备环境
# 个人环境:centos 7
[root@fp-21 ~]# hostname -I
10.0.0.21
# 时间校正,这里是阿里云标准时间
[root@fp-21 ~]# ntpdate ntp.aliyun.com
29 Mar 16:36:37 ntpdate[9790]: step time server 203.107.6.88 offset -43198.785235 sec
# 关闭防火墙
[root@fp-21 ~]# systemctl stop firewalld.service
# 修改 selinux 为警告模式
[root@fp-21 ~]# setenforce 0
创建用户
# 创建 prometheus 的组和用户
[root@fp-21 ~]# groupadd prometheus
[root@fp-21 ~]# useradd -M -s /sbin/nologin prometheus -g prometheus
上传安装包,解压,点击下载
# 没有本地安装包,也可以进行下载
[root@fp-21 ~]# cd /opt/ ; mkdir prometheus ; cd prometheus
[root@fp-21 prometheus]# wget -c https://github.com/prometheus/prometheus/releases/download/v2.16.0/prometheus-2.16.0.linux-amd64.tar.gz
# 解压
[root@fp-21 prometheus]# tar xf prometheus-2.16.0.linux-amd64.tar.gz
[root@fp-21 prometheus]# ls
prometheus-2.16.0.linux-amd64 prometheus-2.16.0.linux-amd64.tar.gz
更改文件夹位置
[root@fp-21 prometheus]# mv prometheus-2.16.0.linux-amd64 /usr/local/prometheus
# 创建所需目录
[root@fp-21 prometheus]# mkdir conf relus data
# 修改配置文件路径
[root@fp-21 prometheus]# mv prometheus.yml conf
# 复制可执行文件至 /usr/local/bin
[root@fp-21 prometheus]# cp prometheus promtool tsdb /usr/local/bin/
# 授权
[root@fp-21 prometheus]# chown -R prometheus:prometheus /usr/local/prometheus
修改配置文件
[root@fp-21 prometheus]# cd conf
[root@fp-21 conf]# cp prometheus.yml prometheus.yml.bak
[root@fp-21 conf]# vim prometheus.yml
23 - job_name: 'prometheus' # 标签,监控任务的名称
28 static_configs: # 抓取的目标
29 - targets: ['10.0.0.21:9090']
添加为系统服务
[root@fp-21 ~]# vim /usr/lib/systemd/system/prometheus.service
[Unit]
Description=prometheus-server
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=simple
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/conf/prometheus.yml --web.enable-lifecycle --storage.tsdb.path=/usr/local/prometheus/data --storage.tsdb.retention.time=7d --web.max-connections=512 --web.read-timeout=3m --query.max-concurrency=25 --query.timeout=2m
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
添加开机自启,启动,查看
# 重新加载
[root@fp-21 ~]# systemctl daemon-reload
# 添加至开机自启
[root@fp-21 ~]# systemctl enable prometheus.service
Created symlink from /etc/systemd/system/multi-user.target.wants/prometheus.service to /usr/lib/systemd/system/prometheus.service.
# 启动服务
[root@fp-21 ~]# systemctl start prometheus.service
# 查看端口
[root@fp-21 ~]# ss -lntp |grep prometheus
LISTEN 0 128 :::9090 :::* users:(("prometheus",pid=10887,fd=7))
# 查看进程
[root@fp-21 ~]# ps -ef |grep prometheus
root 10887 1 3 19:59 ? 00:00:00 /usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/conf/prometheus.yml --web.enable-lifecycle --storage.tsdb.path=/usr/local/prometheus/data --storage.tsdb.retention.time=7d --web.max-connections=512 --web.read-timeout=3m --query.max-concurrency=25 --query.timeout=2m
root 10926 9766 0 20:00 pts/0 00:00:00 grep --color=auto prometheus