您好,登錄后才能下訂單哦!
一、keepalive實現雙主:
1.1實驗準備
2臺主機地址分別為: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地址的主節點,vip2地址的備節點
[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地址的備節點,vip2地址的主節點
[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節點的keepalived服務,查看IP地址,狀態:
2個VIP地址都在node6節點上:
(2)再開啟node5節點keepalived服務,查看IP地址和狀態:
VIP1地址已經飄到了node5節點上了
二,Nginx+Keepalived實現站點高可用
2.1實驗準備:
2臺主機:node5地址為172.20.10.9,node6的節點為172.20.10.8
vip地址為172.20.10.10
2.2nginx配置
(1)node5節點
[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節點
[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的主節點,配置檢查nginx的監控腳本:
[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的備節點,配置檢查nginx的監控腳本:
[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當服務都正常時,vip地址在node5節點上,測試訪問172.20.10.10的情況:
3.2當down掉node5節點上nginx服務時,觀察vip地址和其訪問情況:
vip地址飄到node6節點上,curl172.20.10.10,則訪問的時node6節點
3.3當恢復node5節點上nginx服務時,vip地址飄到node5節點上,訪問172.20.10.10則是node5上nginx
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。