目 录CONTENT

文章目录

Prometheus容器化部署

ZiChen D
2021-12-29 / 0 评论 / 0 点赞 / 530 阅读 / 8,888 字 / 正在检测是否收录...

Promethues介绍

Prometheus(普罗米修斯)是一个最初在SoundCloud上构建的监控系统,自2012年成为社区开源项目,就拥有非常活跃的开发人员和用户社区,为强调开源及独立维护,Prometheus于2016年加入云原生云计算基金会(CNCF),成为继Kubernetes之后的第二个托管项目

官方网站

代码仓库

Prometheus组件与架构

  • Prometheus Server:手机指标和存储时间序列数据,并提供查询接口
  • ClientLibrary:客户端库
  • Push Gateway:短期存储指标数据,主要用于临时性的任务
  • Exporters:采集已有的第三方服务监控指标并暴露metrics
  • Alertmanager:告警
  • Web UI:简单的Web控制台

Prometheus基本使用

怎么监控?

如果想监控,前提是获取被监控端指标数据,并且这个数据格式必须遵循Prometheus数据模型,这样才能识别和采集,一般使用exporter提供监控指标数据。

Export列表:
https://prometheus.io/docs/instrumenting/exporters/

监控Linux服务器

node_exporter: 用于监控Linux系统的指标收集器
常用指标:

  • CPU
  • 内存
  • 硬盘
  • 网络流量
  • 文件描述符
  • 系统负载
  • 系统服务

数据接口:http://IP:9100

查询数据

PromQL(Prometheus Query Language)是Prometheus自己开发的数据查询DSL语言,语言表现力非常丰富,支持条件查询,操作符,并且内建了大量内置函数,供我们针对监控数据的各种维度进行查询。

数据模型:

  • Prometheus将所有数据存储为时间序列
  • 具有相同度量名称以及标签属于同一个指标
  • 每个时间序列都由度量标准名称和一组键值对(称为标签)唯一标识,通过标签查询指定指标

指标格式:

<metric name>{<label name>=<label value>...}

部署

下载Prometheus包

[root@prometheus ~]# wget https://github.com/prometheus/prometheus/releases/download/v2.32.1/prometheus-2.32.1.linux-amd64.tar.gz

下载Node-exporters包,后续要用

[root@node1 ~]# wget https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz

[root@node2 ~]# wget https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz

解压包并启动

[root@prometheus ~]# tar xf prometheus-2.32.1.linux-amd64.tar.gz 
[root@prometheus ~]# mv prometheus-2.32.1.linux-amd64 prometheus
[root@prometheus ~]# cd /usr/local/prometheus/
[root@prometheus prometheus]# ls
console_libraries  consoles  LICENSE  NOTICE  prometheus  prometheus.yml  promtool
[root@prometheus prometheus]# ./prometheus 
[root@prometheus prometheus]# ss -anlt
State    Recv-Q    Send-Q       Local Address:Port       Peer Address:Port   Process   
LISTEN   0         128                0.0.0.0:80              0.0.0.0:*                
LISTEN   0         128                0.0.0.0:22              0.0.0.0:*                
LISTEN   0         128              127.0.0.1:1514            0.0.0.0:*                
LISTEN   0         128                   [::]:80                 [::]:*                
LISTEN   0         128                   [::]:22                 [::]:*                
LISTEN   0         128                      *:9090                  *:*                

访问测试:

能访问到,部署完成

容器化部署

[root@prometheus prometheus]# mv prometheus.yml /opt/
[root@prometheus local]# rm -rf prometheus/

安装docker

