真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

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

這篇文章主要為大家展示了“MySQL高可用之keepalived方案的示例分析”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“MySQL高可用之keepalived方案的示例分析”這篇文章吧。

成都創(chuàng)新互聯(lián)公司專注于二道江企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站設(shè)計(jì),成都做商城網(wǎng)站。二道江網(wǎng)站建設(shè)公司,為二道江等地區(qū)提供建站服務(wù)。全流程定制設(shè)計(jì),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)


    
實(shí)驗(yàn)環(huán)境
    mysql master   : 192.168.111.52
    mysql slave     : 192.168.111.53
    keepalived vip :  192.168.111.60
、搭建過程
    1.  mysql雙主的構(gòu)建
        ① 互相 change master 即可,此處省略該過程,著重講下keepalived

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

        ③ 編輯 /etc/keepalived/keepalived.conf   

點(diǎn)擊(此處)折疊或打開

  1. root@192.168.111.52:keepalived# cat keepalived.conf

  2. ! Configuration File for keepalived

  3. global_defs {##全局配置

  4.    notification_email {

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

  6.    }

  7.    notification_email_from dba@163.com    #####郵件發(fā)送者

  8.    smtp_server smtp.163.com               #####SMTP服務(wù)器

  9.    smtp_connect_timeout 30

  10.    router_id haMySQL                      #####routerID 同一組keepalived設(shè)置為相同

  11. }

  12. vrrp_script chk_mysql {##健康檢測(cè)相關(guān)配置

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

  14.     interval 1                            #####檢測(cè)間隔

  15.     weight 2                              #####檢測(cè)返回失敗之后優(yōu)先級(jí)會(huì)減少 2 (如果 master優(yōu)先級(jí)100 , slave 優(yōu)先級(jí) 99 ,master檢測(cè)失敗則優(yōu)先級(jí)為100-2 < 99 ,則slave會(huì)提升為主)

  16. }

  17. vrrp_instance VI_1 {

  18.     state MASTER                          ##### 設(shè)為master

  19.     interface eth2                        ##### 網(wǎng)卡設(shè)定

  20.     virtual_router_id 51                  ##### 針對(duì)該 instance的虛擬id  , 同一組 instance設(shè)置相同

  21.     priority 100                          ##### 優(yōu)先級(jí)設(shè)定

  22.     advert_int 1                          ##### 檢測(cè)時(shí)間間隔

  23.     authentication {

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

  25.         auth_pass 7777

  26.     }

  27.     virtual_ipaddress {

  28.         192.168.111.60                    ##### 設(shè)置虛擬ip ,可以設(shè)置多個(gè)

  29.     }

  30.     

  31.     track_script {

  32.     chk_mysql                             ##### 表示該instance 使用chk_mysql進(jìn)行相關(guān)檢測(cè)

  33.     }

  34.   ##### 以下配置 在該實(shí)例 轉(zhuǎn)換為 master,slave,或者出錯(cuò)的時(shí)候執(zhí)行的腳本(可以設(shè)置郵件通知,或者處理一些其他問題)

  35.    # notify_master "/etc/keepalived/change_master.sh"

  36.    # notify_slave "/etc/keepalived/change_slave.sh"

  37.    # notify_fault "/etc/keepalived/change_fault.sh"

  38. }

  39. root@192.168.111.53:keepalived# cat keepalived.conf

  40. ! Configuration File for keepalived

  41. global_defs {

  42.    notification_email {

  43.      horand_gc@163.com

  44.    }

  45.    notification_email_from dba@163.com

  46.    smtp_server smtp.163.com

  47.    smtp_connect_timeout 30

  48.    router_id haMySQL

  49. }

  50. vrrp_script chk_mysql {

  51.         script "/etc/keepalived/chk_mysql.sh"

  52.         interval 1

  53.         weight 2

  54. }

  55. vrrp_instance VI_1 {

  56.     state BACKUP                            ##### 該主機(jī)作為備機(jī) BACKUP 

  57.     interface eth2

  58.     virtual_router_id 51

  59.     priority 99                             ##### 優(yōu)先級(jí)設(shè)置 小于 master

  60.     advert_int 1

  61.     authentication {

  62.         auth_type PASS

  63.         auth_pass 7777

  64.     }

  65.     virtual_ipaddress {

  66.         192.168.111.60

  67.     }

  68.     

  69.     track_script {

  70.         chk_mysql

  71.     }

  72. }

  73. root@192.168.111.52:keepalived# cat chk_mysql.sh

  74. #!/bin/bash

  75. num=`ps -ef |grep mysqld | grep -v grep | wc -l`        ##### 查看mysqld進(jìn)程數(shù)量 , 正常情況有一個(gè)root起的mysqld_safe守護(hù)進(jìn)程,還有一個(gè)屬于mysql用戶的mysqld進(jìn)程

  76. [[ $num -eq 2 ]] && exit 0 || exit 1

    3. 故障模擬
       ① 啟動(dòng) keepalived    

點(diǎn)擊(此處)折疊或打開

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  22. 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)上
    
    ② 關(guān)閉 111.52上面的mysql

