-------主服務器&從服務器lvs&keepalived------
首先安裝依賴包
yum -y install openssl-devel gcc gcc-c++ ipvsadm
yum安裝lvs
yum -y install ipvsadm
yum安裝keepalived
yum -y installkeepalived
/etc/init.d/ipvsadmrestart
service keepalivedrestart
主服務器配置文件
修改keepalived配置文件(刪除原內容,粘貼後修改IP)
vim/etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_connect_timeout 3
smtp_server 172.18.5.43
router_id LVS_DEVEL
}
vrrp_script chk_schedown {
script "[[ -f /etc/keepalived/down ]] && exit 1 || exit0"
interval 2
weight -2
}
vrrp_instance VI_1 {
interface eth0
state MASTER
priority 101 #(大於從服務器)
virtual_router_id 87
garp_master_delay 1
authentication {
auth_type PASS
auth_pass 1221
}
track_interface {
eth0
}
virtual_ipaddress {
10.1.1.182
}
track_script {
chk_schedown
}
}
virtual_server 10.1.1.182 80 { # (VIP)
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 10.1.1.167 80 { # (web服務IP)
weight 1
HTTP_GET {
url {
path /
status_code 301
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 10.1.1.16880 { # (web服務IP)
weight 1
HTTP_GET {
url {
path /
status_code 301
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
==========================================================================================
附服務器配置文件
修改keepalived配置文件(刪除原內容,粘貼後修改IP)
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_connect_timeout 3
smtp_server 172.18.5.43
router_id LVS_DEVEL
}
vrrp_script chk_schedown {
script "[[ -f /etc/keepalived/down ]] && exit 1 || exit0"
interval 2
weight -2
}
vrrp_instance VI_1 {
interface eth0
state BACKUP
priority 100 #(大於從服務器)
virtual_router_id 87
garp_master_delay 1
authentication {
auth_type PASS
auth_pass 1221
}
track_interface {
eth0
}
virtual_ipaddress {
10.1.1.182
}
track_script {
chk_schedown
}
}
virtual_server 10.1.1.182 80 { # (VIP)
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 51
protocol TCP
real_server 10.1.1.168 80 { # (web服務IP)
weight 1
HTTP_GET {
url {
path /
status_code 301
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 10.1.1.167 80 { # (web服務IP)
weight 1
HTTP_GET {
url {
path /
status_code 301
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
===========================================================================================
查詢
ipvsadm -Ln
service ipvsadmrestart
service keepalivedrestart
web服務段配置(web1和web2)直接粘貼
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
修改IP後粘貼
/sbin/ifconfig lo:0 192.168.11.31broadcast 192.168.11.31 netmask 255.255.255.255 up
/sbin/route add -host 192.168.11.31dev lo:0