Configure LVS-DR mode load balancing in ubuntu 12.04

Source: Internet
Author: User

Make a record about the LVS problems that have recently been tossed! Normally, three or more servers can be used, one for load, and the other two for real servers, but I have only two environments in my hands, that means that one of the logs is used as the load and the actual server is used.

First, check whether Linux kernel supports LVS:

Modprobe-l | grep ipvs
If the output is similar to the following content, it is OK:

Kernel/net/netfilter/S/ip_vs.ko
Kernel/net/netfilter/ipvs/ip_vs_rr.ko
Kernel/net/netfilter/ipvs/ip_vs_wrr.ko
Kernel/net/netfilter/ipvs/ip_vs_lc.ko
Kernel/net/netfilter/ipvs/ip_vs_wlc.ko
Kernel/net/netfilter/ipvs/ip_vs_lblc.ko
Kernel/net/netfilter/ipvs/ip_vs_lblcr.ko
Kernel/net/netfilter/ipvs/ip_vs_dh.ko
Next, install the ipvsadm management tool:

Sudo apt-get install ipvsadm
After successful installation, run the ipvsadm command. If the following words are displayed, the installation is successful:

IP Virtual Server version 1.2.1 (size = 4096)
Prot LocalAddress: Port sched1_flags
-> RemoteAddress: Port Forward Weight ActiveConn InActConn
Next, do not do it step by step. copy the ready-made script directly, change the IP address in it, and use it directly. This is simple and rude!

Procedure and script
Create a new script file lv-dr.sh on the LVS server and throw the following content into it:

#! /Bin/bash
######################
# Data 20150727 #
# Website Gongzi.org #
######################
Vip = 10.46.239.56
Rs1 = 10.46.239.50
Rs2 = 10.46.239.51

# Set vip address and add route
/Sbin/ifconfig bond0: 0 $ vip broadcast $ vip netmask 255.255.255.255 up
/Sbin/route add-host $ vip dev bond0: 0
Route-n

# Update/etc/sysctl. conf
Echo "1">/proc/sys/net/ipv4/ip_forward
Echo "1">/proc/sys/net/ipv4/conf/all/send_redirects
Echo "1">/proc/sys/net/ipv4/conf/default/send_redirects
Echo "1">/proc/sys/net/ipv4/conf/eth0/send_redirects
Echo "1">/proc/sys/net/ipv4/conf/bond0/send_redirects
Sysctl-p

# Clear ipvs tables
Ipvsadm-C

# Set ipvs

Ipvsadm-A-t $ vip: 8080-s wrr-p $(3600*48 ))
Ipvsadm-a-t $ vip: 8080-r $ rs1: 8080-g-w 1
Ipvsadm-a-t $ vip: 8080-r $ rs2: 8080-g-w 1

# Run lvs
/Sbin/ipvsadm
Then modify the information:

Tips!
VIP: your virtual IP address
Rs12: your real server IP address
And the next 8080 is the port you want to load.
In addition, you must set session persistence for the WEB project. Otherwise, a problem may occur. In the code above,-p $ (3600*48) is returned.
Then create a lvs-rs.sh script in the real server and throw it into the following:

#! /Bin/bash
######################
# Data 20150727 #
# Website Gongzi.org #
######################
Vip = 10.46.239.56
Ifconfig lo: 0 $ vip netmask 255.255.255.255 broadcast $ vip up
Route add-host $ vip dev lo: 0
# Echo "0">/proc/sys/net/ipv4/ip_forward # Disable the forwarding function of the real server.
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
Note: The Gateway in this script must be the same as the virtual ip address. The Mask cannot be the same as that of the primary network card (that is, eth0 or 1)
Next, we can test it.

Some remarks and common parameters
The following are some frequently-used ipvs commands and Parameter descriptions. I found them online and extracted several frequently-used commands for your reference:

-A -- add-service adds A new virtual server record in the virtual server table of the kernel. That is, add a new virtual server.
-E -- edit-service: edit a virtual server record in the kernel virtual server table.
-D -- delete-service: delete a virtual server record in the kernel virtual server table.
-C -- clear clears all records in the kernel virtual server table.
-R -- restore virtual server rules
-S -- save saves the virtual server rule and outputs the readable format of the-R option
-A -- add-server adds a new real server record to a record in the kernel virtual server table. That is, add a new real server to a virtual server.
-E -- edit-server: edit a real server record in a virtual server record
-D -- delete-server: delete a real server record in a virtual server record.
-L |-l -- list displays the kernel virtual server table
-Z -- zero virtual service table counters are cleared (the current number of connections is cleared, etc)
-T -- tcp-service-address indicates that the virtual server provides the tcp service [vip: port] or [real-server-ip: port].
-U -- udp-service-address indicates that the virtual server provides the udp service [vip: port] or [real-server-ip: port].
-R -- real-server-address Real Server [real-server: port]
-G -- gatewaying specifies that the LVS working mode is the direct routing mode (also the default LVS mode)
-I -- ipip: specify the LVS working mode as tunnel mode.
-M -- masquerading: specify the LVS working mode as NAT
-W -- weight actual server weight
There are several other modes, such as NAT. Here we talk about DR, which is also the best performance.

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.