Combat lvs+keepalived for load balancing and high availability

Source: Internet
Author: User
Tags install openssl

1. Software Introduction

LVS is a shorthand for Linux virtual server, that is, a virtual servers cluster system, which can realize simple load balancing under the Linux platform.

LVS Load Balancer has 4 load balancing methods

A.dr mode

B.nat mode

C.tunnel mode

D.full NAT Mode

8 Scheduling algorithms of LVS load Balancing

(RR,WRR,LC,WLC,LBLC,LBLCR,DH,SH)



The principle of each mode is not described here, this paper uses the DR Mode, RR scheduling algorithm.

Keepalived is run on the LVS, its main function is to realize the real machine fault isolation and load balancer between the failure switch, improve the availability of the system.


2. Actual installation


Environment configuration

[Email protected] ~]# cat/etc/redhat-release

CentOS Release 6.8 (Final)

[Email protected] ~]# uname-r

2.6.32-642.6.1.el6.x86_64

Software

Ipvsadm-1.26.tar.gz keepalived-1.1.19.tar.gz


List of information

Server name IP

LVS (Lvs-master) 10.89.3.102
keepalived Preparation (Lvs-backup) 10.89.3.103
Lamp do Web1 (real_server)
10.89.3.100
Lnmp do Web2 (real_server) 10.89.3.101
LVS bound VIP (VIP) 10.89.3.168


Lvs+keepalived Installation and configuration-2 computers (lvs,keepalived) are installed.


Installing the LVS Software

Ln-s/usr/src/kernels/2.6.32-642.6.1.el6.x86_64//usr/src/linux

Yum install libnl* popt*-y

Tar zxf ipvsadm-1.26.tar.gz

CD ipvsadm-1.26

Make

Make install


[Email protected] ipvsadm-1.26]# modprobe ip_vs #把ip_vs模块加入内核

[Email protected] ipvsadm-1.26]# Lsmod|grep ip_vs #查看是否安装

Ip_vs 126897 0

LIBCRC32C 1246 1 Ip_vs

IPv6 336282 265 Ip_vs

#出现上面的结果, indicates installation OK


Installing keepalived Software


Ln-s/usr/src/kernels/2.6.32-642.6.1.el6.x86_64/usr/src/linux

Yum Install OpenSSL openssl-devel-y

Tar XF keepalived-1.1.19.tar.gz

CD keepalived-1.1.19

./configure

Make

Make install


Configuring the Keepalived specification to start


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

/bin/cp/usr/local/etc/sysconfig/keepalived/etc/sysconfig/

Mkdir/etc/keepalived-p

/bin/cp/usr/local/etc/keepalived/keepalived.conf/etc/keepalived/

/bin/cp/usr/local/sbin/keepalived/usr/sbin/

/etc/init.d/keepalived start

Ps-ef|grep Keep

Root 6342 1 0 17:42? 00:00:00 keepalived-d

Root 6344 6342 0 17:42? 00:00:00 keepalived-d

Root 6345 6342 0 17:42? 00:00:00 keepalived-d

Root 6347 3490 0 17:42 pts/0 00:00:00 grep keep

#出现 3 keepalived-d, indicating an installation OK


3. Configure keepadlived

The configuration file for Lvs-master is as follows:


[Email protected] ~]# cat/etc/keepalived/keepalived.conf

! Configuration File for Keepalived


Global_defs {

Notification_email {

[Email protected]

}

Notification_email_from [email protected]

Smtp_server 127.0.0.1

Smtp_connect_timeout 30

router_id lvs_7

}


Vrrp_instance Vi_1 {

State MASTER

Interface eth0

VIRTUAL_ROUTER_ID 55

Priority 150

Advert_int 1

Authentication {

Auth_type PASS

Auth_pass 1111

}

virtual_ipaddress {

10.89.3.168/24

}

}


