이 가이드는 Kubernetes Gateway API를 소개합니다. 이는 Kubernetes에서 인그레스(Ingress) 및 트래픽 라우팅을 관리하는 현대적이고 확장 가능한 접근 방식입니다. 이번 데모에서는 NGINX Gateway Controller를 사용하지만, 이 문서에서 설명하는 개념과 API는 Gateway API를 지원하는 모든 컨트롤러에서 동일하게 적용됩니다.
📚 공식 문서: https://gateway-api.sigs.k8s.io
Gateway API는 Custom Resource(사용자 정의 리소스)를 정의합니다. 하지만 이를 실제로 동작시키기 위해서는 컨트롤러가 필요합니다. 여기서는 NGINX Gateway Controller를 사용하며, 이 컨트롤러는 Gateway API의 모든 표준 리소스를 지원합니다.
다음 명령어를 실행하여 설치합니다.
kubectl kustomize "https://github.com/nginx/nginx-gateway-fabric/config/crd/gateway-api/standard?ref=v1.6.2" | kubectl apply -f -
kubectl kustomize "https://github.com/nginx/nginx-gateway-fabric/config/crd/gateway-api/experimental?ref=v1.6.2" | kubectl apply -f -
helm install ngf oci://ghcr.io/nginx/charts/nginx-gateway-fabric --create-namespace -n nginx-gateway
설명:
🔗 NGINX Gateway Fabric 참조
GatewayClass는 특정 컨트롤러가 구현하는 Gateway의 집합을 정의합니다. 쉽게 말해, 어떤 컨트롤러가 이 Gateway를 관리할지 Kubernetes에게 알려주는 설계도입니다.
apiVersion: gateway.networking.k8s.io/v1
kind: GatewayClass
metadata:
name: nginx
spec:
controllerName: nginx.org/gateway-controller
설명:
controllerName: 해당 GatewayClass를 관리하는 컨트롤러 이름입니다. 여기서는 nginx.org/gateway-controller로 NGINX를 사용합니다.🔗 GatewayClass 문서 참조
Gateway는 외부 트래픽이 클러스터로 진입하는 방식을 정의하는 Kubernetes 리소스입니다. 어떤 프로토콜, 어떤 포트, 어떤 규칙으로 트래픽을 처리할지 명시합니다.
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: nginx-gateway
namespace: default
spec:
gatewayClassName: nginx
listeners:
- name: http
protocol: HTTP
port: 80
allowedRoutes:
namespaces:
from: All
설명:
gatewayClassName: 사용할 GatewayClass 이름입니다.
listeners: 이 Gateway가 트래픽을 어떤 방식으로 수신할지를 정의합니다.
protocol: HTTP 프로토콜 사용port: 80번 포트에서 수신allowedRoutes: 모든 네임스페이스의 Route 리소스를 허용합니다.HTTPRoute는 HTTP 트래픽을 어떤 서비스로 전달할지를 정의합니다. Gateway와 연결되어 작동하며, 경로나 헤더 등의 조건에 따라 트래픽을 분배합니다.
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: basic-route
namespace: default
spec:
parentRefs:
- name: nginx-gateway
namespace: nginx-gateway
# 만약 다른 namespace에 참조하는 gateway가 존재한다면 명시 해줘야함
rules:
- matches:
- path:
type: PathPrefix
value: /app
backendRefs:
- name: my-app
port: 80
설명:
parentRefs: 어떤 Gateway와 연결할지 지정합니다.
rules: 트래픽 매칭 및 전달 규칙
matches: /app으로 시작하는 경로를 매칭backendRefs: 해당 트래픽을 my-app 서비스의 80번 포트로 전달🔗 HTTP Routing 가이드
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: https-redirect
namespace: default
spec:
parentRefs:
- name: nginx-gateway
rules:
- filters:
- type: RequestRedirect
requestRedirect:
scheme: https
설명:
🔗 HTTP Redirects 가이드
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: rewrite-path
namespace: default
spec:
parentRefs:
- name: nginx-gateway
rules:
- matches:
- path:
type: PathPrefix
value: /old
filters:
- type: URLRewrite
urlRewrite:
path:
replacePrefixMatch: /new
backendRefs:
- name: my-app
port: 80
설명:
/old로 시작하는 요청 경로를 /new로 변경한 뒤 my-app 서비스로 전달합니다.🔗 HTTP Rewrite 가이드
요청 또는 응답 헤더를 추가, 수정, 삭제할 수 있습니다.
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: header-mod
namespace: default
spec:
parentRefs:
- name: nginx-gateway
rules:
- filters:
- type: RequestHeaderModifier
requestHeaderModifier:
add:
x-env: staging
backendRefs:
- name: my-app
port: 80
설명:
x-env: staging 값을 추가합니다.🔗 HTTP Header 가이드
버전별 트래픽 분배나 카나리 배포에 활용됩니다.
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: traffic-split
namespace: default
spec:
parentRefs:
- name: nginx-gateway
rules:
- backendRefs:
- name: v1-service
port: 80
weight: 80
- name: v2-service
port: 80
weight: 20
설명:
v1-service, 20%는 v2-service로 전달됩니다.🔗 HTTP Traffic Splitting 가이드
요청을 주 서비스로 전달하면서 동시에 복사본을 다른 서비스로 보내 테스트나 모니터링 용도로 사용할 수 있습니다.
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: request-mirror
namespace: default
spec:
parentRefs:
- name: nginx-gateway
rules:
- filters:
- type: RequestMirror
requestMirror:
backendRef:
name: mirror-service
port: 80
backendRefs:
- name: my-app
port: 80
설명:
my-app으로 전달되며,mirror-service로 보내집니다.🔗 HTTP Traffic Request 가이드
TLS는 클라이언트와 서버 간의 트래픽을 암호화합니다. Gateway에서 TLS를 종료(terminate)하여 클러스터 내부로는 암호화되지 않은 트래픽으로 전달할 수 있습니다.
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: nginx-gateway-tls
namespace: default
spec:
gatewayClassName: nginx
listeners:
- name: https
protocol: HTTPS
port: 443
tls:
mode: Terminate
certificateRefs:
- kind: Secret
name: tls-secret
allowedRoutes:
namespaces:
from: All
설명:
tls-secret이라는 Kubernetes Secret에서 인증서와 키를 참조🔗 TLS Configuration 가이드
Gateway API는 HTTP뿐만 아니라 TCP, UDP, gRPC 등의 다양한 프로토콜을 지원합니다.
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: tcp-gateway
namespace: default
spec:
gatewayClassName: nginx
listeners:
- name: tcp
protocol: TCP
port: 3306
allowedRoutes:
namespaces:
from: All
설명:
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: udp-gateway
namespace: default
spec:
gatewayClassName: nginx
listeners:
- name: udp
protocol: UDP
port: 53
allowedRoutes:
namespaces:
from: All
설명:
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: grpc-route
namespace: default
spec:
parentRefs:
- name: nginx-gateway
rules:
- matches:
- method:
service: my.grpc.Service
method: GetData
backendRefs:
- name: grpc-service
port: 50051
설명:
my.grpc.Service의 GetData 메서드를 호출하는 gRPC 요청을 grpc-service의 50051 포트로 전달합니다.Gateway API는 구조적이고 선언적인 방식으로 라우팅을 구성할 수 있게 해줍니다. HTTP 헤더 수정, 트래픽 분배, TLS, TCP, UDP, gRPC 등 다양한 프로토콜과 기능을 지원합니다. HTTP 기반의 기본적인 설정에서 시작해 TLS와 TCP 같은 고급 기능으로 확장할 수 있으며, Kubernetes 클러스터의 인그레스 전략을 더욱 안전하고 확장 가능하게 만들어 줍니다.