暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

第一章:通过二进制的方式安装prometheus

运维家 2024-08-08
40

1、从官网下载最新版本

官网地址:https://prometheus.io/download/访问界面如下,我们需要注意的是,要选择带有“LTS”样式的版本,这是稳定版本,推荐。

2、上传解压

[root@loaclhost home]# ls
prometheus-2.45.5.linux-amd64.tar.gz 
[root@loaclhost home]# tar xf prometheus-2.45.5.linux-amd64.tar.gz 
[root@loaclhost home]# ls
prometheus-2.45.5.linux-amd64  prometheus-2.45.5.linux-amd64.tar.gz
[root@loaclhost home]# mv prometheus-2.45.5.linux-amd64 prometheus
[root@loaclhost home]# ls
prometheus  prometheus-2.45.5.linux-amd64.tar.gz
[root@loaclhost home]# cd prometheus
[root@loaclhost prometheus]# ll
total 229792
drwxr-xr-x 2 1001 127      4096 May  2 17:42 console_libraries
drwxr-xr-x 2 1001 127      4096 May  2 17:42 consoles
-rw-r--r-- 1 1001 127     11357 May  2 17:42 LICENSE
-rw-r--r-- 1 1001 127      3773 May  2 17:42 NOTICE
-rwxr-xr-x 1 1001 127 121066245 May  2 17:00 prometheus
-rw-r--r-- 1 1001 127       934 May  2 17:42 prometheus.yml
-rwxr-xr-x 1 1001 127 114206977 May  2 17:01 promtool
[root@loaclhost prometheus]

3、启动prometheus

[root@loaclhost prometheus]# nohup ./prometheus &
[1] 25427
[root@loaclhost prometheus]# nohup: ignoring input and appending output to ‘nohup.out’

[root@loaclhost prometheus]

这是最简单的启动方式,直接使用nohup命令就启动了,但是线上不推荐使用这种方式;

查看进程,是否启动成功了;

[root@loaclhost prometheus]# ps -ef | grep prometheus
root     25427 16342  0 14:41 pts/0    00:00:00 ./prometheus
root     25481 16342  0 14:42 pts/0    00:00:00 grep --color=auto prometheus
[root@loaclhost prometheus]# netstat -tunlp | grep 25427
tcp6       0      0 :::9090                 :::*                    LISTEN      25427/./prometheus  
[root@loaclhost prometheus]

可以看到进程存在,且端口号存在,那么我们在“防火墙”关闭的情况下,使用浏览器访问一下看看结果。 浏览器访问地址:http://10.1.5.56:9090/可以看到访问到了,但是有一个warning,提示我们prometheus服务所在的服务器时间存在偏差,那么我们解决一下子;

时间同步

[root@loaclhost prometheus]# yum -y install ntp ntpdate
[root@loaclhost prometheus]# ntpdate cn.pool.ntp.org
 3 Jun 14:32:11 ntpdate[25705]: step time server 84.16.73.33 offset -826.640694 sec
[root@loaclhost prometheus]

然后我们再次浏览器刷新一下访问界面,就会发现这个warning提示消失了。关闭程序:

[root@loaclhost home]# ps -ef | grep prometheus
root     18614 16771  0 11:07 pts/0    00:00:00 grep --color=auto prometheus
root     25427     1  0 Jun03 ?        00:01:36 ./prometheus
[root@loaclhost home]# kill -9 25427
[root@loaclhost home]# ps -ef | grep prometheus
root     18620 16771  0 11:07 pts/0    00:00:00 grep --color=auto prometheus
[root@loaclhost home]

4、使用systemctl命令来控制prometheus

4.1 文件编写

首先我们应该知道systemctl默认service的目录在哪儿,如下:

/usr/lib/systemd/system

所以我们应该创建一个文件,在这个目录下,如下:

[root@loaclhost home]# vim prometheus.service
[Unit]
Description=Prometheus service
After=network-online.target

[Service]
Type=simple
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --log.level=warn --storage.tsdb.path=/usr/local/prometheus/data/
Restart=on-failure
ExecStop=/usr/bin/kill -9 MAINPID

[Install]
WantedBy=multi-user.target
[root@loaclhost home]# mv prometheus.service /usr/lib/systemd/system/
[root@loaclhost home]# ls /usr/lib/systemd/system/prometheus.service 
/usr/lib/systemd/system/prometheus.service
[root@loaclhost home]# systemctl daemon-reload

