[Redundancy] 06. HAProxy 구성 (1) - 기본 구성, SSL 설정
카테고리: REDUNDANCY
태그: redundancy linux
06-1. HAProxy 설정 파일(/etc/haproxy/haproxy.cfg)
06-2. HAProxy 설치 및 설정(CentOS 8 Stream 기준)
-----------+---------------------------+--------------------------+------------
| | |
|192.168.219.134 |192.168.219.135 |192.168.219.136
+----------+-----------+ +-----------+----------+ +-----------+----------+
| [ dlp.test.srv ] | | [ node01.test.srv ] | | [ node02.test.srv ] |
| HAProxy | | Web Server#1 | | Web Server#2 |
+----------------------+ +----------------------+ +----------------------+
■ HAProxy 설치
[root@dlp ~]# dnf -y install haproxy
■ HAProxy 설정
[root@dlp ~]# vi /etc/haproxy/haproxy.cfg
# 기존 [frontend ***] [backend ***] 섹션에 대해 모두 주석 처리하고 끝에 다음을 추가
frontend http-in
bind *:80
# 기본 백엔드 설정
default_backend backend_servers
# X-Forwarded-For 헤더 보내기
option forwardfor
# 백엔드 정의
backend backend_servers
# 부하분산 알고리즘 - 라운드로빈 설정
balance roundrobin
# 백엔드 서버 정의
server node01 192.168.219.135:80 check
server node02 192.168.219.136:80 check
■ HAProxy 시작 및 HAProxy가 수신하는 포트 허용
[root@dlp ~]# {
systemctl enable --now haproxy
firewall-cmd --add-service=http --permanent
firewall-cmd --reload
}
■ HAProxy 서버 - HAproxy의 기본 설정에 따라 로그는 [local2]으로 전송되므로 파일에 기록하도록 Rsyslog를 구성
[root@dlp ~]# vi /etc/rsyslog.conf
# 19,20행 : 주석을 제거하고 한 줄 추가
module(load="imudp") # needs to be done just once
input(type="imudp" port="514")
$AllowedSender UDP, 127.0.0.1
# 46행 : 다음과 같이 변경
*.info;mail.none;authpriv.none;cron.none;local2.none /var/log/messages
local2.* /var/log/haproxy.log
[root@dlp ~]# systemctl restart rsyslog
■ 내부 WEB 서버 - 백엔드 웹 서버(httpd)의 설정을 X-Forwarded-For 헤더 로깅으로 변경
“X-Forwarded-For(XFF)” 헤더는 HTTP 프록시나 로드 밸런서를 통해 웹 서버에 접속하는 클라이언트의 원 IP 주소를 식별하는 사실상의 표준 헤더이다. 클라이언트와 서버 중간에서 트래픽이 프록시나 로드 밸런서를 거치면, 서버 접근 로그에는 프록시나 로드 밸런서의 IP 주소만을 담고 있다. 클라이언트의 원 IP 주소를 보기위해 X-Forwarded-For 요청 헤더가 사용된다.
[root@node01 ~]# vi /etc/httpd/conf/httpd.conf
# 199행 : 다음과 같이 변경
LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
[root@node01 ~]# systemctl restart httpd
■ 결과 확인
06-3. HAProxy SSL/TLS 설정 - “SSL Termination”방식
시작하기 전 내용 참고 (도메인 등록 및 SSL 인증서 발급) : https://revenge1005.github.io/rocky/ch01-17/
■ HAProxy 서버 - SSL/TLS에 대한 설정
[root@dlp ~]# certbot certonly --standalone -d chsrv.duckdns.org
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for chsrv.duckdns.org
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/chsrv.duckdns.org/fullchain.pem
Key is saved at: /etc/letsencrypt/live/chsrv.duckdns.org/privkey.pem
This certificate expires on 2023-11-19.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
* Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
* Donating to EFF: https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[root@dlp ~]# cd /etc/letsencrypt/live/chsrv.duckdns.org/
[root@dlp chsrv.duckdns.org]# cat fullchain.pem privkey.pem > haproxy.pem
[root@dlp chsrv.duckdns.org]# cd
[root@dlp ~]# vi /etc/haproxy/haproxy.cfg
# frontend 섹션에 추가
frontend http-in
bind *:80
bind *:443 ssl crt /etc/letsencrypt/live/chsrv.duckdns.org/haproxy.pem
■ HAProxy 재시작 및 HTTPS 포트 허용
{
systemctl restart haproxy
firewall-cmd --add-service=https --permanent
firewall-cmd --reload
}
■ 결과 확인
06-4. HAProxy SSL/TLS 설정 - “SSL Passthrough”방식
■ 내부 WEB 서버 - SSL/TLS에 대한 설정
# 내부 서버들 중 하나 선택해 인증서 발급
[root@node01 ~]# certbot certonly --standalone -d chsrv2.duckdns.org
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for chsrv2.duckdns.org
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/chsrv2.duckdns.org/fullchain.pem
Key is saved at: /etc/letsencrypt/live/chsrv2.duckdns.org/privkey.pem
This certificate expires on 2023-11-19.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
* Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
* Donating to EFF: https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# 발급한 인증서를 다른 web 서버에 복사
[root@node01 ~]# {
scp -r /etc/letsencrypt/archarchive root@192.168.219.136:/etc/letsencrypt/
scp -r /etc/letsencrypt/live root@192.168.219.136:/etc/letsencrypt/
scp -r /etc/letsencrypt/renewal root@192.168.219.136:/etc/letsencrypt/
scp -r /etc/letsencrypt/renewal-hooks root@192.168.219.136:/etc/letsencrypt/
dnf -y install mod_ssl
}
[root@node01 ~]# vi /etc/httpd/conf.d/ssl.conf
SSLCertificateFile /etc/letsencrypt/live/chsrv2.duckdns.org/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/chsrv2.duckdns.org/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/chsrv2.duckdns.org/chain.pem
[root@node01 ~]# vi /etc/httpd/conf.d/vhost.conf
<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName chsrv2.duckdns.org
# 추가
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>
[root@node01 ~]# systemctl restart httpd
■ HAProxy 서버 설정
[root@dlp ~]# vi /etc/haproxy/haproxy.cfg
...
defaults
mode tcp # 인스턴스가 처리할 프로토콜
log global
option tcplog # tcp 로그 포맷 사용
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
frontend https_in # https connection만 허용
mode tcp # proxying의 기능 수행만을 위해 tcp 모드로 설정
bind *:443
option tcplog
default_backend backend_servers
backend backend_servers # backend에서 ssl termination을 수행하도록 함
mode tcp # proxying의 기능 수행만을 위해 tcp 모드로 설정
balance roundrobin
option ssl-hello-chk # ssl connection health check
server node01 192.168.219.135:443 check
server node02 192.168.219.136:443 check
■ 결과 확인
댓글 남기기