久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

MySQL高可用之keepalived方案的示例分析

148次閱讀
沒有評論

共計 11048 個字符,預計需要花費 28 分鐘才能閱讀完成。

這篇文章主要為大家展示了“MySQL 高可用之 keepalived 方案的示例分析”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓丸趣 TV 小編帶領大家一起研究并學習一下“MySQL 高可用之 keepalived 方案的示例分析”這篇文章吧。

 
實驗環境
    mysql master   : 192.168.111.52
    mysql slave    : 192.168.111.53
    keepalived vip :  192.168.111.60
、搭建過程
    1.  mysql 雙主的構建
    ① 互相 change master 即可,此處省略該過程,著重講下 keepalived

    2. keepalived 相關
    ① yum -y install keepalived 安裝 keepalived
 ② root@192.168.111.52:~# keepalived -v
 Keepalived v1.2.7 (02/21,2013)

    ③ 編輯 /etc/keepalived/keepalived.conf   

點擊 (此處) 折疊或打開

root@192.168.111.52:keepalived# cat keepalived.conf

! Configuration File for keepalived

global_defs {## 全局配置

 notification_email {

 horand_gc@163.com    ##### 郵件接收者

 }

 notification_email_from dba@163.com ##### 郵件發送者

 smtp_server smtp.163.com   #####SMTP 服務器

 smtp_connect_timeout 30

 router_id haMySQL  #####routerID 同一組 keepalived 設置為相同

}

vrrp_script chk_mysql {## 健康檢測相關配置

 script /etc/keepalived/chk_mysql.sh ##### 設置腳本 或者直命令   返回 0 則表示成功 0 則表示失敗,詳情可以后面的腳本內容

 interval 1    ##### 檢測間隔

 weight 2  ##### 檢測返回失敗之后優先級會減少 2 (如果 master 優先級 100,slave 優先級 99 ,master 檢測失敗則優先級為 100-2 99,則 slave 會提升為主)

}

vrrp_instance VI_1 {

 state MASTER  ##### 設為 master

 interface eth2    ##### 網卡設定

 virtual_router_id 51  ##### 針對該 instance 的虛擬 id  , 同一組 instance 設置相同

 priority 100  ##### 優先級設定

 advert_int 1      ##### 檢測時間間隔

 authentication {

 auth_type PASS    ##### 同一組 instance 之間的認證方式為 PASS ,pass 為 7777,必須相同(防止 有用戶惡意偽造 vrrp)

 auth_pass 7777

 }

 virtual_ipaddress {

 192.168.111.60    ##### 設置虛擬 ip , 可以設置多個

 }

 

 track_script {

 chk_mysql     ##### 表示該 instance 使用 chk_mysql 進行相關檢測

 }

  ##### 以下配置 在該實例 轉換為 master,slave,或者出錯的時候執行的腳本(可以設置郵件通知,或者處理一些其他問題)

   # notify_master /etc/keepalived/change_master.sh

   # notify_slave /etc/keepalived/change_slave.sh

   # notify_fault /etc/keepalived/change_fault.sh

}

root@192.168.111.53:keepalived# cat keepalived.conf

! Configuration File for keepalived

global_defs {

 notification_email {

 horand_gc@163.com

 }

 notification_email_from dba@163.com

 smtp_server smtp.163.com

 smtp_connect_timeout 30

 router_id haMySQL

}

vrrp_script chk_mysql {

 script /etc/keepalived/chk_mysql.sh

 interval 1

 weight 2

}

vrrp_instance VI_1 {

 state BACKUP    ##### 該主機作為備機 BACKUP 

 interface eth2

 virtual_router_id 51

 priority 99     ##### 優先級設置 小于 master

 advert_int 1

 authentication {

 auth_type PASS

 auth_pass 7777

 }

 virtual_ipaddress {

 192.168.111.60

 }

 

 track_script {

 chk_mysql

 }

}

root@192.168.111.52:keepalived# cat chk_mysql.sh

#!/bin/bash

num=`ps -ef |grep mysqld | grep -v grep | wc -l`    ##### 查看 mysqld 進程數量,正常情況有一個 root 起的 mysqld_safe 守護進程,還有一個屬于 mysql 用戶的 mysqld 進程

[[$num -eq 2]] exit 0 || exit 1

    3. 故障模擬
   ① 啟動 keepalived   

點擊 (此處) 折疊或打開

root@192.168.111.52:keepalived# tail /var/log/messages

Apr 29 13:45:24 localhost Keepalived_healthcheckers[24184]: Registering Kernel netlink command channel

