首页 > 其他分享 >四、使用LVS+Keepalive实现web服务的负载均衡以及LB的高可用

四、使用LVS+Keepalive实现web服务的负载均衡以及LB的高可用

时间:2023-03-28 14:14:44浏览次数:43  
标签:web LB lvs LVS lo keepalived 192.168 etc conf

4.1 环境说明

主机

IP地址

备注

lb01

192.168.1.5
vip:192.168.1.3

lvs+keepalived实现负载均衡与高可用,vip用于提供外部访问

lb02

192.168.1.6

lvs+keepalived实现负载均衡与高可用

web01

192.168.1.7

配置ARP

web02

192.168.1.8

挂载nfs共享

web03

192.168.1.9

挂载nfs共享

4.2 安装配置lvs及keepalive

执行主机lb01、lb02

yum install -y ipvsadm keepalive

配置keepalive,添加访问服务以及后端服务器,Lb02需将state更改为BACKUP

cat > /etc/keepalived/keepalived.conf << EOF
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.3
    }
}
virtual_server 192.168.1.3 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 0
    protocol TCP

    real_server 192.168.1.7 80 {
        weight 1
        HTTP_GET {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
}
    real_server 192.168.1.8 80 {
        weight 1
        HTTP_GET {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
}
    real_server 192.168.1.9 80 {
        weight 1
        HTTP_GET {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
EOF

启动keepalived服务

systemctl enable keepalived
systemctl start keepalived

 

4.3 配置RS的虚拟地址及ARP响应

执行主机web01、web02、web03

4.3.1 配置虚拟地址

cat > /etc/sysconfig/network-scripts/ifcfg-lo:1 << EOF
DEVICE=lo:1
IPADDR=192.168.1.3
NETMASK=255.255.255.255
ONBOOT=yes
EOF
4.3.1 配置不进行ARP响应
cat >> /etc/sysctl.conf << EOF
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
EOF

4.4 其他

配置IP以及ARP两步骤也可写成脚本,添加到开机执行

编写脚本文件

cat > /etc/lvs.sh << EOF
#!/bin/bash
vip=192.168.1.3
ifconfig lo:1 $vip/32 up &>/dev/null
route add -host $vip lo:1 &>/dev/null
echo "1" > proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" > proc/sys/net/ipv4/conf/lo/arp_ignore
echo "1" > proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" > proc/sys/net/ipv4/conf/all/arp_ignore
EOF

执行脚本

sh /etc/lvs.sh

添加到开机执行

cat >> /etc/bashrc  << EOF
sh /etc/lvs.sh
EOF

 

标签:web,LB,lvs,LVS,lo,keepalived,192.168,etc,conf
From: https://www.cnblogs.com/isxiefeng/p/17264943.html

相关文章