系統(tǒng)運(yùn)維
一、keepalive實(shí)現(xiàn)雙主:
1.1實(shí)驗準(zhǔn)備
2臺主機(jī)地址分別為:172.20.10.9,172.20.10.10,
vip1為172.20.10.10,vip2為172.20.10.11
1.2通知腳本:
[root@node5 /]# vim /etc/keepalived/notify.sh
#!/bin/bash
contact=\'root@localhost\'
notify() {
local mailsubject=$(hostname) to be $1, vip floating
local mailbody=$(date +\'%F %T\'): vrrp transition, $(hostname) changed to be $1
echo $mailbody | mail -s $mailsubject $contact
}
case $1 in
master)
notify master
;;
backup)
notify backup
;;
fault) notify fault
;;
*)
echo Usage: $(basename $0) {master|backup|fault}
exit 1
;;
esac
1.3配置node5為vip1地址的主節(jié)點(diǎn),vip2地址的備節(jié)點(diǎn)
[root@node5 ~]# vim /etc/keepalived/keepalived.conf
! 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 node5
vrrp_mcast_group4 224.0.100.19
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.20.10.10/28 dev ens33 label ens33:0
}
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 ens33
virtual_router_id 52
priority 98
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.20.10.11/28 dev ens33 label ens33:1
}
notify_master /etc/keepalived/notify.sh master
notify_backup /etc/keepalived/notify.sh backup
notify_fault /etc/keepalived/notify.sh fault
}
1.4配置node6為vip1地址的備節(jié)點(diǎn),vip2地址的主節(jié)點(diǎn)
[root@node6 ~]# vim /etc/keepalived/keepalived.conf
! 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 node5
vrrp_mcast_group4 224.0.100.19
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 98
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.20.10.10/28 dev ens33 label ens33:0
}
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 MASTER
interface ens33
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.20.10.11/28 dev ens33 label ens33:1
}
notify_master /etc/keepalived/notify.sh master
notify_backup /etc/keepalived/notify.sh backup
notify_fault /etc/keepalived/notify.sh fault
}
1.5驗證:
(1)先開啟node6節(jié)點(diǎn)的keepalived服務(wù),查看IP地址,狀態(tài):
2個VIP地址都在node6節(jié)點(diǎn)上:
(2)再開啟node5節(jié)點(diǎn)keepalived服務(wù),查看IP地址和狀態(tài):
VIP1地址已經(jīng)飄到了node5節(jié)點(diǎn)上了
二,Nginx+Keepalived實(shí)現(xiàn)站點(diǎn)高可用
2.1實(shí)驗準(zhǔn)備:
2臺主機(jī):node5地址為172.20.10.9,node6的節(jié)點(diǎn)為172.20.10.8
vip地址為172.20.10.10
2.2nginx配置
(1)node5節(jié)點(diǎn)
[root@node5 ~]# yum install nginx -y
[root@node5 /]# vim /usr/share/nginx/html/index.html
node5 web page
[root@node5 ~]# systemctl start nginx
(2)node6節(jié)點(diǎn)
[root@node6 /]# yum install -y nginx
[root@node6 /]# vim /usr/share/nginx/html/index.html
node6 web page
[root@node6 ~]# systemctl start nginx
2.3配置node5為vip的主節(jié)點(diǎn),配置檢查nginx的監(jiān)控腳本:
[root@node5 /]# vim /etc/keepalived/keepalived.conf
! 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 node5
vrrp_mcast_group4 224.0.100.19
}
vrrp_script chk_nginx {
script /usr/bin/killall -0 nginx && exit 0 || exit 1
interval 1
weight -5
fall 2
rise 1
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.20.10.10/28 dev ens33 label ens33:0
}
track_script {
chk_nginx
}
track_interface {
ens33
}
notify_master /etc/keepalived/notify.sh master
notify_backup /etc/keepalived/notify.sh backup
notify_fault /etc/keepalived/notify.sh fault
}
[root@node5 keepalived]# systemctl start keepalived
2.4配置node6為vip的備節(jié)點(diǎn),配置檢查nginx的監(jiān)控腳本:
[root@node6 /]# vim /etc/keepalived/keepalived.conf
! 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 node5
vrrp_mcast_group4 224.0.100.19
}
vrrp_script chk_nginx {
script /usr/bin/killall -0 nginx && exit 0 || exit 1
interval 1
weight -5
fall 2
rise 1
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 98
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.20.10.10/28 dev ens33 label ens33:0
}
track_script {
chk_nginx
}
track_interface {
ens33
}
notify_master /etc/keepalived/notify.sh master
notify_backup /etc/keepalived/notify.sh backup
notify_fault /etc/keepalived/notify.sh fault
}
[root@node6 html]# systemctl start keepalived
3.驗證:
3.1當(dāng)服務(wù)都正常時,vip地址在node5節(jié)點(diǎn)上,測試訪問172.20.10.10的情況:
3.2當(dāng)down掉node5節(jié)點(diǎn)上nginx服務(wù)時,觀察vip地址和其訪問情況:
vip地址飄到node6節(jié)點(diǎn)上,curl172.20.10.10,則訪問的時node6節(jié)點(diǎn)
3.3當(dāng)恢復(fù)node5節(jié)點(diǎn)上nginx服務(wù)時,vip地址飄到node5節(jié)點(diǎn)上,訪問172.20.10.10則是node5上nginx