點(diǎn)擊(此處)折疊或打開

  1. root@192.168.111.52:keepalived# mysqladmin shutdown

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

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

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

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

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

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

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

  9. 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)上

    ③ 啟動(dòng)111.52上面的mysql

點(diǎn)擊(此處)折疊或打開

  1. root@192.168.111.52:keepalived# mysqld_safe & ; tailf /var/log/messages

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

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

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

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

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

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

  8. 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會(huì)持續(xù)通過track_script的腳本檢查 ,若成功的話會(huì)恢復(fù)原來的優(yōu)先級(jí)100 ,便把vip搶過來了(若不希望優(yōu)先級(jí)高的直接上來直接搶占vip的話 需要再instance 里面配置 nopreempt ,backup無需設(shè)置)

虛擬server
    以上是實(shí)驗(yàn)是通過 vrrp_script以及trace_script 實(shí)現(xiàn)優(yōu)先級(jí)變換來實(shí)現(xiàn)故障轉(zhuǎn)移的,現(xiàn)在看下 通過虛擬server怎么實(shí)現(xiàn)mysql的高可用
    ① 配置

點(diǎn)擊(此處)折疊或打開

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

  2. ! Configuration File for keepalived

  3. global_defs {

  4.    notification_email {

  5.      tab@taomee.com

  6.    }

  7.    notification_email_from dba@taomee.com

  8.    smtp_server smtp.shidc.taomee.com

  9.    smtp_connect_timeout 30

  10.    router_id haMySQL

  11. }

  12. vrrp_script chk_mysql {

  13.     script "/etc/keepalived/chk_mysql.sh"

  14.     interval 1

  15.     weight 2

  16. }

  17. vrrp_instance VI_1 {

  18.     state MASTER

  19.     interface eth2

  20.     virtual_router_id 51

  21.     priority 100

  22.     nopreempt

  23.     advert_int 1

  24.     authentication {

  25.         auth_type PASS

  26.         auth_pass 7777

  27.     }

  28.     virtual_ipaddress {

  29.         192.168.111.60

  30.     }

  31.     

  32. #    track_script {

  33. #    chk_mysql

  34. #    }

  35. }

  36. virtual_server 192.168.111.60 3306 {

  37.     delay_loop 6

  38.     persistence_timeout 300

  39.     protocol TCP

  40.     real_server 192.168.111.52 3306 {

  41.         weight 1

  42.         notify_down /etc/keepalived/kill_self.sh

  43.         TCP_CHECK {

  44.             tcp_port 3306

  45.             connect_timeout 3

  46.         }

  47.     }

  48. }

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

  50. ! Configuration File for keepalived

  51. global_defs {

  52.    notification_email {

  53.      tab@taomee.com

  54.    }

  55.    notification_email_from dba@taomee.com

  56.    smtp_server smtp.shidc.taomee.com

  57.    smtp_connect_timeout 30

  58.    router_id haMySQL

  59. }

  60. vrrp_script chk_mysql {

  61.         script "/etc/keepalived/chk_mysql.sh"

  62.         interval 1

  63.         weight 2

  64. }

  65. vrrp_instance VI_1 {

  66.     state BACKUP

  67.     interface eth2

  68.     virtual_router_id 51

  69.     priority 99

  70.     advert_int 1

  71.     authentication {

  72.         auth_type PASS

  73.         auth_pass 7777

  74.     }

  75.     virtual_ipaddress {

  76.         192.168.111.60

  77.     }

  78.     

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

  80. #        chk_mysql

  81. #    }

  82. }

  83. virtual_server 192.168.111.60 3306 {

  84.     delay_loop 6

  85.     persistence_timeout 300

  86.     protocol TCP

  87.     real_server 192.168.111.53 3306 {### 真實(shí) 服務(wù)

  88.         weight 1                                    #### 權(quán)重,用來多真實(shí)服務(wù) 均衡使用

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

  90.         TCP_CHECK {

  91.                 tcp_port 3306                       #### 檢查端口 繼承 real_server 192.168.111.53 3306 {### 真實(shí) 服務(wù)

  92.             connect_timeout 3                       #### tcp超時(shí)時(shí)間

  93.         }

  94.     }

  95. }

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

  97. #!/bin/bash

  98. killall keepalived


    ②啟動(dòng)keepalived

點(diǎn)擊(此處)折疊或打開

  1. root@192.168.111.52:keepalived# keepalived

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

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

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

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

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

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

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

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

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

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

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

  13. root@192.168.111.53:keepalived# keepalived

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

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

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

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

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

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

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

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

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

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

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

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

點(diǎn)擊(此處)折疊或打開

  1. root@192.168.111.52:keepalived# mysqladmin shutdown

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

  3. [1]+  Done                    mysqld_safe

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

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

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

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

  8. 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

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

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

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

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

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

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

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

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

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

以上是“MySQL高可用之keepalived方案的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!


本文名稱:MySQL高可用之keepalived方案的示例分析
鏈接分享:http://weahome.cn/article/jessdd.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部