Build Practice lvs+keepalived Load Balancing
Keepalived Overview
1.keepalived is a powerful auxiliary tool specifically designed for LVS, mainly used to provide failover and health check function-to determine the LVS load Scheduler, node server availability, timely isolation and replacement of the new server, when the failed host to rejoin the cluster after recovery.
Overview of the 2.keepalived hot-standby principle keepalived uses the VRRP (Virtual Routing Redundancy Protocol) hot-standby protocol to implement the multi-machine hot-standby function of Linux servers in a software way. VRRP is a backup solution for routers-Multiple routes form a hot standby group, providing services externally through a shared virtual IP address; only one primary router is available at the same time in each hot standby group, and the other routers are redundant, and if the current online router fails, the other routers are automatically replaced ( Priority determines the order of succession) virtual IP address to continue to provide services. Each router in the hot spare group is likely to become the primary router, and the IP address of the virtual router can be transferred between the routers within the warm standby group, so it is also known as the Drift IP address. When using keepalived, the drift address implementation does not need to manually establish a virtual interface configuration file, but is automatically managed by the keepalived according to the configuration file.
Structure Overview (image from Network)
1. Environmental description
System version: ubuntu14.04 LTS
LVS Server: 192.168.1.107,192.168.1.105
Live Server (Real): 192.168.1.106,192.168.1.107,192.168.1.108
vip:192.168.1.70
Deployment Purpose:
User request 192.168.1.70 Message forwarded to the other 3 IP machine, not a machine configuration is static IP 106,107,108 has deployed apache2, monitoring 8888 ports, providing the same services.
LVS is built on 105,107, 107 is the main, and 105 is prepared
Configuring the VIP Listener on 2.Real server
Execute script root permissions on the real server, respectively
Vim rs.sh
#!/bin/bash
sns_vip=192.168.1.70
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/sbin/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
Save the script, and then execute. (To have Execute permission)
./rs.sh Start
3.LVS Configuration Master-Slave
Installing the Keeplived Package
Apt-get Install keepalived
sudo vim/etc/keepalived/keepalived.conf
globel_defs{
notification_email{
[Email protected] #指定keepalived在切换时需要发送到的email对象, one line
}
Notification_email_from [email protected] #指定发件人 Smtp_server
127.0.0.1 #指定SMTP服务器地址
Smtp_connection_timeout #指定SMTP连接超时时间
router_id easyfun-107 #设置lvs的id, should be unique within a network
}
Vrrp_instance vi_1{
State Master #指定keepalived的角色, master Main, slave as slave, backup standby
Interface Eth0 #设置实例绑定到那个网卡
virtual_router_id #VRID标记 (0~255), the main preparation to keep the same
Priority #优先级, Master is higher than backup (at least 50)
Advert_int 1 #检查间隔时间, default 1 sec
Authentication {
Auth_type PASS #指定要使用那一种认证 (pass| AH)
Auth_pass 123456 #指定要使用的密码字符串
}
virtual_ipaddress {
192.168.1.70 #定义虚拟IP (VIP), multi-set, per line-a
}
}
#定义对外提供服务的LVS的VIP以及port
Virtual_server 192.168.1.70 8888 {
Delay_loop 1 #设置健康检测时间, units in seconds
Lb_algo WRR #设置LVS调度的算法rr |WRR|LC|WLC|LBLC|LBLCR|SH|DH
Lb_kind DR #设置LVS实现负载的机制, (nat| tun| DR) Three modes
Nat_mask 255.255.255.0
Persistence_timeout #会话保持时间
Protocol TCP #使用的协议
Real_server 192.168.1.106 8888 {#指定real_server1的ip地址
Weight 3 #配置节点权值, the larger the number the higher the weight
Tcp_check {
Connect_timeout #连接远程真实服务器超时时间 (sec)
Nb_get_retry 3 #最大重试次数
Delay_before_retry 3 #连续两次重试的延迟时间 (sec)
Connect_port 8888 #健康检查的端口
}
}
Real_server 192.168.1.108 8888 {#指定real_server2的ip地址
Weight 3 #配置节点权重
Tcp_check {
Connect_timeout 10
Nb_get_retry 3
Delay_before_retry 3
Connect_port 8888
}
}
Real_server 192.168.1.107 8888 {#指定real_server2的ip地址
Weight 3 #配置节点权重
Tcp_check {
Connect_timeout 10
Nb_get_retry 3
Delay_before_retry 3
Connect_port 8888
}
}
}
Save Exit,
Open keepalived Service
Service keepalived Start
View status Ipvsadm-l-N
Configure LVS from the server (another LVS)
Apt-get Install keepalived
The configuration from the server is roughly the same as the primary server, to change the state from master to backup in keepalived.conf
Change priority from 150 to 100
sudo vim/etc/keepalived/keepalived.conf
Vrrp_instance Vi_1 {
State backup # here instead of backup
Interface eth1
VIRTUAL_ROUTER_ID 51
Priority 100 # is changed here to 100,master Precedence is 150
Advert_int 1
Authentication {
Auth_type PASS
Auth_pass 1111
}
virtual_ipaddress {
192.168.1.70
}
}
Save Exit,
Open keepalived Service
Service keepalived Start
View status Ipvsadm-l-N
4. Verify the Load Balancer master-slave switch, and simulate the real server down the corresponding service, view the status of Ipvsadm ipvsadm-l C
Linux load Balancing concepts and practices (II.)