CentOS 8에서 KeepAlived로 고가용성 NGINX를 설정하는 방법
이 페이지에서
- 전제 조건
- 두 노드 모두에 Nginx 설치
- 두 노드 모두에 Index.html 파일 생성
- Keepalived 설치 및 구성
- 두 노드 모두에서 방화벽 구성\n
- Keepalived 확인\n
- 결론
Nginx는 무료 오픈 소스이며 전 세계에서 가장 인기 있는 웹 서버 중 하나입니다. 리버스 프록시, 로드 밸런서 및 HTTP 캐시로도 사용할 수 있습니다. 고가용성은 장애 발생 시 응용 프로그램이 작업을 다른 시스템으로 다시 라우팅할 수 있도록 합니다. 고가용성 시스템을 설정하는 데 사용할 수 있는 다양한 기술이 있습니다.
Keepalived는 서비스 또는 시스템을 지속적으로 모니터링하고 장애 발생 시 고가용성을 달성하는 시스템 데몬입니다. 한 노드가 다운되면 두 번째 노드가 리소스를 제공합니다.
이 튜토리얼에서는 CentOS 8에서 KeepAlived를 사용하여 고가용성 Nginx 웹 서버를 설정하는 방법을 보여줍니다.
전제 조건
- CentOS 8을 실행하는 두 대의 서버 하나는 마스터 노드용이고 다른 하나는 백업 노드용입니다.\n
- 서버에 루트 암호가 구성되어 있습니다.\n
두 노드 모두에 Nginx 설치
먼저 두 노드 모두에 Nginx 패키지를 설치해야 합니다. 다음 명령을 사용하여 설치할 수 있습니다.
dnf install nginx -y
Nginx가 두 노드에 모두 설치되면 Nginx 서비스를 시작하고 시스템 재부팅 시 시작되도록 활성화합니다.
systemctl start nginx
systemctl enable nginx
완료되면 다음 단계로 진행할 수 있습니다.
두 노드 모두에 Index.html 파일 만들기
다음으로 각 노드를 식별하기 위해 두 노드 모두에 사용자 지정 index.html 파일을 생성해야 합니다.
첫 번째 노드에서 다음 명령을 사용하여 index.html 파일을 만듭니다.
echo "<h1>This is My First NGINX Web Server Node</h1>" | tee /usr/share/nginx/html/index.html
두 번째 노드에서 다음 명령을 사용하여 index.html 파일을 만듭니다.
echo "<h1>This is My Second NGINX Web Server Node</h1>" | tee /usr/share/nginx/html/index.html
완료되면 파일을 저장하고 닫습니다.
Keepalived 설치 및 구성
다음으로 두 노드 모두에 Keepalived를 설치해야 합니다. 기본적으로 Keepalived 패키지는 CentOS 8 기본 리포지토리에서 사용할 수 있습니다. 다음 명령을 실행하여 설치할 수 있습니다.
dnf install keepalived -y
keepalived 패키지가 두 노드에 모두 설치되면 두 노드에서 keepalived 기본 구성 파일을 편집해야 합니다.
첫 번째 노드에서 keepalived.conf 파일을 편집합니다.
nano /etc/keepalived/keepalived.conf
기본 콘텐츠를 제거하고 다음 콘텐츠를 추가합니다.
global_defs { # Keepalived process identifier router_id nginx } # Script to check whether Nginx is running or not vrrp_script check_nginx { script "/bin/check_nginx.sh" interval 2 weight 50 } # Virtual interface - The priority specifies the order in which the assigned interface to take over in a failover vrrp_instance VI_01 { state MASTER interface eth0 virtual_router_id 151 priority 110 # The virtual ip address shared between the two NGINX Web Server which will float virtual_ipaddress { 192.168.1.10/24 } track_script { check_nginx } authentication { auth_type AH auth_pass secret } }
완료되면 파일을 저장하고 닫습니다.
두 번째 노드에서 keepalived.conf 파일을 편집합니다.
nano /etc/keepalived/keepalived.conf
기본 콘텐츠를 제거하고 다음 콘텐츠를 추가합니다.
global_defs { # Keepalived process identifier router_id nginx } # Script to check whether Nginx is running or not vrrp_script check_nginx { script "/bin/check_nginx.sh" interval 2 weight 50 } # Virtual interface - The priority specifies the order in which the assigned interface to take over in a failover vrrp_instance VI_01 { state BACKUP interface eth0 virtual_router_id 151 priority 100 # The virtual ip address shared between the two NGINX Web Server which will float virtual_ipaddress { 192.168.1.10/24 } track_script { check_nginx } authentication { auth_type AH auth_pass secret } }
파일을 저장하고 닫은 다음 Nginx 서비스가 실행 중인지 여부를 확인하는 스크립트를 만들어야 합니다. 다음 명령을 사용하여 만들 수 있습니다.
참고: 위의 구성 파일에서 MASTER를 BACKUP으로, 110을 100으로 바꾸었습니다.
nano /bin/check_nginx.sh
다음 줄을 추가합니다.
#!/bin/sh if [ -z "`pidof nginx`" ]; then exit 1 fi
파일을 저장하고 닫은 후 다음 명령을 사용하여 적절한 권한을 설정하십시오.
chmod 755 /bin/check_nginx.sh
마지막으로 keepalived 서비스를 시작하고 다음 명령을 사용하여 시스템 재부팅 시 시작되도록 활성화합니다.
systemctl start keepalived
systemctl enable keepalived
다음 명령을 사용하여 keepalived 서비스의 상태를 확인할 수도 있습니다.
systemctl status keepalived
다음과 같은 결과가 표시되어야 합니다.
? keepalived.service - LVS and VRRP High Availability Monitor Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled) Active: active (running) since Thu 2021-04-08 04:24:22 EDT; 5s ago Process: 3141 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS) Main PID: 3142 (keepalived) Tasks: 2 (limit: 12524) Memory: 2.1M CGroup: /system.slice/keepalived.service ??3142 /usr/sbin/keepalived -D ??3143 /usr/sbin/keepalived -D Apr 08 04:24:22 node1 Keepalived_vrrp[3143]: (VI_01) Changing effective priority from 110 to 160 Apr 08 04:24:25 node1 Keepalived_vrrp[3143]: (VI_01) Receive advertisement timeout Apr 08 04:24:25 node1 Keepalived_vrrp[3143]: (VI_01) Entering MASTER STATE Apr 08 04:24:25 node1 Keepalived_vrrp[3143]: (VI_01) setting VIPs. Apr 08 04:24:25 node1 Keepalived_vrrp[3143]: Sending gratuitous ARP on eth0 for 192.168.1.10 Apr 08 04:24:25 node1 Keepalived_vrrp[3143]: (VI_01) Sending/queueing gratuitous ARPs on eth0 for 192.168.1.10 Apr 08 04:24:25 node1 Keepalived_vrrp[3143]: Sending gratuitous ARP on eth0 for 192.168.1.10 Apr 08 04:24:25 node1 Keepalived_vrrp[3143]: Sending gratuitous ARP on eth0 for 192.168.1.10 Apr 08 04:24:25 node1 Keepalived_vrrp[3143]: Sending gratuitous ARP on eth0 for 192.168.1.10 Apr 08 04:24:25 node1 Keepalived_vrrp[3143]: Sending gratuitous ARP on eth0 for 192.168.1.10
다음 명령을 사용하여 마스터 노드에서 가상 IP 주소 상태를 확인할 수도 있습니다.
ip add show
다음 출력에 가상 IP 주소 192.168.1.10이 표시되어야 합니다.
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 00:00:2d:3a:20:9b brd ff:ff:ff:ff:ff:ff inet 45.58.32.155/24 brd 45.58.32.255 scope global noprefixroute eth0 valid_lft forever preferred_lft forever inet 192.168.1.10/24 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::200:2dff:fe3a:209b/64 scope link valid_lft forever preferred_lft forever 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 00:00:0a:3a:20:9b brd ff:ff:ff:ff:ff:ff inet6 fe80::200:aff:fe3a:209b/64 scope link valid_lft forever preferred_lft forever
완료되면 다음 단계로 진행할 수 있습니다.
두 노드 모두에서 방화벽 구성
다음으로 포트 80을 허용하고 두 노드 모두에서 VRRP를 허용해야 합니다. 다음 명령을 사용하여 수행할 수 있습니다.
firewall-cmd --permanent --add-service=http
firewall-cmd --add-rich-rule='rule protocol value="vrrp" accept' --permanent
다음으로 방화벽을 다시 로드하여 변경 사항을 적용합니다.
firewall-cmd –reload
Keepalived 확인
이 시점에서 Nginx 및 Keepalived가 설치되고 구성됩니다. Nginx 고가용성이 작동하는지 여부를 테스트할 시간입니다.
웹 브라우저를 열고 URL http://your-virtual-ip에 액세스합니다. 다음 페이지가 표시됩니다.
이제 마스터 노드에서 Nginx 서비스를 중지하고 가상 IP가 노드 1에서 노드 2로 전환되는지 테스트합니다.
마스터 노드에서 다음 명령을 사용하여 Nginx 서비스를 중지합니다.
systemctl stop nginx
그런 다음 Node2에 로그인하고 다음 명령을 사용하여 가상 IP를 확인합니다.
ip add show
다음 출력에 가상 IP가 표시되어야 합니다.
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 00:00:2d:3a:26:37 brd ff:ff:ff:ff:ff:ff inet 45.58.38.55/24 brd 45.58.38.255 scope global noprefixroute eth0 valid_lft forever preferred_lft forever inet 192.168.1.10/24 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::200:2dff:fe3a:2637/64 scope link valid_lft forever preferred_lft forever 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 00:00:0a:3a:26:37 brd ff:ff:ff:ff:ff:ff inet6 fe80::200:aff:fe3a:2637/64 scope link valid_lft forever preferred_lft forever
이제 URL http://your-virtual-ip를 사용하여 Nginx 웹 서버에 액세스합니다. Node2 페이지가 표시되어야 합니다.
결론
축하합니다! Keepalived로 고가용성 Nginx 서버를 성공적으로 설정했습니다. 프로덕션 환경에서 고가용성 Nginx 서버를 설정할 수 있는 충분한 지식을 갖추셨기를 바랍니다.