Apr 29 13:45:24 localhost Keepalived_healthcheckers[24184]: Opening file /etc/keepalived/keepalived.conf .

Apr 29 13:45:24 localhost Keepalived_healthcheckers[24184]: Configuration is using : 7417 Bytes

Apr 29 13:45:24 localhost Keepalived_vrrp[24185]: Opening file /etc/keepalived/keepalived.conf .

Apr 29 13:45:24 localhost Keepalived_vrrp[24185]: Configuration is using : 65552 Bytes

Apr 29 13:45:24 localhost Keepalived_vrrp[24185]: Using LinkWatch kernel netlink reflector…

Apr 29 13:45:24 localhost Keepalived_healthcheckers[24184]: Using LinkWatch kernel netlink reflector…

Apr 29 13:45:25 localhost Keepalived_vrrp[24185]: VRRP_Script(chk_mysql) succeeded

Apr 29 13:45:25 localhost Keepalived_vrrp[24185]: VRRP_Instance(VI_1) Transition to MASTER STATE

Apr 29 13:45:26 localhost Keepalived_vrrp[24185]: VRRP_Instance(VI_1) Entering MASTER STATE

root@192.168.111.53:keepalived# tailf /var/log/messages

Apr 29 13:49:27 localhost Keepalived_healthcheckers[32569]: Registering Kernel netlink reflector

Apr 29 13:49:27 localhost Keepalived_healthcheckers[32569]: Registering Kernel netlink command channel

Apr 29 13:49:27 localhost Keepalived_vrrp[32570]: Opening file /etc/keepalived/keepalived.conf .

Apr 29 13:49:27 localhost Keepalived_vrrp[32570]: Configuration is using : 65550 Bytes

Apr 29 13:49:27 localhost Keepalived_vrrp[32570]: Using LinkWatch kernel netlink reflector…

Apr 29 13:49:27 localhost Keepalived_healthcheckers[32569]: Opening file /etc/keepalived/keepalived.conf .

Apr 29 13:49:27 localhost Keepalived_healthcheckers[32569]: Configuration is using : 7415 Bytes

Apr 29 13:49:27 localhost Keepalived_vrrp[32570]: VRRP_Instance(VI_1) Entering BACKUP STATE

Apr 29 13:49:27 localhost Keepalived_healthcheckers[32569]: Using LinkWatch kernel netlink reflector…

Apr 29 13:49:27 localhost Keepalived_vrrp[32570]: VRRP_Script(chk_mysql) succeeded

    ip a 可以查看到 vip 192.168.111.60 在 192.168.111.52(master)上
 
    ② 關閉 111.52 上面的 mysql

點擊 (此處) 折疊或打開

root@192.168.111.52:keepalived# mysqladmin shutdown

root@192.168.111.52:keepalived# tailf /var/log/messages

Apr 29 14:19:30 localhost Keepalived_vrrp[24862]: VRRP_Script(chk_mysql) failed

Apr 29 14:19:32 localhost Keepalived_vrrp[24862]: VRRP_Instance(VI_1) Received higher prio advert

Apr 29 14:19:32 localhost Keepalived_vrrp[24862]: VRRP_Instance(VI_1) Entering BACKUP STATE

root@192.168.111.53:keepalived# tailf /var/log/messages

Apr 29 14:19:55 localhost Keepalived_vrrp[566]: VRRP_Instance(VI_1) forcing a new MASTER election

Apr 29 14:19:56 localhost Keepalived_vrrp[566]: VRRP_Instance(VI_1) Transition to MASTER STATE

Apr 29 14:19:57 localhost Keepalived_vrrp[566]: VRRP_Instance(VI_1) Entering MASTER STATE

    ip a 可以查看到 vip 192.168.111.60 在 192.168.111.53(master)上

    ③ 啟動 111.52 上面的 mysql

點擊 (此處) 折疊或打開

root@192.168.111.52:keepalived# mysqld_safe  tailf /var/log/messages

Apr 29 14:24:21 localhost Keepalived_vrrp[24862]: VRRP_Script(chk_mysql) succeeded

Apr 29 14:24:22 localhost Keepalived_vrrp[24862]: VRRP_Instance(VI_1) forcing a new MASTER election

Apr 29 14:24:23 localhost Keepalived_vrrp[24862]: VRRP_Instance(VI_1) Transition to MASTER STATE

Apr 29 14:24:24 localhost Keepalived_vrrp[24862]: VRRP_Instance(VI_1) Entering MASTER STATE

root@192.168.111.53:keepalived# tailf /var/log/messages

Apr 29 14:24:45 localhost Keepalived_vrrp[566]: VRRP_Instance(VI_1) Received higher prio advert

