龙空技术网

keepalive(单机)+lvs项目(DR模式)

运维实战课程 535

前言:

此时朋友们对“lvs轮询模式”都比较注意,朋友们都想要分析一些“lvs轮询模式”的相关资讯。那么小编也在网络上搜集了一些对于“lvs轮询模式””的相关内容,希望各位老铁们能喜欢,咱们一起来学习一下吧!

keepalive(单机)+lvs项目(DR模式)(cen6.6和red6.5)

如果对运维课程感兴趣,可以在b站上搜索我的账号: 运维实战课程,可以关注我,学习更多免费的运维实战技术视频

在服务器192.168.231.128(nat公网)上安装lvs(yum安装)

[root@localhost ~]# ifconfig eth0 |head -2

eth0 Link encap:Ethernet HWaddr 00:0C:29:E6:85:D2

inet addr:192.168.231.128 Bcast:192.168.231.255 Mask:255.255.255.0

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

[root@localhost ~]# /etc/init.d/ipvsadm status

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

-> RemoteAddress:Port Forward Weight ActiveConn InActConn

[root@localhost ~]# ipvsadm -Ln

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

-> RemoteAddress:Port Forward Weight ActiveConn InActConn

在服务器192.168.231.128(nat公网)安装keepalive部署虚拟IP192.168.231.5且配后端web服务器

[root@localhost ~]# yum -y install gcc gcc-c++

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

源码安装keepalive

[root@localhost ~]# mkdir /keepalive

[root@localhost ~]# ls /keepalive/

keepalived-1.2.19.tar.gz

[root@localhost ~]# cd /keepalive/

[root@localhost keepalive]# tar -zxf keepalived-1.2.19.tar.gz

[root@localhost keepalive]# ls

keepalived-1.2.19 keepalived-1.2.19.tar.gz

[root@localhost keepalive]# cd keepalived-1.2.19

[root@localhost keepalived-1.2.19]# ls

AUTHOR ChangeLog configure.in COPYING genhash install-sh keepalived.spec.in Makefile.in TODO

bin configure CONTRIBUTORS doc INSTALL keepalived lib README VERSION

[root@localhost keepalived-1.2.19]# ./configure

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

查看安装keepalive后默认放置的位置:(也可不查看)

[root@localhost keepalived-1.2.19]# ls /usr/local/sbin/keepalived

/usr/local/sbin/keepalived

[root@localhost keepalived-1.2.19]# ls /usr/local/etc/keepalived/

keepalived.conf samples

[root@localhost keepalived-1.2.19]# ls /usr/local/etc/rc.d/init.d/keepalived

/usr/local/etc/rc.d/init.d/keepalived

[root@localhost keepalived-1.2.19]# ls /usr/local/etc/sysconfig/keepalived

/usr/local/etc/sysconfig/keepalived

做软链接:

[root@localhost keepalived-1.2.19]# ln -s /usr/local/sbin/keepalived /usr/sbin/

[root@localhost keepalived-1.2.19]# ln -s /usr/local/etc/keepalived/ /etc/

[root@localhost keepalived-1.2.19]# ln -s /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/

[root@localhost keepalived-1.2.19]# ln -s /usr/local/etc/sysconfig/keepalived /etc/sysconfig/

编辑keepalive配置文件,vim /etc/keepalive/keepalive.conf

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

global_defs {

………

router_id LVS_1 #路由id编号,写个就行,两个不要重复,默认: router_id LVS_DEVEL

}

vrrp_instance VI_1 {

state MASTER #默认为主,此处只有一个,默认即可

interface eth0

virtual_router_id 51 #虚拟路由ID,同一组的两个调度器编号应一致,此处只有一个,无所谓

priority 100 #优先级设置,此处只有一个,无所谓

advert_int 1

authentication {

auth_type PASS

auth_pass 1234 #认证密码(默认1111),同一组的两个调度器应一致,此处只有一个,无所谓

}

virtual_ipaddress {

192.168.231.5 #设置部署的虚拟IP

}

}

#下面是集群配置,修改成如下内容即可,可将配置文件下面的原内容都删除,修改成如下即可:

virtual_server 192.168.231.5 80 {

delay_loop 6 #延迟时间

lb_algo rr #轮询

lb_kind DR #工作模式,也可是nat模式

nat_mask 255.255.255.0

# persistence_timeout 50 #打开后,会访问某台固定的服务器

protocol TCP

real_server 192.168.231.129 80 {

weight 2 #设置权重

TCP_CHECK { #监控检查

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

real_server 192.168.231.130 80 {

weight 2

TCP_CHECK {

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

}

………#下面的全部删除(也是配置集群的,防止影响)

wq

[root@localhost keepalived-1.2.19]# /etc/init.d/keepalived restart

[root@localhost keepalived-1.2.19]# cd

[root@localhost ~]# ps -ef |grep keepalive

有进程

[root@localhost ~]# ip addr |grep 192.168.231.5

inet 192.168.231.5/32 scope global eth0

inet 192.168.231.5/24 brd 192.168.231.255 scope global secondary eth0:0

[root@localhost ~]# ipvsadm -Ln

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

-> RemoteAddress:Port Forward Weight ActiveConn InActConn

TCP 192.168.231.5:80 rr

-> 192.168.231.129:80 Route 2 0 0

-> 192.168.231.130:80 Route 2 0 0

后端两web配置:

1)web1机器(192.168.231.129(nat公网)配置:

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

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

[root@localhost ~]# /etc/init.d/httpd restart

[root@localhost ~]# vim /etc/sysctl.conf #防止arp冲突配置

…….最后添加下面内容:

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

[root@localhost ~]# sysctl -p

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-lo:0 #配置集群的vip(虚拟IP)

IPADDR=192.168.231.5

NETMASK=255.255.255.255 #注意是4个255

ONBOOT=yes

[root@localhost ~]# /etc/init.d/network restart

[root@localhost ~]# ifconfig eth0 |head -2

eth0 Link encap:Ethernet HWaddr 00:0C:29:06:81:24

inet addr:192.168.231.129 Bcast:192.168.231.255 Mask:255.255.255.0

[root@localhost ~]# ifconfig lo:0 |head -2

lo:0 Link encap:Local Loopback

inet addr:192.168.231.5 Mask:255.255.255.255

2)web2机器(192.168.231.130(nat公网))配置:

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

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

[root@localhost ~]# /etc/init.d/httpd restart

[root@localhost ~]# vim /etc/sysctl.conf #防止arp冲突配置

…….最后添加下面内容:

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

[root@localhost ~]# sysctl -p

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-lo:0 #配置集群的vip(虚拟IP)

IPADDR=192.168.231.5

NETMASK=255.255.255.255 #注意是4个255

ONBOOT=yes

[root@localhost ~]# /etc/init.d/network restart

[root@localhost ~]# ifconfig eth0 |head -2

eth0 Link encap:Ethernet HWaddr 00:0C:29:4C:CF:B4

inet addr:192.168.231.130 Bcast:192.168.231.255 Mask:255.255.255.0

[root@localhost ~]# ifconfig lo:0 |head -2

lo:0 Link encap:Local Loopback

inet addr:192.168.231.5 Mask:255.255.255.255

客户端192.168.231.131(nat公网)访问调度器:(轮询)

[root@localhost ~]# curl

web2

[root@localhost ~]# curl

web1

[root@localhost ~]# curl

web2

[root@localhost ~]# curl

web1

[root@localhost ~]# curl

web2

[root@localhost ~]# curl

web1

测试lvs的健康检查机制:当web2的httpd服务停止,则会被集群剔除该web2

web2机器(192.168.231.130)的httpd服务停止:

[root@localhost ~]# /etc/init.d/httpd stop

调度器lvs上(192.168.231.128)查看集群:

[root@localhost ~]# ipvsadm -Ln

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

-> RemoteAddress:Port Forward Weight ActiveConn InActConn

TCP 192.168.231.5:80 rr

-> 192.168.231.129:80 Route 2 0 0

客户端(192.168.231.131)访问调度器,只有web1在工作

[root@localhost ~]# curl

web1

[root@localhost ~]# curl

web1 当web2恢复,会重新被加入集群,不再演示

如果对运维课程感兴趣,可以在b站上搜索我的账号: 运维实战课程,可以关注我,学习更多免费的运维实战技术视频

标签: #lvs轮询模式