基於amoeba+keepalived+mmm實現mysql讀寫分離高可用架構

來源:互聯網
上載者:User


更多博文請關註:沒有傘的孩子必須努力奔跑 (www.xuchanggang.cn)

 

     在上一篇已經通過mysql-mmm實現對mysql資料庫的動態監控,這一篇我們將結合amoeba實現對資料庫訪問的讀寫分離,通過keepalived來實現amoeba的高可用[keepalived用於監控主機心跳,軟體是否宕掉無法監測,我們預設如果出問題就是amoeba的主機崩潰]


一、本次環境拓撲圖如下:650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131229/221211I95-0.png" title="amoeba+mmm+keepalived.png" alt="204724124.png" />二、環境描述   系統:red hat linux 6(2.6.32)   資料庫:mysql 5.1.61(這裡直接使用rpm包安裝,也不使用最新版本資料庫)   MMM:mysql-mmm  2.2.21   DB server地址分配:          192.168.1.100(master)          192.168.1.101(master)          192.168.1.102(slave)          192.168.1.103(slave)   MMM地址:          192.168.1.104(monitor)   虛擬IP地址(vip):          192.168.1.12(write)          192.168.1.13(read)          192.168.1.14(read)          192.168.1.15(read)  amoeba1:192.168.1.112  amoeba1:192.168.1.113  amoeba vip:192.168.1.17(對外的ip地址)  本次環境軟體下載:http://www.kuaipan.cn/file/id_119710994921422893.htm我們這裡的配置是基於上一篇的環境,也就是說mysql-mmm和mysql的主從複製都配置好了,下面主要描述amoeba和keepalived
三、配置示範:      Keepalived簡介:Keepalived是Linux下面實現VRRP備份路由的高可靠性運行件,基於Keepalived設計的服務模式能夠真正做到主伺服器和備份伺服器故障時IP瞬間無縫交接,從而提高系統的可用性1.將需要的包傳送到伺服器並解壓[我這裡預設安裝包已經上傳到伺服器家目錄]
# 在192.168.1.112/192.168.1.113上分別解壓如下幾個包[root@client112 ~]# tar -xf keepalived-1.2.9.tar.gz[root@client112 ~]# tar -xf jdk-7u15-linux-x64.tar.gz -C /usr/local/# 在192.168.1.113[root@client113 ~]# tar -xf keepalived-1.2.9.tar.gz[root@client113 ~]# tar -xf jdk-7u15-linux-x64.tar.gz -C /usr/local/
2.分別到192.168.1.112/192.168.1.113上配置java環境[這裡只示範在112上的操作,113上重複此操作即可]
[root@client112 ~]# cd /usr/local/[root@client112 local]# mkdir java# 將解壓出來的內容移到java目錄[root@client112 local]# mv jdk1.7.0_15/*  java# 設定java環境變數[root@client112 local]# echo $JAVA_HOME[root@client112 local]# vim /etc/profile# 在末尾添加JAVA_HOME=/usr/local/javaAMOEBA_HOME=/usr/local/amoebaexport PATH=$PATH:$AMOEBA_HOME/bin:$JAVA_HOME/bin:$JAVA_HOME/jre/bin # 使變數生效[root@centos local]# source /etc/profile
3.分別到192.168.1.112/192.168.1.113上配置amoeba[這裡只示範在112上的操作,113上重複此操作即可](1).配置amoeba中的dbServer.xml(後端mysql 伺服器串連配置)
# 建立相應amoeba目錄[root@client112 local]# mkdir /usr/local/amoeba# 將壓縮包解壓到指定目錄[root@client112 local]# tar -xf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba[root@client112 local]# cd /usr/local/amoeba/[root@client112 local]# cd conf/# 配置後端mysql 伺服器串連[dbServer.xml][root@client112 conf]# vim dbServers.xml.........................(省略)        <dbServer name="abstractServer" abstractive="true">                <factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory">                        <property name="manager">${defaultManager}</property>                        <property name="sendBufferSize">64</property>                        <property name="receiveBufferSize">128</property># 設定 mysql 資料庫的連接埠                        <!-- mysql port -->                        <property name="port">3306</property># 設定預設的資料庫,當串連amoeba時,動作表必須顯式的指定資料庫名,即採用dbname.tablename的方式,# 不支援 use dbname指定預設庫,因為操作會調度到各個後端dbserver                        <!-- mysql schema -->                        <property name="schema">kongzhong</property> # 設定amoeba串連後端資料庫伺服器的帳號和密碼,需在後端資料庫器上建立該使用者,並授權amoeba串連# 此處需要特別注意:**** 密碼預設是注釋掉,需要去掉注釋!!!                        <!-- mysql user -->                        <property name="user">kongzhong</property>                        <!--  mysql password -->                        <property name="password">kongzhong</property>                </factoryConfig>                <poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool"> # 最大串連數[預設]                        <property name="maxActive">500</property> # 最大空閑串連數[預設]                        <property name="maxIdle">500</property>                        <property name="minIdle">10</property>                        <property name="minEvictableIdleTimeMillis">600000</property>                        <property name="timeBetweenEvictionRunsMillis">600000</property>                        <property name="testOnBorrow">true</property>                        <property name="testOnReturn">true</property>                        <property name="testWhileIdle">true</property>                </poolConfig>        </dbServer> # 設定一個後端的dbServer,名為master ,這個可以隨便取,但是為了明確其含義,最好給予特殊含義的單詞# 下面指定的資料庫IP地址,為mmm提供給對外訪問的虛擬IP地址 <dbServer name="master"  parent="abstractServer"><factoryConfig> <!-- mysql ip --> <property name="ipAddress">192.168.1.12</property> </factoryConfig></dbServer> <dbServer name="slave1"  parent="abstractServer"><factoryConfig> <!-- mysql ip --> <property name="ipAddress">192.168.1.13</property></factoryConfig></dbServer><dbServer name="slave2"  parent="abstractServer"><factoryConfig><!-- mysql ip --><property name="ipAddress">192.168.1.14</property></factoryConfig></dbServer><dbServer name="slave3"  parent="abstractServer"><factoryConfig><!-- mysql ip --><property name="ipAddress">192.168.1.15</property></factoryConfig></dbServer><dbServer name="slave4"  parent="abstractServer"><factoryConfig><!-- mysql ip --><property name="ipAddress">192.168.1.16</property></factoryConfig></dbServer># 指定一個虛擬dbServer,將上面定義的dbserver加入這個虛擬dbserver,相當於組成一個組[這裡我們將讀的資料庫組成一個組]# 這裡 需要將 name="mul..." 改成自己想要取的名字,這個名字也需要有含義,後面會用到        <dbServer name="virtualslave" virtual="true">                <poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">                        <!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA--># 選擇調度演算法 1 是輪詢 2 是權重 3 是HA 這裡選擇1 輪詢                        <property name="loadbalance">1</property>                        <!-- Separated by commas,such as: server1,server2,server1 --># 負載平衡,slave1,slave2當成2個伺服器進行調度,這類比量加權的調度演算法。# 注意這裡使用的dbserver必須是已經定義了的,可以寫多個,如slave1,slave2                        <property name="poolNames">slave1,slave2,slave3,slave4</property>                </poolConfig>        </dbServer></amoeba:dbServers>

