一、拓扑
二、下载软件
官网下载地址:https://prometheus.io/download/
1.主程序下载 prometheus-2.35.0.linux-amd64.tar.gz
2.、警告工具 alertmanager-0.24.0.linux-amd64.tar.gz
- grafana 下载 grafana-7.4.0-1.x86_64.rpm
https://mirrors.bfsu.edu.cn/grafana/yum/rpm/
4 . node_exporter 下载 node_exporter-1.3.1.linux-amd64.tar.gz
5.grafana仪表盘下载地址
https://grafana.com/grafana/dashboards ,在页面中搜索 node exporter 或者docker
https://mirrors.tuna.tsinghua.edu.cn/grafana/yum/rpm/Packages/ 国内清华源
百度云下载
1.node_exporter 下载 node_exporter-1.3.1.linux-amd64.tar.gz
链接:https://pan.baidu.com/s/1rTomtXsSPnnGbbnd-JrSig
提取码:eepq
2.警告工具 alertmanager-0.24.0.linux-amd64.tar.gz
链接:https://pan.baidu.com/s/1BKB6TAeQpJXWuBjZFJee5w
提取码:s812
3.主程序下载 prometheus-2.35.0.linux-amd64.tar.gz
链接:https://pan.baidu.com/s/1sYKz7kabw3Ugka5bvSbaYQ
提取码:42h0
4.3. grafana 下载 grafana-7.4.0-1.x86_64.rpm
链接:https://pan.baidu.com/s/1rNoGtZsoaHHb9tMQ_rvqcA
提取码:sbp4
5.windoes exporter 64位(下面两种 一个MSI 一个EXE)
链接:https://pan.baidu.com/s/1ig8dc232CYwfLbcYf0wmUw
提取码:2qfy
链接:https://pan.baidu.com/s/1B4CkZbBnaF2nJ-qg4q0KBw
提取码:up2f
部署安装之前先做系统必要设置如下:ubuntu 20.04
(centos 7 基本也都差不多)
三、服务器基础设置
1.设置root密码
sudo passwd root
显示如下 先输入test的密码
之后输入root 需要设置的密码 2次
[sudo] password for ccpit:
New password:
Retype new password:
passwd: password updated successfully
2.使用root登陆系统
su root
输入新的root 密码 就可以进入了
3.设置root用户 远程SSH 访问权限
编辑文件
sudo vi /etc/ssh/sshd_config
编辑配置文件 找到"#PermitRootLogin prohibit-password"行,在下面添加一行
PermitRootLogin yes
并保存退出。
4.从启动SSH服务
sudo systemctl restart ssh
或者
service ssh restart
4.设置时间同步 及修改系统时间 时区为上海(Asia/Shanghai)
1.修改时区
sudo timedatectl set-timezone Asia/Shanghai
2.查看timesyncd的状态
timedatectl status
显示如下
Local time: Thu 2023-02-09 11:50:35 CST
Universal time: Thu 2023-02-09 03:50:35 UTC
RTC time: Thu 2023-02-09 03:50:34
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
说明:Time zone是时区 System clock synchronized: yes 反映了和远程NTP服务器成功同步, NTP service: active 意味着timesyncd已经启动和运行。RTC in local TZ: no表示硬件时钟(RTC)设置为协调世界时(UTC),yes表示硬件时钟设置为本地时间.
3.查看timesyncd的服务运行情况:
systemctl status systemd-timesyncd
4.修改时间同步服务器(NTP服务器默认为ntp.ubuntu.com)
sudo vim /etc/systemd/timesyncd.conf
原文件下面的内容NTP= FallbackNTP=
[Time]
#NTP=
#FallbackNTP=ntp.ubuntu.com
#RootDistanceMaxSec=5
#PollIntervalMinSec=32
#PollIntervalMaxSec=2048
修改成下面NTP= FallbackNTP= 两行 其他设置不变
[Time]
NTP=ntp.tencent.com
FallbackNTP=ntp1.tencent.com,ntp2.tencent.com,ntp3.tencent.com
RootDistanceMaxSec=5
PollIntervalMinSec=32
PollIntervalMaxSec=2048
保存退出
#5.从启动时间服务
service systemd-timesyncd restart
6设置24小时制
echo "LC_TIME=en_DK.UTF-8" > /etc/default/locale
7.计划任务
crontab -e
选择3
最后一行添加如下
*/5 * * * * ntpdate time1.aliyun.com &> /dev/null hwclock -w
8.软连接
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
2.更改源地址
cat > /etc/apt/sources.list <<'EOF'
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted #优先检索focal的main和restricted组件。
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted
deb http://mirrors.aliyun.com/ubuntu/ focal universe
deb http://mirrors.aliyun.com/ubuntu/ focal-updates universe
deb http://mirrors.aliyun.com/ubuntu/ focal multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu focal-security main restricted
deb http://mirrors.aliyun.com/ubuntu focal-security universe
deb http://mirrors.aliyun.com/ubuntu focal-security multiverse
EOF
3.更新源
apt-get update
4.安装必要软件
sudo apt-get install -y vim net-tools inetutils-ping systemd-timesyncd lrzsz unzip zip
8.设置服务器的固定IP地址(按自己的规划设置 下面举例一台)
备份网卡配置
cp /etc/netplan/ && cp 00-installer-config.yaml 00-installer-config.yaml.bak
#1.编辑配置文件 每台机器不一样 编辑自己文件名的
sudo vim /etc/netplan/00-installer-config.yaml
如下
# This is the network config written by 'subiquity'
network:
ethernets:
ens33:
addresses:
- 10.10.201.160/24
gateway4: 10.10.201.1
nameservers:
addresses:
- 202.106.0.20
search:
- 114.114.114.114
version: 2
保存 退出
#2.重启动服务
sudo netplan apply
#3.修改主机名 和 hosts文件
sudo vi /etc/hostname
如下
prometheus-01
#4.关闭selinux(centos 7 ubuntu 20.04不用关闭)
vim /etc/selinux/config
如下更改
SELINUX=disabled
从启动服务器(必须从启动 否则不生效)
reboot
#5.关闭交换分区(SWMP)
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab
关闭selinux 和防火墙
UBUNTU 20.04最小化安装已经关闭
#6关闭防火墙
ufw disable
#7查看防火墙状态
ufw status
显示下面 为已关闭
Status: inactive
四、Prometheus 服务器端安装部署(10.10.201.86)
上传 prometheus-2.35.0.linux-amd64.tar.gz 到服务器的/usr/local
1.解压缩
安装一个上传文件用的工具
yum -y install lrzsz
1.进入目录
cd /usr/local
2.先将prometheus-2.35.0.linux-amd64.tar.gz 上传至此目录/usr/local 再解压缩
tar xvf prometheus-2.35.0.linux-amd64.tar.gz
3.目录改名为prometheus
mv prometheus-2.35.0.linux-amd64 prometheus
4.进入prometheus目录
cd /usr/local/prometheus
5.备份一份源配置文件
cp /usr/local/prometheus/prometheus.yml /usr/local/prometheus/prometheus.yml.bak
- 编辑配置文件 prometheus.yml
1. 编辑配置文件
vi /usr/local/prometheus/prometheus.yml
2.如下 最后一行添加如下(其他三台服务器和本机)保存后退出
- job_name: "nodes"
static_configs:
- targets:
- 10.10.201.80:9100
- 10.10.201.81:9100
- 10.10.201.82:9100
- 10.10.201.86:9100
- 我的prometheus.yml
static_configs:
- targets: ["localhost:9090"]
- job_name: "nodes"
static_configs:
- targets:
- 10.10.201.84:9100
- 10.10.201.85:9100
- 10.10.201.80:9100
- 10.10.201.81:9100
- 10.10.201.82:9100
- 10.10.201.83:9100
- 10.10.201.86:9100
- 10.10.201.28:9100
- 10.10.201.29:9100
- job_name: "prometheus-containers"
static_configs:
- targets:
- 10.10.201.85:8090
- 10.10.201.173:8090
- job_name: 'elasticsearch'
scrape_interval: 60s
scrape_timeout: 30s
static_configs:
- targets:
- 10.10.201.80:9114
- job_name: "windows-server"
static_configs:
- targets:
- 10.10.201.26:9182
- 10.10.201.71:9182
- 10.10.201.27:9182
- 10.10.201.32:9182
- 10.10.201.31:9182
- 编辑prometheus 为系统服务
1.新建并编辑文件prometheus.service 保存退出
注意ExecStart=你自己的路径 --config.file=你自己的路径
vi /etc/systemd/system/prometheus.service
如下
[Unit]
Descriptinotallow=Prometheus Monitoring System
Documentatinotallow=Prometheus Monitoring System
[Service]
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --web.listen-address=:9090
[Install]
WantedBy=multi-user.target
2.刷新
systemctl daemon-reload
如果不加系统服务 进入目录
cd /usr/local/prometheus
nohup ./prometheus & 后台运行也可以
- 启动prometheus服务后查看下服务状态(如下图)
启动服务
systemctl start prometheus
查看状态
systemctl status prometheus
从启动服务
systemctl restart prometheus
开机自启动
systemctl enable prometheus
停止服务
systemctl stop prometheus
- 服务器端安装完成 浏览器 访问测试
访问http://10.10.201.86:9090/metrics 查看 prometheus 自带的内键指标
顺序点击 Status-----Targets
会看到本机
服务器安装成功!!
五、Prometheus 客户端 node_exporter安装部署(三台都要部署)
(10.10.201.80)(10.10.201.81)(10.10.201.82)
上传文件 node_exporter-1.3.1.linux-amd64.tar.gz 到服务器
- 解压缩
1.进入目录
cd /usr/local
2.解压缩
tar xf node_exporter-1.3.1.linux-amd64.tar.gz
改名
mv /usr/local/node_exporter-1.3.1.linux-amd64 /usr/local/node_exporter
3.拷贝node_exporter到/usr/local/bin/目录
cp /usr/local/node_exporter/node_exporter /usr/local/bin/
#######可以使用scp命令把 node_exporter-1.3.1.linux-amd64.tar.gz 拷贝到其他主机/usr/local###
######需要知道其他主机的root密码############
scp node_exporter-1.3.1.linux-amd64.tar.gz root@10.10.201.81:/usr/local
scp node_exporter-1.3.1.linux-amd64.tar.gz root@10.10.201.82:/usr/local
- 启动服务
1.进入目录
cd /usr/local/bin
2.启动服务
nohup node_exporter &
如停止服务 使用kill -9 杀死进程
先查看进程
ps -ef | grep node_exporter
如下
[root@node-1 bin]# ps -ef | grep node_exporter
root 1913 1 0 17:06 ? 00:00:38 node_exporter
root 2048 2010 0 18:55 pts/0 00:00:00 grep --color=auto node_exporter
杀死进程
kill -9 1913
node_exporter centos 7系统 开机自启动设置
1.新建编辑文件node_exporter.service
vim /etc/systemd/system/node_exporter.service
如下 注意:ExecStart=为自己的路径
[Unit]
Descriptinotallow=node_exporter Monitoring System
Documentatinotallow=node_exporter Monitoring System
[Service]
ExecStart=/usr/local/bin/node_exporter --web.listen-address=:9100
[Install]
WantedBy=multi-user.target
设置开机自动启动之前 需要先停止node_exporter服务
ps -ef | grep node_exporter
kill -9 1867 杀死node_exporter的进程
#设置开机自启
刷新
systemctl daemon-reload
开启服务
systemctl start node_exporter.service
查看状态
systemctl status node_exporter.service
设置开机自启动服务
systemctl enable node_exporter.service
查看端口
netstat -natp | grep :9100
查看进程
ps -ef | grep node_exporter
node_exporter UBUNTU系统 开机自启动设置
1.vim /etc/systemd/system/node_exporter.service
2.
[Unit]
Descriptinotallow=node_exporter Monitoring System
Documentatinotallow=node_exporter Monitoring System
[Service]
ExecStart=/usr/local/bin/node_exporter --web.listen-address=:9100
[Install]
WantedBy=multi-user.target
3.设置开机自启
systemctl daemon-reload
systemctl start node_exporter.service
systemctl status node_exporter.service
systemctl enable node_exporter.service
客户端安装完毕!!
浏览器访问 http://10.10.201.86:9090/targets prometheus服务器
查看是否有四台的数据(包括prometheus自己)
成功获取到数据 完成!
六、grafana安装部署(10.10.201.86)
centos7 ubuntu
- centos 7 安装
上传 grafana-7.4.0-1.x86_64.rpm 到服务器的/usr/local 目录中 UBUNTU为sudo dpkg -i grafana-enterprise_7.4.0_amd64.deb
1.进入目录
cd /usr/local
2.安装grafana
yum install -y grafana-7.4.0-1.x86_64.rpm
#ubuntu系统为
sudo apt-get install -y adduser libfontconfig1
sudo dpkg -i grafana-enterprise_7.4.0_amd64.deb
sudo systemctl daemon-reload
sudo systemctl start grafana-server
sudo systemctl status grafana-server
sudo systemctl enable grafana-server.service
### 下面的选装###
#安装grafana-image-renderer
grafana-cli plugins install grafana-image-renderer
#安装截图需要的依赖库,不装捕获不到图片
yum -y install libatk-bridge* libXss* libgtk*
#修改配置文件,防止中文乱码
vim /etc/grafana/grafana.ini
#修改以下内容
rendering_language = zh
2.启动服务
启动服务
systemctl start grafana-server
添加服务至 开机启动
systemctl enable grafana-server
查看服务状态
systemctl status grafana-server
从启动服务
systemctl status grafana-server
查看grafana端口
netstat -natp | grep :3000
浏览器访问:http://10.10.201.86:3000 ,默认账号和密码为 admin/admin
第一次登陆 需要更改密码 记住就好
其他一些命令
查看安装版本
rpm -qa | grep grafana
卸载
yum remove grafana
3. 访问 http://10.10.201.86:3000 grafana 中配置数据源prometheus 及默认自带仪表盘
Configuration -> Data Sources -> Add data source -> 选择 Prometheus
HTTP -> URL 输入 http://10.10.201.86:3000
点击 Save & Test
点击 上方菜单 Dashboards,Import 所有默认模板
Dashboards -> Manage ,选择 Prometheus 2.0 Stats 或 Prometheus Stats 即可看到 Prometheus job 实例的监控图像
ubuntu 安装
wget https://dl.grafana.com/oss/release/grafana_7.4.0_amd64.deb
sudo apt-get install -y adduser libfontconfig1
sudo dpkg -i grafana_7.4.0_amd64.deb
4.导入新的自定义仪表盘
浏览器访问:https://grafana.com/grafana/dashboards ,在页面中搜索 node exporter ,选择适合的面板,点击 Copy ID 或者 Download JSON
在 grafana 页面中,+ Create -> Import ,输入面板 ID 号或者上传 JSON 文件,点击 Load,即可导入监控面板
linux服务器ID:8919 或者12670 我的LINUX 用的12670中文 或者11074英文
演示一个我使用的指标模板 中文的 (使用)
##########Node Exporter Dashboard 22/04/13 ConsulManager自动同步版####我用的是这个
浏览器访问: https://grafana.com/grafana/dashboards/8919-1-node-exporter-for-prometheus-dashboard-cn-0413-consulmanager/?tab=revisions
下载文件如下
1-node-exporter-for-prometheus-dashboard-cn-0413-consulmanager_rev19.json
1.点击down下载下来中文版本
2.Upload JSON file 上传
3.VictoriaMetrics 下面选择 prometheus
点击Import 完成
成功后的截图
主配置文件
说明
[root@prometheus /usr/local/prometheus-2.27.1.linux-amd64]#egrep -v "^(.)*#(.)*$" prometheus.yml | grep -v "^$"
global:
alerting:
alertmanagers:
- static_configs:
- targets:
rule_files:
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
[root@prometheus /usr/local/prometheus-2.27.1.linux-amd64]#cat prometheus.yml
# my global config
global: #全局组件
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
eval(10s).
# Alertmanager configuration #对接的 altermanager (第三方告警模块)
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files: #告警规则;告警规则可以使用 yml 规则去书写
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs: #数据采集模块
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. #对于所抓取的指标数据采集的来源在job_name中定义
- job_name: 'prometheus' #对于指标需要打上的标签,对于PrometheusSQL(查询语句)的标签:比如prometheus{target='values'}
# metrics_path defaults to '/metrics' #收集数据的路径,展示使用 metrics 模式
# scheme defaults to 'http'. #默认抓取的方式是 http
static_configs: #对于 Prometheus 的静态配置监听端口具体数据收集的位置 默认的端口 9090
- targets: ['localhost:9090']