MYSQL + MHA +keepalive + VIP安裝配置(三)--keepalived安裝配置

sunney發表於2014-05-18

一、概述

  keepalived介紹:Keepalived的作用是檢測web伺服器的狀態,如果有一臺web伺服器當機,或工作出現故障,Keepalived將檢測到,並將有故障的web 伺服器從系統中剔除,當web伺服器工作正常後Keepalived自動將web伺服器加入到伺服器群中,這些工作全部自動完成,不需要人工干涉,需要人 工做的只是修復故障的web伺服器。

二、環境

vip:192.168.1.203\204
mysql-master:192.168.1.231
mysql-slave:192.168.1.232

 

三、keepalived安裝

安裝步驟兩臺機mysql-master、mysql-slave一樣,如下:

1、下載地址:http://www.keepalived.org/software/keepalived-1.2.12.tar.gz

shell>wget http://www.keepalived.org/software/keepalived-1.2.12.tar.gz

2、安裝環境

yum -y install openssl-devel

 否則會報如下錯誤

configure: error:
  !!! OpenSSL is not properly installed on your system. !!!
  !!! Can not include OpenSSL headers files.

3、安裝

shell>tar -zxvf  keepalived-1.2.12.tar.gz
shell>cd keepalived-1.2.12
shell>./configure --prefix=/opt/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64/
shell>make 
shell>make install

 說明:

 --prefix:安裝路徑

 --with-kernel-dir:這個是重要的引數,這個引數並不表示我們要把Keepalived統進核心,而是指使用核心原始碼裡面的標頭檔案,也就是include目錄。

  2.6.32-431.el6.x86_64可以通過

shell>uname -r 命令檢視到

 4、配置

# cp /opt/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
# cp /opt/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
# mkdir /etc/keepalived
# cp /opt/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
# cp /opt/keepalived/sbin/keepalived /usr/sbin/

 5、啟動/停止

shell>service keepalived start
shell>service keepalived stop
shell>service keepalived restart

 

四、keepalived配置

1、mysql-master:192.168.1.231的配置

shell>vim /etc/keepalived/keepalived.conf

 配置如下:

global_defs {
 router_id mysql-master #修改為自己的主機名
 notification_email {
 mengtao10@163.com   #接收郵件,可以有多個,一行一個
}
 #當主、備份裝置發生改變時,通過郵件通知
 notification_email_from lzyangel@126.com
 #傳送郵箱伺服器
 smtp_server stmp.163.com
 #傳送郵箱超時時間
 smtp_connect_timeout 30
 }
##################第一部分###################
vrrp_instance VI_1 {
     state BACKUP    #都修改成BACKUP
     interface eth0    #繫結的網路卡
     virtual_router_id 60 #預設51 主從都修改為60
     priority 100            #優先順序,在mysql-slave上LVS上修改成80
     advert_int 1
     nopreempt #不搶佔資源,意思就是它活了之後也不會再把主搶回來
  
     authentication {
     # 認證方式,可以是PASS或AH兩種認證方式
     auth_type PASS
     # 認證密碼
     auth_pass 1111
     }
virtual_ipaddress {
     192.168.1.203
     192.168.1.204  #這可以增加多個VIP
     }
}
##################第二部分###################
virtual_server 192.168.1.203 3306 {
     delay_loop 6
     lb_algo wrr
     lb_kind DR
     nat_mask 255.255.255.0
     persistence_timeout 50
     protocol TCP
    real_server 192.168.1.231 3306 {
     weight 1
     notify_down /root/mysql_down.sh
     TCP_CHECK {
         connect_timeout 10
         nb_get_retry 3
         connect_port 3306
         }
     }
}

 2、mysql-master:192.168.1.232的配置

shell>vim /etc/keepalived/keepalived.conf

 配置如下:

