keepalived+lvs實現mysql叢集負載均衡

hotdog04發表於2015-05-19

 

一 安裝:
yum install ipvsadm
或者下載二進位制包編譯安裝

二 手工配置單點轉發:

基礎環境:
100.5.120.118/119 是資料庫伺服器 埠號是3308
LVS伺服器:100.5.120.43
虛擬ip:100.5.120.150

echo 1 > /proc/sys/net/ipv4/ip_forward


啟動:
LVS端配置:
ifconfig em1:0 100.5.120.150 netmask 255.255.252.0 broadcast 10.5.15.255 up
ipvsadm -A -t 100.5.120.150:3308 -s rr
ipvsadm -a -t 100.5.120.150:3308 -r 100.5.120.119  -g
ipvsadm -a -t 100.5.120.150:3308 -r 100.5.120.118  -g
ipvsadm

real server(兩臺資料庫伺服器):
ifconfig lo:0 100.5.120.150 netmask 255.255.255.255 broadcast 10.5.15.255 up
route add -host 100.5.120.150 dev lo:0

檢視執行狀態: ipvsadm -l --stats -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port               Conns   InPkts  OutPkts  InBytes OutBytes
  -> RemoteAddress:Port
TCP  100.5.120.150:3308                   0        0        0        0        0
  -> 100.5.120.118:3308                   0        0        0        0        0
  -> 100.5.120.119:3308                   0        0        0        0        0

通過vip登陸資料庫伺服器幾次再次檢視狀態:
(mysql -h100.5.120.150 -P3308 -utest -ptest -e "show databases;")

ipvsadm -l --stats -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port               Conns   InPkts  OutPkts  InBytes OutBytes
  -> RemoteAddress:Port
TCP  100.5.120.150:3308                  15      135        0     7763        0
  -> 100.5.120.118:3308                   7       63        0     3563        0
  -> 100.5.120.119:3308                   8       72        0     4200        0

可以看到LVS複製均衡已經生效了。

關閉:
lvs server
ifconfig em1:0 down
/sbin/ipvsadm -C

real server:
 /sbin/ifconfig lo:0 down


三、結合keepalived做LVS伺服器高可用:

基礎環境:
100.5.120.118/119 是資料庫伺服器 埠號是3308
LVS伺服器:100.5.120.43/2
虛擬ip:100.5.120.150

100.5.120.43/2上分別keepalived安裝好後。
調整配置檔案(43主):
 cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
 global_defs {
     router_id LVS
 }
 
vrrp_instance VI_1 {
      state master    #2上使用backup
      interface em1
      virtual_router_id 115
      priority 100     #2上調整成比100小的值比如80
      advert_int 1
      #nopreempt
      authentication {
          auth_type PASS
          auth_pass 1234
      }
      virtual_ipaddress {
         100.5.120.150
      }
 }

virtual_server 100.5.120.150 3308 {
      delay_loop 6                       
      lb_algo wrr                       
      lb_kind DR                       
      persistence_timeout 60             
      protocol TCP                     
      real_server 100.5.120.118 3308 {         
           weight 3                        
           TCP_CHECK {         
           connect_timeout 10                
           nb_get_retry 3         
           delay_before_retry 3         
           connect_port 3308         
           }        
      } 
      real_server 100.5.120.119 3308 {         
           weight 3                        
           TCP_CHECK {         
           connect_timeout 10                
           nb_get_retry 3         
           delay_before_retry 3         
           connect_port  3308    
           }        
      }      
     
}

先關閉之前手動起來的服務。
兩個lvs伺服器分別執行:
/etc/init.d/keepalived start


此時檢視狀態:
發現master上獲得了vip,並且lvs已經正常啟動了。

PS:測試文件,生產環境使用,還是需要仔細閱讀官方文件優化配置

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/20625855/viewspace-1661948/,如需轉載,請註明出處,否則將追究法律責任。

相關文章