如何更改prometheus监控系统启动的默认端口号

小熊 Prometheus评论7,164字数 1665阅读5分33秒阅读模式

安装好 prometheus 以后,访问的默认端口号是 9090,通常不需要修改。但有时候情况特殊,需要把默认端口号改为自定义端口,例如改为 8091,该如何操作?

一般情况,prometheus 有两种安装运行方式:容器方式和虚机(服务器)方式,更改方法不同,简述如下:

一、容器方式

prometheus 在容器中安装运行,启动方式通常是:

docker run -d -p 9090:9090 \
            -v PWD/prometheus.yml:/etc/prometheus/prometheus.yml \
            -vPWD/alert.rules:/etc/prometheus/alert.rules \
            --name prometheus \
            prom/prometheus \
            -config.file=/etc/prometheus/prometheus.yml \
            -alertmanager.url=http://10.0.2.15:9093

因此,要自定义端口号,启动时直接更改 -p 后面的端口参数就可以了,简单直接。

二、虚机(服务器)方式

prometheus 在虚机(服务器)中安装运行。

命令行启动

在安装完成以后,可以直接在命令行启动。启动方式通常是:

./prometheus --config.file=prometheus.yml &

或者

nohup /opt/prometheus/prometheus &

如果要使用不同于9090的端口号,可以在命令行参数 --web.listen-address中指定,如:

./prometheus --config.file=prometheus.yml --web.listen-address=:8091 &

启动以后,访问http://xxx.xxx.xxx.xxx:8091,可以看到,端口确实更改了。

顺便说一下,要看prometheus的所有命令行参数,可以执行如下命令:

./prometheus -h

服务方式启动

安装完成以后,也可以把 prometheus 配置成自启动的服务,在其中的配置文件中也可以自定义 prometheus 的启动端口。步骤如下:

  1. /etc/systemd/system目录下创建新文件 prometheus.service,其中 ExecStart 字段指定启动参数时,设置自定义端口,内容如下:--web.listen-address=:8091
[Unit]
Description=Prometheus Monitoring System
Documentation=Prometheus Monitoring System

[Service]
ExecStart=/opt/proe/prometheus-2.3.1.linux-amd64/prometheus \
  --config.file=/opt/proe/prometheus-2.3.1.linux-amd64/prometheus.yml --web.enable-admin-api \
  --web.listen-address=:8091

[Install]
WantedBy=multi-user.target

2.执行命令:

systemctl start prometheus.service

如果 prometheus 在运行,有时候要执行如下命令:

systemctl daemon-reload

3.验证 prometheus 是否在新端口正常启动:

输入如下命令:

netstat -lntp | grep prometheus
[root@k8s-node-3 system]# netstat -lntp |grep prometheus
tcp6       0      0 :::8091                 :::*                    LISTEN      11758/prometheus

可见端口已经是自定义的端口了。

访问:http://localhost:8091

看看页面是否出来了,一切正常:

如何更改prometheus监控系统启动的默认端口号

转自:https://blog.csdn.net/palet/article/details/82988100

weinxin
公众号
扫码订阅最新深度技术文,回复【资源】获取技术大礼包
小熊