LVS簡介
LVS是linux Virtual Server的簡寫,意即Linux虛擬服務(wù)器,是一個虛擬的服務(wù)器集群系統(tǒng)。LVS主要用于多服務(wù)器的負載均衡。它工作在網(wǎng)絡(luò)層,可以實現(xiàn)高性能,高可用的服務(wù)器集群技術(shù)。它是我們國家的章文嵩博士的一個開源項目。在linux內(nèi)存2.6中,它已經(jīng)成為內(nèi)核的一部分,在此之前的內(nèi)核版本則需要重新編譯內(nèi)核。
準備工作
- 首先準備2臺主機,3個ip,其中一個ip做虛擬ip
安裝及配置
- yum方式安裝yum install lvs
ipvsadm -Ln查看是否安裝成功
- 配置配置腳本lvs.sh,執(zhí)行l(wèi)vs.sh start。
#!/bin/bash
# description: Config realserver
LVS_VIP=192.168.20.227
source /etc/rc.d/init.d/functions
case "$1" in
start)
/sbin/ifconfig lo:0 $LVS_VIP netmask 255.255.255.255 broadcast $LVS_VIP
/sbin/route add -host $LVS_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)
/sbin/ifconfig lo:0 down
/sbin/route del $LVS_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: $0 {start|stop}"
exit 1
esac
exit 0
Keepalived簡介
Keepalived的作用是檢測服務(wù)器的狀態(tài),如果有一臺web服務(wù)器宕機,或工作出現(xiàn)故障,Keepalived將檢測到,并將有故障的服務(wù)器從系統(tǒng)中剔除,同時使用其他服務(wù)器代替該服務(wù)器的工作,當服務(wù)器工作正常后Keepalived自動將服務(wù)器加入到服務(wù)器群中,這些工作全部自動完成,不需要人工干涉,需要人工做的只是修復故障的服務(wù)器。
安裝及配置
- yum方式安裝
yum install keepalived
#Keepalived 相關(guān)操作命令
#啟動Keepalived
systemctl start keepalived
#關(guān)閉Keepalived
systemctl start keepalived
#重啟Keepalived
systemctl restart keepalived
#查看狀態(tài)Keepalived
systemctl status keepalived
- 配置
編輯vi
/etc/keepalived/keepalived.conf文件,編輯完成后重啟keepalived生效
! Configuration File for keepalived
global_defs {
notification_email {
wangzhiyuan@hec.cn
}
#notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.20.229 #本機ip
smtp_connect_timeout 30
router_id LVS_DEVEL_229
vrrp_skip_check_adv_addr
script_user root
enable_script_security
#vrrp_strict
#vrrp_garp_interval 0
#vrrp_gna_interval 0
}
vrrp_script chk_Nginx {
script "/etc/keepalived/nginx_check.sh" #檢測nginx進程是否存在腳本
interval 1
}
vrrp_instance VI_1 {
state MASTER/BACKUP #MASTER:主 BACKUP:從
interface ens160 #網(wǎng)卡
virtual_router_id 51 #主從兩臺配置id需一致
priority 100 #優(yōu)先級 主優(yōu)先級設(shè)置比從優(yōu)先級高
advert_int 1
authentication {
auth_type PASS
auth_pass keepnginx #密碼 主從兩臺主機配置需一致
}
virtual_ipaddress {
192.168.20.227 #虛擬ip
}
track_script {
chk_nginx
}
}
virtual_server 192.168.20.227 80 { #lvs負載端口
delay_loop 6
lb_algo wrr #負載均衡算法
lb_kind DR
persistence_timeout 0
protocol TCP
real_server 192.168.20.229 80 { #真實ip及端口
weight 1
TCP_CHECK {
connection_timeout 5
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.20.228 80 { #真實ip及端口
weight 1
TCP_CHECK {
connection_timeout 5
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
- nginx檢測腳本
#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
serivce keepalived stop
fi