NGINX Gateway Fabric: 애플리케이션 라우팅

NGINX Gateway Fabric 은 NGINX를 데이터 플레인으로 사용하여 Gateway API를 구현하는 오픈 소스 프로젝트입니다. 이 프로젝트의 목표는 Kubernetes에서 실행되는 애플리케이션에 대한 HTTP 또는 TCP/UDP 로드 밸런서, 리버스 프록시 또는 API Gateway를 구성하기 위해 Gateway, GatewayClass, HTTPRoute, GRPCRoute, TCPRoute, TLSRoute 및 UDPRoute와 같은 핵심 Gateway API를 구현하는 것입니다.

이 포스트에서는 NGINX Gateway Fabric을 사용하여 애플리케이션으로 라우팅하는 방법에 대해 설명합니다.

NGINX Gateway Fabric을 설치하는 방법은 아래 포스트를 확인하세요:

목차

1. NGINX Gateway Fabric
2. NGINX Gateway Fabric 애플리케이션 배포
3. Gateway 리소스 배포
4. HTTPRoute 리소스 배포
5. 애플리케이션 라우팅 확인
6. 결론

1. NGINX Gateway Fabric

NGINX Gateway Fabric을 사용하여 외부 트래픽을 Kubernetes 애플리케이션으로 라우팅하는 방법을 알아봅니다.

Gateway API와 NGINX Gateway Fabric을 사용하여 Kubernetes 애플리케이션으로 트래픽을 라우팅할 수 있습니다. 웹 애플리케이션이나 REST backend API를 NGINX Gateway Fabric을 사용하여 클러스너 외부에 애플리케이션을 노출할 수 있습니다.

2. NGINX Gateway Fabric 애플리케이션 배포

이 가이드에서 사용할 애플리케이션은 하나의 서비스와 두 개의 pod로 구성된 간단한 커피 애플리케이션입니다.

아래는 라우팅에 사용될 애플리케이션 deployment 및 service 구성입니다.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: coffee
  namespace: cafe
spec:
  replicas: 2
  selector:
    matchLabels:
      app: coffee
  template:
    metadata:
      labels:
        app: coffee
    spec:
      containers:
      - name: coffee
        image: nginxdemos/nginx-hello:plain-text
        ports:
        - containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: coffee
  namespace: cafe
spec:
  ports:
  - port: 80
    targetPort: 8080
    protocol: TCP
    name: http
  selector:
    app: coffee

위 구성을 배포하고 확인합니다.

kubectl get pods,svc -n cafe

3. Gateway 리소스 배포

2 섹션에서 배포한 pod로 라우팅하기 위해 Gateway 리소스를 생성합니다.

apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: cafe-gw
  namespace: cafe
spec:
  gatewayClassName: nginx
  listeners:
  - name: http
    port: 80
    protocol: HTTP

이 Gateway 구성은 gatewayClassName 필드를 통해 NGINX Gateway Fabric과 연결됩니다. coffee 애플리케이션은 80 포트에서 HTTP 요청을 수락하므로 listeners 를 설정합니다.

기본적으로 Gateway는 동일한 namespace에 있는 경우에만 라우트(예: HTTPRoutes)가 연결되도록 허용합니다. 다른 namespace로 라우트를 연결하려면 allowedRoutes 필드를 사용할 수 있습니다.

4. HTTPRoute 리소스 배포

HTTPRoute는 HTTP 요청을 라우팅하는 방법을 제공합니다.

apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: cafe
  namespace: cafe
spec:
  parentRefs:
  - name: cafe-gw
  hostnames:
  - cafe.example.com
  rules:
  - matches:
    - path:
        type: PathPrefix
        value: /
    backendRefs:
    - name: coffee
      port: 80

HTTPRoute를 cafe Gateway에 연결하기 위해 parentRefs 필드에 gateway 이름을 지정합니다. backendRefs 필드를 사용하여 연결할 서비스를 지정합니다.

5. 애플리케이션 라우팅 확인

배포된 상황은 아래와 같습니다.

# kubectl get pods,svc -n nginx-gateway

NAME                                 READY   STATUS    RESTARTS   AGE
pod/nginx-gateway-79875d4d9f-4w2kz   2/2     Running   0          22h

NAME                    TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)                      AGE
service/nginx-gateway   NodePort   10.98.171.13   <none>        80:30205/TCP,443:32372/TCP   24h


# kubectl get pods,svc,gateway,httproute -n cafe
NAME                          READY   STATUS    RESTARTS   AGE
pod/coffee-68b959b546-kxqm6   1/1     Running   0          22h
pod/coffee-68b959b546-t2wtb   1/1     Running   0          22h

NAME             TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
service/coffee   ClusterIP   10.108.230.29   <none>        80/TCP    23h

NAME                                     CLASS   ADDRESS   PROGRAMMED   AGE
gateway.gateway.networking.k8s.io/cafe-gw   nginx             True         22h

NAME                                       HOSTNAMES              AGE
httproute.gateway.networking.k8s.io/cafe   ["cafe.example.com"]   20h

curl 명령을 사용하여 확인합니다.

# curl cafe.example.com:30205

Server address: 10.244.1.152:8080
Server name: coffee-68b959b546-t2wtb
Date: 09/Jan/2025:05:49:35 +0000
URI: /
Request ID: 6a5f9349541ae4f8051955ec0bb1b5b5

# curl cafe.example.com:30205

Server address: 10.244.2.15:8080
Server name: coffee-68b959b546-kxqm6
Date: 09/Jan/2025:05:49:37 +0000
URI: /
Request ID: f108127742bffa337c34e991642f2020

6. 결론

NGINX Gateway Fabric을 사용하여 복잡한 트래픽 관리와 다양한 서비스 간의 원활한 통신을 할 수 있습니다.

또한, NGINX Gateway Fabric을 사용하여 경로, 메소드, 헤더, 쿼리 매개변수와 같은 요청 조건을 사용하여 여러 애플리케이션에 라우팅할 수 있습니다.

NGINX Gateway Fabric의 상업용 버전을 사용해 보시려면 30일 무료 평가판을 신청하거나 NGINX STORE에 연락하여 논의하십시오.

NGINX STORE를 통한 솔루션 도입 및 기술지원 무료 상담 신청

* indicates required