NGINX로 로드 밸런싱
NGINX로 로드 밸런싱 (Load balancing with NGINX)
Nomad의 [template 스탠자][template-stanza]를 사용해 [NGINX]를 로드 밸런서로 구성하고 서비스와 함께 확장되도록 동적으로 갱신하는 방법을 알아봐요.
출처: 문서
본문
Nomad의 [template 스탠자][template-stanza]를 사용해 [NGINX]를 로드 밸런서로 구성할 수 있어요. 이렇게 하면 서비스와 함께 확장되도록 로드 밸런서 구성을 동적으로 갱신할 수 있어요.
이 시나리오에서 NGINX의 주요 사용 사례는 인터넷에서 들어오는 HTTP(S) 및 TCP 요청을 처리할 수 있는 프런트엔드 서비스로 분산하는 것이에요.
다음 단계에 따라 NGINX를 로드 밸런서로 사용해요.
- 애플리케이션을 배포해요.
- NGINX를 구성하고 배포해요.
이 가이드는 데모 웹 애플리케이션의 로드 밸런서로 NGINX를 배포하는 방법을 보여줘요.
사전 요구 사항
Nomad 설치가 Consul을 사용하도록 구성되어야 해요. 자세한 내용은 Consul 통합 콘텐츠를 참고해요.
데모 웹 애플리케이션 배포
데모 웹 애플리케이션에 대한 작업 명세를 만들고 파일 이름을 webapp.nomad.hcl로 지어요. 이 작업 명세는 NGINX 구성에서 대상으로 삼을 데모 웹 애플리케이션의 인스턴스 세 개를 만들어요.
webapp.nomad.hcl
job "demo-webapp" {
datacenters = ["dc1"]
group "demo" {
count = 3
network {
port "http" {
to = -1
}
}
service {
name = "demo-webapp"
port = "http"
check {
type = "http"
path = "/"
interval = "2s"
timeout = "2s"
}
}
task "server" {
env {
PORT = "${NOMAD_PORT_http}"
NODE_IP = "${NOMAD_IP_http}"
}
driver = "docker"
config {
image = "hashicorp/demo-webapp-lb-guide"
ports = ["http"]
}
}
}
}
nomad run 명령으로 웹 애플리케이션 작업을 배포해요.
$ nomad run webapp.nomad.hcl
==> Monitoring evaluation "ea1e8528"
Evaluation triggered by job "demo-webapp"
Allocation "9b4bac9f" created: node "e4637e03", group "demo"
Allocation "c386de2d" created: node "983a64df", group "demo"
Allocation "082653f0" created: node "f5fdf017", group "demo"
Evaluation status changed: "pending" -> "complete"
==> Evaluation "ea1e8528" finished with status "complete"
NGINX 작업 만들고 실행하기
NGINX에 대한 작업을 만들고 이름을 nginx.nomad.hcl로 지어요. 이 NGINX 인스턴스는 배포된 웹 애플리케이션 인스턴스 전반에 요청을 분산해요.
이 작업 명세는 NGINX에 8080의 정적 포트를 사용해요. 이 덕분에 클러스터 안 어디에서든 nginx.service.consul:8080을 조회해 데모 웹 애플리케이션에 도달할 수 있어요.
template 스탠자는 Consul Template을 사용해 NGINX를 구성해요. 템플릿은 데모 웹 애플리케이션의 작업 명세에 구성된 서비스 이름인 demo-webapp이라는 서비스의 주소와 포트를 Consul에서 동적으로 조회해요. 구문 세부 사항은 Consul Template Go 언어 참조를 참고해요. 서비스 엔드포인트 중 하나의 상태가 변경되면 Consul은 NGINX에 즉시 알려요. 그러면 NGINX는 정상 서비스 인스턴스만 포함하는 새 로드 밸런서 구성 파일을 다시 렌더링해요.
nginx.nomad.hcl
job "nginx" {
datacenters = ["dc1"]
group "nginx" {
count = 1
network {
port "http" {
static = 8080
}
}
service {
name = "nginx"
port = "http"
}
task "nginx" {
driver = "docker"
config {
image = "nginx"
ports = ["http"]
volumes = [
"local:/etc/nginx/conf.d",
]
}
template {
data = <<EOF
upstream backend {
{{ range service "demo-webapp" }}
server {{ .Address }}:{{ .Port }};
{{ else }}server 127.0.0.1:65535; # force a 502
{{ end }}
}
server {
listen 8080;
location / {
proxy_pass http://backend;
}
}
EOF
destination = "local/load-balancer.conf"
change_mode = "signal"
change_signal = "SIGHUP"
}
}
}
}
작업 명세에 NGINX 구성용 인라인 템플릿이 포함되어 있지만, task.artifact 스탠자와 함께 task.template 스탠자를 사용해 S3 버킷 같은 원격 소스에서 입력 템플릿을 다운로드할 수도 있어요. 자세한 내용은 template 예시를 참고해요.
nomad run 명령으로 NGINX 작업을 배포해요.
$ nomad run nginx.nomad.hcl
==> Monitoring evaluation "45da5a89"
Evaluation triggered by job "nginx"
Allocation "c7f8af51" created: node "983a64df", group "nginx"
Evaluation status changed: "pending" -> "complete"
==> Evaluation "45da5a89" finished with status "complete"
로드 밸런서 구성 확인
다음 단계에 따라 NGINX 구성을 확인해요.
- nomad status nginx 명령을 실행해 NGINX 배포의 할당 ID를 가져와요.
$ nomad status nginx
ID = nginx
Name = nginx
...
Summary
Task Group Queued Starting Running Failed Complete Lost
nginx 0 0 1 0 0 0
Allocations
ID Node ID Task Group Version Desired Status Created Modified
76692834 f5fdf017 nginx 0 run running 17m40s ago 17m25s ago
- nomad alloc fs 명령을 실행해 구성을 읽어요.
$ nomad alloc fs 766 nginx/local/load-balancer.conf
upstream backend {
server 172.31.48.118:21354;
server 172.31.52.52:25958;
server 172.31.52.7:29728;
}
server {
listen 80;
location / {
proxy_pass http://backend;
}
}
로드 밸런서에 요청하기
NGINX 로드 밸런서에 접근하면 다음 예시와 유사한 응답을 받아야 해요. 클러스터 안의 노드에서 curl 명령을 실행해요.
$ curl nginx.service.consul:8080
Welcome! You are on node 172.31.48.118:21354
NGINX가 세 개의 Nomad 클라이언트에 걸쳐 있는 데모 웹 애플리케이션의 배포 인스턴스 중 하나로 요청을 전달한 것을 주목해요. 출력은 데모 웹 애플리케이션 호스트의 IP 주소를 보여줘요. 요청을 반복하면 어떤 백엔드 웹 서버 인스턴스가 요청을 받았는지에 따라 IP 주소가 바뀌어요.
Nomad 클라이언트 노드를 클라우드 제공자 로드 밸런서 뒤에 배치하기
Nomad 클라이언트 노드는 시간이 지나며 바뀔 수 있으므로 최종 사용자에게 서비스 접근을 위한 단일 엔드포인트를 제공하는 것이 중요해요. AWS Elastic Load Balancing (ELB), Azure Load Balancer, Google Cloud Load Balancing 같은 클라우드 제공자의 로드 밸런서 뒤에 Nomad 클라이언트 노드를 배치해 이를 수행할 수 있어요.
기본 단계는 로드 밸런서를 만들고, 클라이언트 노드를 등록하고, 리스너를 만들고, 건강 검사를 구성하는 것을 포함해요. 이 가이드를 따랐다면 NGINX에 대한 인그레스 경로를 만들어야 해요.
클라우드 제공자의 로드 밸런서를 구성한 후에는 포트 8080에서 클라우드 로드 밸런서 DNS 이름에 접근해 데모 웹 애플리케이션에 도달할 수 있어야 해요.