CentOS7下安装Prometheus安装

https://prometheus.io/docs/prometheus/latest/getting_started/

转载博客 侵权立删
http://www.eryajf.net/2468.html

Prometheus软件包下载地址。go语言编写。基本无需下载环境依赖软件。
https://prometheus.io/download/
当前最新版本prometheus-2.24.0

运行prometheus

一、解压 运行prometheus

1.解压

tar zxvf prometheus-2.24.0.linux-amd64.tar.gz 

cd prometheus-2.24.0.linux-amd64

2.设置配置文件 prometheus.yml

vim prometheus.yml 
global:
  scrape_interval:     15s # By default, scrape targets every 15 seconds.

  # Attach these labels to any time series or alerts when communicating with
  # external systems (federation, remote storage, Alertmanager).
  external_labels:
    monitor: 'codelab-monitor'

# 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>` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s

    static_configs:
      - targets: ['localhost:9090']		# 监控本机的prometheus 没有实际意义但是可以测试。

3.运行prometheus
默认情况下数据会存储到 当前目录下的 /data 目录(./data),可以通过 --store.tsdb.path参数指定

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

其它可指定参数。
https://blog.csdn.net/qq_38814358/article/details/102777567

二、运行node-exporter

prometheus,可以通过不同exporter获取各种信息,这里实验选择常用的node-exporter。
[https://prometheus.io/download/]](https://prometheus.io/download/)

安装node-expoeter 当前最新版本node_exporter-1.0.1.linux-amd64

tar -zxvf node_exporter-1.0.1.linux-amd64
cd node_exporter-1.0.1.linux-amd64

运行软件node_exporter 。

./node_exporter 

相当于

./node_exporter --web.listen-address 0.0.0.0:9100
三、prometheus设置配置文件加入node-exporter作为targets之一

修改prometheus.yml 配置文件

vim prometheus.yml 
global:
  scrape_interval:     15s # By default, scrape targets every 15 seconds.

  # Attach these labels to any time series or alerts when communicating with
  # external systems (federation, remote storage, Alertmanager).
  external_labels:
    monitor: 'codelab-monitor'

# 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>` to any timeseries scraped from this config.
  # 可以有多个job_name
  - job_name: 'prometheus'

    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s

    static_configs:
      - targets: ['localhost:9090']		# 监控本机的prometheus 没有实际意义但是可以测试。
	  
	  
  - job_name:       'node'	
    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s

    static_configs:
      - targets: ['localhost:9100']	# 其它主机上的exporter只用将localhost改为主机IP,不同的主机之间 , 隔开
        labels:		# 为当前targets设置自定义的label,一般不要设置instance,会重复。
          testlabel: 'test1'	# 随便设置一个label 可以不设置labels。

运行prometheus

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

默认会带上当前目录的 prometheus.yml。如果prometheus和prometheus.yml配置文件在一个目录 可以省略–config.file

./prometheus
四、后端运行命令
nohup ./prometheus --config.file=prometheus.yml >> ./prometheus.log 2>&1 &
nohup ./node_exporter >> ./node_exporter.log 2>&1 &

查看正在运行的的后台任务

jobs -l

以service服务形式启动停止 prometheus

一、创建prometheus 用户 组
groupadd prometheus
useradd -g prometheus -m -d /var/lib/prometheus -s /sbin/nologin prometheus
二、解压安装包到 /usr/local/ 并将解压后的包重命名为prometheus
tar zxvf prometheus-2.24.0.linux-amd64.tar.gz -C /usr/local/
cd /usr/local/
mv prometheus-2.24.0.linux-amd64/ prometheus

将/usr/local/prometheus 目录所有者设置为 prometheus

chown -R prometheus:prometheus /usr/local/prometheus
三、创建prometheus启动脚本
vim /usr/lib/systemd/system/prometheus.service
[Unit]
Description=prometheus
After=network.target

[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus --storage.tsdb.retention=15d --log.level=info
Restart=on-failure

[Install]
WantedBy=multi-user.target

启动

systemctl enable prometheus.service
systemctl start prometheus.service
systemctl status prometheus.service
四、以service服务形式启动停止 node_exporte
tar zxvf node_exporter-1.0.1.linux-amd64.tar.gz -C /usr/local/
cd /usr/local/
mv node_exporter-1.0.1.linux-amd64/ node_exporter

chown -R prometheus:prometheus node_exporter/

创建prometheus启动脚本

vim /usr/lib/systemd/system/node_exporter.service 

添加如下内容:

[Unit]
Description=node_export
Documentation=https://github.com/prometheus/node_exporter
After=network.target
 
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure

[Install]
WantedBy=multi-user.target

启动node-exporter

systemctl enable node_exporter
systemctl start node_exporter
systemctl status node_exporter

需要增加 targets时,只需修改/usr/local/prometheus/prometheus.yml配置文件,然后重启prometheus即可

systemctl restart prometheus.service

使用docker运行 prometheus。

关于docker v的挂载。 转
https://blog.csdn.net/hnmpf/article/details/80924494

docker 安装

yum -y install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum -y install docker-ce docker-ce-cli containerd.io

docker 运行

systemctl enable docker
systemctl start docker

docker 拉取镜像

docker pull prom/prometheus
docker pull prom/node-exporter
一。node_exporter安装

node_exporter设计用于监控主机系统。不建议将其部署为Docker容器,因为它需要访问主机系统。请注意,您要监视的任何非根安装点都需要绑定到容器中。如果启动容器以进行主机监视,请指定path.rootfs参数。此参数必须与host root的bind-mount中的路径匹配。node_exporter将path.rootfs用作访问主机文件系统的前缀。

docker run -d --name node_exporter \
  -p 9100:9100 \
  -v "/proc:/host/proc:ro" \
  -v "/sys:/host/sys:ro" \
  -v "/:/rootfs:ro" \
  --net="host" \
  prom/node-exporter

由于是不建议使用docker容器运行 node_exporter,也就不多做研究了。能用就行

http://IP:9100

访问有结果即可说明docker node_exporter运行成功

二。prometheus安装

一般运行时会 在主机上设置配置文件,然后映射到容器内。

mkdir /etc/prometheus
vim /etc/prometheus/prometheus.yml
global:
  scrape_interval:     15s # By default, scrape targets every 15 seconds.

  # Attach these labels to any time series or alerts when communicating with
  # external systems (federation, remote storage, Alertmanager).
  external_labels:
    monitor: 'codelab-monitor'

# 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>` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s

    static_configs:
      - targets: ['localhost:9090']

  - job_name:       'node'
    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s

    static_configs:
      - targets: ['192.168.199.80:9100']        # docker运行的prometheus和node_exporter。即使运行在同一host,主机名不能是localhost
      

映射到容器有两种方法,一种是配置文件直接映射,另一种是映射配置文件所在的目录。容器内部默认配置文件路径是 /etc/prometheus/prometheus.yml

方式一 映射配置文件

docker run --name prometheus -d \
    -p 9090:9090 \
    -v /etc/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
    prom/prometheus

方式二 映射配置文件目录

docker run --name prometheus -d \
    -p 9090:9090 \
    -v /etc/prometheus:/etc/prometheus \
    prom/prometheus
三。设置卷保存prometheus容器数据
The Prometheus image uses a volume to store the actual metrics. 
For production deployments it is highly recommended to use a named volume to ease managing the data on Prometheus upgrades.
Prometheus镜像使用一个卷来存储实际的。对于生产部署,
强烈建议使用命名卷方便管理prometheus数据。

使用docker inspect dockerID 命令可以查看容器信息,其中mounts段

        "Mounts": [
            {
                "Type": "bind",
                "Source": "/etc/prometheus",
                "Destination": "/etc/prometheus",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            },
            {
                "Type": "volume",
                "Name": "33589e4b694ef35192ec4b0bb610ef0fbf9b0b6bfeee06dbbe79d04a1aee0574",
                "Source": "/var/lib/docker/volumes/33589e4b694ef35192ec4b0bb610ef0fbf9b0b6bfeee06dbbe79d04a1aee0574/_data",
                "Destination": "/prometheus",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],

使用docker prometheus 容器内数据存储路径是/prometheus 实际对应主机的一个卷(这个卷未指定的话会自动创建)。

docker run --name prometheus -d \
    -p 9090:9090 \
    -v /etc/prometheus:/etc/prometheus \
    prom/prometheus

相当于

docker run --name prometheus -d \
    -p 9090:9090 \
    -v /etc/prometheus:/etc/prometheus \
    -v /prometheus \
    prom/prometheus

-v /prometheus只有宿主机内路径,于是自动在/var/lib/docker/volumes 目录中创建一个卷对应。

ls -l /var/lib/docker/volumes/
1.可以手动设置卷名,然后对应/prometheus

创建docker卷,卷名为 prometheus-storage。卷目录在
/var/lib/docker/volumes/

docker volume create prometheus-storage
docker run --name prometheus -d \
	-p 9090:9090 \
	-v /etc/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
	-v prometheus-storage:/prometheus \
	prom/prometheus

Mounts 变为

        "Mounts": [
            {
                "Type": "bind",
                "Source": "/etc/prometheus/prometheus.yml",
                "Destination": "/etc/prometheus/prometheus.yml",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            },
            {
                "Type": "volume",
                "Name": "prometheus-storage",
                "Source": "/var/lib/docker/volumes/prometheus-storage/_data",
                "Destination": "/prometheus",
                "Driver": "local",
                "Mode": "z",
                "RW": true,
                "Propagation": ""
            }
        ],
2.可以直接将一个目录映射到容器的/prometheus
mkdir -p /data/prometheus2	

必须保证容器对该目录有 读写权限。
可以运行容器时指定 --user 对该目录有读写权限的用户名或ID

docker run --name prometheus2 -d \
	--user root \
	-p 9092:9090 \
	-v /etc/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
	-v /data/prometheus2:/prometheus \
	prom/prometheus

或者直接将该 目录权限设置为 777

mkdir -p /data/prometheus3
chmod 777 /data/prometheus3
docker run --name prometheus3 -d \
	--user root \
	-p 9093:9090 \
	-v /etc/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
	-v /data/prometheus3:/prometheus \
	prom/prometheus
建议使用方式1手动设置卷名。
四。grafana安装。

prometheus自带的graph并不是特别适合当作图形显示。可以配合grafana使用。

grafana 同样是如果没有指定用于信息存储的位置,那么一旦停止容器,所有Grafana数据就会消失。要保存数据,需要为容器设置持久存储或绑定挂载。

依然是两种方式。
1.运行具有持久性存储的Grafana容器(建议)

docker volume create grafana-storage

docker run -d -p 3000:3000 --name=grafana -v grafana-storage:/var/lib/grafana grafana/grafana

也绑定配置文件

docker run -d -p 3000:3000 --name=grafana \
	-v grafana-storage:/var/lib/grafana \
	-v /etc/grafana/grafana.ini:/etc/grafana/grafana.ini \
	grafana/grafana

2.使用绑定挂载运行Grafana容器.
创建目录,然后设置目录用户权限。或在创建容器时指定–user

略。基本上同prometheus

influxdb设置prometheus数据源,导入模板。
http://192.168.199.80:3000

influxdb使用prometheus数据源
可以导入 自带的prometheus dashboard模板或者从grafana官网导入dashboard模板。
prometheus模板

导入官网的node_exporter的dashboard查看效果
https://grafana.com/dashboards/405

node_exporter的dashboard模板

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值