Kubernetes

HAProxy 설치하기

[혜안] 2024. 3. 20. 11:55
728x90

이전 포스팅에서 우분투 위에 KVM을 설치하고, 그 위에 VM들을 올려서 Kubernetes 클러스터까지 구축을 했습니다.

그리고 이 클러스터에 뭔가 서비스를 올려서 서비스를 하려면, Host 레벨에서 접근이 가능하도록 Path를 열어주어야 합니다.

클러스터가 Host OS 안에 NAT로 구성되어 있기 때문인데요.

가장 쉬운 해결책으로 HAProxy 설치를 해보려고 합니다.

아래의 구성입니다.

그림을 보면 약간 이상한 부분을 눈치챘을껍니다.

Host OS에 80으로 들어오는 요청과 443으로 들어오는 요청이 모두 Guest의 30080 포트로 전달되도록 되어 있습니다.

엄밀히 말하자면 80으로 들어오는 요청을 443으로 redirect하고, 443으로 redirection된 요청이 Guest OS의 30080으로 전달되는 구조입니다.

443은 예상하셨겠지만 TLS(https)통신입니다.

결국 보안연결을 위해, 외부에서 평문으로 들어오는 요청을 안전하게 https 로 전환한 후, 

https로 다시 들어오는 요청을 백앤드에 평문으로 풀어서 보내는 구성입니다.

일단 https 통신을 위해서는 인증서가 필요합니다.

공인인증서가 없다면 아래와 같이 간단하게 사설인증서로 대체할 수 있습니다.

Self-signed 인증서 생성

openssl genrsa -out mycert.key 2048
openssl req -new -key mycert.key -out mycert.csr -subj "/O=myhome/CN=*.viewise.pe.kr"

openssl x509 -req -sha256 -days 3650 -in mycert.csr -key mycert.key -out mycert.crt -extensions SAN \
 -extfile <(printf "\n[SAN]\nsubjectAltName=DNS:viewise.pe.kr,DNS:harbor.viewise.pe.kr,DNS:gitlab.viewise.pe.kr\nextendedKeyUsage=serverAuth")

cat mycert.key mycert.crt >> mycert.pem

openssl x509 -in mycert.pem -text -noout

그리고 HAProxy를 Host OS #01에 설치합니다.

HAProxy 설치

sudo su

apt update
apt install haproxy -y

 

설정파일을 수정합니다.

HAProxy 설정

 

 

vi /etc/haproxy/haproxy.cfg
...
frontend http_proxy
        mode http
        bind :80
        bind :443 ssl crt /etc/haproxy/mycert.pem
        http-request redirect scheme https unless { ssl_fc }
        http-request add-header X-Forwarded-Proto https if { ssl_fc }
        http-request add-header X-Forwarded-Port %[dst_port]        
        option httpclose
        default_backend ingress

backend ingress
        balance source
        server node01 192.168.100.201:30080 check
        server node02 192.168.100.202:30080 check
        server node03 192.168.100.203:30080 check
        server node04 192.168.100.209:30080 check
        server node05 192.168.100.210:30080 check
        server node06 192.168.100.211:30080 check
        
systemctl restart haproxy

주요 설정내용은 아래와같습니다.

http-request redirect scheme https unless { ssl_fc }
→ 80으로 들어오는 요청을 443으로 redirection하는 설정입니다.

http-request add-header X-Forwarded-Proto https if { ssl_fc }
클라이언트의 프로토콜 정보를 백앤드로 포워딩하는 설정입니다.

http-request add-header X-Forwarded-Port %[dst_port]  
포트 정보를 백앤드로 포워딩하는 설정입니다.

Kubernetes 클러스터에 올라가는 서비스에 따라 다르겠지만, 인증이 중요한 서비스의 경우 Source IP가 달라지거나 프로토콜이 변조되었다고 판단되면 인증이 거부되어 서비스를 사용할 수 없게 될 수 있습니다.

때문에 위와같이 클라이언트 정보를 백앤드로 포워딩해 줄 필요가 있습니다.

http 프로토콜이 아닌 다른 종류의 서비스를 올리고자 한다면 mode를 tcp로 설정하여 HAProxy의 역할을 최소화 시킬 수도 있습니다.

관련해서는 HAProxy의 공식문서를 참조하시는걸 추천드립니다.

https://www.haproxy.com/documentation/

 

HAProxy Technologies | World's Fastest Load Balancer

HAProxy products and services deliver websites and applications with the utmost performance, observability, and security at any scale and in any environment.

www.haproxy.com

 

 

728x90