//配置阿里源
[root@prometheus ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo

[root@prometheus ~]# yum -y install docker-ce

//配置加速
[root@prometheus ~]# cat > /etc/docker/daemon.json <<EOF
> {
>   "registry-mirrors": ["https://1aq1u7oh.mirror.aliyuncs.com"]
> }
> EOF

创建运行prometheus的容器

[root@prometheus ~]# docker run -d --name prometheus -p 9090:9090 prom/prometheus

访问:

目前是用的容器来访问,能够访问到

将prometheus.yml文件映射到容器里

[root@prometheus ~]# docker run -d --name prometheus -p 9090:9090 -v /opt/prometheus.yml:/etc/prometheus/prometheus.yml:ro --restart always prom/prometheus
c5d7cf90d2de1a5a2032cf54dcdf162b3735e43e1d2ac933033f937197600a16

容器化部署grafana

[root@prometheus ~]# docker run -d --name grafana -p 3000:3000 --restart always grafana/grafana

使用IP+3000端口号去访问

使用默认账号admin,以及默认密码admin登录

配置Node_exporter

[root@node1 ~]# tar xf node_exporter-1.3.1.linux-amd64.tar.gz 
[root@node1 ~]# mv node_exporter-1.3.1.linux-amd64 node_exporter
[root@node1 ~]# mv node_exporter /usr/local/

[root@node1 node_exporter]# cat /usr/lib/systemd/system/node_exporter.service 
[unit]
Description=The node_exporter Server
After=network.target

[Service]
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure
RestartSec=15s
SyslogIdentifier=node_exporter

[Install]
WantedBy=multi-user.target

[root@node1 node_exporter]# systemctl enable --now node_exporter.service 
Created symlink /etc/systemd/system/multi-user.target.wants/node_exporter.service → /usr/lib/systemd/system/node_exporter.service.
[root@node1 node_exporter]# ss -anlt
State    Recv-Q   Send-Q       Local Address:Port        Peer Address:Port   Process   
LISTEN   0        128              127.0.0.1:10248            0.0.0.0:*                
LISTEN   0        128              127.0.0.1:10249            0.0.0.0:*                
LISTEN   0        128                0.0.0.0:30000            0.0.0.0:*                
LISTEN   0        128                0.0.0.0:22               0.0.0.0:*                
LISTEN   0        128                0.0.0.0:31000            0.0.0.0:*                
LISTEN   0        128              127.0.0.1:36605            0.0.0.0:*                
LISTEN   0        128                      *:10250                  *:*                
LISTEN   0        128                      *:9100                   *:*                
LISTEN   0        128                      *:10256                  *:*                
LISTEN   0        128                   [::]:22                  [::]:*                

访问测试:

修改Prometheus配置文件

[root@prometheus ~]# vim /opt/prometheus.yml
  - job_name: "Linux Servers"             //此段添加
    static_configs:
      - targets: ["192.168.160.124:9100"]    //这里使用安装了node_exporter的主机IP加端口号

[root@prometheus ~]# docker restart prometheus 
prometheus

再去访问:

添加node2

[root@node1 local]# scp -r node_exporter/ 192.168.160.125:/usr/local

[root@node1 local]# scp /usr/lib/systemd/system/node_exporter.service 192.168.160.125:/usr/lib/systemd/system/node_exporter.service

[root@node2 ~]# systemctl enable --now node_exporter.service 
Created symlink /etc/systemd/system/multi-user.target.wants/node_exporter.service → /usr/lib/systemd/system/node_exporter.service.
[root@node2 ~]# ss -anlt
State    Recv-Q   Send-Q       Local Address:Port        Peer Address:Port   Process   
LISTEN   0        128              127.0.0.1:10248            0.0.0.0:*                
LISTEN   0        128              127.0.0.1:10249            0.0.0.0:*                
LISTEN   0        128                0.0.0.0:30000            0.0.0.0:*                
LISTEN   0        128                0.0.0.0:22               0.0.0.0:*                
LISTEN   0        128                0.0.0.0:31000            0.0.0.0:*                
LISTEN   0        128              127.0.0.1:41829            0.0.0.0:*                
LISTEN   0        128                      *:10250                  *:*                
LISTEN   0        128                      *:9100                   *:*                
LISTEN   0        128                      *:10256                  *:*                
LISTEN   0        128                   [::]:22                  [::]:*                

//修改配置文件
[root@prometheus ~]# vim /opt/prometheus.yml 
  - job_name: "Linux Servers"
    static_configs:
      - targets: 
        - "192.168.160.124:9100"
        - “192.168.160.125:9100”

[root@prometheus ~]# docker restart prometheus 
prometheus

查看

保存dashboard

方便以后查看

0

评论区