监控神器Prometheus(2)

icegoblin
发布于 2022-7-4 17:10
浏览
0收藏

 

在监控神器Prometheus(1)中已经介绍了Prometheus的相关特性与数据类型,现在我们讲解下如何安装配置使用Grafana图形展示,并接入Email、Dingtalk、Wechat警报,Prometheus已经完美的支持Email、Slack、Dingtalk、Wechat警报。


系统/软件版本

  • System:Ubuntu 18.04
  • Prometheus 2.13.0
  • Node_Exporter 1.18.0
  • Alaermanager 1.18.0
  • Dingtalk-webhook 0.3.0
  • Grafana 6.4.0
     

Prometheus安装及配置
二进制安装

PROM_PATH='/data/prometheus'
mkdir -p ${PROM_PATH}
mkdir -p ${PROM_PATH}/{data,conf,logs,bin}
useradd prometheus
cd /usr/local/src
wget https://github.com/prometheus/prometheus/releases/download/v2.13.0/prometheus-2.13.0.linux-amd64.tar.gz
tar -xvf prometheus-2.13.0.linux-amd64.tar.gz
cd prometheus-2.13.0.linux-amd64/
cp prometheus promtool ${PROM_PATH}/bin/
cp prometheus.yml ${PROM_PATH}/config/
chown -R prometheus.prometheus /data/prometheus
# Setting Variables
cat >> /etc/profile <<EOF
PATH=/data/prometheus/bin:$PATH:$HOME/bin
EOF

 

创建Systemd Prometheus服务

cat >>/etc/systemd/system/prometheus.service <<EOF
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target

[Service]
Type=simple
User=prometheus
ExecStart=/data/prometheus/bin/prometheus --config.file=/data/prometheus/conf/prometheus.yml --storage.tsdb.path=/data/prometheus/data --web.external-url=http://prom.k8stech.net --storage.tsdb.retention=90d
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

systemctl enable prometheus.service
systemctl start prometheus.service
systemctl status prometheus.service
# 查看端口是否正常
netstat -anplt|grep 9090
tcp        0      0 127.0.0.1:47186         127.0.0.1:9090          ESTABLISHED 32069/prometheus
tcp        0      0 127.0.0.1:47188         127.0.0.1:9090          ESTABLISHED 32069/prometheus
tcp6       0      0 :::9090                 :::*                    LISTEN      32069/prometheus
tcp6       0      0 127.0.0.1:9090          127.0.0.1:47188         ESTABLISHED 32069/prometheus
tcp6       0      0 127.0.0.1:9090          127.0.0.1:47186         ESTABLISHED 32069/prometheus

 

Prometheus配置文件

# Alertmanager Rule 目录 与 文件
CONF_PATH='/data/prometheus/conf'
# 目录必须提前创建,否则Prometheus服务会无法启动
mkdir -p ${CONF_PATH}/rule/{op,ssl,prod}
mkdir -p ${CONF_PATH}/prod/domain_config

# prometheus conf file
cat > /data/prometheus/conf/prometheus.yml << EOF
# https://prometheus.io/docs/prometheus/latest/configuration/configuration/
# 全局配置
global:
  scrape_interval:     30s # 每15秒抓取一次数据,默认值为1分钟
  scrape_timeout: 30s
  evaluation_interval: 60s # 每15分钟检测一次可用性,默认值为1分钟
  #scrape_timeout: 60s # 全局设置超时时间,这个注掉了。

# Alertmanager配置,需要在targets添加ip和端口,也可以使用主机名和域名
alerting:
  alertmanagers:
    - static_configs:
        - targets: ['127.0.0.1:9093']

# 根据全局文件 'evaluation_interval' 的时间,根据 rule 文件进行检查,可配置多个。
rule_files:
  - "/data/prometheus/conf/rule/prod/*.yml"
  - "/data/prometheus/conf/rule/op/*.yml"
  - "/data/prometheus/conf/rule/ssl/*.yml"
  # - "second_rules.yml"
# 抓取配置配置
scrape_configs:
  - job_name: 'blackbox'
    metrics_path: /probe
    params:
      module: [http_2xx]  # Look for a HTTP 200 response.
    scrape_interval: 30s
    file_sd_configs:
      - files:
        - /data/prometheus/conf/prod/domain_config/*.yml
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: 127.0.0.1:9115  # The blackbox exporter's real hostname:port.
  - job_name: 'prom'
    #honor_labels: true
    scrape_interval: 10s
    static_configs:
    - targets: ['172.26.42.229:9100']
      labels:
         op_region: 'cn-north-1'
         app:   'Prometheus'
         env:  'Server'
EOF

 

Nginx配置Basic_Auth访问

# 安装 Apache工具包
apt install apache2-utils
htpasswd -bc /etc/nginx/.prom_htpasswd admin admin
# nginx conf
cat > /etc/nginx/conf.d/prom.conf <<EOF
server {
    listen       80;
    server_name  prom.k8stech.net;
        auth_basic "Please input password";
        auth_basic_user_file /etc/nginx/.prom_htpasswd;
    location / {
            try_files $uri @prom;
    }
    location @prom {
            internal;
            proxy_pass http://localhost:9090;
    }
}
EOF

 

使用浏览器访问
http://prom.k8stech.net

user:admin
pass:admin

二进制安装Node_exporter

# prom server 安装
NODE_PATH='/data/prometheus/node_exporter/'
cd /usr/local/src/
mkdir -p ${NODE_PATH}
wget https://github.com/prometheus/node_exporter/releases/download/v0.18.0/node_exporter-0.18.0.linux-amd64.tar.gz && tar xvf node_exporter-0.18.0.linux-amd64.tar.gz
cp node_exporter-0.18.0.linux-amd64/node_exporter ${NODE_PATH}
chown -R prometheus.prometheus ${NODE_PATH}

# node节点安装
NODE_PATH='/data/prometheus/node_exporter/'
useradd prometheus && mkdir -p ${NODE_PATH}
cd /usr/local/src/
wget https://github.com/prometheus/node_exporter/releases/download/v0.18.0/node_exporter-0.18.0.linux-amd64.tar.gz && tar xvf node_exporter-0.18.0.linux-amd64.tar.gz
cp node_exporter-0.18.0.linux-amd64/node_exporter ${NODE_PATH}
chown -R prometheus.prometheus ${NODE_PATH}

 

创建Systemd Node_exporter服务

# 创建配置文件 Centos7 路径是/usr/lib/systemd/
cat > /lib/systemd/system/node_exporter.service <<EOF
[Unit]
Description=node_exporter
Documentation=https://prometheus.io/
After=network.target

[Service]
Type=simple
User=prometheus
ExecStart=/data/prometheus/node_exporter/node_exporter
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

# 开机启动并运行服务
systemctl enable node_exporter.service
systemctl start node_exporter.service
systemctl status node_exporter.service

# 查看端口是否正常
netstat -anplt|grep 9100
tcp        0      0 172.26.42.229:58364     172.26.42.229:9100      ESTABLISHED 32220/prometheus
tcp6       0      0 :::9100                 :::*                    LISTEN      972/node_exporter
tcp6       0      0 172.26.42.229:9100      172.26.42.229:58364     ESTABLISHED 972/node_exporter

 

Node Metrics查看 访问9100端口即可。

 

欢迎大家关注我的公众号ID:k8stech

 

文章转自公众号:Kubernetes技术栈

标签
已于2022-7-4 17:10:31修改
收藏
回复
举报
回复