Apr 29 14:24:45 localhost Keepalived_vrrp[566]: VRRP_Instance(VI_1) Entering BACKUP STATE

    ip a 可以查看到 vip 192.168.111.60 在 192.168.111.52(master)上,也就是說 111.52 會持續通過 track_script 的腳本檢查,若成功的話會恢復原來的優先級 100,便把 vip 搶過來了(若不希望優先級高的直接上來直接搶占 vip 的話 需要再 instance 里面配置 nopreempt ,backup 無需設置)

虛擬 server
  以上是實驗是通過 vrrp_script 以及 trace_script 實現優先級變換來實現故障轉移的,現在看下 通過虛擬 server 怎么實現 mysql 的高可用
    ① 配置

點擊 (此處) 折疊或打開

root@192.168.111.52:keepalived# cat /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

 notification_email {

 tab@taomee.com

 }

 notification_email_from dba@taomee.com

 smtp_server smtp.shidc.taomee.com

 smtp_connect_timeout 30

 router_id haMySQL

}

vrrp_script chk_mysql {

 script /etc/keepalived/chk_mysql.sh

 interval 1

 weight 2

}

vrrp_instance VI_1 {

 state MASTER

 interface eth2

 virtual_router_id 51

 priority 100

 nopreempt

 advert_int 1

 authentication {

 auth_type PASS

 auth_pass 7777

 }

 virtual_ipaddress {

 192.168.111.60

 }

 

#    track_script {

# chk_mysql

#    }

}

virtual_server 192.168.111.60 3306 {

 delay_loop 6

 persistence_timeout 300

 protocol TCP

 real_server 192.168.111.52 3306 {

 weight 1

        notify_down /etc/keepalived/kill_self.sh

 TCP_CHECK {

     tcp_port 3306

 connect_timeout 3

 }

 }

}

root@192.168.111.53:keepalived# cat /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

 notification_email {

 tab@taomee.com

 }

 notification_email_from dba@taomee.com

 smtp_server smtp.shidc.taomee.com

 smtp_connect_timeout 30

 router_id haMySQL

}

vrrp_script chk_mysql {

 script /etc/keepalived/chk_mysql.sh

 interval 1

 weight 2

}

vrrp_instance VI_1 {

 state BACKUP

 interface eth2

 virtual_router_id 51

 priority 99

 advert_int 1

 authentication {

 auth_type PASS

 auth_pass 7777

 }

 virtual_ipaddress {

 192.168.111.60

 }

 

#    track_script {    ######### 這里先注釋掉 通過追蹤腳本的檢查

#        chk_mysql

#    }

}

virtual_server 192.168.111.60 3306 {

 delay_loop 6

 persistence_timeout 300

 protocol TCP

 real_server 192.168.111.53 3306 {### 真實 服務

 weight 1    #### 權重,用來多真實服務 均衡使用

        notify_down /etc/keepalived/kill_self.sh    #### 在檢查該服務不可用時執行該腳本(用來殺死 keepalived 實現 vip 飄逸)

 TCP_CHECK {

 tcp_port 3306   #### 檢查端口 繼承  real_server 192.168.111.53 3306 {###  真實 服務

 connect_timeout 3   #### tcp 超時時間

 }

 }

}

root@192.168.111.53:keepalived# cat /etc/keepalived/kill_self.sh

#!/bin/bash

killall keepalived

    ②啟動 keepalived

點擊 (此處) 折疊或打開

root@192.168.111.52:keepalived# keepalived

root@192.168.111.52:keepalived# tailf /var/log/messages

Apr 29 14:48:22 localhost Keepalived_vrrp[20482]: Opening file /etc/keepalived/keepalived.conf .

Apr 29 14:48:22 localhost Keepalived_vrrp[20482]: Configuration is using : 64590 Bytes

Apr 29 14:48:22 localhost Keepalived_vrrp[20482]: Using LinkWatch kernel netlink reflector…

Apr 29 14:48:22 localhost Keepalived_healthcheckers[20481]: IPVS: Scheduler not found

Apr 29 14:48:22 localhost kernel: IPVS: Scheduler module ip_vs_ not found

Apr 29 14:48:22 localhost Keepalived_healthcheckers[20481]: IPVS: Service not defined

Apr 29 14:48:22 localhost Keepalived_healthcheckers[20481]: Using LinkWatch kernel netlink reflector…

Apr 29 14:48:22 localhost Keepalived_healthcheckers[20481]: Activating healthchecker for service [192.168.111.52]:3306

