Keepalived+Nginx+Tomcat 高可用负载均衡

一、4台机器角色

机器IP角色服务
192.168.52.138Nginx1 + Keepalived MasterNginx、Keepalived主
192.168.52.139Nginx2 + Keepalived BackupNginx、Keepalived备
192.168.52.135Tomcat1后端应用
192.168.52.136Tomcat2后端应用

VIP:192.168.52.100(对外入口)


二、所有机器通用初始化(4台都执行)

# 关闭防火墙、SELinuxsystemctl stop firewalldsystemctl disable firewalldsetenforce 0sed -i 's/^SELINUX=.*/SELINUX=disabled/'/etc/selinux/config# 安装基础依赖dnf install -y wget tar gcc make java-1.8.0-openjdk-devel

三、Tomcat1(135)+ Tomcat2(136)配置(两台一样)

1. 安装 Tomcat 9

cd /usr/localwget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.85/bin/apache-tomcat-9.0.85.tar.gztar -zxvf apache-tomcat-9.0.85.tar.gzmv apache-tomcat-9.0.85 tomcat

2. 写测试页面(区分两台)

# Tomcat1(135)执行echo "<h1>Tomcat-135</h1>">/usr/local/tomcat/webapps/ROOT/index.jsp# Tomcat2(136)执行echo "<h1>Tomcat-136</h1>">/usr/local/tomcat/webapps/ROOT/index.jsp

3. 启动 Tomcat

/usr/local/tomcat/bin/startup.sh

4. 验证(访问自己IP:8080)

curl localhost:8080

四、Nginx1(138)+ Nginx2(139)安装(两台一样)

dnf install -y nginx

配置 Nginx 反向代理 到 Tomcat

cat >/etc/nginx/nginx.conf <<EOFuser nginx;worker_processes auto;error_log /var/log/nginx/error.log;pid /run/nginx.pid;events {    worker_connections 1024;}http {    log_format  main  '\$remote_addr - \$remote_user [\$time_local] "\$request" ''\$status \$body_bytes_sent "\$http_referer" ''" \$http_user_agent" "\$http_x_forwarded_for"';access_log  /var/log/nginx/access.log  main;    upstream tomcat_servers {        server 192.168.52.135:8080;        server 192.168.52.136:8080;    }    server {        listen 80;        server_name localhost;location/ {            proxy_pass http://tomcat_servers;            proxy_set_header Host \$host;            proxy_set_header X-Real-IP \$remote_addr;        }    }}EOFAI写代码

启动 Nginx

systemctl enable --now nginx

测试 Nginx 代理是否正常

curl localhost# 会交替出现 Tomcat-135/ Tomcat-136

五、Keepalived 配置(核心:138主 + 139备)

1. 两台都安装 Keepalived

dnf install -y keepalived

2. 配置 138(Master)

cat >/etc/keepalived/keepalived.conf <<EOFglobal_defs {    router_id NGINX_MASTER}# 监控 Nginx 是否存活vrrp_script chk_nginx {    script "killall -0 nginx"    interval 2    weight -20}vrrp_instance VI_1 {    state MASTERinterface ens160  # 改成你实际网卡名(ip addr 看)    virtual_router_id 52    priority 150    advert_int 1    authentication {        auth_type PASS        auth_pass 123456    }    virtual_ipaddress {192.168.52.100/24    }    track_script {        chk_nginx    }}EOFAI写代码

3. 配置 139(Backup)

cat >/etc/keepalived/keepalived.conf <<EOFglobal_defs {    router_id NGINX_BACKUP}vrrp_script chk_nginx {    script "killall -0 nginx"    interval 2    weight -20}vrrp_instance VI_1 {    state BACKUPinterface ens160  # 改成你实际网卡名    virtual_router_id 52    priority 100    advert_int 1    authentication {        auth_type PASS        auth_pass 123456    }    virtual_ipaddress {192.168.52.100/24    }    track_script {        chk_nginx    }}EOFAI写代码

4. 两台都启动 Keepalived

systemctl enable --now keepalived

六、最终测试(任意机器执行)

curl 192.168.52.100

✅ 不断刷新会交替显示 Tomcat-135 / 136
✅ 停掉 138 的 Nginx 或 Keepalived,VIP 自动飘到 139
✅ 停掉任意一台 Tomcat,Nginx 自动跳过故障节点


七、你可能需要的排查命令

# 看VIP在哪ip addr# 看Keepalived日志tail -f /var/log/messages# 抓VRRP包tcpdump -i ens160 host 224.0.0.1

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注