Keepalived+HA

關於MySQL-HA,目前有多種解決方案,比如heartbeat、drbd、mmm、共享存儲,但是它們各有優缺點。heartbeat、drbd配置較爲複雜,需要自己寫腳本才能實現MySQL自動切換,對於不會腳本語言的人來說,這無疑是一種腦裂問題;對於mmm,生產環境中很少有人用,且mmm

 

關於MySQL-HA,目前有多種解決方案,比如heartbeat、drbd、mmm、共享存儲,但是它們各有優缺點。heartbeat、drbd配置較爲複雜,需要自己寫腳本才能實現MySQL自動切換,對於不會腳本語言的人來說,這無疑是一種腦裂問題;對於mmm,生產環境中很少有人用,且mmm 管理端需要單獨運行一臺服務器上,要是想實現高可用,就得對mmm管理端做HA,這樣無疑又增加了硬件開支;對於共享存儲,個人覺得MySQL數據還是放在本地較爲安全,存儲設備畢竟存在單點隱患。使用MySQL雙master+keepalived是一種非常好的解決方案,在MySQL-HA環境中,MySQL互爲主從關係,這樣就保證了兩臺MySQL數據的一致性,然後用keepalived實現虛擬IP,通過keepalived自帶的服務監控功能來實現MySQL故障時自動切換。

  下面,我把即將上線的一個生產環境中的架構與大家分享一下,看一下這個架構中,MySQL-HA是如何實現的,環境拓撲如下

 

  1. MySQL-VIP:192.168.1.200    
  2. MySQL-master1:192.168.1.201    
  3. MySQL-master2:192.168.1.202    
  4.    
  5. OS版本:CentOS 5.4    
  6. MySQL版本:5.0.89    
  7. Keepalived版本:1.1.20  

  一、MySQL master-master配置

  1、修改MySQL配置文件

  兩臺MySQL均如要開啓binlog日誌功能,開啓方法:在MySQL配置文件[MySQLd]段中加上log-bin=MySQL-bin選項

  兩臺MySQL的server-ID不能一樣,默認情況下兩臺MySQL的serverID都是1,需將其中一臺修改爲2即可

  2、將192.168.1.201設爲192.168.1.202的主服務器

  在192.168.1.201上新建授權用戶

 

  1. MySQL> grant replication slave on *.* to 'replication'@'%' identified by 'replication';    
  2. Query OK, 0 rows affected (0.00 sec)    
  3.    
  4. MySQL> show master status;    
  5. +------------------+----------+--------------+------------------+    
  6. | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |    
  7. +------------------+----------+--------------+------------------+    
  8. | MySQL-bin.000003 |      374 |              |                  |     
  9. +------------------+----------+--------------+------------------+    
  10. 1 row in set (0.00 sec)  

  在192.168.1.202上將192.168.1.201設爲自己的主服務器

 

  1. MySQL> change master to master_host='192.168.1.201',master_user='replication',master_password='replication',master_log_file='MySQL-bin.000003',master_log_pos=374;    
  2. Query OK, 0 rows affected (0.05 sec)    
  3.    
  4. MySQL> start slave;    
  5. Query OK, 0 rows affected (0.00 sec)    
  6.    
  7. MySQL> show slave status/G    
  8. *************************** 1. row ***************************    
  9.              Slave_IO_State: Waiting for master to send event    
  10.                 Master_Host: 192.168.1.201    
  11.                 Master_User: replication    
  12.                 Master_Port: 3306    
  13.               Connect_Retry: 60    
  14.             Master_Log_File: MySQL-bin.000003    
  15.         Read_Master_Log_Pos: 374    
  16.              Relay_Log_File: MySQL-master2-relay-bin.000002    
  17.               Relay_Log_Pos: 235    
  18.       Relay_Master_Log_File: MySQL-bin.000003    
  19.            Slave_IO_Running: Yes    
  20.           Slave_SQL_Running: Yes    
  21.             Replicate_Do_DB:     
  22.         Replicate_Ignore_DB:     
  23.          Replicate_Do_Table:     
  24.      Replicate_Ignore_Table:     
  25.     Replicate_Wild_Do_Table:     
  26. Replicate_Wild_Ignore_Table:     
  27.                  Last_Errno: 0    
  28.                  Last_Error:     
  29.                Skip_Counter: 0    
  30.         Exec_Master_Log_Pos: 374    
  31.             Relay_Log_Space: 235    
  32.             Until_Condition: None    
  33.              Until_Log_File:     
  34.               Until_Log_Pos: 0    
  35.          Master_SSL_Allowed: No    
  36.          Master_SSL_CA_File:     
  37.          Master_SSL_CA_Path:     
  38.             Master_SSL_Cert:     
  39.           Master_SSL_Cipher:     
  40.              Master_SSL_Key:     
  41.       Seconds_Behind_Master: 0    
  42. 1 row in set (0.00 sec)  

  3、將192.168.1.202設爲192.168.1.201的主服務器

  在192.168.1.202上新建授權用戶

 

  1. MySQL> grant replication slave on *.* to 'replication'@'%' identified by 'replication';    
  2. Query OK, 0 rows affected (0.00 sec)    
  3.    
  4. MySQL> show master status;    
  5. +------------------+----------+--------------+------------------+    
  6. | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |    
  7. +------------------+----------+--------------+------------------+    
  8. | MySQL-bin.000003 |      374 |              |                  |     
  9. +------------------+----------+--------------+------------------+    
  10. 1 row in set (0.00 sec)  

  在192.168.1.201上,將192.168.1.202設爲自己的主服務器

 

  1. MySQL> change master to master_host='192.168.1.202',master_user='replication',master_password='replication',master_log_file='MySQL-bin.000003',master_log_pos=374;    
  2. Query OK, 0 rows affected (0.05 sec)    
  3.    
  4. MySQL> start slave;    
  5. Query OK, 0 rows affected (0.00 sec)    
  6.    
  7. MySQL> show slave status/G    
  8. *************************** 1. row ***************************    
  9.              Slave_IO_State: Waiting for master to send event    
  10.                 Master_Host: 192.168.1.202    
  11.                 Master_User: replication    
  12.                 Master_Port: 3306    
  13.               Connect_Retry: 60    
  14.             Master_Log_File: MySQL-bin.000003    
  15.         Read_Master_Log_Pos: 374    
  16.              Relay_Log_File: MySQL-master1-relay-bin.000002    
  17.               Relay_Log_Pos: 235    
  18.       Relay_Master_Log_File: MySQL-bin.000003    
  19.            Slave_IO_Running: Yes    
  20.           Slave_SQL_Running: Yes    
  21.             Replicate_Do_DB:     
  22.         Replicate_Ignore_DB:     
  23.          Replicate_Do_Table:     
  24.      Replicate_Ignore_Table:     
  25.     Replicate_Wild_Do_Table:     
  26. Replicate_Wild_Ignore_Table:     
  27.                  Last_Errno: 0    
  28.                  Last_Error:     
  29.                Skip_Counter: 0    
  30.         Exec_Master_Log_Pos: 374    
  31.             Relay_Log_Space: 235    
  32.             Until_Condition: None    
  33.              Until_Log_File:     
  34.               Until_Log_Pos: 0    
  35.          Master_SSL_Allowed: No   
  36.          Master_SSL_CA_File:     
  37.          Master_SSL_CA_Path:     
  38.             Master_SSL_Cert:     
  39.           Master_SSL_Cipher:     
  40.              Master_SSL_Key:     
  41.       Seconds_Behind_Master: 0    
  42. 1 row in set (0.00 sec)  

  4、MySQL同步測試

  如上述均正確配置,現在任何一臺MySQL上更新數據都會同步到另一臺MySQL,MySQL同步在此不再演示

 

 

