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

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

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

一、容器方式

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


docker run -d -p 9090:9090 \
            -v $PWD/prometheus.yml:/etc/prometheus/prometheus.yml \
            -v $PWD/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

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值