ubuntu上安装prometheus和grafana

环境概述

        在ubuntu22.04上二进制安装prometheus,alertmanager,grafana,node_exporter。

        参考教程:【2023年最新】企业监控Prometheus和Grafana生产实战_哔哩哔哩_bilibili

        下面的步骤是我看学习视频自己整理的部分,当然也要谢谢前面大佬的分享。

名称版本功能
ubuntu20.04和22.04都可行基础系统
prometheus2.45.2 TLS版本监控服务核心
grafana10.2.3监控可视化大屏,prometheus提供数据支持
alertmanager0.26.0告警服务
node_exporter1.7.0节点收集服务,和prometheus配合使用
pushgateway(这里暂无步骤)1.6.2汇总exporter信息后发给prometheus

安装包下载

github网络允许的

wget https://github.com/prometheus/prometheus/releases/download/v2.45.2/prometheus-2.45.2.linux-amd64.tar.gz
wget https://github.com/prometheus/alertmanager/releases/download/v0.26.0/alertmanager-0.26.0.linux-amd64.tar.gz
wget https://github.com/prometheus/node_exporter/releases/download/v1.7.0/node_exporter-1.7.0.linux-amd64.tar.gz
wget https://dl.grafana.com/enterprise/release/grafana-enterprise-10.2.3.linux-amd64.tar.gz
wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.24.0/blackbox_exporter-0.24.0.linux-amd64.tar.gz
wget https://github.com/prometheus/consul_exporter/releases/download/v0.11.0/consul_exporter-0.11.0.linux-amd64.tar.gz
wget https://github.com/prometheus/graphite_exporter/releases/download/v0.15.0/graphite_exporter-0.15.0.linux-amd64.tar.gz
wget https://github.com/prometheus/memcached_exporter/releases/download/v0.14.2/memcached_exporter-0.14.2.linux-amd64.tar.gz
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.15.1/mysqld_exporter-0.15.1.linux-amd64.tar.gz
wget https://github.com/prometheus/promlens/releases/download/v0.3.0/promlens-0.3.0.linux-amd64.tar.gz
wget https://github.com/prometheus/pushgateway/releases/download/v1.6.2/pushgateway-1.6.2.linux-amd64.tar.gz
wget https://github.com/prometheus/statsd_exporter/releases/download/v0.26.0/statsd_exporter-0.26.0.linux-amd64.tar.gz

网络不允许的

        直接下载我这边上传绑定的资源包也行。

安装prometheus

官网下载地址:

Download | Prometheus

#下载LTS长期支持版本2.45.2

cd /opt/

wget https://github.com/prometheus/prometheus/releases/download/v2.45.2/prometheus-2.45.2.linux-amd64.tar.gz
tar -xf prometheus-2.45.2.linux-amd64.tar.gz
mkdir /opt/prometheus
mv prometheus-2.45.2.linux-amd64 /opt/prometheus/prometheus



#创建prometheus用户
useradd -M -s /sbin/nologin prometheus

id prometheus

#授权
chown -R prometheus.prometheus /opt/prometheus/

#创建service启动文件
cat > /etc/systemd/system/prometheus.service << EOF
[Unit]
Description=Prometheus Server
After=network-online.target

[Service]
Type=simple
User=prometheus
Group=prometheus
Restart=always
ExecStart=/opt/prometheus/prometheus/prometheus \
  --config.file=/opt/prometheus/prometheus/prometheus.yml \
  --storage.tsdb.path=/opt/prometheus/prometheus/data \
  --web.enable-lifecycle
  
[Install]
WantedBy=multi-user.target
EOF


#开启服务测试
systemctl start prometheus
systemctl enable prometheus

#访问
http://192.168.48.4:9090

安装Alertmanager

官网下载地址:

Download | Prometheus

#下载
cd /opt/
wget https://github.com/prometheus/alertmanager/releases/download/v0.26.0/alertmanager-0.26.0.linux-amd64.tar.gz

tar -xf alertmanager-0.26.0.linux-amd64.tar.gz 
mv alertmanager-0.26.0.linux-amd64 /opt/prometheus/alertmanager

#设置prometheus授权
chown -R prometheus.prometheus  /opt/prometheus/alertmanager


#编写service启动文件
cat >/etc/systemd/system/alertmanager.service <<EOF
[Unit]
Description=Alert Manager
After=network-online.target

[Service]
Type=simple
User=prometheus
Group=prometheus
Restart=always
ExecStart=/opt/prometheus/alertmanager/alertmanager \
  --config.file=/opt/prometheus/alertmanager/alertmanager.yml \
  --storage.path=/opt/prometheus/alertmanager/data 
  
[Install]
WantedBy=multi-user.target
EOF


#启动服务
systemctl start alertmanager

#访问测试
http://192.168.48.4:9093