global_defs {
 router_id mysql-master #修改為自己的主機名
 notification_email {
 mengtao10@163.com   #接收郵件,可以有多個,一行一個
}
 #當主、備份裝置發生改變時,通過郵件通知
 notification_email_from lzyangel@126.com
 #傳送郵箱伺服器
 smtp_server stmp.163.com
 #傳送郵箱超時時間
 smtp_connect_timeout 30
 }
##################第一部分###################
vrrp_instance VI_1 {
     state BACKUP    #都修改成BACKUP
     interface eth0    #繫結的網路卡
     virtual_router_id 60 #預設51 主從都修改為60
     priority 80           #優先順序,在mysql-master上LVS上修改成100
     advert_int 1
     authentication {
     # 認證方式,可以是PASS或AH兩種認證方式
     auth_type PASS
     # 認證密碼
     auth_pass 1111
     }
virtual_ipaddress {
     192.168.1.203
     192.168.1.204  #這可以增加多個VIP
     }
}
##################第二部分###################
virtual_server 192.168.1.203 3306 {
     delay_loop 6
     lb_algo wrr
     lb_kind DR
     nat_mask 255.255.255.0
     persistence_timeout 50
     protocol TCP
    real_server 192.168.1.232 3306 {
     weight 1
     notify_down /root/mysql_down.sh
     TCP_CHECK {
         connect_timeout 10
         nb_get_retry 3
         connect_port 3306
         }
     }
}

 3、myslq_down.sh配置,兩臺機都要配置這一步!!!

    這裡需要注意的是,notify_down /root/mysql_down.sh這個選項,這個是是在keepalived檢測不到mysql的時候要執行的指令碼,從上面的配置檔案來看real伺服器只有本機。那麼,keeaplived如果啟動,客戶端也只是訪問本機的mysql。nopreempt這個選下也得注意,這個是不搶佔資源在優先順序高的機器上配置就可以。
看下這個指令碼的內容:

# vim /root/mysql_down.sh
#!/bin/bash
pkill keepalived
# chmod +x /root/mysql_down.sh #授權可執行許可權

 指令碼內容就一條命令:pkill keepalived,主要作用是如果本機的mysql掛掉了,那麼同時會殺死本機的keepalived,這樣另外一臺就會接替他工作,虛擬IP也會被另一臺接管,如果不殺死keepalived虛擬IP不會被另一臺接管,mysql訪問也就不會切換過去。

 注意:必須要先啟動MYSQL,再啟動keepalived,否則keepalived啟動後會執行mysql_down.sh指令碼,等於自殺。

4、mysql-master(231)啟動後檢視IP的繫結情況,如下:

shell> ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:cc:92:22 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.232/24 brd 192.168.1.255 scope global eth0
    inet 192.168.1.203/32 scope global eth0
    inet 192.168.1.204/32 scope global eth0
    inet6 fe80::a00:27ff:fecc:9222/64 scope link 
       valid_lft forever preferred_lft forever

 

五、測試

1、231、232兩臺機同時啟動MYSQL及Keepalived.

   通過win cmd 命令列的客戶端(或程式)連線VIP:192.168.1.203或204,如下:

C:\Users\Duncan>mysql -h192.168.1.203 -usunney -psunney
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1440
Server version: 5.5.37-log MySQL Community Server (GPL)

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sunney             |
| test               |
+--------------------+
5 rows in set (0.04 sec)

mysql>

注意:這時儲存的資料庫是mysql-master(192.168.1.231)因為他是主庫。配置 priority 100 優先順序高。

2、mysql-slave(232)的機同時關閉MYSQL及Keepalived.這時關閉上以上的是一樣的。因為他沒有走這個庫。

3、mysql-master(231)同時關閉MYSQL及Keepalived,mysql-slave(232)同時啟動MYSQL及Keepalived

    切換很快,大概在2到3秒之間!

   通過win cmd 命令列的客戶端(或程式)連線VIP:192.168.1.203或204,如下:

C:\Users\Duncan>mysql -h192.168.1.203 -usunney -psunney
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1440
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sunney             |
| test               |
+--------------------+
5 rows in set (0.04 sec)

mysql>

 成功!

 

相關文章