Nginx集群负载(基于LVS和Keepalived)搭建详细说明以及案例
2025-03-09
30
Nginx集群负载(基于LVS和Keepalived)搭建详细说明
1. 环境准备
操作系统: CentOS 7.x
Nginx: 1.18.0
LVS: IPVS
Keepalived: 2.0.10
服务器: 3台(1台LVS主节点,1台LVS备节点,1台Nginx后端服务器)
2. 安装Nginx
在所有Nginx后端服务器上安装Nginx:
sudo yum install -y nginx sudo systemctl start nginx sudo systemctl enable nginx
3. 安装LVS和Keepalived
在LVS主节点和备节点上安装LVS和Keepalived:
sudo yum install -y ipvsadm keepalived
4. 配置Keepalived
在LVS主节点上配置/etc/keepalived/keepalived.conf
:
vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.100 } } virtual_server 192.168.1.100 80 { delay_loop 6 lb_algo rr lb_kind DR protocol TCP real_server 192.168.1.101 80 { weight 1 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 192.168.1.102 80 { weight 1 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } }
在LVS备节点上配置/etc/keepalived/keepalived.conf
,将state
改为BACKUP
,priority
改为90
。
5. 启动Keepalived
在LVS主节点和备节点上启动Keepalived:
sudo systemctl start keepalived sudo systemctl enable keepalived
6. 配置Nginx后端服务器
在Nginx后端服务器上配置ARP抑制:
echo "net.ipv4.conf.all.arp_ignore = 1" | sudo tee -a /etc/sysctl.conf echo "net.ipv4.conf.all.arp_announce = 2" | sudo tee -a /etc/sysctl.conf sudo sysctl -p
7. 验证配置
访问虚拟IP
192.168.1.100
,查看是否能够负载均衡到后端Nginx服务器。关闭LVS主节点的Keepalived,查看备节点是否接管虚拟IP。
案例
假设有三台服务器:
LVS主节点: 192.168.1.10
LVS备节点: 192.168.1.11
Nginx后端服务器1: 192.168.1.101
Nginx后端服务器2: 192.168.1.102
配置完成后,访问192.168.1.100
,请求将被负载均衡到192.168.1.101
和192.168.1.102
。如果LVS主节点故障,备节点将接管虚拟IP,确保服务高可用。
本篇文章内容来源于:Nginx集群负载(基于LVS和Keepalived)搭建详细说明以及案例
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。