1、拉取Prometheus镜像
docker pull bitnami/prometheus:2.43.0 --platform linux/arm64
ps:后面这个是指定系统的(使用uname -m 查看)一般默认拉下来是amd64的,如果系统不是amd64的最好指定一下
2、创建挂载目录
mkdir /app/prometheus/conf
创建挂载配置文件,在/app/prometheus文件夹下
cd /app/prometheus
vim prometheus.yml
写入如下内容:
# 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: "prometheus"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ["127.0.0.1:9090"] #Prometheus地址
#这是新增的监控job,是配置nacos-consul-adapter适配器的服务,也就是我的网关
- job_name: 'consul-prometheus'
metrics_path: '/actuator/prometheus'
consul_sd_configs:
#这里是nacos-consul-adapter适配器的服务,也就是我的网关的ip和端口号(这里是nacos伪装为consul动态发现服务,具体下面解释,可以写多个-server: )
- server: 'xxx.xxx.xxx.xxx:xxxx'
services: []
relabel_configs:
- source_labels: [__address__]
regex: '[^/]+(/.*)' # capture '/...' part
target_label: __metrics_path__ # change metrics path
- source_labels: [__address__]
regex: '([^/]+)/.*' # capture host:port
target_label: __address__ # change target
- job_name: 'node_exporter'
scrape_interval: 10s #这个时间最好不要低于10s,否则容易报错:Get "xxx": context deadline exceeded
file_sd_configs:
- files:
- targets/node/nodes-instances.json
refresh_interval: 10s
relabel_configs:
- action: replace
source_labels: ['__address__']
regex: (.*):(.*)
replacement: $1
target_label: hostname
- action: labeldrop
regex: __meta_filepath
- job_name: 'redis_exporter'
scrape_interval: 10s
file_sd_configs:
- files:
- targets/redis/redis-instances.json
metrics_path: /scrape
relabel_configs:
- action: replace
source_labels: ['__address__']
regex: (.*):(.*)
replacement: $1
target_label: hostip
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: {ip}:9123 #此处的ip和端口对应下面启动的redis_exporter的IP和端口
完成Prometheus的准备工作。
3、启动Prometheus
docker run -d --name prometheus --restart=always -p 9090:9090 -p 9091:9091 -v /app/prometheus/conf:/opt/bitnami/prometheus/conf -v /app/prometheus/prometheus.yml:/opt/bitnami/prometheus/conf/prometheus.yml bitnami/prometheus:2.43.0
ps: /app/prometheus/conf 这个路径的指定挂载是为了读取配置文件,就像上面配置的node-exporter和redis-exporter的文件地址需要创建在这个目录下,否则Prometheus会读取不到文件
3.1、查看是否启动成功
docker ps
登录网页查看http://{IP}:9090/targets ,注意防火墙等开放问题
4、搭建node-export
4.1、拉取镜像
docker pull bitnami/node-exporter:1.5.0 --platform linux/arm64
4.2、创建配置
在/app/prometheus/conf目录下创建
mkdir -p targets/node/
cd /app/prometheus/conf/targets/node/
vi nodes-instances.json
[
{
"targets": [ "ip:port"]
}
]
PS:注意配置文件格式,targets里面可添加多个逗号隔开
4.3、启动node-exporter
docker run -d --name node-exporter --restart=always -p 9199:9100 -v /app/node-exporter/proc:/host/proc:ro -v /app/node-exporter/sys:/host/sys:ro -v /app/node-exporter/:/rootfs:ro bitnami/node-exporter:1.5.0
启动完成后,docker ps查看,访问http://{ip}:9199/metrics查看,注意防火墙开放,最后登录到Prometheus(http://{IP}:9199/metrics)查看
5、搭建redis_export
5.1、拉取镜像
docker pull oliver006/redis_exporter:v1.50.0 --platform linux/arm64
5.2、创建配置
在/app/prometheus/conf目录下创建
mkdir -p targets/redis/
cd /app/prometheus/conf/targets/redis/
vi redis-instances.json
[
{
"targets": [ "ip:port"],#此处是reids的ip和端口,不是redis_exporter的IP和端口多个逗号隔开
"labels": {"env":"test","cluster":"test_one"}
}
]
5.3、启动redis_exporter
docker run -d --name redis_exporter -p 9123:9121 oliver006/redis_exporter:v1.50.0 --redis.addr redis://{ip}:6379 --redis.password "******"
这里的9123对应Prometheus配置的ip和端口
最后去 Prometheus(http://{IP}:9199/metrics)查看
6、nacos伪装成功consul
6.1、在你的微服务网关(gateway)引入
如果拉不到包
6.2、需要监控的微服务中需要加入
org.springframework.boot spring-boot-starter-actuator io.micrometer micrometer-registry-prometheus runtime
6.3、需要监控的微服务配置文件中必须加入
management.endpoints.web.exposure.include=*
PS:这样配置之后,就微服务就不用去修改Prometheus的配置文件,可以自己发现,不过node-exporter如果有改变,还是需要去nodes-instances.json修改,不过不用重启Prometheus,还有redis集群也一样,如果有变动,需要修改一下redis-instances.json文件