首頁>技術>

概述

隨著網際網路的發展,資料的量級也是撐指數的增長,從GB到TB到PB。對資料的各種操作也是愈加的困難,傳統的關係型資料庫已經無法滿足快速查詢與插入資料的需求。這個時候NoSQL的出現暫時解決了這一危機。它通過降低資料的安全性,減少對事務的支援,減少對複雜查詢的支援,來獲取效能上的提升。但是,在有些場合NoSQL一些折衷是無法滿足使用場景的,就比如有些使用場景是絕對要有事務與安全指標的。這個時候NoSQL肯定是無法滿足的,所以還是需要使用關係型資料庫。如何使用關係型資料庫解決海量儲存的問題呢?此時就需要做資料庫叢集,為了提高查詢效能將一個數據庫的資料分散到不同的資料庫中儲存,為應對此問題就出現了——MyCat。

下面介紹下基於mycat+haproxy+keepalived搭建高可用負載均衡mysql叢集中的keepalived和最終測試部分。

一、keepalived安裝

1、安裝依賴

2、keepalived編譯安裝

3、keepalived配置

# vim /etc/keepalived/keepalived.conf

#######配置主機1的keepalived##########! Configuration Fileforkeepalivedvrrp_script chk_http_port {script"/etc/keepalived/scripts/check_haproxy.sh"interval 2weight 2}vrrp_instance VI_1 {state MASTERinterface ens160virtual_router_id 51priority 150advert_int 1authentication {auth_type PASSauth_pass 1111}track_script {chk_http_port}virtual_ipaddress {172.26.151.99 dev ens160 scope global}notify_master /etc/keepalived/scripts/haproxy_master.shnotify_backup /etc/keepalived/scripts/haproxy_backup.shnotify_fault /etc/keepalived/scripts/haproxy_fault.shnotify_stop /etc/keepalived/scripts/haproxy_stop.sh}##########################配置主機2的keepalived(網絡卡為ens160)##########################! Configuration File for keepalived#VRRP指令碼vrrp_script chk_http_port {#執行的指令碼script"/etc/keepalived/scripts/check_haproxy.sh" #指令碼執行間隔interval 2#指令碼結果導致的優先順序變更:2表示優先順序+10;-2則表示優先順序-2weight 2}#vrrp例項vrrp_instance VI_1 {state BACKUP#本機網絡卡interface ens160##VRRP組名,兩個節點的設定必須一樣,以指明各個節點屬於同一VRRP組virtual_router_id 51#優先順序,數值愈大,優先順序越高priority 120#檢查間隔,預設1秒advert_int 1#設定認證authentication {#認證方式auth_type PASS#認證密碼auth_pass 1111}track_script {#呼叫指令碼check_haproxy.sh檢查haproxy是否存活chk_http_port}#虛擬IP地址池,可以有多個IP,每個IP佔一行virtual_ipaddress {172.26.151.99 dev ens160 scope global}#指定當切換到master時,執行的指令碼notify_master /etc/keepalived/scripts/haproxy_master.sh#指定當切換到backup時,執行的指令碼notify_backup /etc/keepalived/scripts/haproxy_backup.sh#故障時執行的指令碼notify_fault /etc/keepalived/scripts/haproxy_fault.shnotify_stop /etc/keepalived/scripts/haproxy_stop.sh}

chmod 600 /etc/keepalived/keepalived.conf --許可權需要600

4、建立配置檔案和指令碼

4.1、建立檢查haproxy是否存活的指令碼

#mkdir /etc/keepalived/scripts#vim /etc/keepalived/scripts/check_haproxy.sh=====================================================#!/bin/bashSTARTHAPROXY="/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg"STOPKEEPALIVED="/etc/init.d/keepalived stop"LOGFILE="/usr/local/keepalived/var/log/keepalived-haproxy-state.log"echo "[check_haproxy status]" >> $LOGFILEA=`ps -C haproxy --no-header |wc -l`echo "[check_haproxy status]" >> $LOGFILEdate >> $LOGFILE#如果沒有haproxy程序存在,就啟動haproxy,停止keepalived,並寫入日誌if [ $A -eq 0 ];thenecho $STARTHAPROXY >> $LOGFILE$STARTHAPROXY >> $LOGFILE 2>&1sleep 5fiif [ `ps -C haproxy --no-header |wc -l` -eq 0 ];thenexit 0elseexit 1fi=====================================================

4.2、建立切換到MASTER與BACKUP指令碼

#vim /etc/keepalived/scripts/haproxy_master.sh#vim /etc/keepalived/scripts/haproxy_backup.sh===========兩個檔案中增加以下內容==========================#!/bin/bashSTARTHAPROXY=`/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg`STOPHAPROXY=`ps -ef | grep sbin/haproxy | grep -v grep | awk '{print $2}'| xargs kill -s 9`LOGFILE="/usr/local/keepalived/var/log/keepalived-haproxy-state.log"echo "[master]" >> $LOGFILEdate >> $LOGFILEecho "Being master...." >> $LOGFILE 2>&1echo "stop haproxy...." >> $LOGFILE 2>&1$STOPHAPROXY >> $LOGFILE 2>&1echo "start haproxy...." >> $LOGFILE 2>&1$STARTHAPROXY >> $LOGFILE 2>&1echo "haproxy stared ..." >> $LOGFILE=====================================================

4.3、建立故障時執行的指令碼

# vim /etc/keepalived/scripts/haproxy_fault.sh=====================================================#!/bin/bashLOGFILE=/usr/local/keepalived/var/log/keepalived-haproxy-state.logecho "[fault]" >> $LOGFILEdate >> $LOGFILE=====================================================

4.4、建立停止vrrp指令碼

#vim /etc/keepalived/scripts/haproxy_stop.sh===============================================#!/bin/bashLOGFILE=/usr/local/keepalived/var/log/keepalived-haproxy-state.logecho "[stop]" >> $LOGFILEdate >> $LOGFILE===============================================

5、授權

chmod 777 /etc/keepalived/scripts/*

6、啟動keepalived並加入開機自啟動

二、最終測試

1、在瀏覽器輸入http://172.26.151.99:48800/admin_stats

2、兩臺haproxy檢視8067與9067埠是否在監聽

3、連線8067埠

最新評論
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • 基於SpringBoot2的開源管理後臺系統springboot-plus