内容简介:本文风哥教程参考Linux官方文档、Red Hat Enterprise Linux官方文档、Ansible Automation Platform官方文档、Docker官方文档、Kubernetes官方文档和Podman官方文档等内容,详细介绍了相关技术的配置和使用方法。
本文档介绍Linux高可用集群配置实战案例。
风哥提示:
Part01-集群架构设计
1.1 高可用架构规划
[root@fgedu-ha1 ~]# cat > /root/ha-cluster-design.txt << 'EOF' FGEDU高可用集群架构 ================== 1. 集群节点 - fgedu-ha1: 192.168.1.10 (主节点) - fgedu-ha2: 192.168.1.11 (备节点) - VIP: 192.168.1.100 2. 软件组件 - Keepalived: VIP漂移 - HAProxy: 负载均衡 - Pacemaker: 集群资源管理 3. 服务资源 - Nginx: Web服务 - MySQL: 数据库服务学习交流加群风哥QQ113257174 - Redis: 缓存服务 4. 故障切换 - 主节点故障自动切换 - 服务健康检查 - 自动恢复机制 EOF
Part02-Keepalived配置
2.1 安装配置Keepalived
[root@fgedu-ha1 ~]# yum install -y keepalived
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Installing:
keepalived x86_64 2.2.8-1.el9 appstream 261 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 261 k
Installed size: 1.0 M
Downloading Packages:
keepalived-2.2.8-1.更多视频教程www.fgedu.net.cnel9.x86_64.rpm 1.2 MB/s | 261 kB 00:00
——————————————————————————–
Complete!
# 配置主节点Keepalived
[root@fgedu-ha1 ~]# cat > /etc/keepalived/keepalived.conf << 'EOF'
! Configuration File for keepalived
global_defs {
router_id FGEDU_HA1
script_user root
enable_script_security
}
vrrp_script check_nginx {
script "/usr/local/bin/check_nginx.sh"
interval 2
weight -20
fall 3
rise 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass fgedu123
}
virtual_ipaddress {
192.168.1.100/24
}
track_script {
check_nginx
}
notify_master "/usr/local/bin/notify_master.sh"
notify_backup "/usr/local/bin/notify_backup.sh"
}
EOF
# 创建健康检查脚本
[root@fgedu-ha1 ~]# cat > /usr/local/bin/check_nginx.sh <
link/ether 00:0c:29:ab:cd:ef brd ff:ff:ff:ff:ff:ff
inet 192.168.1.10/24 brd 192.168.1.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet 192.168.1.100/24 scope global secondary eth0
valid_lft forever preferred_lft forever
Part03-HAProxy配置
3.1 负载均衡配置
[root@fgedu-ha1 ~]# yum install -y haproxy
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Installing:
haproxy x86_64 2.4.22-1.el9 appstream 1.4 M
Transaction Summary
================================================================================
Install 1 Package
Total download size: 1.4 M
Installed size: 4.2 M
Downloading Packages:
haproxy-2.4.22-1.el9.x86_64.rpm 2.5 MB/s | 1.4 MB 00:00
——————————————————————————–
Complete!
# 配置HAProxy
[root@fgedu-ha1 ~]# cat > /etc/haproxy/haproxy.cfg << 'EOF'
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin
stats timeout 30s
user haproxy
group haproxy
daemon
maxconn 4000
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000
retries 3
option redispatch
# 监控页面
listen stats
bind *:8404
stats enable
stats uri /stats
stats refresh 10s
stats auth admin:admin123
# Web服务负载均衡
frontend fgedu-web
bind *:80
mode http
acl url_static path_beg -i /static /images /javascript /stylesheets
use_backend fgedu-static if url_static
default_backend fgedu-app
backend fgedu-app
mode http
balance roundrobin
option httpchk GET更多学习教程公众号风哥教程itpux_com /health
http-check expect status 200
server web1 192.168.1.20:80 check inter 2000 rise 2 fall 3
server web2 192.168.1.21:80 check inter 2000 rise 2 fall 3
server web3 192.168.1.22:80 check inter 2000 rise 2 fall 3
backend fgedu-static
mode http
balance roundrobin
server static1 192.168.1.30:80 check
# MySQL负载均衡
listen fgedu-mysql
bind *:3306
mode tcp
option mysql-check user haproxy_check
balance roundrobin
server mysql1 192.168.1.40:3306 check inter 2000 rise 2 fall 3
server mysql2 192.168.1.41:3306 check inter 2000 rise 2 fall 3 backup
# Redis负载均衡
listen fgedu-redis
bind *:6379
mode tcp
balance roundrobin
server redis1 192.168.1.50:6379 check inter 2000 rise 2 fall 3
server redis2 192.168.1.51:6379 check inter 2000 rise 2 fall 3
EOF
# 启动HAProxy
[root@fgedu-ha1 ~]# systemctl enable haproxy --now
Created symlink /etc/systemd/system/multi-user.target.wants/haproxy.service → /usr/lib/systemd/system/haproxy.service.
# 验证HAProxy状态
[root@fgedu-ha1 ~]# systemctl status haproxy
● haproxy.service - HAProxy Load Balancer
Loaded: loaded (/usr/lib/systemd/system/haproxy.service; enabled; preset: disabled)
Active: active (running) since Sat 2026-04-04 23:00:00 CST; 10s ago
Part04-故障切换测试
4.1 验证高可用
[root@fgedu-ha1 ~]# systemctl stop keepalived
# 查看备节点接管VIP
[root@fgedu-ha2 ~]# ip addr show eth0
2: eth0:
link/ether 00:0c:29:12:34:56 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.11/24 brd 192.168.1.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet 192.168.1.100/24 scope global secondary eth0
valid_lft forever preferred_lft forever
# 查看HAProxy监控页面
[root@fgedu-ha2 ~]# curl -s http://192.168.1.100:8404/stats | head -20
…
# 恢复主节点
[root@fgedu-ha1 ~]# systemctl start keepalived
# 验证VIP回到主节点
[root@fgedu-ha1 ~]# ip addr show eth0 | grep 192.168.1.100
inet 192.168.1.100/24 scope global secondary eth0
# 测试服务连续性
[root@client ~]# while true; do curl -s http://192.168.1.100/health; sleep 1; done
OK
OK
OK
OK
OK
# 创建高可用测试脚本
[root@fgedu-ha1 ~]# cat > /usr/local/bin/ha-test.sh << 'EOF'
#!/bin/bash
# ha-test.sh
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
VIP="192.168.1.100"
echo "=== 高可用集群测试 ==="
echo "测试时间: $(date)"
echo ""
echo "1. VIP状态检查"
ip addr show | grep -q "$VIP" && echo "VIP在本节点" || echo "VIP不在本节点"
echo ""
echo "2. Keepalived状态"
systemctl is-active keepalived
echo ""
echo "3. HAProxy状态"
systemctl is-active haproxy
echo ""
echo "4. 后端服务状态"
curl -s http://localhost:8404/stats | grep -c "UP"
echo ""
echo "5. 网络连通性测试"
ping -c 1 $VIP > /dev/null && echo “VIP可达” || echo “VIP不可达”
echo “”
echo “=== 测试完成 ===”
EOF
[root@fgedu-ha1 ~]# chmod +x /usr/local/bin/ha-test.sh
- 使用Keepalived实现VIP漂移
- 配置健康检查脚本
- 使用HAProxy实现负载均衡
- 定期进行故障切换演练
- 配置监控告警机制
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
