kubernetes
  • Introduction
  • 安装
    • 组件端口
    • 二进制安装
    • Kubeadm
      • 安装单Master集群
      • 安装高可用集群(手动分发证书)
      • 安装高可用集群(自动上传证书)
      • 安装ETCD集群
      • 安装高可用集群(外部ETCD)
    • 启动参数解析
      • ETCD相关参数
  • 负载均衡
    • Service
    • Ingress
    • 安装MetalLB
    • Nginx-ingress-controller
      • 转发TCP与UDP服务
      • 启动参数
      • 自定义Nginx模板
  • 存储
    • Volume
    • PV与PVC
    • StorageClass
    • Local-PV
      • Static-Provisioner
    • 实践
      • Ceph-RBD
      • NFS
  • 有状态服务
    • Mysql实践
    • Operator
      • Etcd
      • Zookeeper
      • Mysql
  • 认证与授权
    • 认证
      • 实践
    • 授权
  • Helm
    • 安装
    • Chart
      • 依赖
    • Helm命令
    • Repository
  • 日志
  • 监控
    • Prometheus体系
      • Prometheus
        • 内置函数
        • 配置
          • 规则文件
        • PromQL
      • Exporter
        • Metrics
      • Grafana
        • 配置
      • AlertManager
        • 配置
    • 容器监控
      • Cadvisor的指标
      • k8s中部署Prom与Cadvisor
  • Istio
  • 资源预留
    • imagefs与nodefs
    • 总结
  • 集群联邦
    • 联邦DNS原理
    • 联邦DNS安装
    • 安装federation-v1
  • Other
    • ImagePullSecret
    • QOS
    • Apiserver的代理
    • 资源配额
Powered by GitBook
On this page
  • Prometheus + NodeExporter + Grafana + AlertManager
  • 安装Prometheus
  • 安装NodeExporter
  • 配置Prometheus抓取NodeExporter的数据
  • 安装Grafana
  • 安装AlertManager
  • 为Prometheus配置告警规则
  • Reference

Was this helpful?

  1. 监控

Prometheus体系

Prometheus + NodeExporter + Grafana + AlertManager

本文介绍 Prometheus + NodeExporter + Grafana + AlertManager 的安装,监控宿主机CPU与内存等情况

安装Prometheus

