KeepAlived的實現示例
KeepAlived的實現
HA Cluster配置準備:
各節點時間必須同步
ntp(6), chrony(7)
1》在centos6上
ntpdate 172.18.0.1
vim /etc/ntp.conf
server ip地址 iburst #與誰的服務址進行同步
service ntpd start
chkconfig ntpd on
2》在centos7上
ntpdate 172.18.0.1
vim /etc/chrony.conf
server ip地址 iburst
systemctl start chronyd
systemctl enable chronyd
3》爲何要先同步時間再去修改配置文件?
由於若是時間相差的太可能是不會同步的,因此必須用ntpdate命令將時間先同步後再去修改配置文件。
ntpdate命令是暴力同步命令,而服務ntpd和chronyd是弱同步,會進行不斷的更新。
確保iptables 及selinux 不會成爲阻礙
各節點之間可經過主機名互相通訊(對KeepAlived 並不是必須)建議使用/etc/hosts 文件實現
1》在centos6上
vim /etc/sysconfig/network
HOSTNAME=
vim /etc/hosts
ip地址 HOSTNAME
退出從新登陸
2》在centos7上
hostnamectl set-hostname HOSTNAME
vim /etc/hosts
ip地址 HOSTNAME
退出從新登陸
3》測試,ping HOSTNAME
各節點之間的root 用戶能夠基於密鑰認證的ssh服務完成互相通訊(對KA並不是必須)
1》在centos6和centos7上
生成密鑰對
ssh-keygen
將公鑰拷貝給對方
ssh-copy-id -i id_rsa.pub HOSTNAME(ip地址)
2》測試,直接使用ssh 登陸
keepalived 安裝配置:
CentOS 6.4+ Base源
程序環境:
主配置文件:/etc/keepalived/keepalived.conf
主程序文件:/usr/sbin/keepalived
Unit File :/usr/lib/systemd/system/keepalived.service
Unit File 的環境配置文件:/etc/sysconfig/keepalived
高可用的ipvs 集羣示例
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id node1
vrrp_mcast_group4 224.0.100.10
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 6
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 571f97b2
}
virtual_ipaddress {
172.16.0.10/16 dev eth0
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
virtual_server 172.16.0.10 80 {
delay_loop 3
lb_algo rr
lb_kind DR
protocol TCP
sorry_server 127.0.0.1 80
real_server 172.16.0.11 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 1
nb_get_retry 3
delay_before_retry 1
}
}
real_server 172.16.0.12 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 1
nb_get_retry 3
delay_before_retry 1
}
}
}
雙主模式的lvs集羣,拓撲、實現過程
其中一個節點:
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from kaadmin@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id node1
vrrp_mcast_group4 224.0.100.100
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 6
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass f1bf7fde
}
virtual_ipaddress {
172.16.0.80/16 dev eth0 label eth0:0
}
track_interface {
eth0
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 8
priority 98
advert_int 1
authentication {
auth_type PASS
auth_pass f2bf7ade
}
virtual_ipaddress {
172.16.0.90/16 dev eth0 label eth0:1
}
track_interface {
eth0
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
virtual_server fwmark 3 {
delay_loop 2
lb_algo rr
lb_kind DR
nat_mask 255.255.0.0
protocol TCP
sorry_server 127.0.0.1 80
real_server 172.16.0.11 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 2
nb_get_retry 3
delay_before_retry 3
}
}
real_server 172.16.0.12 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 2
nb_get_retry 3
delay_before_retry 3
}
}
}
keepalived 調用腳本進行資源監控
keepalived 調用外部的輔助腳本進行資源監控,並根據監控的結果狀態能實現優先動態調整。
vrrp_script:
自定義一個資源監控腳本或服務監控腳本,腳本能夠返回成功或失敗的值,vrrp實例腳本根據腳本返回的狀態修改調度。
公共定義,可被多個實例調用,定義在vrrp 實例以外。
track_script:
調用vrrp_script 定義的腳本去監控資源,定義在實例以內,調用事先定義的vrrp_script。
調用跟蹤腳本,使當前調度器減去必定的數值,使其優化級低於master配置。
分兩步:(1) 先定義一個腳本;(2)調用此腳本
vrrp_script <SCRIPT_NAME> {
script ""
interval INT
weight -INT
}
track_script {
SCRIPT_NAME_1
SCRIPT_NAME_2
}
高可用的nginx
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id node1
vrrp_mcast_group4 224.0.100.100
}
vrrp_script chk_down {
script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
interval 1
weight -20
}
vrrp_script chk_nginx {
script "killall -0 nginx && exit 0 || exit 1"
interval 1
weight -20
fall 2 #2 次檢測失敗爲失敗
rise 1 #1 次檢測成功爲成功
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 14
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 571f97b2
}
virtual_ipaddress {
172.18.0.93/16 dev eth0
}
track_script {
chk_down
chk_nginx
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
同步組
LVS NAT模型VIP 和DIP須要同步,須要同步組
vrrp_sync_group VG_1 {
group {
VI_1 # name of vrrp_instance (below)
VI_2 # One for each moveable IP.
}
}
vrrp_instance VI_1 {
eth0
vip
}
vrrp_instance VI_2 {
eth1
dip
}