keepalived + rsync +inotify-tools實現雙機熱備
阿新 • • 發佈:2019-02-18
## 1.需求
實現資料交換系統的主備自動切換功能,當資料交換的master主機故障,網路故障以及資料交換mysql服務出現故障時,能自動切換到備機執行。
## 2.實現思路
採用keepalived+rsync+ inotify雙backup模式,並設定不搶佔資源。當master掛機,切換為backup,完成修復後,不會搶佔為maste!避免不必要的切換。 假設兩臺主機如下。
• master host:192.168.1.151,debain
• backup host:192.168.1.152,debain
安裝步驟如下:
1. 安裝apt-get install keepalived
2. 安裝apt-get install rsync
3. 安裝apt-get install inotify-tools
4. 啟動
## 3.安裝keeplived
```
apt-get install keeplived
```
#### 修改master主機上的/etc/keepalived/keepalived.conf配置檔案
```
cat /etc/keepalived/keepalived.conf
```
```
! Configuration File for keepalived
global_defs {
router_id xchange_router # all node must same
}
vrrp_instance xchange {
state BACKUP # master or backup
interface eth1 # 熱備通訊網口
virtual_router_id 55 # all node must same
priority 100 # 優先順序,高的先執行。主機異常時切換到低優先順序熱備機,副機一般設為99
advert_int 1 # send keepalived msg every 1 second
nopreempt # must need
authentication { # all node must same
auth_type PASS
auth_pass 1111
}
track_interface { #需要檢測的其它
eth0 #在些新增的網路介面會被檢測,eth0不正常會切換熱備服務
}
notify_master /etc/keepalived/to_master.sh #切換為master時執行
notify_backup /etc/keepalived/to_backup.sh #切換為backup時執行
}
virtual_server 127.0.0.1 3306 { #表示需要檢測的服務
delay_loop 2
lb_algo rr
lb_kind NAT
persistence_timeout 60
protocol TCP
real_server 127.0.0.1 3306 {
weight 3
notify_down /etc/keepalived/shutdown.sh #服務檢測失敗執行操作
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
```
#### 修改backup主機上的/etc/keepalived/keepalived.conf
```
! Configuration File for keepalived
global_defs {
router_id xchange_router # all node must same
}
vrrp_instance xchange {
state BACKUP # master or backup
interface eth1 # 熱備通訊網口
virtual_router_id 55 # all node must same
priority 99 # 優先順序,高的先執行。主機異常時切換到低優先順序熱備機,副機一般設為99
advert_int 1 # send keepalived msg every 1 second
#nopreempt # must need
authentication { # all node must same
auth_type PASS
auth_pass 1111
}
track_interface { #需要檢測的其它
eth0 #在些新增的網路介面會被檢測,eth0不正常會切換熱備服務
}
notify_master /etc/keepalived/to_master.sh #切換為master時執行
notify_backup /etc/keepalived/to_backup.sh #切換為backup時執行
}
virtual_server 127.0.0.1 3306 { #表示需要檢測的服務
delay_loop 2
lb_algo rr
lb_kind NAT
persistence_timeout 60
protocol TCP
real_server 127.0.0.1 3306 {
weight 3
notify_down /etc/keepalived/shutdown.sh #服務檢測失敗執行操作
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
```
#### 建立shell指令碼,放在/etc/keepalived目錄下
> 指令碼1:shutdown.sh
```
#!/bin/bash
echo "shutdown.sh" >> /var/log/messages
killall keepalived
/etc/init.d/manager stop
```
>指令碼2:to_master.sh
```
#!/bin/bash
echo "to_master" >> /var/log/messages
/etc/init.d/manager stop
/etc/init.d/manager start
```
>指令碼3:to_backup.sh
```
#!/bin/bash
echo "to_backup" >> /var/log/messages
/etc/init.d/manager stop
```
>指令碼4:keepalived_sync.sh,
```
#!/bin/bash
# usage: sh keepalived_sync.sh
USER=appbackup # the account to login rsyc server. Notice, it's not the linux user
IP=192.168.1.152 # the rsync server's ip.主備對應修改,192.168.1.151/192.168.1.152
DES="xchange"
SRC="/usr/app/xchange/repository"
PASSWD_FILE="/etc/rsync_client.secret"
LOGFILE="/var/log/messages.log"
/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move ${src} | while read file
do
/usr/bin/rsync -vzrtopg --delete --progress --password-file=$PASSWD_FILE $SRC [email protected]$IP::$DES
echo "${files} was rsynced" >>$LOGFILE 2>&1
done
```
#### keepalived的相關配置檔案儲存位置如下:
```
[email protected]:/etc/keepalived$ tree /etc/keepalived/
/etc/keepalived/
├── keepalived.conf
├── to_backup.sh
├── shutdown.sh
├── to_master.sh
└── keepalived_sync.sh
1 directory, 5 files
```
## 安裝Rsync
```
apt-get install rsync
```
#### 修改/etc/default/rsync檔案
```
RSYNC_ENABLE=true
```
#### 配置rsyncd的/etc/rsyncd.conf
```
cat /etc/rsyncd.conf
```
```
# sample rsyncd.conf configuration file
# GLOBAL OPTIONS
#motd file=/etc/motd
log file=/var/log/messages
# for pid file, do not use /var/run/rsync.pid if
# you are going to run rsync out of the init.d script.
# The init.d script does its own pid file handling,
# so omit the "pid file" line completely in that case.
# pid file=/var/run/rsyncd.pid
syslog facility=daemon
#socket options=
# MODULE OPTIONS
[xchange]
comment = public archive
path = /usr/app/xchange/repository
use chroot = yes
# max connections=10
lock file = /var/lock/rsyncd
# the default for read only is yes...
read only = no
list = yes
uid = root
gid = root
# exclude =
# exclude from =
# include =
# include from =
auth users =appbackup
secrets file = /etc/rsyncd.secrets
strict modes = yes
# hosts allow =
# hosts deny =
ignore errors = no
ignore nonreadable = yes
transfer logging = no
# log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes.
timeout = 600
refuse options = checksum dry-run
dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz
```
#### 修改/etc/rsync.secret
```
sudo cat /etc/rsync.secret
```
```
appbackup:passwd
```
> 修改/etc/rsync.secret檔案屬性:
```
chown root:root /etc/rsync.secret
chmod 600 /etc/rsync.secret
```
#### 啟動rsync伺服器
```
service rsync restart
```
#### 配置rsync客戶端,
> 檔案/etc/rsync_client.secret內容如下
```
passwd
```
> 修改檔案許可權
```
sudo chmod 600 /etc/rsync_client.secret
```
** 注意:如果不修改許可權,則會報錯ERROR: password file must not be other-accessible**
## 安裝 inotify-tools
```
apt-get install inotify-tools
```
## 5.啟動
#### 1. 啟動master主機上的rsync
```
service rsync restart
```
#### 2. 啟動master主機上的keepalived
```
service keepalived restart
```
#### 5. 檢視啟動日誌
```
tail -f /var/log/messages.log
```
## 6.自動切換測試
#### 確認keepalived、rsync已經啟動
```
ps -ef | grep keepalived
ps -ef | grep rsync
```
## 7.加入開機指令碼
#echo "/data/sh/inotifyrsync.sh &" >> /etc/rc.local
## 8.manager去掉開機啟動
#### 切換測試
> 1.停止master主機上的mysql程序,檢視master主機上的manager服務是否停止,檢查backup主機是否啟動manager服務。
> 2.或者關機master,查backup主機是否啟動manager服務。
執行流程說明:
預設情況下主副機器都不會執行manager服務,主副機啟動keepalived服務,首行執行優先順序高的主機to_master指令碼,啟動manager服務,如果出現mysql停止或宕機情況,會執行shutdown.sh停止熱備和manager服務,此時主副切換,副機執行to_master指令碼,過程中如果主機修復,主機會搶佔熱備服務,副機執行to_backup指令碼並停止manager服務,主機重新執行to_master並開啟manager
實現資料交換系統的主備自動切換功能,當資料交換的master主機故障,網路故障以及資料交換mysql服務出現故障時,能自動切換到備機執行。
## 2.實現思路
採用keepalived+rsync+ inotify雙backup模式,並設定不搶佔資源。當master掛機,切換為backup,完成修復後,不會搶佔為maste!避免不必要的切換。 假設兩臺主機如下。
• master host:192.168.1.151,debain
• backup host:192.168.1.152,debain
安裝步驟如下:
1. 安裝apt-get install keepalived
2. 安裝apt-get install rsync
3. 安裝apt-get install inotify-tools
4. 啟動
## 3.安裝keeplived
```
apt-get install keeplived
```
#### 修改master主機上的/etc/keepalived/keepalived.conf配置檔案
```
cat /etc/keepalived/keepalived.conf
```
```
! Configuration File for keepalived
global_defs {
router_id xchange_router # all node must same
}
vrrp_instance xchange {
state BACKUP # master or backup
interface eth1 # 熱備通訊網口
virtual_router_id 55 # all node must same
priority 100 # 優先順序,高的先執行。主機異常時切換到低優先順序熱備機,副機一般設為99
advert_int 1 # send keepalived msg every 1 second
nopreempt # must need
authentication { # all node must same
auth_type PASS
auth_pass 1111
}
track_interface { #需要檢測的其它
eth0 #在些新增的網路介面會被檢測,eth0不正常會切換熱備服務
}
notify_master /etc/keepalived/to_master.sh #切換為master時執行
notify_backup /etc/keepalived/to_backup.sh #切換為backup時執行
}
virtual_server 127.0.0.1 3306 { #表示需要檢測的服務
delay_loop 2
lb_algo rr
lb_kind NAT
persistence_timeout 60
protocol TCP
real_server 127.0.0.1 3306 {
weight 3
notify_down /etc/keepalived/shutdown.sh #服務檢測失敗執行操作
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
```
#### 修改backup主機上的/etc/keepalived/keepalived.conf
```
! Configuration File for keepalived
global_defs {
router_id xchange_router # all node must same
}
vrrp_instance xchange {
state BACKUP # master or backup
interface eth1 # 熱備通訊網口
virtual_router_id 55 # all node must same
priority 99 # 優先順序,高的先執行。主機異常時切換到低優先順序熱備機,副機一般設為99
advert_int 1 # send keepalived msg every 1 second
#nopreempt # must need
authentication { # all node must same
auth_type PASS
auth_pass 1111
}
track_interface { #需要檢測的其它
eth0 #在些新增的網路介面會被檢測,eth0不正常會切換熱備服務
}
notify_master /etc/keepalived/to_master.sh #切換為master時執行
notify_backup /etc/keepalived/to_backup.sh #切換為backup時執行
}
virtual_server 127.0.0.1 3306 { #表示需要檢測的服務
delay_loop 2
lb_algo rr
lb_kind NAT
persistence_timeout 60
protocol TCP
real_server 127.0.0.1 3306 {
weight 3
notify_down /etc/keepalived/shutdown.sh #服務檢測失敗執行操作
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
```
#### 建立shell指令碼,放在/etc/keepalived目錄下
> 指令碼1:shutdown.sh
```
#!/bin/bash
echo "shutdown.sh" >> /var/log/messages
killall keepalived
/etc/init.d/manager stop
```
>指令碼2:to_master.sh
```
#!/bin/bash
echo "to_master" >> /var/log/messages
/etc/init.d/manager stop
/etc/init.d/manager start
```
>指令碼3:to_backup.sh
```
#!/bin/bash
echo "to_backup" >> /var/log/messages
/etc/init.d/manager stop
```
>指令碼4:keepalived_sync.sh,
```
#!/bin/bash
# usage: sh keepalived_sync.sh
USER=appbackup # the account to login rsyc server. Notice, it's not the linux user
IP=192.168.1.152 # the rsync server's ip.主備對應修改,192.168.1.151/192.168.1.152
DES="xchange"
SRC="/usr/app/xchange/repository"
PASSWD_FILE="/etc/rsync_client.secret"
LOGFILE="/var/log/messages.log"
/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move ${src} | while read file
do
/usr/bin/rsync -vzrtopg --delete --progress --password-file=$PASSWD_FILE $SRC
echo "${files} was rsynced" >>$LOGFILE 2>&1
done
```
#### keepalived的相關配置檔案儲存位置如下:
```
[email protected]:/etc/keepalived$ tree /etc/keepalived/
/etc/keepalived/
├── keepalived.conf
├── to_backup.sh
├── shutdown.sh
├── to_master.sh
└── keepalived_sync.sh
1 directory, 5 files
```
## 安裝Rsync
```
apt-get install rsync
```
#### 修改/etc/default/rsync檔案
```
RSYNC_ENABLE=true
```
#### 配置rsyncd的/etc/rsyncd.conf
```
cat /etc/rsyncd.conf
```
```
# sample rsyncd.conf configuration file
# GLOBAL OPTIONS
#motd file=/etc/motd
log file=/var/log/messages
# for pid file, do not use /var/run/rsync.pid if
# you are going to run rsync out of the init.d script.
# The init.d script does its own pid file handling,
# so omit the "pid file" line completely in that case.
# pid file=/var/run/rsyncd.pid
syslog facility=daemon
#socket options=
# MODULE OPTIONS
[xchange]
comment = public archive
path = /usr/app/xchange/repository
use chroot = yes
# max connections=10
lock file = /var/lock/rsyncd
# the default for read only is yes...
read only = no
list = yes
uid = root
gid = root
# exclude =
# exclude from =
# include =
# include from =
auth users =appbackup
secrets file = /etc/rsyncd.secrets
strict modes = yes
# hosts allow =
# hosts deny =
ignore errors = no
ignore nonreadable = yes
transfer logging = no
# log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes.
timeout = 600
refuse options = checksum dry-run
dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz
```
#### 修改/etc/rsync.secret
```
sudo cat /etc/rsync.secret
```
```
appbackup:passwd
```
> 修改/etc/rsync.secret檔案屬性:
```
chown root:root /etc/rsync.secret
chmod 600 /etc/rsync.secret
```
#### 啟動rsync伺服器
```
service rsync restart
```
#### 配置rsync客戶端,
> 檔案/etc/rsync_client.secret內容如下
```
passwd
```
> 修改檔案許可權
```
sudo chmod 600 /etc/rsync_client.secret
```
** 注意:如果不修改許可權,則會報錯ERROR: password file must not be other-accessible**
## 安裝 inotify-tools
```
apt-get install inotify-tools
```
## 5.啟動
#### 1. 啟動master主機上的rsync
```
service rsync restart
```
#### 2. 啟動master主機上的keepalived
```
service keepalived restart
```
#### 5. 檢視啟動日誌
```
tail -f /var/log/messages.log
```
## 6.自動切換測試
#### 確認keepalived、rsync已經啟動
```
ps -ef | grep keepalived
ps -ef | grep rsync
```
## 7.加入開機指令碼
#echo "/data/sh/inotifyrsync.sh &" >> /etc/rc.local
## 8.manager去掉開機啟動
#### 切換測試
> 1.停止master主機上的mysql程序,檢視master主機上的manager服務是否停止,檢查backup主機是否啟動manager服務。
> 2.或者關機master,查backup主機是否啟動manager服務。
執行流程說明:
預設情況下主副機器都不會執行manager服務,主副機啟動keepalived服務,首行執行優先順序高的主機to_master指令碼,啟動manager服務,如果出現mysql停止或宕機情況,會執行shutdown.sh停止熱備和manager服務,此時主副切換,副機執行to_master指令碼,過程中如果主機修復,主機會搶佔熱備服務,副機執行to_backup指令碼並停止manager服務,主機重新執行to_master並開啟manager