LVS+Keepalive

1. 자주 사용 하 는 부하 균형 소프트웨어:
Nginx  응용 층 부하
LVS      네트워크 계층 부하
HAProxy  응용 층 부하
자주 사용 하 는 부하 균형 하드웨어:
F5 、Netscale
2. LVS 의 네 가지 작업 모델
1) VS / NAT 모드 (네트워크 주소 번역)
NAT 변환 표를 통 해 부하 가 진행 되 며, 하 도 급 과 하 도 급 은 모두 표를 검사 해 야 한다.
2) VS / TUN 모드 (터널 링)
패 킷 에 IP 헤드 를 달다
3) DR 모드 (직접 라 우 팅)
패 킷 에 MAC 헤드 를 연결 합 니 다.
4)fulnat
이중 NAT 변환
3. LVS 설정 (NAT 모드)
세 대의 호스트, 한 대 는 부하 전송 (dir), 두 대 는 업무 (rs)
hostname dir
loginout
hostname rs1
loginout
hostname rs2
loginout
= = = = = = = = = = = = = = = dir 설정
yum install ipvsadm -y
#dir   ipvsadm 
vim /usr/local/sbin/lvs_nat.sh
#! /bin/bash# director             : echo 1 > /proc/sys/net/ipv4/ip_forward #   icmp    echo 0 > /proc/sys/net/ipv4/conf/all/send_redirectsecho 0 > /proc/sys/net/ipv4/conf/default/send_redirectsecho 0 > /proc/sys/net/ipv4/conf/eth0/send_redirectsecho 0 > /proc/sys/net/ipv4/conf/eth1/send_redirects
# director   nat   
iptables -t nat -F
iptables -t nat -X
iptables -t nat -A POSTROUTING -s 192.168.2.0/24  -j MASQUERADE   #      
# director  ipvsadm
IPVSADM='/sbin/ipvsadm'
$IPVSADM -C
$IPVSADM -A -t 192.168.1.200:80 -s rr  
$IPVSADM -a -t 192.168.1.200:80 -r 192.168.2.1:80 -m        
$IPVSADM -a -t 192.168.1.200:80 -r 192.168.2.2:80 -m 
/bin/bash /usr/local/sbin/lvs_nat.sh
#    
ipvsadm -ln   
#       
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.1.200:80 rr
  -> 192.168.2.1:80               Masq    1      0          0         
  -> 192.168.2.2:80               Masq    1      0          0

= = = = = = = = = = = = = = = = = = = = = = = = rs 설정
yum install nginx -y
#rs       nginx    
echo "111master" > /usr/share/nginx/html/index.html 
yum install nginx -y
echo "222slave" > /usr/share/nginx/html/index.html 
[root@dir ~]# curl 192.168.1.200:80
111master
[root@dir ~]# curl 192.168.1.200:80
222slave
[root@dir ~]# curl 192.168.1.200:80
111master
[root@dir ~]# curl 192.168.1.200:80
222slave

테스트 성공
4. LVS 설정 (DR 모드)
ipvsadm -C   
ipvsadm -ln
iptables -t nat -F
#    
rs     dir,         ,      IP,  IP
vim /usr/local/sbin/lvs_dr.sh
#! /bin/bash
echo 1 > /proc/sys/net/ipv4/ip_forward
ipv=/sbin/ipvsadm
vip=192.168.1.205
rs1=192.168.1.201
rs2=192.168.1.202
ifconfig eth0:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip dev eth0:0
$ipv -C
$ipv -A -t $vip:80 -s rr 
$ipv -a -t $vip:80 -r $rs1:80 -g -w 1
$ipv -a -t $vip:80 -r $rs2:80 -g -w 1
/bin/bash /usr/local/sbin/lvs_dr.sh            #    
ipvsadm -ln         #    

= = = = = = = = = = = = = = = = = = = = = =
#! /bin/bash
vip=192.168.1.205
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip 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
/bin/bash  /usr/local/sbin/lvs_dr_rs.sh

테스트: 리 눅 스 를 한 대 더 여 는 것 이 좋 습 니 다. 브 라 우 저 에 캐 시 가 있 습 니 다.
5. LVS + keepalived
    keepalived,    ,dir rs2   keepalive
[root@dir ~]# ipvsadm -C 
#    
yum install -y keepalived ipvsadm 
#dir rs2  
cp  /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak 
>  /etc/keepalived/keepalived.conf
vim  /etc/keepalived/keepalived.conf
#dir       
vrrp_instance VI_1 {
    state MASTER   #        BACKUP
    interface eth0
    virtual_router_id 51
    priority 100  #       90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.205
    }
}
virtual_server 192.168.1.205 80 {
    delay_loop 6                  #(  10   realserver  )
    lb_algo wlc                  #(lvs   )
    lb_kind DR                  #(Direct Route)
    persistence_timeout 60        #(  IP   60         realserver)
    protocol TCP                #( TCP    realserver  )
    real_server 192.168.1.201 80 {
        weight 100               #(  )
        TCP_CHECK {
        connect_timeout 10       #(10      )
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
    }
real_server 192.168.1.202 80 {
        weight 100
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
     }
}
/etc/init.d/keepalived start                       #  
     keepalived:                                      [  ]
ip add                                                       #    IP    
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:e2:dc:da brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.200/24 brd 192.168.1.255 scope global eth0
    inet 192.168.1.205/32 scope global eth0
    inet6 fe80::20c:29ff:fee2:dcda/64 scope link 
       valid_lft forever preferred_lft forever

= = = = = = = = = = = = = = = = = = = = keeplived 에서 설정
cp  /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak 
>  /etc/keepalived/keepalived.conf
vim  /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
    state BACKUP   #        BACKUP
    interface eth0
    virtual_router_id 51
    priority 90  #       90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.205
    }
}
virtual_server 192.168.1.205 80 {
    delay_loop 6                  #(  10   realserver  )
    lb_algo wlc                  #(lvs   )
    lb_kind DR                  #(Direct Route)
    persistence_timeout 60        #(  IP   60         realserver)
    protocol TCP                #( TCP    realserver  )
    real_server 192.168.1.201 80 {
        weight 100               #(  )
        TCP_CHECK {
        connect_timeout 10       #(10      )
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
    }
real_server 192.168.1.202 80 {
        weight 100
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
     }
}
/etc/init.d/keepalived start         
/etc/init.d/ipvsadm start
=====================
    rs Nginx  ,       ,  Iptables    
[root@dir ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.1.205:80 wlc persistent 60
  -> 192.168.1.201:80             Route   100    0          0         
  -> 192.168.1.202:80             Route   100    0          0

    
성공 하 다.
다운 테스트:
rs1 비 즈 니스 네트워크 카드 닫 기
[root@dir ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.1.205:80 wlc persistent 60
  -> 192.168.1.202:80             Route   100    0          0         
   
[root@dir ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.1.205:80 wlc persistent 60
  -> 192.168.1.201:80             Route   100    0          0         
  -> 192.168.1.202:80             Route   100    0          0

   
keeplived 고가 용 테스트
/etc/init.d/keepalived stop       #   
 [root@rs2 ~]# ipvsadm -ln              
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.1.205:80 wlc persistent 60
  -> 192.168.1.201:80             Route   100    0          0         
  -> 192.168.1.202:80             Local   100    0          0

 
성공 하 다.

좋은 웹페이지 즐겨찾기