二、keepalived安裝及配置

  1、192.168.1.201服務器上keepalived安裝及配置

  安裝keepalived

 

  1. #tar zxvf keepalived-1.1.20.tar.gz    
  2. #cd keepalived-1.1.20    
  3. #./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.18-164.el5-i686    
  4. #make && make install  

  配置keepalived

  我們自己在新建一個配置文件,默認情況下keepalived啓動時會去/etc/keepalived目錄下找配置文件

 

  1. #mkdir /etc/keepalived    
  2. #vi /etc/keepalived/keepalived.conf    
  3. ! Configuration File for keepalived    
  4. global_defs {    
  5.      notification_email {    
  6.      [email protected]    
  7.      }    
  8.      notification_email_from [email protected]    
  9.      smtp_server 127.0.0.1    
  10.      smtp_connect_timeout 30    
  11.      router_id MySQL-ha    
  12.      }    
  13.    
  14. vrrp_instance VI_1 {    
  15.      state BACKUP   #兩臺配置此處均是BACKUP    
  16.      interface eth0    
  17.      virtual_router_id 51    
  18.      priority 100   #優先級,另一臺改爲90    
  19.      advert_int 1    
  20.      nopreempt  #不搶佔,只在優先級高的機器上設置即可,優先級低的機器不設置    
  21.      authentication {    
  22.      auth_type PASS    
  23.      auth_pass 1111    
  24.      }    
  25.      virtual_ipaddress {    
  26.      192.168.1.200    
  27.      }    
  28.      }    
  29.    
  30. virtual_server 192.168.1.200 3306 {    
  31.      delay_loop 2   #每個2秒檢查一次real_server狀態    
  32.      lb_algo wrr   #LVS算法    
  33.      lb_kind DR    #LVS模式    
  34.      persistence_timeout 60   #會話保持時間    
  35.      protocol TCP    
  36.      real_server 192.168.1.201 3306 {    
  37.      weight 3    
  38.      notify_down /usr/local/MySQL/bin/MySQL.sh  #檢測到服務down後執行的腳本    
  39.      TCP_CHECK {    
  40.      connect_timeout 10    #連接超時時間    
  41.      nb_get_retry 3       #重連次數    
  42.      delay_before_retry 3   #重連間隔時間    
  43.      connect_port 3306   #健康檢查端口    
  44.      }    
  45.      }  

  編寫檢測服務down後所要執行的腳本

 

  1. #vi /usr/local/MySQL/bin/MySQL.sh    
  2. #!/bin/sh    
  3. pkill keepalived    
  4. #chmod +x /usr/local/MySQL/bin/MySQL.sh  

  注:此腳本是上面配置文件notify_down選項所用到的,keepalived使用notify_down選項來檢查real_server的服務狀態,當發現real_server服務故障時,便觸發此腳本;我們可以看到,腳本就一個命令,通過pkill keepalived強制殺死keepalived進程,從而實現了MySQL故障自動轉移。另外,我們不用擔心兩個MySQL會同時提供數據更新操作,因爲每臺MySQL上的keepalived的配置裏面只有本機MySQL的IP+VIP,而不是兩臺MySQL的IP+VIP

  啓動keepalived

 

  1. #/usr/local/keepalived/sbin/keepalived –D    
  2. #ps -aux | grep keepalived  

  測試

  找一臺局域網PC,然後去ping MySQL的VIP,這時候MySQL的VIP是可以ping的通的

  停止MySQL服務,看keepalived健康檢查程序是否會觸發我們編寫的腳本

  2、192.168.1.202上keepalived安裝及配置

  安裝keepalived

 

  1. #tar zxvf keepalived-1.1.20.tar.gz    
  2. #cd keepalived-1.1.20    
  3. #./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.18-164.el5-i686    
  4. #make && make install  

  配置keepalived

  這臺配置和上面基本一樣,但有三個地方不同:優先級爲90、無搶佔設置、real_server爲本機IP

 

  1. #mkdir /etc/keepalived    
  2. #vi /etc/keepalived/keepalived.conf    
  3. ! Configuration File for keepalived    
  4. global_defs {    
  5.      notification_email {    
  6.      [email protected]    
  7.      }    
  8.      notification_email_from [email protected]    
  9.      smtp_server 127.0.0.1    
  10.      smtp_connect_timeout 30    
  11.      router_id MySQL-ha    
  12.      }    
  13.    
  14. vrrp_instance VI_1 {    
  15.      state BACKUP    
  16.      interface eth0    
  17.      virtual_router_id 51    
  18.      priority 90    
  19.      advert_int 1    
  20.      authentication {    
  21.      auth_type PASS    
  22.      auth_pass 1111    
  23.      }    
  24.      virtual_ipaddress {    
  25.      192.168.1.200    
  26.      }    
  27.      }    
  28.    
  29. virtual_server 192.168.1.200 3306 {    
  30.      delay_loop 2    
  31.      lb_algo wrr    
  32.      lb_kind DR    
  33.      persistence_timeout 60    
  34.      protocol TCP    
  35.      real_server 192.168.1.202 3306 {    
  36.      weight 3    
  37.      notify_down /usr/local/MySQL/bin/MySQL.sh    
  38.      TCP_CHECK {    
  39.      connect_timeout 10    
  40.      nb_get_retry 3    
  41.      delay_before_retry 3    
  42.      connect_port 3306    
  43.      }    
  44.      }   

  編寫檢測服務down後所要執行的腳本

 

  1. #vi /usr/local/MySQL/bin/MySQL.sh    
  2. #!/bin/sh    
  3. pkill keepalived    
  4. #chmod +x /usr/local/MySQL/bin/MySQL.sh    
  5.    
  6. 啓動keepalived    
  7. #/usr/local/keepalived/sbin/keepalived –D    
  8. #ps -aux | grep keepalived 

  測試

  ●停止MySQL服務,看keepalived健康檢查程序是否會觸發我們編寫的腳本

  三、測試

  ● MySQL遠程登錄測試

  我們找一臺安裝有MySQL客戶端的windows,然後登錄VIP,看是否能登錄,在登錄之兩臺MySQL服務器都要授權允許從遠程登錄

 

  1. MySQL> grant all privileges on *.* to 'root'@'%' identified by '123456';    
  2. Query OK, 0 rows affected (0.00 sec)    
  3.    
  4. MySQL> flush privileges;    
  5. Query OK, 0 rows affected (0.00 sec)   

  使用客戶端登錄VIP測試

 

  1. C:/MySQL/bin>MySQL.exe -uroot -p123456 -h192.168.1.200 -P3306    
  2. Welcome to the MySQL monitor.  Commands end with ; or /g.    
  3. Your MySQL connection id is 224    
  4. Server version: 5.0.89-log Source distribution    
  5.    
  6. Type 'help;' or '/h' for help. Type '/c' to clear the current input statement.    
  7.    
  8. MySQL>   

  ● keepalived故障轉移測試

  ※在windows客戶端一直去ping VIP,然後關閉192.168.1.201上的keepalived,正常情況下VIP就會切換到192.168.1.202上面去

  ※開啓192.168.1.201上的keepalived,關閉192.168.1.202上的keepalived,看是否能自動切換,正常情況下VIP又會屬於192.168.1.201

  注:keepalived切換速度還是非常塊的,整個切換過程只需1-3秒

  ● MySQL故障轉移測試

  ※在192.168.1.201上關閉MySQL服務,看VIP是否會切換到192.168.1.202上

  ※開啓192.168.1.201上的MySQL和keepalived,然後關閉192.168.1.202上的MySQL,看VIP是否會切換到192.168.1.201上

  下面是用windows客戶端連接的MySQL的VIP,在切換時我執行了一個MySQL查詢命令,從執行show databases到顯示出結果時間爲3-5秒(大家可以看到上面有個錯誤提示,不過不用擔心,因爲我們的keepalived切換大概爲3秒左右,這3秒左右VIP是誰都不屬於的)

 

  1. MySQL> show databases;    
  2. ERROR 2006 (HY000): MySQL server has gone away    
  3. No connection. Trying to reconnect...    
  4. Connection id:    592    
  5. Current database: *** NONE ***    
  6.    
  7. +--------------------+    
  8. | Database           |    
  9. +--------------------+    
  10. | information_schema |    
  11. | MySQL              |    
  12. | test               |    
  13. +--------------------+    
  14. 3 rows in set (9.01 sec)   

  後話:世間萬事萬物,都不具備絕對的完美,就像上面的MySQL-HA一樣,keepalived只能做到對3306的健康檢查,但是做不到比如像MySQL複製中的slave-SQL、slave-IO進程的檢查。所以要想做到一些細緻的健康檢查,還得需要藉助額外的監控工具,比如nagios,然後用nagios實現短信、郵件報警,從而能夠有效地解決問題。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章