Detailed configuration of Server Load balancer in lvs dr mode in CentOS

Source: Internet
Author: User

Detailed configuration of Server Load balancer in lvs dr mode in CentOS

1. Prepare for LVS installation:

1. Prepare four Centos 6.2 x86_64

Note: SELinux and IPtables firewalls are disabled in this experiment.


Management IP address role remarks 192.168.1.101LVS Master scheduler (Master ctor) provides external services with VIP 192.168.1.180192.168.1.114LVS slave scheduler (Backup Director) which can be used as RS (Real Server) 192.168.1.20.rs1 (Real Server) 192.168.1.103RS2 (Real Server)

2. Configure simple HTTP Services

The following uses the apache service as an example to install the httpd service using yum-y install httpd.

Perform the following operations on 192.168.1.103 and 192.168.1.104 respectively:


 
  1. [Root @ centos ~] # Yum-y install httpd
  2. Echo 103>/var/www/html/index.html # (change the 104 field to 103 on 104)
  3. [Root @ centos ~] # Service httpd start # start the http service
  4. [Root @ centos ~] # Ps-ef | grep httpd # Check if the process has similar output, indicating that the startup is successful.
  5. Root 1535 1 0 23:27? 00:00:00/usr/sbin/httpd
  6. Apache 1537 1535 0? 00:00:00/usr/sbin/httpd
  7. Apache 1538 1535 0? 00:00:00/usr/sbin/httpd
  8. Apache 1539 1535 0? 00:00:00/usr/sbin/httpd
  9. [Root @ centos ~] # Setenforce 0 # disable SELinux
  10. [Root @ centos ~] #/Etc/rc. d/init. d/iptables stop # disable the Firewall

3. Test the http service configuration:

Note: The last digit of the IP address represents the RS content, which is easy for everyone to see the test results. In actual business, the RS service content should be identical.

4. Start LVS Installation

Download related software packages:

 
  1. [Root @ centos1 ~] # Mkdir download
  2. [Root @ centos1 ~] # Cd download/
  3. [Root @ centos1 download] # wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz

5. Installation commands

Note: The following installation operations must be performed on 192.168.1.101 and 192.168.1.104:

 
  1. [Root @ centos1 download] # uname-r # view the Linux kernel version
  2. 2.6.32-220. el6.x86 _ 64
  3. [Root @ centos1 download] # ln-s/usr/src/kernels/2.6.32-220. el6.x86 _ 64 // usr/src/linux # If this step is not performed, an error is reported during installation.
  4. Note: the path of the ln command must be the same as the kernel version output by uname-r. If no kernel version is available, the path is/usr/src/kernels/2.6.32-220. el6.x86 _ 64/the kernel-devel package must be installed.
  5. [Root @ centos1 download] # tar zxvf ipvsadm-1.24.tar.gz
  6. [Root @ centos1 download] # cd ipvsadm-1.24
  7. [Root @ centos1 ipvsadm-1.24] # make
  8. [Root @ centos1 ipvsadm-1.24] # make install
  9. [Root @ centos1 ipvsadm-1.24] # ipvsadm # execute the ipvsadm command to add LVS to the Linux Kernel
  10. IP Virtual Server version 1.2.1 (size = 4096)
  11. Prot LocalAddress: Port sched1_flags
  12. -> RemoteAddress: Port Forward Weight ActiveConn InActConn
  13. [Root @ centos1 ipvsadm-1.24] # lsmod | grep ip_vs # Check whether LVS has been added to the Linux kernel, if the following output indicates that it has been successful.
  14. Ip_vs 108133 0
  15. Ipv6 322029 154 ip_vs, ip6t_REJECT, nf_conntrack_ipv6, nf_defrag_ipv6

6. Configure The LVS Server

Configure the lvs dr mode on 192.168.1.101 and 192.168.1.114 respectively.

1) Configure LVS and create a script to configure LVS

Note: The configuration on 192.168.1.101 and 192.168.1.114 is the same.

