构建LVS-DR+Keepalive高可用集群

时间:2023-03-08 16:37:24

------client----------主LVS----------从LVS------------WEB1-------------WEB2---------

2.2.2.250  2.2.2.10     2.2.2.20      2.2.2.30      2.2.2.40

lo:2.2.2.2    lo:2.2.2.2

一、配置LVS服务器(主-从两台)

1、配置IP

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0

[root@localhost ~]#service network restart

2、配置相应参数(主--从配置一样)

[root@localhost ~]#vim /etc/sysctl.conf

添加:

net.ipv4.conf.all.send_redirects = 0

net.ipv4.conf.default.send_redirects = 0

net.ipv4.conf.eth0.send_redirects = 0

[root@localhost ~]#sysctl -p

3、安装并配置ipvsadm (主--从配置一样)

[root@localhost ~]#rpm -ihv /mnt/Packages/ipvsadm-1.26-2.el6.x86_64.rpm

[root@localhost ~]#service ipvsadm start

[root@localhost ~]#chkconfig --add ipvsadm

[root@localhost ~]#chkconfig ipvsadm on

二、配置keepalive服务器

4、安装keepalive软件(主--从配置一样)

[root@localhost ~]# yum -y install kernel-devel openssl-devel popt-devel

[root@localhost ~]# tar -zxvf keepalived-1.2.13.tar.gz -C /usr/src/

[root@localhost ~]# cd /usr/src/keepalived-1.2.13/

[root@localhost keepalived-1.2.13]# ./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64

[root@localhost keepalived-1.2.13]# make && make install

[root@localhost ~]# /etc/init.d/keepalived start

[root@localhost ~]# netstat -anp | grep keepalived

[root@localhost ~]# chkconfig --add keepalived

[root@localhost ~]# chkconfig keepalived on

5、主服务器配置

[root@localhost ~]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf_bak

[root@localhost ~]# vim /etc/keepalived/keepalived.conf

修改:

global_defs {

router_id LVS_DEVEL_R1    //查阅说明文档得知route_id配置是为了标识当前节点,我将其设置为LVS_DEVEL_R1。

}

vrrp_instance VI_1 {      #定义虚拟路由实例,不同实例ID不同。

   state MASTER        #定义服务器在keepalived中的角色主服务器

   interface eth0          #定义进行检测的端口eth0

   virtual_router_id 1       #定义虚拟路由ID,同一个实例的主从一样。

   priority 100                #定义在虚拟路由器组的权限,越大越高

   advert_int 1             #定义检测时间间隔

authentication {              #定义认证方式密码,主从必须一样

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {        #指定虚拟IP地址

   2.2.2.2

}

}

virtual_server 2.2.2.2 80 {          #定义虚拟服务,需指定IP地址和端口,空格隔开。

   delay_loop 15      #定义RS运行情况监测时间间隔

    lb_algo rr         #定义负载调度算法

   lb_kind DR         #定义LVS的工作模式

     protocol TCP         #指定转发协议

  real_server 2.2.2.30 80 {    #定义真实服务器IP地址和端口

       weight 1      #定义RS的权重

   TCP_CHECK {    #RS server健康检查部分

     connect_port 80    #定义健康检查端口

     connect_timeout 3      #定义超出3s连接超时

    nb_get_retry 3       #定义重试次数

     delay_before_retry 4      #定义重试时间间隔

}

}

real_server 2.2.2.40 80 {

     weight 1

   TCP_CHECK {

    connect_port 80  

    connect_timeout 3

     nb_get_retry 3

    delay_before_retry 4

}

}

}

[root@localhost ~]# service keepalived restart

[root@localhost ~]# ip add show dev eth0

6、从服务器配置

[root@localhost ~]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf_bak

[root@localhost ~]# vim /etc/keepalived/keepalived.conf

修改:

global_defs {

router_id LVS_DEVEL_R2

}

vrrp_instance VI_1 {

state BACKUP

priority 99

......

---其他参数与主服务器保持一致----

}

说明:这里主LVS-DR-MASTER和LVS-DR-BACKUP之间的配置的差别就只有红色部分:HA的角色(MASTER,BACKUP)和优先级不同,还有router_id。

[root@localhost ~]# service keepalived restart

[root@localhost ~]# ip add show dev eth0

三、配置WEB节点服务器

1、配置IP地址

1)设置IP

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0

2)设置VIP

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-lo

修改:

DEVICE=lo:0

IPADDR=2.2.2.2

NETMASK=255.255.255.255

ONBOOT=yes

3)添加路由

[root@localhost ~]# route add -host 2.2.2.2 dev lo

2、调整响应参数

[root@localhost ~]# vim /etc/sysctl.conf

添加:

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

[root@localhost ~]# sysctl -p

3、配置http服务

WEB-1:

[root@localhost ~]# echo "welcome to 2.2.2.30 web server" > /var/www/html/index.html

[root@localhost ~]# service httpd restart

[root@localhost ~]# chkconfig --add httpd

[root@localhost ~]# chkconfig httpd on

WEB-2:

[root@localhost ~]# echo "welcome to 2.2.2.40 web server" > /var/www/html/index.html

[root@localhost ~]# service httpd restart

[root@localhost ~]# chkconfig --add httpd

[root@localhost ~]# chkconfig httpd on

五、负载均衡配置(主--从配置一样)

接下来就是配置LVS,就是在负载均衡器(2.2.2.10)上建一个虚拟ip,然后用ipvsadm建立转发规则:

ipvsadm -C
ipvsadm -A -t 2.2.2.2:80 -s rr
ipvsadm -a -t 2.2.2.2:80 -r 2.2.2.30 -g -w 1

ipvsadm -a -t 2.2.2.2:80 -r 2.2.2.40 -g -w 1

注意:2.2.2.30和2.2.2.40分别运行了一个Web服务器,端口都是80,在此之前一定要确保这两个Web服务器通过浏览器可以正常访问到。


如上命令执行完毕之后,可以再次运行命令:
ipvsadm

如果看到如下信息,则说明配置成功:
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  eric-app-server.local:8888 rr
  -> ubuntu-2.local:8888          Route   1      0          0         
  -> ubuntu.local:8888            Route   1      0          0

三、验证:

1)使用ping命令客户端:ping -t 2.2.2.2

断开主LVS的网卡,查看客户端的ping情况。重启主LVS的网卡,查看客户端的ping情况。

2)客户端

1、访问:http://2.2.2.2/

在主LVS上查看:[root@localhost ~]# ipvsadm -Lnc

2、断开主LVS的网卡

客户端访问:http://2.2.2.2/