上篇主要介绍Prometheus一些理论知识,此篇主要记录搭建过程。
环境准备
- Centos Linux 8
- Prometheus
- snmp_exporter
- alertmanager
- Grafana
系统安装
我一般习惯最小化安装系统,然后安装一些必要软件。
1
| yum -y install vim net-tools wget
|
1 2 3 4
| vim /etc/selinux/config - SELINUX=enforcing + SELINUX=disabled
|
- 关闭防火墙或卸载防火墙,我一般习惯卸载,需要的话使用iptables。
1
| yum -y install net-snmp-utils
|
Prometheus相关组件安装
1 2 3 4 5 6
| wget https://github.com/prometheus/prometheus/releases/download/v2.31.1/prometheus-2.31.1.linux-amd64.tar.gz
tar -zxvf prometheus-2.31.1.linux-amd64.tar.gz # 解压缩 mv prometheus-2.31.1.linux-amd64 /opt/prometheus # 移动至/opt/下并重命名
|
1 2 3
| wget https://github.com/prometheus/snmp_exporter/releases/download/v0.20.0/snmp_exporter-0.20.0.linux-amd64.tar.gz tar -zxvf snmp_exporter-0.20.0.linux-amd64.tar.gz mv snmp_exporter-0.20.0.linux-amd64 /opt/snmp_exporter
|
1 2 3
| wget https://github.com/prometheus/alertmanager/releases/download/v0.23.0/alertmanager-0.23.0.linux-amd64.tar.gz tar -zxvf alertmanager-0.23.0.linux-amd64.tar.gz mv alertmanager-0.23.0.linux-amd64 /opt/alertmanager
|
- 为了方便使用系统命令进行启动&关闭&重启&开机自启等操作,还需执行以下命令。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| cat > /etc/systemd/system/prometheus.service <<EOF [Unit] Description=Prometheus After=network.target [Service] ExecStart=/opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus.yml --storage.tsdb.path=/opt/prometheus/data User=prometheus [Install] WantedBy=multi-user.target EOF
cat > /etc/systemd/system/snmp_exporter.service <<EOF [Unit] Description=node_exporter After=network.target [Service] ExecStart=/opt/snmp_exporter/snmp_exporter --config.file=/opt/snmp_exporter/snmp.yml Restart=on-failure [Install] WantedBy=multi-user.target EOF
cat > /etc/systemd/system/alertmanager.service <<EOF [Unit] Description=node_exporter After=network.target [Service] ExecStart=/opt/alertmanager/alertmanager --config.file=/opt/alertmanager/alertmanager.yml Restart=on-failure [Install] WantedBy=multi-user.target EOF
|
- 添加prometheus用户,并赋予其三个软件的权限。
1 2
| useradd prometheus chown -R prometheus. /opt/{snmp_exporter,prometheus,alertmanager}
|
- 设置Prometheus、snmp_exporter、alertmanager服务开机自启。
1
| systemctl enable prometheus && systemctl enable snmp_exporter && systemctl enable alertmanager
|
- 启动Prometheus、snmp_exporter、alertmanager服务。
1
| systemctl start prometheus && systemctl start snmp_exporter && systemctl start alertmanager
|
1 2 3 4 5
| netstat -anptu # 服务正常启动的话,输入上面这条命令可以看到监听端口 prometheus 监听9090 snmp_exporter 监听9116 alertmanager 监听9093和9094
|
安装Grafana
- 可以选择docker版本安装,我这里选择的yum安装
1 2
| systemctl enable grafana-server systemctl start grafana-server
|
后续