实验拓扑图:

Keepalived & Lvs集群搭建实验-LMLPHP

实验原理:

Keepalived 是基于 LVS ,并与 LVS 高度融合的

LVS和keepalived的关系:lvs起的是负载均衡功能,而keepalived则是高可用(热

备)的支持,两者均可以单独使用。

keeaplived可以在主从都运行良好的情况下检测主备状态,当主服务器down掉,

或者进程意外终止时,先将VIP绑定到备服务器上,然后通过指定的脚本来提升备

服务器的应用层面的角色定位。

实验环境:

RealServer1     CentOS7  192.168.81.57

RealServer2     CentOS7  192.168.81.58

K&L Master     CentOS7  192.168.81.59

K&L Master     CentOS7  192.168.81.60

$VIP 192.168.81.150

实验过程:

一、在RealServer搭建网站(为方便验证。此处使用服务器IP地址)

[root@localhost ~]# systemctl stop firewalld

[root@localhost ~]# setenforce 0

[root@localhost ~]#  yum -y install httpd

[root@localhost ~]# systemctl start httpd

[root@localhost ~]# echo '192.168.81.57'>/var/www/html/index.html

浏览器访问验证

Keepalived & Lvs集群搭建实验-LMLPHP

[root@localhost~]# ifconfig lo:0 192.168.81.150 netmask 255.255.255.255 broadcast 192.168.81.150

[root@localhost ~]# /sbin/route add -host 192.168.81.150 dev lo:0

[root@localhost ~]# echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore

[root@localhost ~]# echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce

[root@localhost ~]# echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore

[root@localhost ~]# echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce

[root@localhost ~]# sysctl –p

二、配置Keepalived & Lvs服务器

[root@localhost ~]# systemctl stop firewalld

[root@localhost ~]# setenforce 0

[root@localhost ~]# yum -y install gcc gcc-c++ make openssl* libtool-devel* popt-devel*

[root@localhost ~]# ls(上传lvs和keepalived安装包)

ipvsadm-1.26-4.el6.x86_64.rpm  keepalived-1.2.19.tar.gz  lvs.sh

----------------K & L------------------------

[root@localhost ~]# yum -y install gcc gcc-c++ make openssl* libtool-devel* popt-devel* net-tools

[root@localhost ~]# yum -y install ipvsadm-1.26-4.el6.x86_64.rpm

[root@localhost ~]# tar -xzvf keepalived-1.2.19.tar.gz

[root@localhost ~]# cd keepalived-1.2.19

[root@localhost ~]# ./configure

[root@localhost ~]# make && make install

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

------------Master----------------

vrrp_instance VI_1 {

state MASTER

interface eno16777736

virtual_router_id 88

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

192.168.81.150/24

}

}

virtual_server 192.168.81.150 80 {

delay_loop 6

lb_algo wrr

lb_kind DR

#   persistence_timeout 50

protocol TCP

real_server 192.168.81.57 80 {

weight 2

TCP_CHECK {

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

connect_port 80

}

}

real_server 192.168.81.58 80 {

weight 3

TCP_CHECK {

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

connect_port 80

}

}

}

------------Backup----------------

vrrp_instance VI_1 {

state BACKUP

interface eno16777736

virtual_router_id 88

priority 50

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

192.168.81.150/24

}

}

virtual_server 192.168.81.150 80 {

delay_loop 6

lb_algo wrr

lb_kind DR

#   persistence_timeout 50

protocol TCP

real_server 192.168.81.57 80 {

weight 2

TCP_CHECK {

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

connect_port 80

}

}

real_server 192.168.81.58 80 {

weight 3

TCP_CHECK {

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

connect_port 80

}

}

}

------------------------------------

[root@localhost keepalived-1.2.19]# keepalived -D -f /usr/local/etc/keepalived/keepalived.conf

[root@localhost keepalived-1.2.19]# ip a(VIP只出现在MASTER上)

Keepalived & Lvs集群搭建实验-LMLPHP

Keepalived & Lvs集群搭建实验-LMLPHP

三、检测结果

测试负载平衡(这里在IP地址为192.168.81.12的虚拟机进行测试.57/58出现的频率为2:3)

Keepalived & Lvs集群搭建实验-LMLPHP

关闭MASTER的KEEPALIVE,再查看IP是否漂移

Keepalived & Lvs集群搭建实验-LMLPHP

Keepalived & Lvs集群搭建实验-LMLPHP

05-26 13:19