Reproduced below:
Get ready
L System: CENTOS6 (three)
L Load Balancer: LVS + keepalived
L Server 1:http
L Server 2:http
IP configuration
1. VIP (virtual IP): The IP address used to provide the virtual Server service. are bound on the director of a physical network card (external receiving request packet) and the loopback device RS (loopback device needs to bind two IP, one is 127.0.0.1, the other is VIP).
2, DIP (director IP): with the VIP bound on a physical network card, used to forward the request packet to the RS rip corresponding Mac, this device can get the MAC address of RIP corresponding to the ARP request.
3. RIP (real server IP): It is bound on a physical NIC on RS to receive the request packets forwarded from directory.
Shutting down the firewall
[[Email protected] ~]# service iptables status
[[Email protected] ~]# service iptables stop
[Email protected] ~]# chkconfig iptables--list
[Email protected] ~]# chkconfig iptables off
The first step:add on two live servers onlinelo:0Interface,and set the virtualIPon this interface
[Email protected] ~]# cd/etc/init.d/
[[Email protected] ~] #touch realserver
[[Email protected] ~] #vim realserver
sns_vip=192.168.8.150
. /etc/rc.d/init.d/functions
Case "$" in
Start
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
The above parameter description
sns_vip=192.168.1.98 # defining VIP variables
. /etc/rc.d/init.d/functions # Guide Script Library
Case "$" in #case statement passed to the first parameter of the shell script
Start
Ifconfig lo:0 $SNS _vip netmask 255.255.255.255 broadcast $SNS _VIP # set lo:0 VIP netmask and broadcasting
/sbin/route add-host $SNS _vip Dev lo:0 # #route del add local route
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 #-P <file> (default/etc/sysctl.conf) Enter standard information into the device empty file
echo "Realserver Start OK"
;;
Stop
Ifconfig lo:0 Down
Route del $SNS _vip >/dev/null 2>&1 #route del delete local route
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}" #$0 is the name of the script itself
Exit 1 # indicates that the process exited gracefully
Esac #case End
Exit 0 # indicates that the process is not properly exited
[[Email protected] ~] #chmod realserver # give this script permission
[[Email protected] ~] #service realserver Start # Open this script
Realserver Start OK # indicates a successful startup !
[[Email protected] ~] #ifconfig # to see if the lo:0 interface was actually successfully added
Step Two:turn on the two live servers on your ownApacheCompany'sHttpServer
[[Email protected]st01 ~] #service httpd Start # turn on the Http Server
[[Email protected] ~] #cd/var/www/html/# Enter The default directory of the Http server
[[Email protected] ~] #touch index.html # Create Http Server default access page
[[Email protected] ~] #vim index.html # Edit Http Server default access page
I am HTTP IP: Real server's own IP ( ex : 192.168.200.201)
Access in a browser : http://192.168.200.201 default port
See if the results were edited by yourself : I am http IP: Real server's own IP ( ex : 192.168.200.201)
The same as two real servers
Step Three:Start building Load Balancing
1: Install Ipvsadm keepalived
Command: Yum–y install Ipvsadm keepalived
2: Configure keepalived
Command: vim/etc/keepalived/keepalived.conf
Global_defs {
# notification_email {
# [Email protected]
# }
# Notification_email_from [email protected]
# Smtp_server Smtp.exmail.qq.com
# Smtp_connect_timeout 30
router_id Lvs_devel
}
Vrrp_instance Vi_1 {
State MASTER
Interface eth0
VIRTUAL_ROUTER_ID 51
Priority 100
Advert_int 1
Authentication {
Auth_type PASS
Auth_pass 1111
}
virtual_ipaddress {
192.168.200.201
}
}
Virtual_server 192.168.200.201 80 {
Delay_loop 6
Lb_algo WRR
Lb_kind DR
Nat_mask 255.255.255.0
Persistence_timeout 0
Protocol TCP
Real_server 192.168.200.139 80 {
Weight 3
Tcp_check {
Connect_timeout 10
Nb_get_retry 3
Delay_before_retry 3
Connect_port 80
}
}
Real_server 192.168.200.140 80 {
Weight 3
Tcp_check {
Connect_timeout 10
Nb_get_retry 3
Delay_before_retry 3
Connect_port 80
}
}
}
The above parameter description
global_defs {# # global configuration section
# notification_email {# # # # # # # # # # # The following lines are global notification configurations, can be implemented after a problem alarm, but limited functionality, so commented out, and using Nagios monitoring lvs< /c3> Operating conditions
# [Email protected]
# }
# Notification_email_from [email protected]
# Smtp_server Smtp.exmail.qq.com
# Smtp_connect_timeout 30
router_id Lvs_devel # # Set the IDof the LVS , which should be unique within a network
}
Vrrp_instance vi_1 {# # set vrrp Group, unique and same LVS Server group to be the same
State MASTER # # backup LVS Server set to backup
Interface Eth0 # # set interface for external services
VIRTUAL_ROUTER_ID # # set virtual route ID
priority 100 # Set priority, the higher the number, the higher the priority, backup set to 99 master After the outage, automatically backup master master master backup
Advert_int 1 # # set sync time interval
Authentication {# # set Authentication type and password,master and buckup must be set as
Auth_type PASS
Auth_pass 1111
}
virtual_ipaddress {# # set VIP, can be multiple, one row per
192.168.18.60
}
}
Virtual_server 192.168.18.60 80 {
Delay_loop 6 # # Health Check time interval, unit s
Lb_algo WRR # # # load Balancing scheduling algorithm set to weighted round call
Lb_kind DR # # Load Balancer Forwarding rules
Nat_mask 255.255.255.0 # # Network mask,DR mode to protect the real server and LVS in the same network segment
Persistence_timeout # # # Session hold time, unit s
Protocol TCP # # Protocol
Real_server 192.168.18.61 {# # Real server configuration, Port
Weight 3 # # weight
Tcp_check {# # server detection mode settings keepalived Health Check methods are:http_get| Ssl_get| tcp_check| smtp_check| Misc_check
Connect_timeout 0 # # connection time-out
Nb_get_retry 3 # # number of failed retries
Delay_before_retry 3 # # time interval for failed retries
Connect_port-# # # connected back-end ports
}
}
Real_server 192.168.18.62 80 {
Weight 3
Tcp_check {
Connect_timeout 10
Nb_get_retry 3
Delay_before_retry 3
Connect_port 80
}
}
}
Start the keepalived service
[[Email protected] ~]# service keepalived start
Note: Because the keepalived configuration file can be started with syntax errors, so see started the LVS service, does not mean that the configuration file does not have errors, if the LVS can not be properly forwarded, the timely tracking log for processing.
Log tracking methods
1, open two SSH windows connected to the LVS server, the first window run the following command:
[Email protected] ~]# tail-f/var/log/message
2, the second window restarts the Keepalived service, while observing the changes in the log in Windows 1, and then according to the log prompt to resolve.
Browser access to VirtualIPView Results
Two real servers appear on the page , showing that the results are correct
Build a load-balanced environment (using four CentOS on a virtual machine)