LVS + keepalived for high-availability clusters

Source: Internet
Author: User

Http://linux.chinaunix.net/techdoc/net/2009/07/21/1125256.shtml

# Modprobe ip_vs

LVS + keepalived for high-availability clusters
 
Source: chinaunix blog Date: 2009.07.21 (comments in total) I want to comment
 
Operating System Platform: centos5.2
Software: LVS + keepalived

LVS + keepalived Introduction

LVS

LVS is short for Linux virtual server, which is a virtual server cluster system. This project was established by Dr. Zhang Wenyu in May 1998 and is one of the earliest free software projects in China. Currently, three IP Server Load balancer technologies are available (Vs/NAT, VS/TUN, and VS/DR );
Ten scheduling algorithms (RRR | WRR | LC | wlc | lblc | lblcr | DH | sh | sed | NQ ).
Keepalvied
Keepalived is mainly used for RealServer health check and Failover implementation between loadbalance host and backup host.



IP configuration information:
  • LVS-DR-master 192.168.20.6
  • LVS-DR-BACKUP 192.168.20.7
  • LVS-DR-VIP 192.168.2.170
  • WEB1-Realserver 192.168.2.171
  • WEB2-Realserver 192.168.2.172
  • Gateway 192.168.2.253
    Install the LVS and keepalvied packages

    1. Download related software packages
    # Mkdir/usr/local/src/LVS
    # Cd/usr/local/src/LVS
    # Wget
    Http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
    # Wget
    Http://www.keepalived.org/software/keepalived-1.1.15.tar.gz

    2. Install LVS and keepalived
  • # Lsmod | grep ip_vs
  • # Uname-R
  • 2.6.18-53. el5pae
  • # Ln-S/usr/src/kernels/2.6.18-53. el5PAE-i686 // usr/src/Linux
  • # Tar zxvf ipvsadm-1.24.tar.gz
  • # Cd ipvsadm-1.24
  • # Make & make install
  • # Find/-name ipvsadm # view the location of ipvsadm
  • # Tar zxvf keepalived-1.1.15.tar.gz
  • # Cd keepalived-1.1.15
  • #./Configure & make install
  • # Find/-name keepalived # view the location of keepalived
  • # Cp/usr/local/etc/rc. d/init. d/keepalived/etc/rc. d/init. d/
  • # Cp/usr/local/etc/sysconfig/keepalived/etc/sysconfig/
  • # Mkdir/etc/keepalived
  • # Cp/usr/local/etc/keepalived. CONF/etc/keepalived/
  • # Cp/usr/local/sbin/keepalived/usr/sbin/
  • # Service keepalived START | stop # Make the system startup service easy to manage.
    4. Configure LVS for Load Balancing

    1. LVS-DR, configure LVS script for Load Balancing

    VI/usr/local/sbin/lvs-dr.sh

    #! /Bin/bash
    # Description: Start LVS of directorserver
    # Written by: netseek
    Http://www.linuxtone.org
    GW = 192.168.2.253

    # Website Director VIP.
    Web_vip = 192.168.2.170
    Web_rip1 = 192.168.2.171
    Web_rip2 = 192.168.2.172
    ./Etc/rc. d/init. d/functions

    Logger $0 called with $1

    Case "$1" in

    Start)
    # Clear all iptables rules.
    /Sbin/iptables-F
    # Reset iptables counters.
    /Sbin/iptables-z
    # Clear all rules SADM rules/services.
    /Sbin/ipvsadm-C

    # Set lvs vip for Dr
    /Sbin/ipvsadm -- set 30 5 60
    /Sbin/ifconfig eth0: 0 $ web_vip broadcast $ web_vip netmask 255.255.255 up
    /Sbin/route add-host $ web_vip Dev eth0: 0
    /Sbin/ipvsadm-a-t $ web_vip: 80-s WRR-P 3
    /Sbin/ipvsadm-a-t $ web_vip: 80-r $ web_rip1: 80-g-W 1
    /Sbin/ipvsadm-a-t $ web_vip: 80-r $ web_rip2: 80-g-W 1
    Touch/var/lock/subsys/ipvsadm>/dev/null 2> & 1

    # Set ARP
    /Sbin/arping-I eth0-C 5-S $ web_vip $ Gw>/dev/null 2> & 1
    ;;
    Stop)
    /Sbin/ipvsadm-C
    /Sbin/ipvsadm-z
    Ifconfig eth0: 0 down
    Route del $ web_vip>/dev/null 2> & 1
    Rm-RF/var/lock/subsys/ipvsadm>/dev/null 2> & 1
    /Sbin/arping-I eth0-C 5-S $ web_vip $ GW
    Echo "ipvsadm stoped"
    ;;

    Status)

    If [! -E/var/lock/subsys/ipvsadm]; then
    Echo "ipvsadm is stoped"
    Exit 1
    Else
    Ipvsadm-ln
    Echo "...... wait SADM is OK ."
    Fi
    ;;

    *)
    Echo "Usage: $0 {START | stop | status }"
    Exit 1
    Esac

    Exit 0
    2. Configure the RealServer script.

    #! /Bin/bash
    # Written by netseek
    # Description: config RealServer Lo and apply noarp
    Web_vip = 192.168.2.170

    ./Etc/rc. d/init. d/functions

    Case "$1" in
    Start)
    Ifconfig lo: 0 $ web_vip netmask 255.255.255.255 broadcast $ web_vip
    /Sbin/route add-host $ web_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 $ web_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"
    ;;
    Status)
    # Status of LVS-DR Real Server.
    Islothere = '/sbin/ifconfig lo: 0 | grep $ web_vip'
    Isrothere = 'netstat-Rn | grep "Lo: 0" | grep $ web_vip'
    If [! "$ Islothere"-o! "Isrothere"]; then
    # Either the route or the Lo: 0 device
    # Not found.
    Echo "LVS-DR Real Server stopped ."
    Else
    Echo "LVS-DR running ."
    Fi
    ;;
    *)
    # Invalid entry.
    Echo "$0: Usage: $0 {START | status | stop }"
    Exit 1
    ;;
    Esac
    Exit 0

    Attach/etc/sysctl. conf on the RealServer:

    # Kernel sysctl configuration file for Red Hat Linux
    #
    # For binary values, 0 is disabled, 1 is enabled. See sysctl (8) and
    # Sysctl. conf (5) for more details.
    # Controls IP packet forwarding
    Net. ipv4.ip _ forward = 1
    # Controls source route verification
    Net. ipv4.conf. Default. rp_filter = 1
    # Do not accept Source Routing
    Net. ipv4.conf. Default. accept_source_route = 0
    # Controls the system request debugging functionality of the kernel
    Kernel. sysrq = 0
    # Controls whether core dumps will append the PID to the core filename.
    # Useful for debugging multi-threaded applications.
    Kernel. core_uses_pid = 1
    Net. ipv4.conf. Lo. arp_ignore = 1
    Net. ipv4.conf. Lo. arp_announce = 2
    Net. ipv4.conf. All. arp_ignore = 1
    Net. ipv4.conf. All. arp_announce = 2
    Or use secondary IP address.
    # Vi/etc/sysctl. conf

    Add the following content as shown above:

    Net. ipv4.conf. Lo. arp_ignore = 1
    Net. ipv4.conf. Lo. arp_announce = 2
    Net. ipv4.conf. All. arp_ignore = 1
    Net. ipv4.conf. All. arp_announce = 2
    # Sysctl-P
    # Ip addr add 61.164.122.8/32 Dev Lo
    # IP add list to check whether it is bound
    3. Start the LVS-Dr script and the RealServer startup. On the DR, you can view the current LVS status:
    # Watch ipvsadm-ln

    5. Use keepalvied to achieve load balancing and high availability

    1. Configure keepalived. conf on the master server Load balancer.
    # Vi/etc/keepalived. conf (Master Scheduler)
    ! Configuration file for keepalived
    Global_defs {
    Notification_email {

    [Email protected]

    [Email protected]

    [Email protected]
    }
    Notification_email_from
    [Email protected]
    Smtp_server 127.0.0.1
    Smtp_connect_timeout 30
    Router_id lvs_devel
    }
    Vrrp_instance vi_1 {
    State master
    Interface eth0
    Virtual_router_id 51
    Priority100
    Advert_int 1
    Authentication {
    Auth_type pass
    Auth_pass 1111
    }
    Virtual_ipaddress {
    192.168.2.170
    }
    }
    Virtual_server 192.168.2.170 80 {
    Delay_loop 6
    Lb_algo WRR
    Lb_kind Dr
    Persistence_timeout 60
    Protocol TCP

    Real_server 192.168.2.171 80 {
    Weight 3
    Tcp_check {
    Connect_timeout 10
    Nb_get_retry 3
    Delay_before_retry 3
    Connect_port 80
    }
    }
    Real_server 192.168.2.172 80 {
    Weight 3
    Tcp_check {
    Connect_timeout 10
    Nb_get_retry 3
    Delay_before_retry 3
    Connect_port 80
    }
    }
    }

    On the slave Scheduler:
    # Vi/etc/keepalived. conf (Backup Scheduler)
    ! Configuration file for keepalived
    Global_defs {
    Notification_email {

    [Email protected]

    [Email protected]

    [Email protected]
    }
    Notification_email_from
    [Email protected]
    Smtp_server 127.0.0.1
    Smtp_connect_timeout 30
    Router_id lvs_devel
    }
    Vrrp_instance vi_1 {
    State backup
    Interface eth0
    Virtual_router_id 51
    Priority 99
    Advert_int 1
    Authentication {
    Auth_type pass
    Auth_pass 1111
    }
    Virtual_ipaddress {
    192.168.2.170
    }
    }
    Virtual_server 192.168.2.170 80 {
    Delay_loop 6
    Lb_algo WRR
    Lb_kind Dr
    Persistence_timeout 60
    Protocol TCP

    Real_server 192.168.2.171 80 {
    Weight 3
    Tcp_check {
    Connect_timeout 10
    Nb_get_retry 3
    Delay_before_retry 3
    Connect_port 80
    }
    }
    Real_server 192.168.2.172 80 {
    Weight 3
    Tcp_check {
    Connect_timeout 10
    Nb_get_retry 3
    Delay_before_retry 3
    Connect_port 80
    }
    }
    }

    2. Configure the backup server as above. Install LVS and then install keepalived. Then, configure/etc/keepalived. conf. You only need to change the red part.
    3. VI/etc/rc. Local
    #/Usr/local/sbin/lvs-dr.sh to annotate the lvs-dr.sh script.
    #/Usr/local/sbin/lvs-dr.sh Stop LVS-Dr script
    #/Etc/init. d/keepalived start the keepalived service, and keepalived can be configured with keepalived. conf.
    Set files to achieve load balancing and high availability.
    4. Check whether the LVS service is normal.

    # Watch ipvsadm-ln
    IP Virtual Server version 1.2.1 (size = 4096)
    Prot localaddressort sched1_flags
    -> Remoteaddressort forward weight activeconn inactconn
    TCP 61.164.122.8: 80 WRR persistent 60
    -> 61.164.122.10: 80 Route 3 0 0
    -> 61.164.122.9: 80 Route 3 0 0
    Copy code
    # Tail-F/var/log/message: Listen to logs, check the status, and test whether LVS Server Load balancer and high availability are valid.
    5. Stop the keepalived Service of the master server and check whether the bakcup server can take over the service normally.
Related Article

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.