【Mysql】keepalive的安裝與配置
參考文件:
http://blog.jobbole.com/100390/
http://lanlian.blog.51cto.com/6790106/1303195
環境:主庫:192.168.6.119
備庫:192.168.6.121
vip: 192.168.6.120
1 master與slave端都安裝keepalive
2. 配置 keepalived
上面的指令碼簡單的檢視http程式是否存在,不存在就退出返回假。
實驗二:
實驗一二的區別:一個是透過權重來控制vip的飄逸。一個直接是控制keepalive程式的存活來控制vip的飄逸
3.如何在狀態轉換時進行通知?
參考文件
http://kaifly.blog.51cto.com/3209616/1665729 --MySQL主庫高可用 -- 雙主單活故障自動切換方案
---結合keepalived實現redis群集高可用故障自動切換
http://blog.jobbole.com/100390/
http://lanlian.blog.51cto.com/6790106/1303195
環境:主庫:192.168.6.119
備庫:192.168.6.121
vip: 192.168.6.120
1 master與slave端都安裝keepalive
-
[root@MYSQL src]# wget http://www.keepalived.org/software/keepalived-1.2.12.tar.gz
-
[root@MYSQL src]# tar -xf keepalived-1.2.12.tar.gz
-
[root@MYSQL src]# cd keepalived-1.2.12
-
[root@MYSQL src]# yum -y install gcc gcc-c++ gcc-g77 ncurses-devel bison libaio-devel cmake libnl* libpopt* popt-static openssl-devel
-
[root@MYSQL keepalived-1.2.12]# ./configure
-
[root@MYSQL keepalived-1.2.12]# make && make install
-
[root@MYSQL src]#mkdir /etc/keepalived/
-
-
[root@MYSQL src]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
-
[root@MYSQL src]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
-
[root@MYSQL src]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
- [root@MYSQL src]#cp /usr/local/sbin/keepalived /usr/sbin/
2. 配置 keepalived
-
[root@MYSQL keepalived]# vi keepalived.conf
-
! Configuration File for keepalived
-
-
global_defs {
-
notification_email {
-
acassen@firewall.loc
-
failover@firewall.loc
-
sysadmin@firewall.loc
-
}
-
notification_email_from Alexandre.Cassen@firewall.loc
-
smtp_server 192.168.200.1
-
smtp_connect_timeout 30
-
router_id LVS_DEVEL ##配置是為了標識當前節點,兩個節點的此項設定可相同,也可不相同
-
}
-
vrrp_script chk_http_port { ---配合指令碼使用,比如說檢查nginx是否存活,檢查mysql是否存活等
script "/usr/local/keepalived/nginx.sh" #在這裡新增指令碼連結
interval 3 #指令碼執行間隔
weight -2 #指令碼結果導致的優先順序變更 ###如果檢測返回值不為真weight -2 表示減2,權重值降低,backup server權重值(99)>現Master(100-2)的,切換
}
-
-
vrrp_instance VI_1 {
-
state MASTER #指定A節點為主節點 備用節點上設定為BACKUP即可
-
interface eth0
-
virtual_router_id 51 #VRRP組名,兩個節點的設定必須一樣,以指明各個節點屬於同一VRRP
-
priority 100 #主節點的優先順序(1-254之間),備用節點必須比主節點優先順序低
-
###nopreempt ###這個引數也可以防止切換到從庫後,主keepalived恢復後自動切換回主庫
-
advert_int 1
-
authentication { #設定驗證資訊,兩個節點必須一致
-
auth_type PASS
-
auth_pass 1111
-
}
-
track_script {
chk_http_port #新增指令碼執行
}
-
virtual_ipaddress {
-
192.168.6.120 =====vip,指定一個或多個vip
-
}
-
}
-
-
[root@MYSQL keepalived]# vi keepalived.conf
-
! Configuration File for keepalived
-
-
global_defs {
-
notification_email {
-
acassen@firewall.loc
-
failover@firewall.loc
-
sysadmin@firewall.loc
-
}
-
notification_email_from Alexandre.Cassen@firewall.loc
-
smtp_server 192.168.200.1
-
smtp_connect_timeout 30
-
router_id LVS_DEVEL ##配置是為了標識當前節點,兩個節點的此項設定可相同,也可不相同
-
}
-
vrrp_script chk_http_port { ---配合指令碼使用,比如說檢查nginx是否存活,檢查mysql是否存活等
script "/usr/local/keepalived/nginx.sh" #在這裡新增指令碼連結
interval 3 #指令碼執行間隔
weight -2 #指令碼結果導致的優先順序變更 ###如果檢測返回值不為真weight -2 表示減2,權重值降低,backup server權重值(99)>現Master(100-2)的,切換
}
-
-
vrrp_instance VI_1 {
- state MASTER #指定A節點為主節點 備用節點上設定為BACKUP即可
-
interface eth0
-
virtual_router_id 51 #VRRP組名,兩個節點的設定必須一樣,以指明各個節點屬於同一VRRP
-
priority 100 #主節點的優先順序(1-254之間),備用節點必須比主節點優先順序低
- ###nopreempt ###這個引數也可以防止切換到從庫後,主keepalived恢復後自動切換回主庫
-
advert_int 1
-
authentication { #設定驗證資訊,兩個節點必須一致
-
auth_type PASS
-
auth_pass 1111
-
}
-
track_script {
chk_http_port #新增指令碼執行
}
chk_http_port #新增指令碼執行
}
-
virtual_ipaddress {
-
192.168.6.120 =====vip,指定一個或多個vip
-
}
- }
上面的指令碼簡單的檢視http程式是否存在,不存在就退出返回假。
點選(此處)摺疊或開啟
-
#!/bin/bash
A=`netstat -nputl|grep -i 80 |wc -l`
if [ $A -eq 0 ];then
exit 1;
#/etc/init.d/keepalived stop
fi
- 實驗一:
- master與backup ip分別如下
-
[root@node1 keepalived]# ip a
2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 08:00:27:7a:c6:9c brd ff:ff:ff:ff:ff:ff
inet 192.168.6.114/24 brd 192.168.6.255 scope global eth0
inet 192.168.6.120/32 scope global eth0
inet6 fe80::a00:27ff:fe7a:c69c/64 scope link
valid_lft forever preferred_lft forever
[root@node2 keepalived]# ip a
2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 08:00:27:03:1b:a1 brd ff:ff:ff:ff:ff:ff
inet 192.168.6.115/24 brd 192.168.6.255 scope global eth0
inet6 fe80::a00:27ff:fe03:1ba1/64 scope link
valid_lft forever preferred_lft forever
-
- 關閉主庫http
-
[root@node1 keepalived]# /etc/init.d/httpd stop
Stopping httpd: [ OK ]
- 主庫與從庫ip:
-
[root@node1 keepalived]# ip a
2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 08:00:27:7a:c6:9c brd ff:ff:ff:ff:ff:ff
inet 192.168.6.114/24 brd 192.168.6.255 scope global eth0
inet6 fe80::a00:27ff:fe7a:c69c/64 scope link
valid_lft forever preferred_lft forever -
[root@node2 keepalived]# ip a
2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 08:00:27:03:1b:a1 brd ff:ff:ff:ff:ff:ff
inet 192.168.6.115/24 brd 192.168.6.255 scope global eth0
inet 192.168.6.120/32 scope global eth0 ---vip飄到從庫了
inet6 fe80::a00:27ff:fe03:1ba1/64 scope link
valid_lft forever preferred_lft forever
- 恢復主庫http
-
[root@node1 keepalived]# /etc/init.d/httpd start
Starting httpd: [ OK ] - 主庫與從庫ip:
-
[root@node1 keepalived]# ip a
2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 08:00:27:7a:c6:9c brd ff:ff:ff:ff:ff:ff
inet 192.168.6.114/24 brd 192.168.6.255 scope global eth0
inet 192.168.6.120/32 scope global eth0 ---vip又飄回主庫了
inet6 fe80::a00:27ff:fe7a:c69c/64 scope link
valid_lft forever preferred_lft forever
[root@node2 keepalived]# ip a
2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 08:00:27:03:1b:a1 brd ff:ff:ff:ff:ff:ff
inet 192.168.6.115/24 brd 192.168.6.255 scope global eth0
inet6 fe80::a00:27ff:fe03:1ba1/64 scope link
valid_lft forever preferred_lft forever -
nopreempt 引數的作用:靜止vip再飄回來(少用,根據情況配置)-
node1 node2 vip漂移過程(關閉主庫http,恢復主庫http) 原因 無nopreempt 無nopreempt
node1》node2》node1
nopreempt
無nopreempt
node1》node2》node2(vip不飄了) node1靜止vip飄回去
無nopreempt
nopreempt 一直在node1上,飄不動 node2靜止vip飄過去 nopreempt
nopreempt
一直在nide1上,漂不動 node2靜止vip飄過去
-
實驗二:
-
配置檔案為
-
[root@node1 keepalived]# more keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/usr/local/keepalived/nginx.sh"
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port
}
virtual_ipaddress {
192.168.6.120
}
}
-
[root@node1 keepalived]# more keepalived.conf
-
-
#!/bin/bash
A=`netstat -nputl|grep -i 80 |wc -l`
if [ $A -eq 0 ];then
#exit 1;
/etc/init.d/keepalived stop
fi
和實驗一的效果一樣:vip先飄到從庫,恢復http後,啟動keepalive後又飄回主庫,要想不飄回主庫,改小權重 -
#!/bin/bash
實驗一二的區別:一個是透過權重來控制vip的飄逸。一個直接是控制keepalive程式的存活來控制vip的飄逸
3.如何在狀態轉換時進行通知?
- 下面用一個指令碼notify.sh實現狀態轉換通知的簡單示例:
-
-
#!/bin/bash
-
#Author: MageEdu <linuxedu@foxmail.com>
-
#description: An example of notify script
-
#
-
vip=192.168.6.120
-
contact='root@localhost'
-
notify(){
-
mailsubject="`hostname` to be $1: $vip floating"
-
mailbody="`date '+%F %H:%M:%S'`: vrrptransition, `hostname` changed to be $1"
-
echo $mailbody | mail -s"$mailsubject" $contact
-
}
-
case "$1" in
-
master)
-
notify master
-
exit 0
-
;;
-
backup)
-
notify backup
-
exit 0
-
;;
-
fault)
-
notify fault
-
exit 0
-
;;
-
*)
-
echo 'Usage: `basename $0`{master|backup|fault}'
-
exit 1
-
;;
- esac
chmod +x notify.sh -
#!/bin/bash
-
主從配置檔案修改
-
[root@node1 keepalived]# more keepalived.conf
-
! Configuration File for keepalived
-
-
global_defs {
-
notification_email {
-
acassen@firewall.loc
-
failover@firewall.loc
-
sysadmin@firewall.loc
-
}
-
notification_email_from Alexandre.Cassen@firewall.loc
-
smtp_server 192.168.200.1
-
smtp_connect_timeout 30
-
router_id LVS_DEVEL
-
}
-
-
vrrp_script chk_http_port {
-
script "/usr/local/keepalived/nginx.sh"
-
interval 3
-
weight -2
-
}
-
-
vrrp_instance VI_1 {
-
state MASTER
-
interface eth0
-
virtual_router_id 51
-
priority 99
-
# nopreempt
-
advert_int 1
-
authentication {
-
auth_type PASS
-
auth_pass 1111
-
}
-
-
track_script {
-
chk_http_port
-
}
-
-
-
notify_master "/usr/local/keepalived/notify.sh master"
-
notify_backup"/usr/local/keepalived/notify.sh backup"
-
notify_fault"/usr/local/keepalived/notify.sh fault"
-
-
virtual_ipaddress {
-
192.168.6.120
-
}
- }
-
[root@node1 keepalived]# more keepalived.conf
- 測試:
-
-
關閉主庫http服務
-
[root@node1 ~]# /etc/init.d/httpd stop
-
Stopping httpd: [ OK ]
-
[root@node1 ~]# mail
-
Mail version 8.1 6/6/93. Type ? for help.
-
"/var/spool/mail/root": 1 message 1 new
- >N 1 root@localhost.local Thu Jun 30 21:10 16/716 "node1 to be backup:"
-
-
[root@node2 keepalived]# mail
Mail version 8.1 6/6/93. Type ? for help.
"/var/spool/mail/root": 1 message 1 new
>N 1 root@localhost.local Thu Jun 30 21:10 16/716 "node2 to be master:"
- 恢復主庫http服務
-
[root@node1 ~]# mail
Mail version 8.1 6/6/93. Type ? for help.
"/var/spool/mail/root": 2 messages 1 new 2 unread
U 1 root@localhost.local Thu Jun 30 21:10 17/726 "node1 to be backup:"
>N 2 root@localhost.local Thu Jun 30 21:12 16/716 "node1 to be master:" -
[root@node2 keepalived]# mail
Mail version 8.1 6/6/93. Type ? for help.
"/var/spool/mail/root": 2 messages 1 new 2 unread
U 1 root@localhost.local Thu Jun 30 21:10 17/726 "node2 to be master:"
>N 2 root@localhost.local Thu Jun 30 21:12 16/716 "node2 to be backup:"
-
關閉主庫http服務
參考文件
http://kaifly.blog.51cto.com/3209616/1665729 --MySQL主庫高可用 -- 雙主單活故障自動切換方案
---結合keepalived實現redis群集高可用故障自動切換
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29096438/viewspace-2121347/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MYSQL + MHA +keepalive + VIP安裝配置(一)--MYSQL安裝配置MySql
- MYSQL + MHA +keepalive + VIP安裝配置(二)--MHA的配置MySql
- MYSQL + MHA +keepalive + VIP安裝配置(三)--keepalived安裝配置MySql
- 【MySQL】安裝與配置MySql
- MySQL安裝與配置MySql
- CentOs中mysql的安裝與配置CentOSMySql
- MySQL的安裝與配置——詳細教程MySql
- Mysql安裝與配置_win10MySqlWin10
- CentOS6下mysql的安裝與配置CentOSMySql
- 免安裝版MySQL的優化與配置MySql優化
- Centos6.2中mysql的安裝與配置CentOSMySql
- 安裝與配置MySQL,phpmyAdmin;安裝與測試PHPLIB (轉)MySqlPHP
- MySQL安裝配置MySql
- Mysql 安裝 配置MySql
- macOS下MySQL 8.0 安裝與配置教程MacMySql
- PPTP + FreeRADIUS + MySQL 安裝與配置pptpMySql
- MySQL - 下載與安裝配置(Linux)MySqlLinux
- CentOS下Mysql資料庫的安裝與配置CentOSMySql資料庫
- 本地windows搭建spark環境,安裝與詳細配置(jdk安裝與配置,scala安裝與配置,hadoop安裝與配置,spark安裝與配置)WindowsSparkJDKHadoop
- centos7 (阿里雲、linux) 單機spark的安裝與配置詳解(jdk安裝與配置,scala安裝與配置,hadoop安裝與配置,spark安裝與配置)CentOS阿里LinuxSparkJDKHadoop
- keepalive配置mysql自動故障轉移MySql
- Windows下 MySQL-5.7.11 安裝與配置WindowsMySql
- mantis安裝與配置(Windows+Mysql+PHP+IIS)WindowsMySqlPHP
- 3安裝配置mysqlMySql
- mysql安裝及配置MySql
- mysql 非安裝版的配置MySql
- MySQL - 安裝時的安全配置MySql
- Hive的安裝與配置Hive
- PG的安裝與配置
- yum的安裝與配置
- Redis的安裝與配置Redis
- Vue的安裝與配置Vue
- 全面梳理 簡明安裝指南 mysql 8 安裝升級與配置MySql
- WAS與IHS整合的安裝與配置
- 【MySQL資料庫】MySQL5.7安裝與配置、視覺化工具安裝和破解MySql資料庫視覺化
- 教程:MySQL 8安裝與配置及注意事項MySql
- MySQL綠色解壓縮版安裝與配置MySql
- Windows平臺下安裝與配置MySQL9WindowsMySql