prometheus部署与使用
1.二进制方式部署
1.1.安装prometheus
1.下载软件包
[root@prometheus-server ~]# wget https://github.com/prometheus/prometheus/releases/download/v2.23.0/prometheus-2.23.0.linux-amd64.tar.gz
2.安装
[root@prometheus-server ~]# mkdir /data/ -p
[root@prometheus-server ~]# tar xf prometheus-2.23.0.linux-amd64.tar.gz -C /data/
[root@prometheus-server ~]# mv /data/prometheus-2.23.0.linux-amd64 /data/prometheus
3.将可执行程序移动到/usr/bin
[root@prometheus-server ~]# lscd /data/prometheus
[root@prometheus-server prometheus]# cp prom* /usr/bin/
[root@prometheus-server prometheus]# prometheus --version
prometheus, version 2.23.0 (branch: HEAD, revision: 26d89b4b0776fe4cd5a3656dfa520f119a375273)
build user: root@37609b3a0a21
build date: 20201126-10:56:17
go version: go1.15.5
platform: linux/amd64
1.2.启动prometheus
常用启动参数
–config.file="/data/prometheus/prometheus.yml" //指定配置文件路径–web.enable-lifecycle //开启web热加载配置
–storage.tsdb.path= //指定tsdb数据库路径,默认在data/
–storage.tsdb.retention.time= //指定tsdb保留数据的时长,默认15d
启动prometheus
[root@prometheus-server ~]# prometheus --config.file="/data/prometheus/prometheus.yml" --web.enable-lifecycle &
1.3.使用systemctl启动prometheus
编写systemctl文件,写启动命令的时候指定tsdb的路径
1.编写文件
[root@prometheus-server system]# cd /usr/lib/systemd/system
[root@prometheus-server system]# vim prometheus.service
[Unit]
Description=https://prometheus.io
[Service]
Restart=on-failure
ExecStart=/data/prometheus/prometheus --config.file=/data/prometheus/prometheus.yml --web.enable-lifecycle --storage.tsdb.path=/data/prometheus/data
[Install]
WantedBy=multi-user.target
2.加载system配置以及启动服务
[root@prometheus-server system]# systemctl daemon-reload
[root@prometheus-server system]# systemctl start prometheus.service
[root@prometheus-server system]# systemctl enable prometheus.service
3.查看进程
[root@prometheus-server system]# ps aux | grep prome
1.4.访问prometheus
访问:http://loclahost:9090
查看监控主机页面
status—>targets
1.5.将监控主机页面的prometheus地址修改为真实ip地址
1.修改配置,找到targets一行,将localhost修改为真实地址即可
[root@prometheus-server ~]# vim /data/prometheus/prometheus.yml
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: ['192.168.81.210:9090']
2.加载配置
由于启动增加了--web.enable-lifecycle,因此可以使用配置加载功能
[root@prometheus-server ~]# curl -XPOST 192.168.81.210:9090/-/reload
点击endpoint下面的链接即可看到metris度量值
2.docker部署prometheus
2.1.安装docker
1.获取镜像源
[root@prometheus-server ~]# wget -O /etc/yum.repos.d/docker-ce.repo https://download.docker.com/linux/centos/docker-ce.repo
[root@prometheus-server ~]# sed -i 's#download.docker.com#mirrors.tuna.tsinghua.edu.cn/docker-ce#' /etc/yum.repos.d/docker-ce.repo
[root@prometheus-server ~]# yum makecache fast
2.安装docker
[root@prometheus-server ~]# yum -y install docker-ce
3.配置镜像加速器
[root@prometheus-server ~]# tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://zggyaen3.mirror.aliyuncs.com"]
}
EOF
4.启动docker
[root@prometheus-server ~]# systemctl restart docker
2.2.运行prometheus容器
我们将刚刚安装好的prometheus配置文件挂载到容器中,并映射成7090端口
[root@prometheus-server ~]# docker run -d -p7090:9090 -v /data/prom/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus:latest
517523d6da1d4135bbd5d6bb2714576f20b7da832b6d75ce7dc88f1844c1d5f4
[root@prometheus-server ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
517523d6da1d prom/prometheus:latest "/bin/prometheus --c…" 16 seconds ago Up 10 seconds 0.0.0.0:7090->9090/tcp hopeful_tharp