keepalived nginx 기반 더 블 메 인 모델 구현

준비 환경
Director 1
Director 2
Web Server Nginx1
Web Server Nginx
DIP:172.18.42.100
DIP:172.18.42.22
RIP:172.18.42.111
RIP:172.18.42.222
VIP:172.18.42.119(MASTER)VIP:172.18.42.120(BACKUP)
VIP:172.18.42.119(BACKUP)VIP:172.18.42.120(MASTER)
VIP:172.18.42.119VIP:172.18.42.120
VIP:172.18.42.119VIP:172.18.42.120
1. Director 1 설정
1. yum 설치 keepalived
[root@wtc ~]# yum install keepalived -y

2. 프로필 편집
[root@localhost ~]# vim /etc/keepalived/keepalived.conf
global_defs {
   notification_email {
        root@localhost
   }
   notification_email_from www.mageedu.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id Slackware
   vrrp_mcast_group4 224.0.42.10
}
 
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 110
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.18.42.119 dev eth0 label eth0:1
    }
}

virtual_server 172.18.42.119 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    protocol TCP
    real_server 172.18.42.111 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 172.18.42.222 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
} 
vrrp_instance VI_2 {
    state BACKUP
    interface eth0
    virtual_router_id 111
    priority 98
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.18.42.120 dev eth0 label eth0:2
    }
}
 
virtual_server 172.18.42.120 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    protocol TCP
    real_server 172.18.42.111 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 172.18.42.222 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

2. Director 2 설정
1. keepalived 서비스 설치
[root@localhost~]# yum install keepalived -y

2. 프로필 편집
[root@wtc ~]# vim /etc/keepalived/keepalived.conf
global_defs {
   notification_email {
        root@localhost
   }
   notification_email_from www.mageedu.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id Slackware
   vrrp_mcast_group4 224.0.42.10
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 110
    priority 98
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.18.42.119 dev eth0 label eth0:1
    }
}

virtual_server 172.18.42.119 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    protocol TCP
    real_server 172.18.42.111 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 172.18.42.222 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}



vrrp_instance VI_2 {
    state MASTER
    interface eth0
    virtual_router_id 111
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.18.42.120 dev eth0 label eth0:2
    }
}

virtual_server 172.18.42.120 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    protocol TCP
    real_server 172.18.42.111 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 172.18.42.222 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

3. 웹 서버 Nginx 1 설정
1. 웹 서버 Nginx 1 의 커 널 매개 변 수 를 수정 합 니 다.
[root@localhost ~]# echo 1 &> /proc/sys/net/ipv4/conf/all/arp_ignore 
[root@localhost ~]# echo 1 &> /proc/sys/net/ipv4/conf/lo/arp_ignore 
[root@localhost ~]# echo 2 &> /proc/sys/net/ipv4/conf/lo/arp_announce 
[root@localhost ~]# echo 2 &> /proc/sys/net/ipv4/conf/all/arp_announce

2, VIP 추가
[root@localhost ~]# ifconfig lo:1 172.18.42.119 netmask 255.255.255.255 broadcast 172.18.42.119
[root@localhost ~]# ifconfig lo:2 172.18.42.120 netmask 255.255.255.255 broadcast 172.18.42.120
[root@localhost ~]# route add -host 172.18.42.119 dev lo:1
[root@localhost ~]# route add -host 172.18.42.120 dev lo:2

3. nginx 를 설치 하고 시작 합 니 다.
[root@localhost ~]# yum install nginx -y
[root@localhost ~]# nginx

4. 웹 서버 Nginx 2 설정
1. 웹 서버 Nginx 2 의 커 널 매개 변 수 를 수정 합 니 다.
[root@localhost ~]# echo 1 &> /proc/sys/net/ipv4/conf/all/arp_ignore 
[root@localhost ~]# echo 1 &> /proc/sys/net/ipv4/conf/lo/arp_ignore 
[root@localhost ~]# echo 2 &> /proc/sys/net/ipv4/conf/lo/arp_announce 
[root@localhost ~]# echo 2 &> /proc/sys/net/ipv4/conf/all/arp_announce

