keepalived+lvs

準備環境:iptables 和selinux 均是關閉的。
desktop12.example.com
desktop41.example.com(這2 臺和以前的同樣是高可用的HA,假設這2 臺
是真機,也能夠是虛擬機,只要你的電腦內存足夠大)
desktop58.example.com
desktop67.example.com(這2 臺是負載均衡的real server,這2 臺是虛擬機)
步驟(1)在desktop12.example.com
desktop41.example.com
上:lftp 192.168.0.254 cd pub/docs/keepalived
get keepalived-1.2.2.tar.gz
tar zxf keepalived-1.2.2.tar.gz
cd keepalived-1.2.2
./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-71.el6.x86_64/
(在編譯的時候可能會報錯,可能你須要安裝 yum install ipvsadm kernel-devel libnl-devel popt-devel -y直到出現四個:yes
Use IPVS Framework: Yes
IPVS sync daemon support : Yes
IPVS use libnl: Yes
Use VRRP Framework : Yes
注意,這裏必需要安裝kernel-devel.不然可能出現/usr/src/kernels/2.6.32-71.7.1.el6.x86_64/後面的那個補不齊(也就是說安裝完是能夠自動 補齊的!),Use IPVS Framework : No
IPVS sync daemon support : No    
Use VRRP Framework       : Yes
Use Debug flags   : No不能出現四個yes。
make && make install
成功以後:
(2)cd /usr/local/keepalived/etc/keepalived
mkdir /etc/keepalived
cp keepalived.conf /etc/keepalived/
ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/

(3)編輯配置文件:
vi /etc/keepalived/keepalived.confhtml

  
  
  
  
  1. global_defs { 
  2. notification_email { 
  3. 997365139@qq.com #接收警報的 email 地址,能夠添加多個 
  4.  
  5. notification_email_from keepalived@localhost 
  6. smtp_server 127.0.0.1 #使用本機轉發 email 
  7. smtp_connect_timeout 30 
  8. router_id LVS_DEVEL #load balancer 的標識 ID,用於 email 警報 
  9.  
  10. vrrp_instance VI_1 { 
  11. state MASTER       #備機改成 BACKUP,此狀態是由 priority 的值來決定的,當前 
  12. priority 的值小於備機的值,那麼將會失去 MASTER 狀態,在這裏誰的priority
  13. 大誰的優先級高
  14. interface br0  #(根據你的HA機子決定,本人選的是desktop12和
    desktop41因此是br0,要是虛擬的話能夠是eth0) 
  15. virtual_router_id 81   #主、備機的 virtual_router_id 必須相同 
  16. priority 150          #主機的優先級,備份機改成 50 
  17. advert_int 1            #主備之間的通告間隔秒數 
  18. authentication { #主備之間的通告間隔秒數 
  19. auth_type PASS 
  20. auth_pass 1111 
  21. virtual_ipaddress { 
  22. 192.168.0.1          #HA虛擬ip,可加多個 
  23. virtual_server 192.168.0.1 80 { 
  24. delay_loop 6               #每隔6秒查詢realserver狀態 
  25. lb_algo rr                #lvs調度算法,這裏使用輪詢 
  26. lb_kind DR                #lvs負載均衡機制,這裏使用直連路由 
  27. # nat_mask 255.255.255.0 
  28. # persistence_timeout 50      #同一IP的鏈接60秒內被分配到同一臺realserver 
  29. protocol TCP 
  30.  
  31. real_server 192.168.0.67 80 { 
  32. weight 1 
  33. TCP_CHECK { 
  34. connect_timeout 3 
  35. nb_get_retry 3                    #故障重試秒數 
  36. delay_before_retry 3              #重試延遲 
  37.  
  38. real_server 192.168.0.58 80 { 
  39. weight 1 
  40. TCP_CHECK { 
  41. connect_timeout 3 
  42. nb_get_retry 3 
  43. delay_before_retry 3 
  44.       } 
  45.     } 
  46. } (除以上有效語句以外的其他部分最好刪除,不然會影響效果的!)

編輯完以後:scp /etc/keepalived/keepalived.conf desktop41.example.comlinux

以後去desktop41: vi /etc/keepalived/keepalived.conf
state BACKUP
priority 50

(4)分別在realserver 2臺:desktop67.example.com和desktop58.example.com執行如下:
vi /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
(注意將橋接的那幾個註釋掉這幾條的意思是屏蔽全部的arp封包,意思是不讓其進行解析,只經過192.168.0.1這個虛擬IP鏈接過來的哦!)
sysctl -p(使其生效)算法

echo `hostname` > /var/www/html/index.html
service httpd start
服務器

ifconfig eth0:0 192.168.0.1 netmask 255.255.255.255 up
route add -host 192.168.0.1 dev eth0:0

(5)測試:
測試:
訪問 http://192.168.0.1,看到頁面在兩個 realserver 上切換表示成功!
你也能夠經過 ipvsadm -l 查看詳細鏈接狀況!
經過: tail -f /var/log/message查看啓動成功與否!以及出現什麼問題等。
當在一個機子上啓動時,你經過ip addr list 能夠查看到負載均衡


(能夠看到給你添加了 inet 192.168.0.1/32 scope global br0這一條,當你2臺高可用都啓動的時候會自動去訪問的是master那臺,由於優先級高,這裏數值大的優先級高,當一臺關閉的時候會被另外一個 接管)ide

 同時你能夠添加服務vsftpd 在keepalived.conf中添加代碼:oop

  
  
  
  
  1. ! Configuration File for keepalived 
  2.  
  3. global_defs { 
  4.    notification_email { 
  5.      997365139@qq.com 
  6.    } 
  7.    notification_email_from Alexandre.Cassen@firewall.loc 
  8.    smtp_server 127.0.0.1 
  9.    smtp_connect_timeout 30 
  10.    router_id LVS_DEVEL 
  11.  
  12. vrrp_instance VI_1 { 
  13.     state BACKUP 
  14.     interface eth0 
  15.     virtual_router_id 91 
  16.     priority 50 
  17.     advert_int 1 
  18.     authentication { 
  19.         auth_type PASS 
  20.         auth_pass 1111 
  21.     } 
  22.     virtual_ipaddress { 
  23.         192.168.0.2 
  24.         192.168.0.4 
  25.     } 
  26.  
  27. virtual_server 192.168.0.2 80 { 
  28.     delay_loop 6 
  29.     lb_algo rr 
  30.     lb_kind DR 
  31. #    nat_mask 255.255.255.0 
  32. #    persistence_timeout 50 
  33.     protocol TCP 
  34.  
  35.     real_server 192.168.0.73 80 { 
  36.         weight 1 
  37.         TCP_CHECK { 
  38.             connect_timeout 3 
  39.             nb_get_retry 3 
  40.             delay_before_retry 3 
  41.         } 
  42.     } 
  43.     real_server 192.168.0.74 80 { 
  44.         weight 1 
  45.         TCP_CHECK { 
  46.             connect_timeout 3 
  47.             nb_get_retry 3 
  48.             delay_before_retry 3 
  49.         } 
  50.     } 
  51.  
  52. virtual_server 192.168.0.4 21 { 
  53.     delay_loop 6 
  54.     lb_algo rr 
  55.     lb_kind DR 
  56. #    nat_mask 255.255.255.0 
  57. #    persistence_timeout 50 
  58.     protocol TCP 
  59.  
  60.     real_server 192.168.0.73 21 { 
  61.         weight 1 
  62.         TCP_CHECK { 
  63.             connect_timeout 3 
  64.             nb_get_retry 3 
  65.             delay_before_retry 3 
  66.         } 
  67.     } 
  68.     real_server 192.168.0.74 21 { 
  69.         weight 1 
  70.         TCP_CHECK { 
  71.             connect_timeout 3 
  72.             nb_get_retry 3 
  73.             delay_before_retry 3 
  74.         } 
  75.     } 

在使用的時候你能夠有時候在tail -f /var/log/message的時候出現 測試

報錯解決方案spa

1.主服務器中止後,備用服務沒有啓用rest

監控主服務器上的日誌 Jun 28 09:18:32 rust Keepalived_vrrp: receive an invalid ip number count associated with VRID! Jun 28 09:18:32 rust Keepalived_vrrp: bogus VRRP packet received on eth0 !!! Jun 28 09:18:32 rust Keepalived_vrrp: VRRP_Instance(VI_1) Dropping received VRRP packet...   監控備用服務器上的keepalived日誌

Jun 28 06:25:05 stye Keepalived_vrrp: bogus VRRP packet received on eth0 !!!

Jun 28 06:25:05 stye Keepalived_vrrp: VRRP_Instance(VI_1) ignoring received

advertisment...

解決方案:

改變配置文件/etc/keepalived/keepalived.conf virtual_route_id的值

好比

 virtual_router_id 60 主從方都要改,默認爲51或者是中止keepalived服務而後start,不要用restart!

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息