Lvs+keepalived realizes load balancing and dual-machine hot standby

Source: Internet
Author: User
Tags switches

1. Notes

In the "Keepalived implementation of Tomcat dual-standby" in the main describes how to install and configure Keepalived to achieve dual-machine hot standby, the main function is: Master and backup two servers in the hot standby state, the user virtual an IP, shielding the real IP address of the bottom, Users access the current master server through virtual IP, and when the master server is down or other failures occur, backup automatically switches to master, a process that is transparent to the user.

This article mainly describes how to install IPVSADM, how to configure lvs+keepalived to achieve load balancing and dual-machine hot standby function. Function Description: When the user through the virtual IP access, should be load balanced to the primary standby server; Of course, Backup automatically switches to master when the master server is down or other failures occur.

2. Installation and Configuration

2.1 Installation Ipvsadm

First install IPVSADM, I use the version is ipvsadm-1.24-6. can go to the related website to download ipvsadm-1.24-6.src.rpm.

Install Ipvsadm below: (place ipvsadm-1.24-6.src.rpm in root directory)

[~] RPM-IVH ipvsadm-1.24-6.src.rpm [
~] cd/usr/src/redhat/sources
[SOURCES] TAR-ZXVF ipvsadm-1.24.tar.gz
[SOURCES] uname-r
2.6.18-238.el5
[SOURCES] ln-s/usr/src/kernels/2.6.18.238.el5-x86_64//usr/src/ Linux
[SOURCES] cd ipvsadm-1.24
[ipvsadm-1.24] make;

2.2 Configuration Keepalived.conf

Then configure keepalived.conf: (for specific installation please refer to "keepalived implementation of Tomcat dual-machine hot standby")

MASTER (10.10.195.53) configuration:

global_defs {router_id Lvs_devel_1} vrrp_script chk_http_port {script "/opt/tomcat.pid" Interval 5 weight 2 } vrrp_instance vi_1 {State MASTER interface eth0 virtual_router_id-Priority 1 Advert_int hentication {Auth_type pass Auth_pass 1111} track_script {Chk_http_port} virtual_ip
       Address {10.10.195.212 #VIP}} virtual_server 10.10.195.212 8080 {delay_loop 6 Lb_algo WRR Lb_kind DR Protocol TCP real_server 10.10.195.53 8080 {weight 1 tcp_ch ECK {connect_timeout 3 nb_get_retry 3 delay_befor
                E_retry 3 connect_port 8080} real_server 10.10.195.190 8080 { Weight 1 Tcp_check {connect_timeout 3 nb_get
               _retry 3         Delay_before_retry 3 Connect_port 8080}} 

BACKUP (10.10.195.190) configuration:

lobal_defs {router_id lvs_devel_2} vrrp_script chk_http_port {script '/opt/tomcat.pid ' interval 5 Weight 2} vrrp_instance vi_1 {State BACKUP interface eth0 virtual_router_id (priority)
   T 1 authentication {Auth_type pass Auth_pass 1111} track_script {Chk_http_port} virtual_ipaddress {10.10.195.212}} virtual_server 10.10.195.212 8080 {delay_loop 6 Lb_algo
                WRR lb_kind DR Protocol TCP real_server 10.10.195.53 8080 {weight 1 Tcp_check {connect_timeout 3 nb_get_retry 3 delay  _before_retry 3 connect_port 8080} real_server 10.10.195.190 8080
                        {Weight 1 Tcp_check {connect_timeout 3
         Nb_get_retry 3               Delay_before_retry 3 Connect_port 8080}} 

2.3 LVs Script Configuration

After two Web servers have installed the HTTP service (Tomcat), create files and assign permissions to configure the LVs script, which is always ARP broadcast and assigns the request package to the load balanced LVS service.

The main standby's LVS script is the same. The virtual IP (VIP) is 10.10.195.212. Here's the script code:

[~] vim/sbin/realdr.sh

#!/bin/bash vip=10.10.195.211/etc/rc.d/init.d/functions case "in Start" echo "Start LVS of Realserver"
        /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up #/sbin/route add-host $VIP Dev 1o:0 echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce E
Cho "1" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce;
        Stop) echo "Close LVS realserver"/sbin/ifconfig lo:0 down #/sbin/route del-host $VIP Dev lo:0
        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; Status) islothere= '/sbin/ifconfig lo:0 | grep $VIP | Wc-l ' isrothere= ' netstat-rn | grep "lo:0" | grep $VIP | Wc-l ' # echo $islothEre # echo $isrothere if [$islothere-eq 0] Then if [$isrothere-eq 0]
                Then echo "LVS of Realserver stoped."
                else echo "LVS of Realserver Running."
        Fi else echo "LVS of Realserver Running."
fi;;
*) echo "Usage:$0{start|stop}" exit 1;; Esac


Set the LVs script permissions and run:

[~] chmod 755/sbin/realdr.sh
[~]/sbin/realdr.sh Start

3. View

3.1 View virtual IP related information

Then install and start Keepalived:service keepalived start.

You can view (take master as an example) by using the IP addshow command:

1:lo: <LOOPBACK,UP,LOWER_UP> mtu16436 qdisc noqueue link/loopback 00:00:00:00:00:00 BRD 00:00:00:00:00:00
   inet 127.0.0.1/8 Scope host lo
    inet 10.10.195.211/32 BRD 10.10.195.211 scope Global lo:0
2:eth0: <BROADCA ST,MULTICAST,UP,LOWER_UP>MTU 1500 Qdisc pfifo_fast qlen 1000 link/ether 00:0c:29:6b:f2:a8 brd ff:ff:ff:ff:ff:ff
   inet 10.10.195.53/24 BRD 10.10.195.255 scope global eth0 inet 10.10.195.211/32
    Scope Global eth0

You can see that there are virtual IP addresses on both lo and eth0.

3.2 View Load Balancing status

In the terminal input command: Ipvsadm can be viewed.

MASTER (10.10.195.53):

IP Virtual Server version 1.2.1 (size=4096)
prot localaddress:port Scheduler Flags
 -> remoteaddress:port          Forward Weight activeconn inactconn
TCP 10.10.195.212:webcache wrr
 -> 10.10.195.190:webcache      Route   1      0
 -> shr: Webcache                local   1      0         0

BACKUP (10.10.195.190):

IP Virtual Server version 1.2.1 (size=4096)
prot localaddress:port Scheduler Flags
 -> remoteaddress:port< C16/>forward Weight activeconn inactconn
TCP 10.10.195.211:webcache wrr-> local   1      0         0
 -> 10.10.195.53:webcache       Route   1      0         11

4. Test

         If you open the browser more than once and access the Web site through virtual IP, you should load the balance to two servers. The first time you open a browser and enter http://10.10.195.212:8080/to display the contents of the 10.10.195.53 (190) service; the second time you open a browser and enter http://10.10.195.212:8080/, Displays the contents of the 10.10.195.190 (53) server.

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.