Virtual_server 10.89.3.168 80 {

Delay_loop 6

Lb_algo WRR

Lb_kind DR

Nat_mask 255.255.255.0

Persistence_timeout 300

Protocol TCP


Real_server 10.89.3.100 80 {

Weight 1

Tcp_check {

Connect_timeout 8

Nb_get_retry 3

Delay_before_retry 3

Connect_port 80

}


}

Real_server 10.89.3.101 80 {

Weight 1

Tcp_check {

Connect_timeout 8

Nb_get_retry 3

Delay_before_retry 3

Connect_port 80

}


}


}


The configuration file for Lvs-backup is as follows:


[Email protected] ~]# cat/etc/keepalived/keepalived.conf

! Configuration File for Keepalived


Global_defs {

Notification_email {

[Email protected]

}

Notification_email_from [email protected]

Smtp_server 127.0.0.1

Smtp_connect_timeout 30

router_id lvs_2

}


Vrrp_instance Vi_1 {

State BACKUP

Interface eth0

VIRTUAL_ROUTER_ID 55

Priority 100

Advert_int 1

Authentication {

Auth_type PASS

Auth_pass 1111

}

virtual_ipaddress {

10.89.3.168/24

}

}


Virtual_server 10.89.3.168 80 {

Delay_loop 6

Lb_algo WRR

Lb_kind DR

Nat_mask 255.255.255.0

Persistence_timeout 300

Protocol TCP


Real_server 10.89.3.100 80 {

Weight 1

Tcp_check {

Connect_timeout 8

Nb_get_retry 3

Delay_before_retry 3

Connect_port 80

}


}

Real_server 10.89.3.101 80 {

Weight 1

Tcp_check {

Connect_timeout 8

Nb_get_retry 3

Delay_before_retry 3

Connect_port 80

}


}


}


Real_server configuration, 2 client scripts are the same!


[[Email protected]]# cat/server/scripts/ipvs_client.sh

#!/bin/bash

# Description:config Realserver lo and apply Noarp

sns_vip=10.89.3.168

. /etc/rc.d/init.d/functions

Case "$" in

Start

/sbin/ifconfig lo:0 $SNS _vip netmask 255.255.255.255 broadcast $SNS _VIP

/sbin/route add-host $SNS _vip Dev lo:0

echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore

echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce

echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore

echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce

Sysctl-p >/dev/null 2>&1

echo "Realserver Start OK"

;;

Stop

Ifconfig lo:0 Down

Route del $SNS _VIP >/dev/null 2>&1

echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore

echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce

echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore

echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce

echo "Realserver stoped"

;;

*)

echo "Usage: $ {start|stop}"

Exit 1

Esac

Exit 0


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

4. Real-Combat testing

4.1 Test VIP Drift

#使用命令 IP Add View VIP

[[email protected] ~]#/etc/init.d/keepalived start

Starting keepalived: [OK]

[[Email protected] ~]# IP add

1:lo: <LOOPBACK,UP,LOWER_UP> MTU 65536 qdisc noqueue State UNKNOWN

Link/loopback 00:00:00:00:00:00 BRD 00:00:00:00:00:00

inet 127.0.0.1/8 Scope host Lo

INET6:: 1/128 Scope Host

Valid_lft Forever Preferred_lft Forever

2:eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> MTU Qdisc pfifo_fast State up Qlen 1000

Link/ether 00:0C:29:4E:CA:A4 BRD FF:FF:FF:FF:FF:FF

inet 10.89.3.102/24 BRD 10.89.3.255 Scope Global eth0

inet 10.89.3.168/24 scope Global secondary eth0

Inet6 FE80::20C:29FF:FE4E:CAA4/64 Scope link

Valid_lft Forever Preferred_lft Forever

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

[[Email protected] ~]# IP add

1:lo: <LOOPBACK,UP,LOWER_UP> MTU 65536 qdisc noqueue State UNKNOWN

Link/loopback 00:00:00:00:00:00 BRD 00:00:00:00:00:00

inet 127.0.0.1/8 Scope host Lo

INET6:: 1/128 Scope Host

Valid_lft Forever Preferred_lft Forever

2:eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> MTU Qdisc pfifo_fast State up Qlen 1000