官网给出了很多种安装方法(https://prometheus.io/docs/prometheus/latest/installation/),最常用的就是二进制与docker镜像,这里我们使用已经编译好的二进制进行安装。

从官网(https://prometheus.io/download/) 下载最新版本(2.15.2)的二进制包,解压,然后查看prometheus版本信息

$ wget https://github.com/prometheus/prometheus/releases/download/v2.15.2/prometheus-2.15.2.linux-amd64.tar.gz
$ tar xzvf prometheus-2.15.2.linux-amd64.tar.gz
$ cd prometheus-2.15.2.linux-amd64
$ ./prometheus --version

当使用二进制进行安装时,最好用systemd来管理。我们把整个prometheus-2.15.2.linux-amd64文件夹移动到/usr/local/目录下,并重命名为prometheus

$ mv ./prometheus-2.15.2.linux-amd64 /usr/local/prometheus

然后创建文件/usr/lib/systemd/system/prometheus.service,内容如下

[Unit]
Description=prometheus
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus
Restart=on-failure

[Install]
WantedBy=multi-user.target

关于启动参数可以使用命令/usr/local/prometheus/prometheus --help查看。这里我们把数据的存储目录指定到/var/lib/prometheus

然后启动prometheus,并查看prometheus是否启动成功

$ systemctl daemon-reload && systemctl enable prometheus
$ systemctl start prometheus && systemctl status prometheus

如果启动失败,则可以通过journalctl -xeu prometheus --no-pager查看错误日志(因为systemd管理的service的stdout日志都由journald接管)。如果成功,此时我们可以通过http://ip:9090访问prometheus自带的UI

安装NodeExporter

从官网(https://prometheus.io/download/#node_exporter)下载最新版本(0.18.1)的二进制的NodeExporter,解压

$ wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz
$ tar xzvf node_exporter-0.18.1.linux-amd64.tar.gz

同样,我们使用systemd来管理这个服务。我们把解压后的整个node_exporter-0.18.1.linux-amd64文件夹移动到/usr/local/目录下,并重命名为node_exportor

$ mv ./node_exporter-0.18.1.linux-amd64 /usr/local/node_exportor

然后创建文件/usr/lib/systemd/system/node_exporter.service,内容如下

[Unit]
Description=node_exporter
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure

[Install]
WantedBy=multi-user.target

关于启动参数可以使用命令/usr/local/node_exporter/node_exporter --help查看。

然后启动node_exporter,并查看是否启动成功

$ systemctl daemon-reload && systemctl enable node_exporter
$ systemctl start node_exporter && systemctl status node_exporter

配置Prometheus抓取NodeExporter的数据

当node_exporter启动成功后,我们便可以配置prometheus,去抓取node_exporter的数据。node_exporter默认的端口是9100。

一开始的时候,prometheus只有一个抓取对象,就是抓取自已的数据。即一开始时,/usr/local/prometheus/prometheus.yml的scrape_configs:的内容如下:

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['localhost:9090']

修改配置文件/usr/local/prometheus/prometheus.yml,加入新的job,抓取node_exporter的数据,添加完后scrape_configs:的完整内容如下:

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['localhost:9090']

  - job_name: 'node_exporter'
    static_configs:
    - targets: ['localhost:9100']
      labels:       
        instance: peng01

然后重启prometheus

$ systemctl restart prometheus

然后,我们去到prometheus的Status -> Targets,就可以看到上面的两个target,均为UP状态

接下来,我们通过Prometheus来查看一下主机的内存使用率,输入以下查询语句然后就可以看到内存使用率的曲线图(注意:其实在截这个图时,我们的node-exporter与prometheus已经运行了很长一段时间,如果你的运行时间不长,看到的曲线会比较少)

(node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes) / node_memory_MemTotal_bytes * 100

安装Grafana

我们使用rpm包来安装,见官方教程(https://grafana.com/docs/installation/rpm/)。首先下载rpm包,然后安装

$ wget https://dl.grafana.com/oss/release/grafana-5.4.2-1.x86_64.rpm
$ yum -y localinstall ./grafana-5.4.2-1.x86_64.rpm

安装后grafana的相关信息如下:

  • 环境变量文件:/etc/sysconfig/grafana-server

  • 配置文件:/etc/grafana/grafana.ini

  • 数据库文件:/var/lib/grafana/grafana.db

  • 日志目录:/var/log/grafana

启动Grafana

$ systemctl daemon-reload
$ systemctl start grafana-server && systemctl enable grafana-server

默认情况下grafana会监听localhost:3000,用户名与密码为admin/admin。

接下来,我们为grafana配置数据源。详见教程https://grafana.com/docs/features/datasources/prometheus/。

安装AlertManager

$ wget https://github.com/prometheus/alertmanager/releases/download/v0.20.0/alertmanager-0.20.0.linux-amd64.tar.gz

同样,我们使用systemd来管理这个服务。我们把解压后的整个alertmanager-0.20.0.linux-amd64文件夹移动到/usr/local/目录下,并重命名为alertmanager

$ mv ./alertmanager-0.20.0.linux-amd64 /usr/local/alertmanager

然后创建文件/usr/lib/systemd/system/alertmanager.service,内容如下

[Unit]
Description=alertmanager
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/alertmanager/alertmanager --config.file /usr/local/alertmanager/alertmanager.yml
Restart=on-failure

[Install]
WantedBy=multi-user.target

编辑/usr/local/alertmanager/alertmanager.yml,内容如下(参考【5】【6】:

global:
  smtp_smarthost: 'smtp.163.com:25'    # 使用163邮箱服务器发邮件
  smtp_from: 'pshizh@163.com'    # 发件人,填写你的163邮箱
  smtp_auth_username: 'pshizh@163.com'  # 你的163邮箱,与上面保持一致
  smtp_auth_password: 'xxxx'  # 你的163邮箱的密码

route:
  group_by: ['example']
  group_wait: 10s
  group_interval: 10s
  repeat_interval: 1h
  receiver: 'email'

receivers:
- name: 'email'
  email_configs:
  - to: '527103524@qq.com'    # 收件人

启动alertmanager

$ systemctl daemon-reload 
$ systemctl enable alertmanager
$ systemctl start alertmanager
$ systemctl status alertmanager

为Prometheus配置告警规则

编辑/usr/local/prometheus/prometheus.yml文件,在rule_files区域添加如下内容:

rule_files:
  - "/usr/local/prometheus/rule_files/memory_alert.yml"

然后创建文件/usr/local/prometheus/rule_files/memory_alert.yml,内容如下

groups:
- name: example        # 尚不清楚是否需要与alertmanager.yml中的route.groupby的名字保持一致
  rules:
  - alert: NodeMemoryUsage
    expr: (node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes) / node_memory_MemTotal_bytes * 100 > 20
    for: 1m
    annotations:
      summary: "{{$labels.instance}}: High Memory usage detected"
      description: "{{$labels.instance}}: Memory usage is above 20% (current value is:{{ $value }})"

不久后,便可以接收到告警邮件,如下

Reference

Previous监控NextPrometheus

Last updated 5 years ago

Was this helpful?

github上给了AlertManager的几种安装方法:(

这里,我们使用二进制进行安装。首先下载二进制文件,这里我们下载最新版本0.20.0(

【1】 【2】 【3】 【4】 【5】 【6】

https://github.com/prometheus/alertmanager)。
https://prometheus.io/download/#alertmanager)
https://prometheus.io/docs/prometheus/latest/installation/
https://www.cnblogs.com/yanyouqiang/p/7240696.html
https://grafana.com/docs/installation/rpm/
https://grafana.com/docs/features/datasources/prometheus/
https://www.cnblogs.com/longcnblogs/p/9620733.html
https://github.com/prometheus/alertmanager/blob/master/doc/examples/simple.yml