(2).配置 Amoeba 監聽連接埠[amoeba.xml]
[root@client112 conf]# vim amoeba.xml.........................(省略)        <proxy>                <!-- service class must implements com.meidusa.amoeba.service.Service -->                <service name="Amoeba for Mysql" class="com.meidusa.amoeba.net.ServerableConnectionManager">                        <!-- port --> # 設定amoeba監聽的連接埠(這裡如果預設,後面測試需要指定連接埠,就是這裡的連接埠)                        <property name="port">3306</property>                        <!-- bind ipAddress -->                        <!-- # 設定監聽的介面,如果不設定,則監聽所有的IP[選擇預設]                        <property name="ipAddress">127.0.0.1</property>                         -->.........................(省略)                        <property name="authenticator">                                <bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator"># 提供用戶端串連amoeba時需要使用這裡設定的帳號 (這裡的帳號密碼和amoeba串連後端資料庫伺服器的密碼無關)                                        <property name="user">kongzhong123</property># 提供用戶端串連amoeba時需要使用這裡設定的密碼                                        <property name="password">kongzhong123</property>                                        <property name="filter">                                                <bean class="com.meidusa.amoeba.server.IPAccessController">                                                        <property name="ipFile">${amoeba.home}/conf/access_list.conf</property>                                                </bean>                                        </property>                                </bean>                        </property>                </service>.........................(省略)        <queryRouter class="com.meidusa.amoeba.mysql.parser.MysqlQueryRouter">                <property name="ruleLoader">                        <bean class="com.meidusa.amoeba.route.TableRuleFileLoader">                                <property name="ruleFile">${amoeba.home}/conf/rule.xml</property>                                <property name="functionFile">${amoeba.home}/conf/ruleFunctionMap.xml</property>                        </bean>                </property>                <property name="sqlFunctionFile">${amoeba.home}/conf/functionMap.xml</property>                <property name="LRUMapSize">1500</property>                <property name="defaultPool">master</property>                <!--                 --># 把預設注釋掉的讀寫分離選項,把注釋去掉並readpool修改成virtualslave(這個名字,我們前面在dbServer.xml裡設定一個讀資料庫組,這裡是作為唯讀池)                <property name="writePool">master</property>                <property name="readPool">virtualslave</property>                <property name="needParse">true</property>        </queryRouter>
(3).配置amoeba及JAVA的環境變數[這個上面配置java時配置過,如果沒有配置參照下面]
[root@client112 local]# vim /etc/profile# 在末尾添加JAVA_HOME=/usr/local/javaAMOEBA_HOME=/usr/local/amoebaexport PATH=$PATH:$AMOEBA_HOME/bin:$JAVA_HOME/bin:$JAVA_HOME/jre/bin # 使變數生效[root@centos local]# source /etc/profile

