實戰環境(CentOS7) VIP 10.211.55.180 LVS1 10.211.55.151 LVS1 10.211.55.151 RS1 10.211.55.171 RS2 10.211.55.172 初始化環境全部節點 iptalbes -F #清空防火牆 systemctl stop firewalld #關閉防火牆 systemctl disable firewalld #中止防火牆開機自啓動 setenforce 0 #臨時關閉selinux sed -i 's/^SELINUX=.*$/SELINUX=disabled/g' /etc/selinux/config #關閉selinux yum -y install epel-release #安裝epel源 LVS一、2操做 一、安裝keepalived ipvsadm yum -y install keepalived ipvsadm [root@localhost ~]# lsmod |grep ip_vs #檢查ipvs模塊 ip_vs_rr 12600 1 ip_vs 145497 3 ip_vs_rr nf_conntrack 139264 9 ip_vs,nf_nat,nf_nat_ipv4,nf_nat_ipv6,xt_conntrack,nf_nat_masquerade_ipv4,nf_conntrack_netlink,nf_conntrack_ipv4,nf_conntrack_ipv6 libcrc32c 12644 4 xfs,ip_vs,nf_nat,nf_conntrack 二、配置keepalived (LVS一、2) 2.1 修改keepalived的配置文件 [root@localhost ~]# cd /etc/keepalived/ [root@localhost keepalived]# cat keepalived.conf ! Configuration File for keepalived global_defs { notification_email { 314334451@qq.com } notification_email_from 314334451@qq.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL vrrp_skip_check_adv_addr # vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_instance VI_1 { state MASTER #LVS1的這裏是MASTER,則LVS2的改成BACKUP interface eth0 virtual_router_id 51 priority 100 #LVS1的是100,則LVS2的爲100如下就行,好比90,權重越大,VIP則飄在哪一邊 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.211.55.180 #VIP } } virtual_server 10.211.55.180 80 { #VIP delay_loop 6 lb_algo wrr lb_kind DR persistence_timeout 50 protocol TCP real_server 10.211.55.171 80 { #後端RS1檢測,若有問題則移除服務器,再也不往該服務器發送請求 weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 10.211.55.172 80 { #後端RS2檢測,若有問題則移除服務器,再也不往該服務器發送請求 weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } } 2.2 設置開機啓動(LVS一、2) systemctl enable keepalived systemctl start keepalived 三、配置LVS,執行下面的腳本(LVS一、2),並把腳本設置爲開機啓動 vi /opt/lvs_dr.sh #!/bin/sh # description: Start LVS of Director server VIP=192.168.30.100 RIP1=192.168.30.71 RIP2=192.168.30.72 case "$1" in start) echo " start LVS of Director Server" # set the Virtual IP Address and sysctl parameter # /sbin/ifconfig eth0:0 $VIP broadcast $VIP netmask 255.255.255.255 up # route add -host $VIP dev eth0:0 echo "1" >/proc/sys/net/ipv4/ip_forward #Clear IPVS table /sbin/ipvsadm -C #set LVS /sbin/ipvsadm -A -t $VIP:80 -s wrr # -p 600 #須要會話持久化則把此處的-p 600取消註釋,或使用sh算法 /sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g -w1 #-g DR模式 /sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -g -w1 #Run LVS /sbin/ipvsadm ;; stop) echo "close LVS Directorserver" echo "0" >/proc/sys/net/ipv4/ip_forward /sbin/ipvsadm -C # /sbin/ifconfig eth0:0 down ;; *) echo "Usage: $0 {start|stop}" exit 1 esac chmod +x /opt/lvs_dr.sh #加執行權限 echo "/opt/lvs_dr.sh start" >>/etc/profile #開機自啓動 四、配置RS操做,執行下面操做(RS一、2) 安裝nginx yum -y install nginx #RS一、2 都安裝 #配置測試頁面 echo 10.211.55.171 >/usr/share/nginx/html/index.html #RS1上操做 echo 10.211.55.172 >/usr/share/nginx/html/index.html #RS2上操做 設置RS腳本,並設置開機自啓動 vi /opt/lvs_rs.sh #!/bin/bash VIP=192.168.30.100 /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up #添加虛擬IP /sbin/route add -host $VIP dev lo:0 #添加網關 echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce #end 此操做是在迴環設備上綁定了一個虛擬IP地址,並設定其子網掩碼爲255.255.255.255,與Director Server上的虛擬IP保持互通,而後禁止了本機的ARP請求。 因爲虛擬ip,也就是上面的VIP地址,是Director Server和全部的Real server共享的,若是有ARP請求VIP地址時,Director Server與全部Real server都作應答的話,就出現問題了,所以,須要禁止Real server響應ARP請求。而lvsrs腳本的做用就是使Real Server不響應arp請求。 chmod + /opt/lvs_rs.sh #加執行權限 echo "/opt/lvs_rs.sh " #開機自啓動 五、測試 找一個客戶機,執行下面的命令 ->$ for i in `seq 20`;do curl 10.211.55.180;done 10.211.55.171 10.211.55.172 10.211.55.171 10.211.55.172 10.211.55.171 10.211.55.172 10.211.55.171 10.211.55.172 10.211.55.171 10.211.55.172 10.211.55.171 10.211.55.172 10.211.55.171 10.211.55.172 10.211.55.171 10.211.55.172 10.211.55.171 10.211.55.172 10.211.55.171 10.211.55.172