Prometheus系列——Prometheus Server安装
因为一些特殊的原因,最近更新公司的监控系统,从zabbix更换为prometheus,也刚好之前看过《Prometheus监控实战》这本书,一直没有机会实践,正好借此机会实战一下。
Prometheus就不介绍了吧,网上很多我也不想去抄那些简介了,在此推荐官网的文档Prometheus官方文档,或者中文的文档推荐Prometheus中文文档,他们写的应该比我转述的更牛逼。
话不多说开始安装,现在此声明一下,笔者安装使用的服务器:CentOS7.6 64位,因为centos6已经稍微落伍了。
一、下载二进制包安装Prometheus Server
本文主要介绍使用二进制的方式安装Prometheus,这种方式比较推荐,github的release也有相应的编译好的二进制包,下载对应的版本,就能很好的使用了。笔者安装的环境时CentOS7.6,64位,所以我的二进制包下载地址是:https://github.com/prometheus/prometheus/releases/download/v2.17.1/prometheus-2.17.1.linux-amd64.tar.gz
cd /usr/local/src/
wget https://github.com/prometheus/prometheus/releases/download/v2.17.1/prometheus-2.17.1.linux-amd64.tar.gz
tar xzf prometheus-2.17.1.linux-amd64.tar.gz
mv prometheus-2.17.1.linux-amd64 /usr/local/prometheus
二、修改配置文件
笔者将配置文件放到了/etc/prometheus 这个文件夹下面
mkdir -pv /etc/prometheus
cp -a /usr/local/prometheus/prometheus.yml /etc/prometheus
##配置文件内容如下,这个是默认的配置文件,暂时先保持默认:
# 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:
# - 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']
三、创建prometheus用户以及创建数据存储目录
## 创建用户
useradd -s /sbin/nologin -M prometheus
## 创建数据目录
mkdir -pv /usr/local/prometheus/data
## 相关目录赋权,因为接下来我配置prometheus启动用户是prometheus这个用户
chown -R promethues.root /etc/prometheus
chown -R promethues.root /usr/local/prometheus
四、启动Prometheus服务
1、配置systemd启动prometheus
其实prometheus启动很简单“/usr/local/prometheus/prometheus --config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/usr/local/prometheus/data”这条命令就能启动,但是不易管理,所以配置systemd管理,使用systemd进行启停prometheus
# vim /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/prometheus/prometheus --config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/usr/local/prometheus/data
Restart=on-failure
[Install]
WantedBy=multi-user.target
上面这个文件中,如果不指定storage.tsdb.path的话,这个目录会默认配置在prometheus命令所在目录下的data目录中(好像我配置这个就是默认的位置,哈哈哈,就这么任性),哦,对了,为了某些不记得systemd如何启动的同学,我罗列下使用systemd管理prometheus,
# 启动prometheus
systemctl start prometheus
# 查看prometheus状态
systemctl status prometheus
# 开机启动???或者让prometheus成为systemd的一个服务
systemctl enable prometheus
# 重启prometheus
systemctl restart prometheus
# 停止prometheus
systemctl stop prometheus
2、配置supervisor启动prometheus
supervisor配置文件编写
# vim /etc/supervisord.d/prometheus-server.ini
[program:prometheus-server] #
command=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/usr/local/prometheus/data --storage.tsdb.retention=60d ; the program (relative uses PATH, can take args)
numprocs=1 ; number of processes copies to start (def 1)
directory=/usr/local/prometheus/ ; directory to cwd to before exec (def no cwd)
autostart=true ; start at supervisord start (default: true)
autorestart=true ; retstart at unexpected quit (default: true)
startsecs=30 ; number of secs prog must stay running (def. 1)
startretries=3 ; max # of serial start failures (default 3)
exitcodes=0,2 ; 'expected' exit codes for process (default 0,2)
stopsignal=QUIT ; signal used to kill process (default TERM)
stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
user=root ; setuid to this UNIX account to run the program
redirect_stderr=true ; redirect proc stderr to stdout (default false)
stdout_logfile=/usr/local/prometheus/prometheus.stdout.log ; stderr log path, NONE for none; default AUTO
stdout_logfile_maxbytes=64MB ; max # logfile bytes b4 rotation (default 50MB)
stdout_logfile_backups=4 ; # of stdout logfile backups (default 10)
stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
stdout_events_enabled=false ; emit events on stdout writes (default false)
stopasgroup=true
killasgroup=true
superviso.r方式启动
supervisorctl update
supervisorctl start prometheus-server
supervisorctl stop prometheus-server
五、打开prometheus自带的web界面
使用四中的命令启动Prometheus,然后访问prometheus默认端口,http://ip:9090/graph
好了,至此prometheus的server端已经完成安装。
- 点赞
- 收藏
- 关注作者
评论(0)