본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

GRPCRoute를 사용한 gRPC 트래픽 프록시

원문 보기 위키 갱신

GRPCRoute를 사용한 gRPC 트래픽 프록시 (Proxy gRPC traffic using GRPCRoute)

이 문서는 Kong Operator로 GRPCRoute 리소스를 활용해 gRPC 트래픽을 TLS 보안과 함께 프록시하는 방법을 알려줘요. 백엔드 서비스에 어노테이션을 추가하고 Gateway를 설정한 후 GRPCRoute를 생성하면 Kong Operator가 자동으로 Kong Gateway의 Service와 Route로 변환해 준답니다. 사전 준비로 Kong Operator와 gRPCurl이 필요하니 미리 설치해 주세요.

출처: 문서

본문

요약

Kong Operator로 gRPC 트래픽을 라우팅하려면 어떻게 하나요? 백엔드 Service에 konghq.com/protocol=grpcs 어노테이션을 추가하고, Gateway에 HTTPS 리스너를 추가한 다음 GRPCRoute 리소스를 생성하세요. Kong Operator가 GRPCRoute를 Kong Gateway Service 및 Route로 변환해요.

사전 요구 사항

Kong Operator 실행 중 (Enterprise 라이선스 포함)

  1. Kong Helm 차트를 추가하세요:

    helm repo add kong https://charts.konghq.com
    helm repo update
    
  2. Helm을 사용하여 Kong Operator를 설치하세요:

    helm upgrade --install kong-operator kong/kong-operator -n kong-system \
      --create-namespace \
      --set image.tag=2.3 \
    

    cert-manager가 admission 및 conversion webhook 인증서를 발급하고 순환하도록 하려면 cert-manager를 설치하고 다음 단계에서 설치 시 다음 인수를 전달하여 cert-manager 통합을 활성화하세요:

    --set global.webhooks.options.certManager.enabled=true
    

    이 기능을 활성화하지 않으면 차트가 자체 서명된 인증서를 자동으로 생성하고 주입해요. 이러한 인증서의 수명 주기를 관리하려면 cert-manager를 활성화하는 것을 권장해요. Kong Operator는 제어 플레인과 데이터 플레인 간의 mTLS 통신을 위한 인증서를 서명할 인증 기관이 필요해요. 이 작업은 Helm 차트에 의해 자동으로 처리돼요. 사용자 정의 CA 인증서를 제공해야 하는 경우 Helm 차트의 values.yaml에 있는 certificateAuthority 섹션을 참조하여 자신의 CA 인증서를 생성하고 참조하는 방법을 알아보세요.

KongLicense를 적용하세요. 라이선스가 ./license.json에 있다고 가정해요.

echo "
apiVersion: configuration.konghq.com/v1alpha1
kind: KongLicense
metadata:
 name: kong-license
rawLicenseString: '$(cat ./license.json)'
" | kubectl apply -f -

gRPCurl 설치됨

gRPCurl을 설치하세요. gRPCurl은 gRPC 서버와 상호작용할 수 있는 CLI예요. 이 튜토리얼에서는 검증을 위해 gRPCurl이 필요해요.

GRPCRoute는 gRPC 트래픽을 라우팅하기 위한 Kubernetes Gateway API 리소스예요. 이 가이드에서는 Kong Operator를 구성하여 TLS로 보호된 gRPC 트래픽을 백엔드 Service로 프록시하는 방법을 보여줘요.

kong 네임스페이스 생성

Kubernetes 클러스터에 kong 네임스페이스를 생성하세요. 데모가 실행될 곳이에요:

kubectl create namespace kong

{: data-test-step="block" }

Gateway 구성

HTTP 리스너가 있는 GatewayConfiguration, GatewayClass 및 Gateway를 생성하세요:

echo '
apiVersion: gateway-operator.konghq.com/v2beta1
kind: GatewayConfiguration
metadata:
  name: kong-gateway-configuration
  namespace: kong
spec:
  dataPlaneOptions:
    deployment:
      podTemplateSpec:
        spec:
          containers:
            - image: kong/kong-gateway:3.16
              name: proxy
---
apiVersion: gateway.networking.k8s.io/v1
kind: GatewayClass
metadata:
  name: kong-grpc
spec:
  controllerName: konghq.com/gateway-operator
  parametersRef:
    group: gateway-operator.konghq.com
    kind: GatewayConfiguration
    name: kong-gateway-configuration
    namespace: kong
---
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: kong-grpc-gateway
  namespace: kong
spec:
  gatewayClassName: kong-grpc
  listeners:
    - name: http
      port: 80
      protocol: HTTP' | kubectl apply -f -

gRPC 백엔드 배포

