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

Service 버전 간 트래픽 분할

원문 보기 위키 갱신

Service 버전 간 트래픽 분할 (Split traffic between versions of a Service)

이 문서는 Kong Operator 환경에서 Service의 여러 버전 사이로 트래픽을 나누는 방법을 소개해요. HTTPRoute의 backendRefs에 버전별 Service를 하나씩 지정하고 각 버전에 weight를 부여해 트래픽 분배 비율을 정의하는 게 핵심이에요. Konnect 또는 온프레미스 배포 준비부터 샘플 Service 배포, 가중치 기반 HTTPRoute 작성, 동작 검증까지 차근차근 따라 해 볼 수 있어요

출처: 문서

본문

TL;DR

Service의 여러 버전 간에 트래픽을 분할하려면 어떻게 해야 하나요? HTTPRoute의 backendRefs에 각 Service 버전별 항목을 하나씩 추가하고, 각 버전에 가중치를 할당해서 트래픽을 어떻게 분할할지 정의하세요.

전제 조건

Konnect 배포: Kong Konnect

Konnect 계정이 없다면, 온보딩 마법사를 통해 빠르게 시작할 수 있어요.

  1. 이 튜토리얼을 완료하려면 다음 Konnect 항목들이 필요해요:

    • 개인 액세스 토큰(PAT): Konnect PAT 페이지를 열고 Generate Token을 선택해서 새 개인 액세스 토큰을 생성하세요.
  2. 개인 액세스 토큰을 환경 변수로 설정하세요:

    export KONNECT_TOKEN='YOUR KONNECT TOKEN'
    

Kong Operator 실행

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

    helm repo add kong https://charts.konghq.com
    helm repo update
    
  2. Helm으로 Kong Operator를 설치하세요:

    Konnect 배포

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

    온프레미스 배포

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

    cert-manager가 admission 및 conversion 웹훅 인증서를 발급하고 갱신하도록 하려면, 클러스터에 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 -

Gateway 리소스 생성

온프레미스 배포

kong 네임스페이스를 생성하세요:

kubectl create namespace kong

기본 설정으로 GatewayConfiguration, GatewayClass, Gateway 리소스를 생성하세요:

echo '
apiVersion: gateway-operator.konghq.com/v2beta1
kind: GatewayConfiguration
metadata:
  name: 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: gateway-class
spec:
  controllerName: konghq.com/gateway-operator
  parametersRef:
    group: gateway-operator.konghq.com
    kind: GatewayConfiguration
    name: gateway-configuration
    namespace: kong
---
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: kong
  namespace: kong
spec:
  gatewayClassName: gateway-class
  listeners:
    - name: http
      port: 80
      protocol: HTTP' | kubectl apply -f -

트래픽 분할은 카나리 릴리스나 블루/그린 배포라고도 불리며, 서비스의 여러 버전 간에 트래픽을 전환할 수 있게 해줘요.

Kong Operator와 Gateway API를 사용하면 트래픽 분할이 HTTPRoute 가중치를 통해 네이티브하게 관리돼요.

샘플 Service 배포

같은 Service의 두 버전을 배포하세요:

echo '
apiVersion: v1
kind: Service
metadata:
  name: echo-v1
spec:
  selector:
    app: echo-v1
  ports:
  - name: http
    port: 80
    targetPort: 1027
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: echo-v1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: echo-v1
  template:
    metadata:
      labels:
        app: echo-v1
    spec:
      containers:
      - name: echo
        image: kong/go-echo:latest
        env:
        - name: NODE_NAME
          value: "v1"
---
apiVersion: v1
kind: Service
metadata:
  name: echo-v2
spec:
  selector:
    app: echo-v2
  ports:
  - name: http
    port: 80
    targetPort: 1027
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: echo-v2
spec:
  replicas: 1
  selector:
    matchLabels:
      app: echo-v2
  template:
    metadata:
      labels:
        app: echo-v2
    spec:
      containers:
      - name: echo
        image: kong/go-echo:latest
        env:
        - name: NODE_NAME
          value: "v2"
' | kubectl apply -f - -n kong

가중치가 적용된 HTTPRoute 생성

backendRefs 섹션에 두 Service를 모두 참조하는 HTTPRoute 리소스를 정의하세요. 각 참조에는 weight가 포함돼요. 이 예제에서는 두 Service 간에 50/50으로 분할되도록 설정해요:

echo '
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: canary-route
  namespace: kong
spec:
  parentRefs:
  - name: kong
  rules:
  - matches:
    - path:
        type: PathPrefix
        value: /echo
    backendRefs:
    - name: echo-v1
      port: 80
      weight: 50
    - name: echo-v2
      port: 80
      weight: 50' | kubectl apply -f -

가중치는 상대적이에요. 가중치가 50과 50인 백엔드 두 개가 있으면 트래픽은 50/50으로 분할돼요. 가중치가 90과 10이면 90/10으로 분할돼요.

검증

  1. Gateway의 외부 IP를 가져오세요:

    export PROXY_IP=$(kubectl get gateway kong -n kong -o jsonpath='{.status.addresses[0].value}')
    
  2. Route에 여러 번 요청을 보내세요:

    for i in {1..10}; do curl -s http://$PROXY_IP/echo; done
    

    v1과 v2가 균등하게 분할된 결과를 볼 수 있어요:

    Welcome, you are connected to node v1.
    
    Welcome, you are connected to node v2.
    
    Welcome, you are connected to node v1.
    
    Welcome, you are connected to node v1.
    
    Welcome, you are connected to node v2.
    
    Welcome, you are connected to node v1.
    
    Welcome, you are connected to node v2.
    
    Welcome, you are connected to node v1.
    
    Welcome, you are connected to node v2.
    
    Welcome, you are connected to node v2.
    

    {:.no-copy-code}

더 알아보기 (Learn more)

  • Konnect 계정이 아직 없다면 온보딩 마법사를 통해 빠르게 가입하고 시작할 수 있어요 — onboarding wizard
  • 이 튜토리얼에 필요한 Konnect 개인 액세스 토큰(PAT)을 발급할 수 있는 페이지예요 — Konnect PAT page