[Root @ centos1 bin] # vim lvs_dr.sh

 
  1. #! /Bin/bash
  2. ./Etc/init. d/functions
  3. Vim lvs_dr.sh
  4. #! /Bin/bash
  5. GW = 192.168.1.1
  6. # Website director vip.
  7. SNS_VIP = 192.168.1.181
  8. SNS_RIP1 = 192.168.1.103
  9. SNS_RIP2 = 192.168.1.104
  10. Logger $0 called with $1
  11. Case "$1" in
  12. Start)
  13. # Set squid vip
  14. /Sbin/ipvsadm -- set 30 5 60
  15. /Sbin/ifconfig eth0: 0 $ SNS_VIP broadcast $ SNS_VIP netmask 255.255.255 up
  16. /Sbin/route add-host $ SNS_VIP dev eth0: 0
  17. /Sbin/ipvsadm-A-t $ SNS_VIP: 80-s wrr-p 3
  18. /Sbin/ipvsadm-a-t $ SNS_VIP: 80-r $ SNS_RIP1: 80-g-w 1
  19. /Sbin/ipvsadm-a-t $ SNS_VIP: 80-r $ SNS_RIP2: 80-g-w 1
  20. Touch/var/lock/subsys/ipvsadm>/dev/null 2> & 1
  21. ;;
  22. Stop)
  23. /Sbin/ipvsadm-C
  24. /Sbin/ipvsadm-Z
  25. Ifconfig eth0: 0 down
  26. Ifconfig eth0: 1 down
  27. Route del $ SNS_VIP
  28. Route del $ SS_VIP
  29. Rm-rf/var/lock/subsys/ipvsadm>/dev/null 2> & 1
  30. Echo "ipvsadm stoped"
  31. ;;
  32. Status)
  33. If [! -E/var/lock/subsys/ipvsadm]; then
  34. Echo "ipvsadm stoped"
  35. Exit 1
  36. Else
  37. Echo "ipvsadm OK"
  38. Fi
  39. ;;
  40. *)
  41. Echo "Usage: $0 {start | stop | status }"
  42. Exit 1
  43. Esac
  44. Exit 0
 
  1. [Root @ centos1 bin] # chmod + x lvs_dr.sh # Add the execution permission to the SHELL script
  2. [Root @ centos1 bin] # cp lvs_dr.sh/etc/rc. d/init. d/# Easy to start
  3. [Root @ centos1 bin] # service lvs_dr.sh start # start The lvs service

Run the ipvsadm-Ln command to check whether the following output exists. If yes, The LVS configuration is successful.

 
  1. [Root @ centos1 bin] # ipvsadm
  2. IP Virtual Server version 1.2.1 (size = 4096)
  3. Prot LocalAddress: Port sched1_flags
  4. -> RemoteAddress: Port Forward Weight ActiveConn InActConn
  5. TCP 192.168.1.181: http wrr persistent 3
  6. -> 192.168.1.104: http Route 1 0 0
  7. -> 192.168.1.103: http Route 1 0 0

7. Configure The lvs rs Server

[Root @ centos bin] # vim lvs_dr.sh

 
  1. #! /Bin/bash
  2. ./Etc/init. d/functions
  3. SNS_VIP = 192.168.1.181
  4. Case "$1" in
  5. Start)
  6. Ifconfig lo: 0 $ SNS_VIP netmask 255.255.255.255 broadcast $ SNS_VIP
  7. /Sbin/route add-host $ SNS_VIP dev lo: 0
  8. Echo "1">/proc/sys/net/ipv4/conf/lo/arp_ignore
  9. Echo "2">/proc/sys/net/ipv4/conf/lo/arp_announce
  10. Echo "1">/proc/sys/net/ipv4/conf/all/arp_ignore
  11. Echo "2">/proc/sys/net/ipv4/conf/all/arp_announce
  12. Sysctl-p>/dev/null 2> & 1
  13. Echo "RealServer Start OK"
  14. ;;
  15. Stop)
  16. Ifconfig lo: 0 down
  17. Route del $ SNS_VIP>/dev/null 2> & 1
  18. Echo "0">/proc/sys/net/ipv4/conf/lo/arp_ignore
  19. Echo "0">/proc/sys/net/ipv4/conf/lo/arp_announce
  20. Echo "0">/proc/sys/net/ipv4/conf/all/arp_ignore
  21. Echo "0">/proc/sys/net/ipv4/conf/all/arp_announce
  22. Echo "RealServer Stoped"
  23. ;;
  24. *)
  25. Echo "Usage: $0 {start | stop }"
  26. Exit 1
  27. Esac

[Root @ centos bin] # cp lvs_dr.sh/etc/rc. d/init. d/# Easy to start

[Root @ centos bin] # service lvs_dr.sh start # start The lvs RS Server

RealServer Start OK

8. verify whether the lvs dr mode is configured successfully.

Enter LVS http: // 192.168.1.181 in the browser

Open another browser and enter http: // 192.168.1.181 again to check whether the content is the same.

The content accessed by the two browsers is different, and the lvs dr mode is successfully configured.

By now, the lvs dr mode has been configured. The following describes how to configure lvs dr + Keepalived high-availability server Load balancer.

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.