이 솔루션은 2개의 VIP 주소를 사용하며 프런트엔드는 2개의 머신을 사용하며, 서로가 마스터와 백업이고, 기계가 동시에 작동하고 있는데, 기계 중 하나에 장애가 발생하면 두 기계의 요청이 한 기계의 부담으로 이전됩니다. 이는 현재 아키텍처 환경에 매우 적합하므로 이 솔루션은 높은 성능을 구현하는 데 사용됩니다. 웹사이트의 가용성 아키텍처.
아래에서는 두 번째 구성을 사용하여 듀얼 마스터 고가용성 로드 밸런싱을 보여줍니다.
그룹 1
VIP1: 192.168.36.100
기본 nginx: 192.168.36.99
백업 nginx: 192.168.36.86
2그룹
VIP2: 192.168.36.100
기본 nginx: 192.168.36.86
백업 nginx: 192.168.36.99
웹 서비스:
web1: 192.168.36.215
web2: 192.168.36.80
1. (설치 생략)
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; upstream web1 { server 192.168.36.215:80 max_fails=3 fail_timeout=3s; } upstream web2 { server 192.168.36.80:80 max_fails=3 fail_timeout=3s; } server { listen 88; server_name localhost; location / { root html; index index.html index.htm; proxy_pass http://web1; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } server { listen 99; server_name localhost; location / { root html; index index.html index.htm; proxy_pass http://web2; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } }192.168.36.99 및 192.168.36.86의 nginx를 구성하고 시작합니다. 접속이 정상적인지 확인해주세요.
web1
http://192.168.36.86:88 /
http://192.168.36.99:88/
web2
http://192.168.36.86:99/
http://192.168.36.99:99/
2. keepalived 설치 및 구성(설치 생략)
1)192.168.36.86 구성
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { notification_email { monitor@3evip.cn #failover@firewall.loc } notification_email_from tianwei7518@163.com smtp_server smtp.163.com smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_nginx { script "/etc/keepalived/chk_nginx.sh" interval 2 weight 2 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 100 priority 50 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_nginx } virtual_ipaddress { 192.168.36.100 } } vrrp_instance VI_2 { state MASTER interface eth0 virtual_router_id 200 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_nginx } virtual_ipaddress { 192.168.36.200 } }2)192.168.36.99 구성
vi /etc/ keepalived/keepalived.conf
! Configuration File for keepalived global_defs { notification_email { monitor@3evip.cn #failover@firewall.loc } notification_email_from tianwei7518@163.com smtp_server smtp.163.com smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_nginx { script "/etc/keepalived/chk_nginx.sh" interval 2 weight 2 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 100 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_nginx } virtual_ipaddress { 192.168.36.100 } } vrrp_instance VI_2 { state BACKUP interface eth0 virtual_router_id 200 priority 50 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_nginx } virtual_ipaddress { 192.168.36.200 } }3) 두 머신 모두 nginx 프로세스 검사 스크립트가 있어야 하며 프로세스가 감지되지 않으면 nginx를 다시 시작해야 합니다. 다시 시작할 수 없는 경우 VIP가 백업 시스템으로 전환할 수 있도록 연결 유지를 종료합니다.
vi /etc/keepalived/chk_nginx.sh
#!/bin/sh #description: # 如果启动失败,则停止keepalived status=$( ps -C nginx --no-heading| wc -l) if [ "${status}" = "0" ]; then /usr/local/nginx/sbin/nginx status2=$( ps -C nginx --no-heading| wc -l) if [ "${status2}" = "0" ]; then service keepalived stop fi fi실행 권한 설정: chmod +x /etc/keepalived/chk_nginx.sh
3. 시작 및 테스트
각각 nginx 및 keepalived를 시작합니다. : /usr/local/nginx/sbin/nginx
서비스 연결 유지 시작
방문:
web1
http://192.168.36.100:88/
http://192.168.36.200:88/
web2
http://192.168.36.100:99/
http://192.168.36.200:99/
정상적으로 접속 가능합니다.
연결 유지 기능을 꺼볼 수 있습니다. 연결 유지
서비스 연결 유지 중지192.168.36.86 🎜를 닫습니다. >
로그 보기:
tail -f /var/log/messages
192.168.36.86 로그:
Feb 7 00:39:05 slave-d Keepalived[5738]: Stopping Keepalived v1.2.15 (02/07,2015) Feb 7 00:39:05 slave-d Keepalived_vrrp[5741]: VRRP_Instance(VI_2) sending 0 priority Feb 7 00:39:05 slave-d Keepalived_vrrp[5741]: VRRP_Instance(VI_2) removing protocol VIPs. Feb 7 00:39:05 slave-d Keepalived_healthcheckers[5740]: Netlink reflector reports IP 192.168.36.200 removed Feb 7 00:39:05 slave-d avahi-daemon[1823]: Withdrawing address record for 192.168.36.200 on eth0.192.168.36.99 로그:
Feb 7 00:39:11 slave-c Keepalived_vrrp[25103]: VRRP_Instance(VI_2) Transition to MASTER STATE Feb 7 00:39:12 slave-c Keepalived_vrrp[25103]: VRRP_Instance(VI_2) Entering MASTER STATE Feb 7 00:39:12 slave-c Keepalived_vrrp[25103]: VRRP_Instance(VI_2) setting protocol VIPs. Feb 7 00:39:12 slave-c Keepalived_healthcheckers[25102]: Netlink reflector reports IP 192.168.36.200 added Feb 7 00:39:12 slave-c avahi-daemon[1832]: Registering new address record for 192.168.36.200 on eth0.IPv4. Feb 7 00:39:12 slave-c Keepalived_vrrp[25103]: VRRP_Instance(VI_2) Sending gratuitous ARPs on eth0 for 192.168.36.200 Feb 7 00:39:17 slave-c Keepalived_vrrp[25103]: VRRP_Instance(VI_2) Sending gratuitous ARPs on eth0 for 192.168.36.200192.168.36.86에 다시 시작 연결 유지:
192.168.36.86 로그:
Feb 7 00:40:42 slave-d Keepalived[6004]: Starting Keepalived v1.2.15 (02/07,2015) Feb 7 00:40:42 slave-d Keepalived[6005]: Starting Healthcheck child process, pid=6007 Feb 7 00:40:42 slave-d Keepalived[6005]: Starting VRRP child process, pid=6008 Feb 7 00:40:42 slave-d Keepalived_vrrp[6008]: Netlink reflector reports IP 192.168.36.86 added Feb 7 00:40:42 slave-d Keepalived_vrrp[6008]: Netlink reflector reports IP fe80::20c:29ff:fe6a:66ff added Feb 7 00:40:42 slave-d Keepalived_vrrp[6008]: Registering Kernel netlink reflector Feb 7 00:40:42 slave-d Keepalived_vrrp[6008]: Registering Kernel netlink command channel Feb 7 00:40:42 slave-d Keepalived_vrrp[6008]: Registering gratuitous ARP shared channel Feb 7 00:40:42 slave-d Keepalived_vrrp[6008]: Opening file '/etc/keepalived/keepalived.conf'. Feb 7 00:40:42 slave-d Keepalived_healthcheckers[6007]: Netlink reflector reports IP 192.168.36.86 added Feb 7 00:40:42 slave-d Keepalived_healthcheckers[6007]: Netlink reflector reports IP fe80::20c:29ff:fe6a:66ff added Feb 7 00:40:42 slave-d Keepalived_healthcheckers[6007]: Registering Kernel netlink reflector Feb 7 00:40:42 slave-d Keepalived_healthcheckers[6007]: Registering Kernel netlink command channel Feb 7 00:40:42 slave-d Keepalived_healthcheckers[6007]: Opening file '/etc/keepalived/keepalived.conf'. Feb 7 00:40:42 slave-d Keepalived_vrrp[6008]: Configuration is using : 44182 Bytes Feb 7 00:40:42 slave-d Keepalived_vrrp[6008]: Using LinkWatch kernel netlink reflector... Feb 7 00:40:42 slave-d Keepalived_vrrp[6008]: VRRP_Instance(VI_1) Entering BACKUP STATE Feb 7 00:40:42 slave-d Keepalived_vrrp[6008]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)] Feb 7 00:40:42 slave-d Keepalived_healthcheckers[6007]: Configuration is using : 7257 Bytes Feb 7 00:40:42 slave-d Keepalived_healthcheckers[6007]: Using LinkWatch kernel netlink reflector... Feb 7 00:40:42 slave-d Keepalived_vrrp[6008]: VRRP_Instance(VI_2) Transition to MASTER STATE Feb 7 00:40:42 slave-d Keepalived_vrrp[6008]: VRRP_Instance(VI_2) Received lower prio advert, forcing new election Feb 7 00:40:43 slave-d Keepalived_vrrp[6008]: VRRP_Instance(VI_2) Entering MASTER STATE Feb 7 00:40:43 slave-d Keepalived_vrrp[6008]: VRRP_Instance(VI_2) setting protocol VIPs. Feb 7 00:40:43 slave-d avahi-daemon[1823]: Registering new address record for 192.168.36.200 on eth0.IPv4. Feb 7 00:40:43 slave-d Keepalived_vrrp[6008]: VRRP_Instance(VI_2) Sending gratuitous ARPs on eth0 for 192.168.36.200 Feb 7 00:40:43 slave-d Keepalived_healthcheckers[6007]: Netlink reflector reports IP 192.168.36.200 added Feb 7 00:40:48 slave-d Keepalived_vrrp[6008]: VRRP_Instance(VI_2) Sending gratuitous ARPs on eth0 for 192.168.36.200192.168.36.99 로그:
Feb 7 00:40:47 slave-c Keepalived_vrrp[25103]: VRRP_Instance(VI_2) Received higher prio advert Feb 7 00:40:47 slave-c Keepalived_vrrp[25103]: VRRP_Instance(VI_2) Entering BACKUP STATE Feb 7 00:40:47 slave-c Keepalived_vrrp[25103]: VRRP_Instance(VI_2) removing protocol VIPs. Feb 7 00:40:47 slave-c Keepalived_healthcheckers[25102]: Netlink reflector reports IP 192.168.36.200 removed Feb 7 00:40:47 slave-c avahi-daemon[1832]: Withdrawing address record for 192.168.36.200 on eth0.닫으신 후에도 정상적으로 접속하실 수 있습니다.
nginx를 종료한 후 바로 nginx가 시작되는 것을 확인할 수 있습니다.
참고 기사:
CentOS6.5 keepalived Nginx 서비스의 고가용성에 대한 자세한 설명 및 구현
NginX+keepalived를 사용하여 고가용성 및 로드 밸런싱 달성
Keepalived+Nginx를 통해 고가용성 및 듀얼 마스터 노드 로드 밸런싱
nginx+keepalived는 nginx 듀얼 마스터 고가용성 로드 밸런싱을 실현합니다