간단한 gRPC 테스트 API를 구현하는 grpcbin Service를 설치하세요:

kubectl apply -f https://developer.konghq.com/manifests/kic/grpcbin-service.yaml -n kong

Kubernetes Service에 어노테이션 추가

Kong Gateway는 기본적으로 Service가 HTTP 또는 HTTPS라고 가정해요. konghq.com/protocol 어노테이션을 추가하여 Kong Operator가 백엔드 Service를 대신 gRPC over TLS로 구성하도록 하세요:

kubectl annotate service -n kong grpcbin 'konghq.com/protocol=grpcs'

TLS 인증서 생성

  1. example.com 호스트 이름에 대한 테스트 인증서를 생성하세요. 이 인증서는 TLS 트래픽을 보호하는 데 사용돼요.

    OpenSSL 1.1.1+

    openssl req -subj '/CN=example.com' -new -newkey rsa:2048 -sha256 \
      -days 365 -nodes -x509 -keyout server.key -out server.crt \
      -addext "subjectAltName = DNS:example.com" \
      -addext "keyUsage = digitalSignature" \
      -addext "extendedKeyUsage = serverAuth" 2> /dev/null;
      openssl x509 -in server.crt -subject -noout
    

    {: data-test-step="block"}

    OpenSSL 0.9.8

    openssl req -subj '/CN=example.com' -new -newkey rsa:2048 -sha256 \
      -days 365 -nodes -x509 -keyout server.key -out server.crt \
      -extensions EXT -config <( \
       printf "[dn]\nCN=example.com\n[req]\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:example.com\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth") 2>/dev/null;
      openssl x509 -in server.crt -subject -noout
    

    이전 OpenSSL 버전(macOS Monterey에 포함된 버전 포함)에서는 이 명령의 대체 버전을 사용해야 해요.

  2. 인증서가 포함된 Secret을 생성하세요:

    kubectl create secret -n kong tls example.com --cert=./server.crt --key=./server.key
    

    {: data-test-step="block"}

  3. Kong Operator는 konghq.com/secret="true" 레이블이 있는 Secret만 감시해요. 방금 생성한 Secret을 Kong Operator가 인식하도록 레이블을 추가하세요:

    kubectl label secret example.com -n kong konghq.com/secret="true"
    

gRPC 트래픽 라우팅

  1. gRPC 트래픽을 위한 추가 HTTPS 리스너와 함께 kong-grpc-gateway Gateway를 다시 적용하세요:

    경고: 이 Gateway를 적용하면 리스너 목록이 대체돼요. 유지하려는 모든 리스너를 포함하세요.

    echo 'apiVersion: gateway.networking.k8s.io/v1
    kind: Gateway
    metadata:
      name: kong-grpc-gateway
      namespace: kong
    spec:
      gatewayClassName: kong-grpc
      listeners:
        - name: http
          port: 80
          protocol: HTTP
        - name: grpc
          port: 443
          protocol: HTTPS
          hostname: example.com
          tls:
            certificateRefs:
              - group: ""
                kind: Secret
                name: example.com' | kubectl apply -f -
    
  2. GRPCRoute를 생성하세요:

    echo 'apiVersion: gateway.networking.k8s.io/v1
    kind: GRPCRoute
    metadata:
      name: grpcbin
      namespace: kong
    spec:
      parentRefs:
        - name: kong-grpc-gateway
          sectionName: grpc
      hostnames:
        - "example.com"
      rules:
        - backendRefs:
            - name: grpcbin
              port: 9001
    ' | kubectl apply -f -
    

이 구성은 Kong Gateway가 example.com에 대한 gRPC 요청을 포트 443에서 grpcbin Service의 포트 9001로 전달하도록 지시해요.

검증

  1. Gateway가 프로그래밍될 때까지 기다리세요:

    kubectl wait gateway/kong-grpc-gateway -n kong \
      --for=condition=Programmed=True \
      --timeout=5m
    
  2. Gateway의 외부 IP를 가져오세요:

    export PROXY_IP=$(kubectl get gateway kong-grpc-gateway -n kong -o jsonpath='{.status.addresses[0].value}')
    echo $PROXY_IP
    
  3. 프록시를 통해 grpcbin 테스트 서비스를 호출하세요:

    grpcurl -insecure -authority example.com \
      -d '{"greeting": "Kong"}' \
      $PROXY_IP:443 hello.HelloService.SayHello
    

    결과는 다음과 같아야 해요:

    {
      "reply": "hello Kong"
    }
    

    {:.no-copy-code}

더 알아보기 (Learn more)

  • Kong Gateway의 Service 엔티티 개념 보기 — Service
  • Kong Gateway의 Route 엔티티 개념 보기 — Route