本文最后更新于121 天前,其中的信息可能已经过时,如有错误请发送邮件到[email protected]
一· 准备机器
keepalived主 192.168.48.128
keepalived备 192.168.48.129
nginx1+prometheus联邦 192.168.48.130
nginx2+prometheus联邦 192.168.48.131
主节点prometheus 192.168.48.132
二· 安装配置
2.1· 安装软件
2.1.1· 所有节点都安装软件
yum -y install wget curl git unzip vim nano net-tools iproute telnet nmap gzip bzip2 zip tree ncdu openssh openssh-server htop iftop istop sysstat fail2ban
2.1.2· keepalived节点安装软件
yum install -y keepalived ipvsadm
2.1.3· nginx+prometheus节点安装软件
yum install -y nginx&& systemctl start nginx && systemctl enable nginx
2.2· 配置keepalived主备节点
我们这里是yum安装的keepalived,内网情况下可以选用编译安装
2.2.1· 主keepalived配置(需要改的用一个#号表示,两个##可变可不变)
vim /etc/keepalived/keepalived.conf # 打开keepalived.conf主配置文件
! Configuration File for keepalived
global_defs {
router_id LVS_R1 # 修改keepalived路由ID,主备节点设置不同
}
vrrp_instance VI_1 {
state MASTER # 主节点配置MASTER,备节点配置SLAVE
interface ens33 # 配置通信网卡
virtual_router_id 62 # 虚拟路由ID,主备必须一致
priority 99 # 节点优先级,数值越大,表示越优先
advert_int 1 ## VRRP 广播间隔时间,单位为秒
authentication {
auth_type PASS ## 认证方式为PASS
auth_pass 1111 ## 密码为1111
}
virtual_ipaddress {
192.168.48.200/24 # 集群vip地址为192.168.48.200/24
}
}
virtual_server 192.168.48.200 80 { # 配置集群地址,端口80
delay_loop 6 ## 健康检查的间隔时间,每3秒检查一次后端服务器状态
lb_algo rr ## 负载均衡算法,使用轮询(rr)方式
lb_kind DR # LVS 集群模式,使用Direct Routing(DR)模式
persistence_timeout 0 # 这里设置成0,在客户端curl集群地址时才会一直变化,不然需要等待一段时间,或是直接把这个timeout这个参数删除
protocol TCP ## 使用TCP协议进行负载均衡
real_server 192.168.48.130 80 { # 配置后端nginx1地址,端口80
weight 1 ## 服务器权重,权重相同表示流量均等分配
TCP_CHECK { ## 健康检查方式
connect_timeout 10 ## TCP健康检查的连接超时时间,3秒内成功连接视为健康
retry 3 ## 重试次数
delay_before_retry 3 ## 重试间隔
connect_port 80 ## 检查的目标端口
}
}
real_server 192.168.48.131 80 { # 配置后端nginx2地址,端口80
weight 1
TCP_CHECK {
connect_timeout 10
retry 3
delay_before_retry 3
connect_port 80
}
}
}
2.2.2· 配置完后重启keepalived(双节点)
systemctl restart keepalived && systemctl enable keepalived
2.3· 配置nginx+prometheus联邦节点
2.3.1· 两个节点都是一样的配置(nginx配置)
vim /etc/nginx/nginx.conf # 打开nginx主配置文件
找到server区域进行配置
server {
listen 80;
server_name keepalived_master.com; # 这里配置的是我们的集群地址,只不过这里使用的是域名解析,在prometheus主节点/etc/hosts配置即可
location / {
proxy_http_version 1.1;
proxy_redirect off;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forworded-For $proxy_add_x_forworded_for;
proxy_set_header X-Nginx-Proxy true;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade"
proxy_set_header "http://192.168.48.130:9090" # 配置成本地prometheus节点或者其他地址prometheus节点
}
}
注: 一定配置完全后重启服务,不然就爽了
systemctl restart nginx && systemctl enable nginx
2.3.2· prometheus配置
首先下载prometheus和node_exporter安装包,要图形显示,可以安装grafana安装包
wget https://github.com/prometheus/prometheus/releases/download/v2.54.1/prometheus-2.54.1.linux-amd64.tar.gz
wget https://github.com/prometheus/node_exporter/releases/download/v1.8.2/node_exporter-1.8.2.linux-amd64.tar.gz
wget https://dl.grafana.com/enterprise/release/grafana-enterprise-11.2.0.linux-amd64.tar.gz
下载完成后解压,随后可以配置systemd启动
prometheus启动(system)
cat > /etc/systemd/system/prometheus.service <<-EOF
[Unit]
Description=prometheus
[Service]
ExecStart=/opt/monitor/prometheus/prometheus --config.file=/opt/monitor/prometheus/prometheus.yml --storage.tsdb.path=/opt/monitor/prometheus/data --web.enable-admin-api --storage.tsdb.max-block-duration=2h --web.external-url=prometheus
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
node_exporter启动(system)
cat > /etc/systemd/systemd/node_exporter.service <<-EOF
[Unit]
Description=node_exporter
Documentation=node_exporter Monitoring System
After=network.target
[Service]
ExecStart=/opt/monitor/node_exporter/node_exporter --web.listen-address=:9100
[Install]
WantedBy=multi-user.target
EOF
grafana-server启动(system)
cat > /etc/systemd/system/grafana.service <<-EOF
[Unit]
Description=Grafana
After=network.target
[Service]
ExecStart=/root/grafana-v11.2.0/bin/grafana-server \
--config=/root/grafana-v11.2.0/conf/defaults.ini \
--homepath=/root/grafana-v11.2.0
[Install]
WantedBy=multi-user.target
EOF
打开prometheus主配置文件
vim /opt/monitor/prometheus/prometheus.conf
- job_name: 'test-linux' # 随便取名字
file_sd_configs:
- files: ['node/linux.yml'] # node的yml配置文件,需要新建node目录
refresh_interval: 30s # 30秒刷新
# 新建目录后,打开yml文件
vim node/linux.yml
- targets:
- 192.168.48.130:9100
- 192.168.48.131:9100
双节点都按照这样配置完成后,分别启动prometheus和node_exporter,因为不一定负载调度到那一台上
三· prometheus主节点配置
3.1· 打开/hosts文件
vim /etc/hosts
192.168.48.200 keepalived-master.com # 把集群地址和域名进行解析
3.2· 修改prometheus主配置文件
- job_name: "keepalived-master"
honor_labels: true
metrics_path: "/prometheus/federate"
params:
'match[]':
- '{job=~".*"}'
static_configs:
- targets: ['keepalived-master.com:80'] # 集群地址和端口信息
配置完成后就可以启动prometheus服务,打开官网查看是否接收到数据即可