Link/ether 00:0c:29:3f:7c:0b BRD FF:FF:FF:FF:FF:FF

inet 10.89.3.103/24 BRD 10.89.3.255 Scope Global eth0

Inet6 FE80::20C:29FF:FE3F:7C0B/64 Scope link

Valid_lft Forever Preferred_lft Forever

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

#观察发现, at this time VIP on the LVS this server, now put this server keepalived service stop

[[email protected] ~]#/etc/init.d/keepalived stop

stopping keepalived: [OK]

[[Email protected] ~]# IP add

1:lo: <LOOPBACK,UP,LOWER_UP> MTU 65536 qdisc noqueue State UNKNOWN

Link/loopback 00:00:00:00:00:00 BRD 00:00:00:00:00:00

inet 127.0.0.1/8 Scope host Lo

INET6:: 1/128 Scope Host

Valid_lft Forever Preferred_lft Forever

2:eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> MTU Qdisc pfifo_fast State up Qlen 1000

Link/ether 00:0C:29:4E:CA:A4 BRD FF:FF:FF:FF:FF:FF

inet 10.89.3.102/24 BRD 10.89.3.255 Scope Global eth0

Inet6 FE80::20C:29FF:FE4E:CAA4/64 Scope link

Valid_lft Forever Preferred_lft Forever


#发现VIP已经漂移到另外一台上了, let's go check it out.

[[Email protected] ~]# IP add

1:lo: <LOOPBACK,UP,LOWER_UP> MTU 65536 qdisc noqueue State UNKNOWN

Link/loopback 00:00:00:00:00:00 BRD 00:00:00:00:00:00

inet 127.0.0.1/8 Scope host Lo

INET6:: 1/128 Scope Host

Valid_lft Forever Preferred_lft Forever

2:eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> MTU Qdisc pfifo_fast State up Qlen 1000

Link/ether 00:0c:29:3f:7c:0b BRD FF:FF:FF:FF:FF:FF

inet 10.89.3.103/24 BRD 10.89.3.255 Scope Global eth0

inet 10.89.3.168/24 scope Global secondary eth0

Inet6 FE80::20C:29FF:FE3F:7C0B/64 Scope link

Valid_lft Forever Preferred_lft Forever

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

4.2 Testing Web Services

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/89/6E/wKiom1gS0YvAMom1AAAZUgsZVIk571.jpg-wh_500x0-wm_3 -wmp_4-s_416387194.jpg "style=" Float:none; "title=" lamp.jpg "alt=" Wkiom1gs0yvamom1aaazugszvik571.jpg-wh_50 "/>

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/89/6C/wKioL1gS0YuhBFG1AAArvSELKE0158.jpg-wh_500x0-wm_3 -wmp_4-s_2050596028.jpg "style=" Float:none; "title=" lnmp.jpg "alt=" Wkiol1gs0yuhbfg1aaarvselke0158.jpg-wh_50 "/>


4.3 Viewing connection conditions

[Email protected] ~]# IPVSADM-LNC

IPVS Connection Entries

Pro Expire State source virtual destination

TCP 01:01 fin_wait 10.89.1.158:62899 10.89.3.168:80 10.89.3.101:80

TCP 00:56 fin_wait 10.89.3.1:58996 10.89.3.168:80 10.89.3.100:80

TCP 00:56 fin_wait 10.89.3.1:58997 10.89.3.168:80 10.89.3.100:80

TCP 02:06 NONE 10.89.3.1:0 10.89.3.168:80 10.89.3.100:80

TCP 03:44 NONE 10.89.1.158:0 10.89.3.168:80 10.89.3.101:80

TCP 01:51 fin_wait 10.89.1.158:62603 10.89.3.168:80 10.89.3.101:80


Summary: lvs+keepalived can be load balanced and highly available, so we verify that the configuration is over!














This article is from the "Knowledge Change Destiny" blog, please be sure to keep this source http://ahtornado.blog.51cto.com/4826737/1866716

Combat lvs+keepalived for load balancing and high availability

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.