Obsidian/Recognition/Programing/Linux/RHEL9/NGINX HA (High Availability...

309 lines
6.1 KiB
Markdown
Raw Permalink Normal View History

2024-03-29 08:36:47 +00:00
```shell
# nginx 설치 확인
nginx
or
dnf install nginx -y
# keepalived 설치 확인
keepalived
or
dnf install keepalived -y
# service 사용, 상태 확인
systemctl enable nginx
systemctl start nginx
systemctl enable keepalived
systemctl start keepalived
# index.thml 수정 (테스트시 노드 구분 하기 위함)
nano /usr/share/nginx/html/index.html
# keepalived 기본 구성 파일을 편집
nano /etc/keepalived/keepalived.conf
--------------------------------------------------------------------------------------------
global_defs {
# Keepalived process identifier
router_id LVS_ELK
}
# Nginx가 실행 중인지 확인하는 스크립트
vrrp_script check_nginx {
script "/etc/keepalived/check_nginx.sh"
interval 2
weight 50
}
# Virtual interface - 우선 순위는 장애 조치 시 할당된 인터페이스가 인계받는 순서를 지정합니다.
vrrp_instance VI_01 {
state MASTER
interface bond0
virtual_router_id 151
priority 110
# 가상IP
virtual_ipaddress {
10.200.31.129/24
}
track_script {
check_nginx
}
authentication {
auth_type AH
auth_pass secret
}
}
--------------------------------------------------------------------------------------------
## MASTER를 BACKUP으로, 110을 100으로 변경
# 실행중인지 확인 스크립트 작성
nano /etc/keepalived/check_nginx.sh
#!/bin/sh
if [ -z "`pidof nginx`" ]; then
exit 1
fi
# 스크립트 권한 설정
chmod 755 /etc/keepalived/check_nginx.sh
systemctl enable keepalived
systemctl start keepalived
# 가상 ip 설정 상태 확인
ip add show
```
```shell
https://www.nakjunizm.com/2020/02/20/haproxy/
https://bryan.wiki/243
https://www.owl-dev.me/blog/74
haproxy
nginx
keepalived
-------------------------------------------------------------------------
https://bkim.tistory.com/12
# nginx 다운로드 (https://nginx.org/download)
# 의존성 라이브러리 다운로드
## PCRE (https://sourceforge.net/projects/pcre/files/pcre/)
## zlib (http://zlib.net) 최신 버전 확인 후 --> http://zlib.net/zlib-1.3.1.tar.gz
## OpenSSL (http://www.openssl.org/source)
# nginx 압축풀기
tar -zxvf nginx-1.20.2.tar.gz
cd nginx-ha/nginx-1.20.2
tar -zxvf pcre-8.39.tar.gz
tar -zxvf zlib-1.3.1.tar.gz
# 설정 파일 수정
nano conf/nginx.conf
## location / { ... 밑에 추가
location /nginx_status {
stub_status;
allow 127.0.0.1;
deny all;
}
# nginx 설치 2
./configure --prefix=/home/gmt/SACP-INSTALL/nginx --user=root --with-http_stub_status_module make && make install
./configure --prefix=/home/gmt/SACP-INSTALL/nginx-ha/nginx --with-zlib=./zlib-1.3.1 --with-pcre=./pcre-8.39 --with-http_stub_status_module
make install
## --with-http_stub_status_module: nginx stats page 적용을 위한 옵션
## ssl 적용 필요시 --with-openssl=./openssl-1.0.2 --with-http_ssl_module
# Service 파일 생성
nano /usr/lib/systemd/system/nginx.service
--------------------------------------------------------------------------------------------
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/home/gmt/SACP-INSTALL/nginx-ha/nginx-1.20.2/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
--------------------------------------------------------------------------------------------
# nginx.confg PID 위치 설정
cd /usr/local/nginx/conf
nano nginx.conf
--------------------------------------------------------------------------------------------
worker_processes 1;
pid /var/run/nginx.pid;
....
http{
...
}
--------------------------------------------------------------------------------------------
# deamon 리로드
systemctl daemon-reload
systemctl status nginx
# keepalived 설치
tar -zxvf keepalived-1.1.19.tar.gz
# Keepalived configureation
cd [keepalived Root]
nano ./keepalived.conf
# MASTER
vrrp_script check_nginx {
script "killall -0 nginx"
interval 2
}
vrrp_instance VI_1 {
interface eth0
state MASTER
priority 200
virtual_router_id 33
virtual_ipaddress {
10.205.199.200 # 가상 IP
}
authentication {
auth_type PASS
auth_pass password
}
track_script {
check_nginx
}
}
# BACKUP
script "killall -0 nginx"
interval 2
}
vrrp_instance VI_1 {
interface eth0
state BACKUP
priority 100
virtual_router_id 33
virtual_ipaddress {
10.205.199.200
}
authentication {
auth_type PASS
auth_pass password
}
track_script {
check_nginx
}
}
# keepalived 실행
```
```bash
# 설치
apt-get install nginx-ha-keepalived
## /usr/bin으로 설치됨
# 스크립트 실행
nginx-ha-setup
## /etc/keepalived/keepalived.conf가 생성됨
# keepalived 구성 스크립트 수정
# health check 기능 구성
vrrp_script chk_nginx_service {
script "/usr/libexec/keepalived/nginx-ha-check" # nginx-ha-check 스크립트 경로
interval 3 # 스크립트가 실행되는 빈도 (초 단위)
weight 50 # 가중치, priority와 함께 어떤 노드가 기본 노드가 될지 결정하는데 사용됨.
# 50 + 101 = 151.. 높을수록 우선순위 높음.
# NGINX Plus 관리자 가이드 문서에서 health check 스크립트 참조.
}
# 노드 VRRP 인스턴스를 구성
vrrp_instance VI_1 {
interface eth0
state BACKUP
priority 101
virtual_router_id 51 # 샘플값(필요에 따라 환경에서 고유하도록 변경)
advert_int 1 # 기본 노드의 VRRP인스턴스가 백업 노드 피어에 보내는 빈도(초)
unicast_src_ip 192.168.100.100 # 로컬 노드
unicast_peer {
192.168.100.101 # 다른 노드
}
authentication {
auth_type PASS
auth_pass f8f0e5114cbe031a3e1e622daf18f82a
}
virtual_ipaddress {
192.168.100.150 # 기본 노드에 할당된 가상 IP
}
track_script {
chk_nginx_service
}
notify "/usr/libexec/keepalived/nginx-ha-notify"
}
```