2, VIP 추가
[root@localhost ~]# ifconfig lo:1 172.18.42.119 netmask 255.255.255.255 broadcast 172.18.42.119
[root@localhost ~]# ifconfig lo:2 172.18.42.120 netmask 255.255.255.255 broadcast 172.18.42.120
[root@localhost ~]# route add -host 172.18.42.119 dev lo:1
[root@localhost ~]# route add -host 172.18.42.120 dev lo:2

3. nginx 를 설치 하고 시작 합 니 다.
[root@localhost ~]# yum install nginx -y
[root@localhost ~]# nginx

5. Director 두 대 를 열 고 테스트 합 니 다.
1、Director 1
[root@localhost ~]# service keepalived start
[root@localhost ~]# ifconfig 
eth0      Link encap:Ethernet  HWaddr 00:0C:29:90:84:C0  
          inet addr:172.18.42.100  Bcast:172.18.255.255  Mask:255.255.0.0
          inet6 addr: fe80::20c:29ff:fe90:84c0/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:182021 errors:0 dropped:0 overruns:0 frame:0
          TX packets:48384 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:28094276 (26.7 MiB)  TX bytes:4476727 (4.2 MiB)
eth0:1    Link encap:Ethernet  HWaddr 00:0C:29:90:84:C0  
          inet addr:172.18.42.119  Bcast:0.0.0.0  Mask:255.255.255.255
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:663 errors:0 dropped:0 overruns:0 frame:0
          TX packets:663 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:49597 (48.4 KiB)  TX bytes:49597 (48.4 KiB)