Apr 29 14:48:23 localhost Keepalived_vrrp[20482]: VRRP_Instance(VI_1) Transition to MASTER STATE

Apr 29 14:48:24 localhost Keepalived_vrrp[20482]: VRRP_Instance(VI_1) Entering MASTER STATE

root@192.168.111.53:keepalived# keepalived

root@192.168.111.53:keepalived# tailf /var/log/messages

Apr 29 14:48:51 localhost Keepalived_healthcheckers[25092]: Configuration is using : 11673 Bytes

Apr 29 14:48:51 localhost Keepalived_vrrp[25093]: Opening file /etc/keepalived/keepalived.conf .

Apr 29 14:48:51 localhost Keepalived_vrrp[25093]: Configuration is using : 64568 Bytes

Apr 29 14:48:51 localhost Keepalived_vrrp[25093]: Using LinkWatch kernel netlink reflector…

Apr 29 14:48:51 localhost Keepalived_vrrp[25093]: VRRP_Instance(VI_1) Entering BACKUP STATE

Apr 29 14:48:51 localhost kernel: IPVS: Scheduler module ip_vs_ not found

Apr 29 14:48:51 localhost Keepalived_healthcheckers[25092]: IPVS: Scheduler not found

Apr 29 14:48:51 localhost Keepalived_healthcheckers[25092]: IPVS: Service not defined

Apr 29 14:48:51 localhost Keepalived_healthcheckers[25092]: Using LinkWatch kernel netlink reflector…

Apr 29 14:48:51 localhost Keepalived_healthcheckers[25092]: Activating healthchecker for service [192.168.111.53]:3306

  此時 ip a 命令可以查看虛擬 ip 111.60 在 111.52(master) 上
   
 ③關閉 111.52 上的 mysql

點擊 (此處) 折疊或打開

root@192.168.111.52:keepalived# mysqladmin shutdown

2017-04-29T07:07:38.121123Z mysqld_safe mysqld from pid file /opt/mysql/mysqld.pid ended

[1]+  Done                    mysqld_safe

root@192.168.111.52:keepalived# tailf /var/log/messages

Apr 29 15:07:31 localhost Keepalived_healthcheckers[23405]: TCP connection to [192.168.111.52]:3306 failed !!!

Apr 29 15:07:31 localhost Keepalived_healthcheckers[23405]: Removing service [192.168.111.52]:3306 from VS [192.168.111.60]:3306

Apr 29 15:07:31 localhost Keepalived_healthcheckers[23405]: IPVS: Service not defined

Apr 29 15:07:31 localhost Keepalived_healthcheckers[23405]: Executing [/etc/keepalived/kill_self.sh] for service [192.168.111.52]:3306 in VS [192.168.111.60]:3306

Apr 29 15:07:31 localhost Keepalived_healthcheckers[23405]: Lost quorum 1-0=1 0 for VS [192.168.111.60]:3306

Apr 29 15:07:31 localhost Keepalived_healthcheckers[23405]: Remote SMTP server [0.0.0.0]:25 connected.

Apr 29 15:07:31 localhost Keepalived[23404]: Stopping Keepalived v1.2.7 (02/21,2013)

Apr 29 15:07:31 localhost Keepalived_healthcheckers[23405]: IPVS: No such service

Apr 29 15:07:31 localhost Keepalived_vrrp[23406]: VRRP_Instance(VI_1) sending 0 priority

root@192.168.111.53:keepalived# tailf /var/log/messages

Apr 29 15:07:32 localhost Keepalived_vrrp[26815]: VRRP_Instance(VI_1) Transition to MASTER STATE

Apr 29 15:07:33 localhost Keepalived_vrrp[26815]: VRRP_Instance(VI_1) Entering MASTER STATE

    此時 ip a 命令可以看到虛擬 ip 111.60 在 111.53(新 master)上

以上是“MySQL 高可用之 keepalived 方案的示例分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注丸趣 TV 行業資訊頻道!

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-07-26發表,共計11048字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 沐川县| 白玉县| 额敏县| 北票市| 麻城市| 乐业县| 灵寿县| 太仓市| 成武县| 铜陵市| 当阳市| 保康县| 铁岭市| 绍兴市| 格尔木市| 偏关县| 长寿区| 蕉岭县| 老河口市| 乌鲁木齐市| 无锡市| 龙州县| 扎鲁特旗| 武定县| 西城区| 郎溪县| 邛崃市| 旅游| 尚义县| 鄱阳县| 安丘市| 贡觉县| 丘北县| 嘉义市| 仲巴县| 理塘县| 宜城市| 延边| 乳源| 封开县| 锡林浩特市|