#修改prometheus.yml配置alertmanager
grep -v '#' /opt/prometheus/prometheus/prometheus.yml | grep -v '^$'
global:
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          - localhost:9093   ##修改的这里
rule_files:
  - "alert.yml"       ##增加的这里
scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:9090"]


#增加alert.yml规则文件

cat > /opt/prometheus/prometheus/alert.yml << EOF
groups:
- name: Prometheus alert
  #对任何实例超过30s无法联系的情况发出警报
  rules:
  - alert: 服务告警
    expr: up == 0
    for: 30s
    labels:
      severity: critical
    annotations:
      instance: "{{ $labels.instance }}"
      description: "{{ $labels.job }} 服务已关闭"
EOF
      
#检查配置yml
cd /opt/prometheus/prometheus/
./promtool check config prometheus.yml


#重启prometheus或重载配置文件(二选一)
systemctl restart prometheus
或
curl -X POST http://localhost:9090/-/reload

安装grafana

官网下载地址:

Download Grafana | Grafana Labs

#下载
cd /opt
wget https://dl.grafana.com/enterprise/release/grafana-enterprise-10.2.3.linux-amd64.tar.gz
tar -zxvf grafana-enterprise-10.2.3.linux-amd64.tar.gz

mv grafana-v10.2.3 /opt/prometheus/grafana


#修改授权
chown -R prometheus.prometheus /opt/prometheus/grafana/


#编写service
cat >/etc/systemd/system/grafana.service <<EOF
[Unit]
Description=Grafana
After=network-online.target

[Service]
Type=simple
User=prometheus
Group=prometheus
Restart=always   #或on-failure
ExecStart=/opt/prometheus/grafana/bin/grafana-server \
  --config=/opt/prometheus/grafana/conf/defaults.ini \
  --homepath=/opt/prometheus/grafana 
  
[Install]
WantedBy=multi-user.target
EOF


#启动服务
systemctl start grafana

#访问
http://192.168.48.4:3000
admin
admin

#这里需要首次登录后重置密码,自己按照提示重置即可

安装node_exporter

官网下载地址:

Download | Prometheus

#下载
cd /opt/
wget https://github.com/prometheus/node_exporter/releases/download/v1.7.0/node_exporter-1.7.0.linux-amd64.tar.gz

tar -xf node_exporter-1.7.0.linux-amd64.tar.gz 
mv node_exporter-1.7.0.linux-amd64 /opt/prometheus/node_exporter
#设置prometheus授权
chown -R prometheus.prometheus  /opt/prometheus/node_exporter


#编写service启动文件
cat >/etc/systemd/system/node_exporter.service <<EOF
[Unit]
Description=Node Exporter
After=network-online.target

[Service]
Type=simple
User=prometheus
Group=prometheus
Restart=always
ExecStart=/opt/prometheus/node_exporter/node_exporter
  
[Install]
WantedBy=multi-user.target
EOF

#启动服务和设置开机自启
systemctl start node_exporter
systemctl enable node_exporter


#修改prometheus.yml
cat >> /opt/prometheus/prometheus/prometheus.yml << EOF
# 在scrape_configs这行下面增加以下配置
  #node-exporter配置
  - job_name: 'node-exporter'
    scrape_interval: 15s
    static_configs:
    - targets: ['localhost:9100']
      labels:
        instance: Prometheus服务器
EOF

#检查配置yml
cd /opt/prometheus/prometheus/
./promtool check config prometheus.yml


#重启prometheus或重载配置文件(二选一)
systemctl restart prometheus
或
curl -X POST http://localhost:9090/-/reload


#访问prometheus和grafana
http://192.168.48.4:9090

http://192.168.48.4:3000
admin
admin(这里要输入自己自定义后的密码)


#进grafana看插件,选择node_exporter的dashboard

https://grafana.com/grafana/dashboards/

1860

设置服务开机自启


root@prom:/opt/prometheus/prometheus# systemctl enable node_exporter
Created symlink /etc/systemd/system/multi-user.target.wants/node_exporter.service → /etc/systemd/system/node_exporter.service.
root@prom:/opt/prometheus/prometheus# systemctl enable alertmanager
Created symlink /etc/systemd/system/multi-user.target.wants/alertmanager.service → /etc/systemd/system/alertmanager.service.
root@prom:/opt/prometheus/prometheus# systemctl enable grafana
Created symlink /etc/systemd/system/multi-user.target.wants/grafana.service → /etc/systemd/system/grafana.service.
root@prom:/opt/prometheus/prometheus# systemctl enable prometheus
Created symlink /etc/systemd/system/multi-user.target.wants/prometheus.service → /etc/systemd/system/prometheus.service.

注意事项

1、prometheus用户创建并授权到/opt/prometheus/(自定义后要做相应修改)

2、每次写了service后,要systemctl start 测试看有没报错。(有报错一般都是第一条的问题)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值