一 基礎環境 1.1 IP規劃 OS:CentOS 6.8 64位 節點類型 IP規劃 主機名 類型 主 Director Server eth0:172.24.8.10 DR1 公共IP eth1:192.168.56.100 心跳 私有IP eth0:0:172.24.8.100 無 虛擬IP ...
一 基礎環境
1.1 IP規劃
OS:CentOS 6.8 64位1.2 架構規劃
二 高可用LVS負載均衡集群部署
2.1 NTP部署
操作略,具體可參考N01.1.1-常見服務《NTP》。 註意:為了保證集群的穩定性,強烈建議在所有節點均部署NTP同步服務,保證所有時鐘一致。2.2 部署httpd集群
1 [root@RServer01 ~]# yum -y install httpd 2 [root@RServer01 ~]# service iptables stop 3 [root@RServer01 ~]# chkconfig iptables off 4 [root@RServer01 ~]# vi /etc/selinux/config 5 SELINUX=disabled 6 [root@master ~]# setenforce 0 #關閉SELinux及防火牆註意:後端所有Real伺服器節點都需要安裝,用於模擬測試。 建議:為了測試方便,建議所有節點關閉防火牆和SELinux,若未關閉防火牆也可通過下列方式放通:
1 firewall-cmd --permanent–-add-service=keepalived 2 firewall-cmd --reload
2.3 安裝Keepalived
1 [root@lvsmaster ~]# yum -y install gcc gcc-c++ make kernel-devel kernel-tools kernel-tools-libs kernel libnl libnl-devel libnfnetlink-devel openssl-devel wget openssh-clients #安裝基礎環境及依賴 2 [root@lvsmaster ~]# ln -s /usr/src/kernels/`uname -r` /usr/src/linux 3 [root@lvsmaster ~]# wget http://www.keepalived.org/software/keepalived-1.3.6.tar.gz 4 [root@lvsmaster ~]# tar -zxvf keepalived-1.3.6.tar.gz #編譯安裝Keepalived 5 [root@lvsmaster ~]# cd keepalived-1.3.6/ 6 [root@lvsmaster keepalived-1.3.6]# ./configure --prefix=/usr/local/keepalived 7 [root@Master keepalived-1.3.9]# make && make install註意:CentOS6.8安裝高於1.3.6版本會出現未知錯誤。
2.4 添加啟動相關服務
1 [root@lvsmaster ~]# mkdir /etc/keepalived 2 [root@lvsmaster ~]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ 3 [root@lvsmaster ~]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ 4 [root@lvsmaster ~]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ 5 [root@lvsmaster ~]# vi /etc/init.d/keepalived #創建Keepalived啟動腳本,如附件 6 #!/bin/sh 7 # 8 # keepalived High Availability monitor built upon LVS and VRRP 9 # 10 # chkconfig: - 86 14 11 # description: Robust keepalive facility to the Linux Virtual Server project \ 12 # with multilayer TCP/IP stack checks. 13 14 ### BEGIN INIT INFO 15 # Provides: keepalived 16 # Required-Start: $local_fs $network $named $syslog 17 # Required-Stop: $local_fs $network $named $syslog 18 # Should-Start: smtpdaemon httpd 19 # Should-Stop: smtpdaemon httpd 20 # Default-Start: 21 # Default-Stop: 0 1 2 3 4 5 6 22 # Short-Description: High Availability monitor built upon LVS and VRRP 23 # Description: Robust keepalive facility to the Linux Virtual Server 24 # project with multilayer TCP/IP stack checks. 25 ### END INIT INFO 26 27 # Source function library. 28 . /etc/rc.d/init.d/functions 29 30 exec="/usr/sbin/keepalived" 31 prog="keepalived" 32 config="/etc/keepalived/keepalived.conf" 33 34 [ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog 35 36 lockfile=/var/lock/subsys/keepalived 37 38 start() { 39 [ -x $exec ] || exit 5 40 [ -e $config ] || exit 6 41 echo -n $"Starting $prog: " 42 daemon $exec $KEEPALIVED_OPTIONS 43 retval=$? 44 echo 45 [ $retval -eq 0 ] && touch $lockfile 46 return $retval 47 } 48 49 stop() { 50 echo -n $"Stopping $prog: " 51 killproc $prog 52 retval=$? 53 echo 54 [ $retval -eq 0 ] && rm -f $lockfile 55 return $retval 56 } 57 58 restart() { 59 stop 60 start 61 } 62 63 reload() { 64 echo -n $"Reloading $prog: " 65 killproc $prog -1 66 retval=$? 67 echo 68 return $retval 69 } 70 71 force_reload() { 72 restart 73 } 74 75 rh_status() { 76 status $prog 77 } 78 79 rh_status_q() { 80 rh_status &>/dev/null 81 } 82 83 84 case "$1" in 85 start) 86 rh_status_q && exit 0 87 $1 88 ;; 89 stop) 90 rh_status_q || exit 0 91 $1 92 ;; 93 restart) 94 $1 95 ;; 96 reload) 97 rh_status_q || exit 7 98 $1 99 ;; 100 force-reload) 101 force_reload 102 ;; 103 status) 104 rh_status 105 ;; 106 condrestart|try-restart) 107 rh_status_q || exit 0 108 restart 109 ;; 110 *) 111 echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}" 112 exit 2 113 esac 114 exit $ 115 [root@lvsmaster ~]# chmod u+x /etc/rc.d/init.d/keepalived 116 [root@lvsmaster ~]# vi /etc/keepalived/keepalived.conf 117 ! Configuration File for keepalived 118 …… 119 smtp_connect_timeout 30 120 router_id LVS_Master #表示運行Keepalived伺服器的一個標識 121 } 122 123 vrrp_instance VI_1 { 124 state MASTER #指定Keepalived的角色 125 interface eth0 #指定HA監測網路的介面 126 virtual_router_id 51 #同一個vrrp實例使用唯一的標識,即同一個vrrp_instance下,Master和Backup必須是一致的 128 priority 100 #定義優先順序,數值越大,優先順序越高 129 advert_int 1 #設定Mater和Backup負載均衡器之間同步檢查時間間隔 130 authentication { 131 auth_type PASS 132 auth_pass 1111 133 } 134 virtual_ipaddress { 135 172.24.8.100 #設置虛擬IP地址 136 } 137 } 138 139 virtual_server 172.24.8.100 80 { 140 delay_loop 6 #運行情況檢查時間 141 lb_algo rr #設置負載均衡演算法 142 lb_kind DR #設置LVS實現負載均衡的機制,有NAT/DR/TUN 143 persistence_timeout 50 #會話保持時間 144 protocol TCP #指定轉發類型 145 146 real_server 172.24.8.12 80 { 147 weight 1 #服務節點的權值,數值越大,權值越高 148 TCP_CHECK { 149 connect_timeout 5 #表示無響應超時時間,單位是秒 150 nb_get_retry 3 #表示重試次數 151 delay_before_retry 3 #表示重試間隔 152 } 153 } 154 real_server 172.24.8.13 80 { 155 weight 1 156 TCP_CHECK { 157 connect_timeout 5 158 nb_get_retry 3 159 delay_before_retry 3 160 } 161 } 162 } 163 [root@lvsmaster ~]# scp /etc/keepalived/keepalived.conf 172.24.8.11:/etc/keepalived/keepalived.conf 164 [root@lvsbackup ~]# vi /etc/keepalived/keepalived.conf 165 state BACKUP 166 priority 80註意;備用Director Server上需要修改狀態為BACKUP和priority優先順序。
2.5 安裝IPVS管理工具
1 [root@lvsmaster ~]# yum -y install ipvsadm
2.6 配置Real Server節點
在LVS的DR和TUN模式下,用戶訪問請求到大Real Server後,Real Server的響應報文直接返回給用戶,而不需經過Director Server。因此,需要在每個Real Server上配置虛擬VIP地址。 註意:迴環介面綁定vip,且禁止arp請求等操作,可通過以下腳本實現:腳本可留言索要。1 [root@RServer01 ~]# vi /etc/init.d/lvsrs 2 [root@RServer01 ~]# chmod u+x /etc/init.d/lvsrs
2.7 啟動集群
1 [root@RServer01 ~]# service httpd start 2 [root@RServer01 ~]# chkconfig httpd on 3 [root@RServer02 ~]# service httpd start 4 [root@RServer02 ~]# chkconfig httpd on 5 6 [root@lvsmaster ~]# service keepalived start 7 [root@lvsmaster ~]# chkconfig keepalived on 8 [root@lvsbackup ~]# service keepalived start 9 [root@lvsbackup ~]# chkconfig keepalived on 10 11 [root@RServer01 ~]# service lvsrs start 12 [root@RServer02 ~]# service lvsrs start
三 測試集群
3.1 高可用功能測試
停止主Director Server伺服器的Keepalived,觀察/var/log/messages日誌,可知備機會立刻變為MASTER,並且接管主機的虛擬ip資源。重啟主Director Server伺服器的Keepalived,備機會重新恢復為BACKUP角色。 具體測試略。3.2 負載均衡測試
1 [root@RServer01 ~]# echo 'This is Real Server01!' >>/var/www/html/index.html 2 [root@RServer01 ~]# echo 'This is Real Server02!' >>/var/www/html/index.html然後瀏覽器訪問:http://172.24.8.100,並不斷的刷新,能分別看到Server01和Server02即可。
3.3 故障切換測試
1 [root@RServer01 ~]# service httpd stop
當關掉其中一個Real Server時,訪問VIP,只會顯示還處於集群中的web節點。