可以看到上面我们指定的目录是/usr/loca/prometheus,所以我们首先移动一下目录,然后重新加载一下。

[root@loaclhost home]# pwd
/home
[root@loaclhost home]# mv prometheus /usr/local/
[root@loaclhost home]# cd /usr/local/prometheus/
[root@loaclhost prometheus]# ls
console_libraries  consoles  data  LICENSE  nohup.out  NOTICE  prometheus  prometheus.yml  promtool
[root@loaclhost prometheus]# systemctl daemon-reload

而后我们尝试用systemctl命令来控制prometheus,验证一下。

4.2 systemctl控制

查看状态:

[root@loaclhost home]# systemctl status prometheus
● prometheus.service - Prometheus service
   Loaded: loaded (/usr/lib/systemd/system/prometheus.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
[root@loaclhost home]

启动:

[root@loaclhost home]# systemctl daemon-reload
[root@loaclhost home]# systemctl start prometheus
[root@loaclhost home]
[root@loaclhost home]# systemctl status prometheus
● prometheus.service - Prometheus service
   Loaded: loaded (/usr/lib/systemd/system/prometheus.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2024-06-04 11:17:11 CST; 3s ago
 Main PID: 19169 (prometheus)
    Tasks: 9
   Memory: 29.5M
   CGroup: /system.slice/prometheus.service
           └─19169 /usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --log.level=warn --storage.tsdb.path=/usr/local/prometheus/data/

Jun 04 11:17:11 loaclhost systemd[1]: Started Prometheus service.
Jun 04 11:17:11 loaclhost prometheus[19169]: ts=2024-06-04T03:17:11.780Z caller=dir_locker.go:77 level=warn component=tsdb msg="A lockfile from a previous exec.../data/lock
Hint: Some lines were ellipsized, use -l to show in full.
[root@loaclhost home]# 

页面访问验证:停止:

[root@loaclhost home]# systemctl stop prometheus
[root@loaclhost home]# systemctl status prometheus
● prometheus.service - Prometheus service
   Loaded: loaded (/usr/lib/systemd/system/prometheus.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Tue 2024-06-04 11:18:35 CST; 6s ago
  Process: 19244 ExecStop=/usr/bin/kill -9 MAINPID (code=exited, status=1/FAILURE)
  Process: 19169 ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --log.level=warn --storage.tsdb.path=/usr/local/prometheus/data/ (code=exited, status=0/SUCCESS)
 Main PID: 19169 (code=exited, status=0/SUCCESS)

Jun 04 11:17:11 loaclhost systemd[1]: Started Prometheus service.
Jun 04 11:17:11 loaclhost prometheus[19169]: ts=2024-06-04T03:17:11.780Z caller=dir_locker.go:77 level=warn component=tsdb msg="A lockfile from a previous exec.../data/lock
Jun 04 11:18:35 loaclhost systemd[1]: Stopping Prometheus service...
Jun 04 11:18:35 loaclhost kill[19244]: kill: cannot find process "
MAINPID"
Jun 04 11:18:35 loaclhost systemd[1]: prometheus.service: control process exited, code=exited status=1
Jun 04 11:18:35 loaclhost prometheus[19169]: ts=2024-06-04T03:18:35.137Z caller=main.go:854 level=warn msg="
Received SIGTERM, exiting gracefully..."
Jun 04 11:18:35 loaclhost systemd[1]: Stopped Prometheus service.
Jun 04 11:18:35 loaclhost systemd[1]: Unit prometheus.service entered failed state.
Jun 04 11:18:35 loaclhost systemd[1]: prometheus.service failed.
Hint: Some lines were ellipsized, use -l to show in full.
[root@loaclhost home]# 

领取红包,能省则省

“🎉🎁 独家福利来啦!美团、饿了么、滴滴打车、菜鸟裹裹、电影票红包大放送!每天都有哦!🎁🎉

👀 快来瞅瞅,动动手指就能省下一笔!别错过,赶紧按照下方操作领取你的专属红包吧!👇

💬 如果你还有其他想要的红包类型,记得留言告诉我们哦,我们会尽力满足大家的需求!🎁💖”


文章转载自运维家,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论