(4)登陸資料庫授權相應使用者權限
#  登陸任意一個主master,授權amoeba用到登陸使用者(kongzhong需要授權,kongzhong123用於給前端登陸的不需要授權)mysql> create database kongzhong;mysql> grant all privileges on kongzhong.* to 'kongzhong'@'192.168.1.%' identified by 'kongzhong';mysql> flush privileges;
(5).啟動amoeba測試
# 如果確認能夠正常啟動,就加>>/dev/null 否則還是看一下提示資訊[root@centos conf]# amoeba start & >>/dev/null# 檢測啟動情況[root@centos conf]# netstat -tulnp |grep java# 停止amoeba測試指令是否可用[root@centos conf]# amoeba stop
(6).利用amoeba登陸測試[任何一台具有mysql用戶端,出現以下提示即為正常]
[root@client100 ~]# mysql -h192.168.1.112 -ukongzhong123 -pkongzhong123Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 1201340161Server version: 5.1.45-mysql-amoeba-proxy-2.2.0 Source distributionCopyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> show databases;+--------------------+| Database           |+--------------------+| information_schema || kongzhong          || test               |+--------------------+
(7).在192.168.1.113配置amoeba,這裡就不再延遲,重複上面配置amoeba的操作
4.安裝和配置keepalived
# keepalived需要openssl依賴包[root@client112 ~]# yum -y install openssl* gcc make# 建立keepalived安裝目錄[root@client112 ~]# mkdir /usr/local/keepalived[root@client112 ~]# cd keepalived-1.2.9[root@client112 keepalived-1.2.9]# ./configure  --prefix=/usr/local/keepalived[root@client112 keepalived-1.2.9]# make[root@client112 keepalived-1.2.9]# make install# 複製相應檔案到指定目錄[root@client112 keepalived-1.2.9]# cp /usr/local/keepalived/sbin/keepalived  /usr/sbin/[root@client112 keepalived-1.2.9]# cp /usr/local/keepalived/etc/sysconfig/keepalived  /etc/sysconfig/[root@client112 keepalived-1.2.9]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived  /etc/init.d/[root@client112 keepalived-1.2.9]# mkdir /etc/keepalived[root@client112 keepalived-1.2.9]# cd /etc/keepalived/# 以上的關於keepalived操作,請重複在192.168.1.113操作一遍# 以下操作,根據提示操作# 編輯設定檔[192.168.1.112上的設定檔][root@client112 keepalived]# vim keepalived.confbal_defs {        notification_email        {# 設定警示郵件地址,每行一個#  需開啟本機sendmail服務[這裡可以暫時忽略]               cloud_data@163.com        }#設定郵件發送地址        notification_email_fromcloud_data@163.com#設定smtp server 地址        smtp_server 127.0.0.1#設定串連 smtp server逾時時間        smtp_connect_timeout 30#運行Keepalived伺服器的一個標誌,郵件主題中顯示        router_id  client112}# vrrp執行個體定義部分vrrp_instance VI_1 {# 指定keepalived的角色,MASTER表示此主機是主伺服器# BACKUP表示是待命伺服器        state MASTER# 指定HA監測網路的介面        interface eth0# 虛擬路由標誌,同一個vrrp執行個體使用一個唯一標誌    # 即同一個vrrp_instance下,MASTER和BACKUP一致        virtual_router_id 51# 優先順序,數字越大優先順序越高,MASTER必須大於BACKUP        priority 100# MASTER和BACKUP同步檢查間隔,秒        advert_int 1        smtp_alert# 設定驗證類型和密碼        authentication {# 驗證類型,PASS和HA                auth_type PASS# 驗證密碼,MASTER和BACKUP密碼相同才能進行通訊                auth_pass kongzhong        }# 虛擬ip地址        virtual_ipaddress        {                192.168.1.17        }}# 編輯設定檔[192.168.1.113上的設定檔][root@client113 keepalived]# vim keepalived.confbal_defs {        notification_email       {                cloud_data@163.com        }        notification_email_from cloud_data@163.com        smtp_server 127.0.0.1        smtp_connect_timeout 30        router_id client113}vrrp_instance VI_1 {# 這裡和192.168.1.112上不同,修改為BACKUP        state BACKUP        interface eth0        virtual_router_id 51# 這裡和192.168.1.112上不同,修改為80        priority 80        advert_int 1        smtp_alert        authentication {# 這裡需要和192.168.1.112上的一樣,不然,無法切換的                auth_type PASS                auth_pass kongzhong        }        virtual_ipaddress        {                192.168.1.17        }}# 分別在192.168.1.112和192.168.1.113上啟動keepalived[root@client112 keepalived]# /etc/init.d/keepalived startStarting keepalived: [  OK  ]# 檢查虛擬ip是否綁定網卡[這裡ip預設會在優先順序比較高的機器上,就是剛才設定的100和80][root@client112 keepalived]# ip add | grep 192.168.1.17    inet 192.168.1.17/32 scope global eth0# 192.168.1.113上啟動keepalived[root@client113 keepalived]# /etc/init.d/keepalived startStarting keepalived: [  OK  ]# 113上如果需要看到ip需要關閉keepalived,也就是類比112宕機[root@client113 keepalived]# ip add | grep 192.168.1.17# 停掉192.168.1.112的keepalived,看看虛擬IP192.168.1.17會不會自動切換到192.168.1.113上

5.現在就可以測試了:簡單測試:(1).使用amoeba的真實ip登陸
[root@client100 ~]# mysql -h192.168.1.112 -ukongzhong123 -pkongzhong123[root@client100 ~]# mysql -h192.168.1.113 -ukongzhong123 -pkongzhong123
(2).使用keepalived提供的虛擬ip訪問
[root@client100 ~]# mysql -h192.168.1.17 -ukongzhong123 -pkongzhong123
(3).類比宕掉一台amoeba[即關閉keepalived],看還能否使用keepalived提供的虛擬ip訪問
# 這時虛擬ip應該切換到另外一台好的amoeba上[root@client100 ~]# mysql -h192.168.1.17 -ukongzhong123 -pkongzhong123




本文出自 “沒有傘的孩子必須努力奔跑” 部落格,請務必保留此出處http://634871.blog.51cto.com/624871/1341688

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.