lvs+keepalived配置
1>環境描述
LVS server1 (Master):10.0.0.5虛擬IP爲:10.0.0.3(裝lvs+keepalived+配置)
LVS server2 (Slave) :10.0.0.6虛擬IP爲:10.0.0.3(裝lvs+keepalived+配置)
WEB server1: 10.0.0.7 (安裝arptables,虛擬ip10.0.0.3)
WEB server2; 10.0.0. 8 (安裝arptables,虛擬ip10.0.0.3)
注意:所有集羣服務器時間要一致
Lvs配置使用DR模式。
調度器DR配置:
1.綁定vip ip addr add 10.0.0.3/24 dev eth0 label eth0:0
2.下載軟件 yum install -y ipvsadm
3.清空所有配置IPvsadm -C
4.添加策略指定vip ipvsadm -A -t 10.0.0.3:80 -s rr (-t指定tcp協議 -s指定rr輪詢)
5.將虛擬ip指定到真實ip ipvsadm -a -t 10.0.0.3:80 -r 10.0.0.7:80 -g (-g指定dr模式 -r 指定真實服務器地址)
6.將虛擬ip指定到真實ip ipvsadm -a -t 10.0.0.3:80 -r 10.0.0.8:80 -g (-g指定dr模式 -r 指定真實服務器地址)
7./etc/init.d/ipvsadm save 保存
真實Server1的配置:
1.添加vip ip addr add 10.0.0.3/24 dev eth0 label eth0:0
在server2的配置
1.添加vip ip addr add 10.0.0.3/24 dev eth0 label eth0:0
在server1和server2上安裝arptables防止客戶端直接訪問到真實服務器(因爲ip一樣)
1.下載arptables yum install -y arptables.x86_64
2.編寫策略 arptables -A INPUT -d 10.0.0.3 -j DROP
Arptables -A OUTPUT -s 10.0.0.3 -j mangle --mangle-ip-s 10.0.0.7
Arptables-save >/etc/sysconfing/arptables 導入策略
至此lvs安裝完畢,使用curl 10.0.0.3可以發現已經實現輪詢。
下面配置高可用
在lvs server1和lvs server2上裝keepalived
yum install -y keepalived
主(master)配置Keepalived配置文件
cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs { #全局定義
notification_email { #定義郵件
[email protected] #定義郵件地址
}
notification_email_from [email protected] #定義郵件地址
smtp_server mail.126.com #郵件服務器
smtp_connect_timeout 30 #郵件超時時間
router_id LVS_DEVEL #route_id標識
}
vrrp_instance VI_1 { #定義VRR組,
state MASTER #定義爲MASTER主
interface eth0 #對外訪問的網絡接口
virtual_router_id 100 #虛擬路由標識。注意主從要一致
priority 100 #主從優先級,主的優先級要高於從。注意
advert_int 1 #:廣播週期秒數
authentication {
auth_type PASS
auth_pass 2209
}
virtual_ipaddress {
10.0.0.3 #:虛擬VIP地址,真實環境這裏應該是公網IP
}
}
virtual_server 10.0.0.3 80 { #:虛擬VIP地址 與 端口,DR架構WEB端口要和虛擬端口監聽一致。否則將無法訪問
delay_loop 6 #健康檢查時間間隔,單位是秒
lb_algo rr #調用算法爲RR
lb_kind DR #調用架構模式爲DR
persistence_timeout 0 #:同一IP 50秒內的請求都發到同個real server
protocol TCP #使用TCP協議
real_server 10.0.0.7 80 {#:真實WEB服務器地址與端口
weight 1 #:轉發伐值,越高調用的越多
TCP_CHECK {
connect_timeout 10 #:連接超時爲10秒
nb_get_retry 3
delay_before_retry 3
connect_port 80 #連接端口爲80,要和上面的保持一致
}
}
real_server 10.0.0.8 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
從(Slave)配置Keepalived配置文件
主從配置區別:
state BACKUP #狀態改成BACKUP
priority 90 #優先級要低於主
interface eth1 #網絡接口注意和本機對應,
其它的配置文件內容和主的一樣
重啓service keepalived restart
配置WEBserver真實服務器,所有的WEB腳本都一樣
ip addr add 10.0.0.3/32 dev lo
cat >>/etc/sysctl.conf<<EOF
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
EOF
sysctl -p