欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

LVS,DR模式+Keepalived部署

程序员文章站 2024-03-21 09:25:16
...

配置信息

LVS-MASTER 10.0.0.41 centos7
LVS-BACKUP 10.0.0.42 centos7
LVS-DR-VIP 10.0.0.100
WEB1 10.0.0.43 centos7
WEB2 10.0.0.44 centos7

注意:请忽略IP地址,以自己环境中的为主。
一、配置LVS
1.分别在master和backup上安装lvs和keepalived软件包

systemctl stop firewalld
setenforce 0
yum –y install keepalived ipvsadm

LVS-MASTER

vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id LVS_R1
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33  #对应网卡
    virtual_router_id 51
    priority 100  #权重
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.0.0.100
    }
}

virtual_server 10.0.0.100 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
!    persistence_timeout 50
    protocol TCP

    real_server 10.0.0.43 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
    real_server 10.0.0.44 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
}

LVS-BACKUP

vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id LVS_R2
}

vrrp_instance VI_1 {
    state BACKUP   
    interface ens33 
    virtual_router_id 51
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.0.0.100
    }
}

virtual_server 10.0.0.100 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
!    persistence_timeout 50
    protocol TCP

    real_server 10.0.0.43 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
    real_server 10.0.0.44 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
}

2、启动lvs

service keepalived restart

LVS,DR模式+Keepalived部署
备keepalived配置文件配置信息:备用服务器可以有多台,配置时候基本相同只要修改路由器名称,热备状态,优先级就可以了。
二、.节点WEB1,WEB2服务器配置
1、使用DR模式的时候,节点服务器也需要配置vip地址,并且调整内核的ARP响应参数以阻止更新VIP的MAC地址,避免发生冲突。

cd /etc/sysconfig/network-scripts/
cp ifcfg-lo  ifcfg-lo:0
vi ifcfg-lo:0

LVS,DR模式+Keepalived部署

ifup lo:0
ifconfig lo:0

LVS,DR模式+Keepalived部署
添加VIP本地访问路由(将访问VIP的数据限制正在本地,避免通信紊乱)

 vi /etc/rc.local

LVS,DR模式+Keepalived部署
执行

route add -host 10.0.0.100 dev lo:0

2、修改内核参数。

vi /etc/sysctl.conf

(最下面添加)

echo 'net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2' >>/etc/sysctl.conf

注意:
arp_announce为2时:只向该网卡回应与该网段匹配的ARP报文。
arp_ignore为1:只响应目的IP地址为接收网卡上的本地地址的arp请求
其主要是实现禁止响应ARP的请求。
3、启动内核参数

sysctl  -p

4、安装httpd,创建网页测试lvs集群
WEB1

yum -y install  httpd
echo "WEB1" >/var/www/html/index.html
systemctl start httpd	

WEB2

yum -y install httpd
echo "WEB2" >/var/www/html/index.html
systemctl start httpd	

测试:

ipvsadm  -lnc

LVS,DR模式+Keepalived部署
LVS,DR模式+Keepalived部署
在没有vip的服务curl一下,浏览器有缓存,就算开启无痕也是一样,要命令行curl

LVS,DR模式+Keepalived部署

相关标签: keepalived