[root@localhost ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.18.42.119:80 rr
  -> 172.18.42.111:80             Route   1      0          0         
  -> 172.18.42.222:80             Route   1      0          1         
TCP  172.18.42.120:80 rr
  -> 172.18.42.111:80             Route   1      0          0         
  -> 172.18.42.222:80             Route   1      0          0

\ # # Director 1 의 VIP 가 시작 되 었 습 니 다.동시에 ipvsadm 규칙 자동 생 성
2、Director 2
[root@wtc ~]# ifconfig 
eth0      Link encap:Ethernet  HWaddr 00:0C:29:0F:A3:6E  
          inet addr:172.18.42.22  Bcast:172.18.255.255  Mask:255.255.0.0
          inet6 addr: fe80::20c:29ff:fe0f:a36e/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:134293 errors:0 dropped:0 overruns:0 frame:0
          TX packets:69576 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:16446378 (15.6 MiB)  TX bytes:5481841 (5.2 MiB)
 
eth0:2    Link encap:Ethernet  HWaddr 00:0C:29:0F:A3:6E  
          inet addr:172.18.42.120  Bcast:0.0.0.0  Mask:255.255.255.255
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
 
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:3431 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3431 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:291422 (284.5 KiB)  TX bytes:291422 (284.5 KiB)
 
[root@wtc ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.18.42.119:80 rr
  -> 172.18.42.111:80             Route   1      0          0         
  -> 172.18.42.222:80             Route   1      0          0         
TCP  172.18.42.120:80 rr
  -> 172.18.42.111:80             Route   1      0          0         
  -> 172.18.42.222:80             Route   1      0          0

웹 서비스 에 접근 해 보기
[root@localhost ~]# curl http://172.18.42.119
<h1>
172.18.42.222 Web Server 2
</h1>
[root@localhost ~]# curl http://172.18.42.119
<h1>
172.18.42.111 Web Server 1
</h1>
[root@localhost ~]# curl http://172.18.42.120
<h1>
172.18.42.222 Web Server 2
</h1>
[root@localhost ~]# curl http://172.18.42.120
<h1>
172.18.42.111 Web Server 1

\ # \ # 부하 균형, 더 블 메 인 모델 실현
6. Director 1 을 닫 기;어떤 상황 이 벌 어 질 까?
Director 1
[root@localhost ~]# service keepalived stop

Director 2
[root@wtc ~]# ifconfig 
eth0      Link encap:Ethernet  HWaddr 00:0C:29:0F:A3:6E  
          inet addr:172.18.42.22  Bcast:172.18.255.255  Mask:255.255.0.0
          inet6 addr: fe80::20c:29ff:fe0f:a36e/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:142076 errors:0 dropped:0 overruns:0 frame:0
          TX packets:73562 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:17381764 (16.5 MiB)  TX bytes:5790752 (5.5 MiB)
 
eth0:1    Link encap:Ethernet  HWaddr 00:0C:29:0F:A3:6E  
          inet addr:172.18.42.119  Bcast:0.0.0.0  Mask:255.255.255.255
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
 
eth0:2    Link encap:Ethernet  HWaddr 00:0C:29:0F:A3:6E  
          inet addr:172.18.42.120  Bcast:0.0.0.0  Mask:255.255.255.255
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
 
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:3435 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3435 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:291770 (284.9 KiB)  TX bytes:291770 (284.9 KiB)
 
[root@wtc ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.18.42.119:80 rr
  -> 172.18.42.111:80             Route   1      0          0         
  -> 172.18.42.222:80             Route   1      0          0         
TCP  172.18.42.120:80 rr
  -> 172.18.42.111:80             Route   1      0          1         
  -> 172.18.42.222:80             Route   1      0          1

\ # # Director 1 을 닫 으 면 Director 2 가 Director 의 VIP 를 빼 앗 습 니 다.
웹 서 비 스 를 다시 방문 하 겠 습 니 다.
[root@localhost ~]# curl http://172.18.42.120
<h1>
172.18.42.222 Web Server 2
</h1>
[root@localhost ~]# curl http://172.18.42.120
<h1>
172.18.42.111 Web Server 1
</h1>
[root@localhost ~]# curl http://172.18.42.119
<h1>
172.18.42.222 Web Server 2
</h1>
[root@localhost ~]# curl http://172.18.42.119
<h1>
172.18.42.111 Web Server 1
</h1>

서 비 스 를 정상적으로 제공 할 수 있 습 니 다.
7. 웹 2 서비스 가 끊 기 면 어떤 상황 이 발생 합 니까?
Web Server Nginx2
[root@localhost ~]# nginx -s stop

Director
[root@wtc ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.18.42.119:80 rr
  -> 172.18.42.222:80             Route   1      0          1         
TCP  172.18.42.120:80 rr
  -> 172.18.42.222:80             Route   1      0          1

\ # \ # 서버 에서 Web1 IP 자동 제거
웹 서비스 다시 시도
[root@localhost ~]# curl http://172.18.42.119
<h1>
172.18.42.222 Web Server 2
</h1>
[root@localhost ~]# curl http://172.18.42.119
<h1>
172.18.42.222 Web Server 2
</h1>
[root@localhost ~]# curl http://172.18.42.120
<h1>
172.18.42.222 Web Server 2
</h1>
[root@localhost ~]# curl http://172.18.42.120
<h1>
172.18.42.222 Web Server 2
</h1>

\ # \ # 요청 에 응답 하 는 것 은 웹 서버 Nginx 2 뿐 입 니 다.
웹 서버 Nginx 1 을 열 면;서버 에 규칙 이 자동 으로 추 가 됩 니까?
[root@localhost ~]# nginx
[root@wtc ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.18.42.119:80 rr
  -> 172.18.42.111:80             Route   1      0          0         
  -> 172.18.42.222:80             Route   1      0          0         
TCP  172.18.42.120:80 rr
  -> 172.18.42.111:80             Route   1      0          0         
  -> 172.18.42.222:80             Route   1      0          0

\ # \ # 웹 서버 Nginx 1 규칙 자동 추가
문제 총화:
1. Director 의 keepalived 설정 파일 에 대해;두 vrrp 예제 의 "vrrp router id" 는 같 을 수 없어 야 합 니 다.
2. 모든 Director 의 시간 은 동기 화 되 어야 합 니 다.ntpdate 기반 구현 가능

좋은 웹페이지 즐겨찾기