인그레스를 통한 컨테이너 기반 부하 분산

이 페이지에서는 Google Kubernetes Engine(GKE)에서 컨테이너 기반 부하 분산을 사용하는 방법을 설명합니다. 부하 분산기는 컨테이너 기반 부하 분산을 통해 Kubernetes 포드를 직접 대상으로 지정하고 트래픽을 포드에 균일하게 분산시킬 수 있습니다.

컨테이너 기반 부하 분산의 이점, 요구사항, 제한사항에 대한 자세한 내용은 컨테이너 기반 부하 분산을 참조하세요.

시작하기 전에

시작하기 전에 다음 태스크를 수행했는지 확인합니다.

  • Google Kubernetes Engine API를 사용 설정합니다.
  • Google Kubernetes Engine API 사용 설정
  • 이 태스크에 Google Cloud CLI를 사용하려면 gcloud CLI를 설치한 후 초기화합니다. 이전에 gcloud CLI를 설치했으면 gcloud components update 명령어를 실행하여 최신 버전을 가져옵니다. 이전 gcloud CLI 버전에서는 이 문서의 명령어를 실행하지 못할 수 있습니다.
  • 기존 VPC 기반 클러스터가 있는지 확인합니다. 필요한 경우 클러스터를 만듭니다. GKE 클러스터는 기본적으로 VPC 기반입니다.

컨테이너 기반 부하 분산 사용

다음 섹션에서는 GKE의 컨테이너 기반 부하 분산 구성을 설명합니다.

배포 만들기

다음 샘플 배포 neg-demo-app은 컨테이너화된 HTTP 서버의 단일 인스턴스를 실행합니다. 포드 준비 피드백을 사용하는 워크로드를 사용하는 것이 좋습니다.

포드 준비 상태 피드백 사용

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    run: neg-demo-app # Label for the Deployment
  name: neg-demo-app # Name of Deployment
spec:
  selector:
    matchLabels:
      run: neg-demo-app
  template: # Pod template
    metadata:
      labels:
        run: neg-demo-app # Labels Pods from this Deployment
    spec: # Pod specification; each Pod created by this Deployment has this specification
      containers:
      - image: registry.k8s.io/serve_hostname:v1.4 # Application to run in Deployment's Pods
        name: hostname # Container name
        ports:
        - containerPort: 9376
          protocol: TCP
  

하드코딩된 지연 사용

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    run: neg-demo-app # Label for the Deployment
  name: neg-demo-app # Name of Deployment
spec:
  minReadySeconds: 60 # Number of seconds to wait after a Pod is created and its status is Ready
  selector:
    matchLabels:
      run: neg-demo-app
  template: # Pod template
    metadata:
      labels:
        run: neg-demo-app # Labels Pods from this Deployment
    spec: # Pod specification; each Pod created by this Deployment has this specification
      containers:
      - image: registry.k8s.io/serve_hostname:v1.4 # Application to run in Deployment's Pods
        name: hostname # Container name
      # Note: The following line is necessary only on clusters running GKE v1.11 and lower.
      # For details, see https://cloud.google.com/kubernetes-engine/docs/how-to/container-native-load-balancing#align_rollouts
        ports:
        - containerPort: 9376
          protocol: TCP
      terminationGracePeriodSeconds: 60 # Number of seconds to wait for connections to terminate before shutting down Pods
  

이 배포에서 각 컨테이너는 HTTP 서버를 실행합니다. HTTP 서버는 애플리케이션 서버의 호스트 이름(서버가 실행되는 포드의 이름)을 응답으로 반환합니다.

이 매니페스트를 neg-demo-app.yaml로 저장한 후 배포를 만듭니다.

kubectl apply -f neg-demo-app.yaml

컨테이너 기반 부하 분산기용 서비스 만들기

배포를 만든 후에 배포의 포드를 서비스로 그룹화해야 합니다.

다음 샘플 서비스 neg-demo-svc는 이전 섹션에서 만든 샘플 배포를 대상으로 합니다.

apiVersion: v1
kind: Service
metadata:
  name: neg-demo-svc # Name of Service
spec: # Service's specification
  type: ClusterIP
  selector:
    run: neg-demo-app # Selects Pods labelled run: neg-demo-app
  ports:
  - name: http
    port: 80 # Service's port
    protocol: TCP
    targetPort: 9376

부하 분산기는 서비스용 인그레스를 만들 때까지 생성되지 않습니다.

이 매니페스트를 neg-demo-svc.yaml로 저장한 후 서비스를 만듭니다.

kubectl apply -f neg-demo-svc.yaml

서비스에 대한 인그레스 만들기

다음 샘플 인그레스 neg-demo-ing는 만든 서비스를 대상으로 합니다.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: neg-demo-ing
spec:
  defaultBackend:
    service:
      name: neg-demo-svc # Name of the Service targeted by the Ingress
      port:
        number: 80 # Should match the port used by the Service

이 매니페스트를 neg-demo-ing.yaml로 저장한 후 인그레스를 만듭니다.

kubectl apply -f neg-demo-ing.yaml

인그레스를 만들면 프로젝트에 애플리케이션 부하 분산기가 생성되고 클러스터가 실행되는 각 영역에 네트워크 엔드포인트 그룹(NEG)이 생성됩니다. NEG의 엔드포인트와 서비스의 엔드포인트는 동기화 상태로 유지됩니다.

인그레스 확인

워크로드를 배포하고 포드를 서비스로 그룹화하고 서비스용 인그레스를 만든 후 인그레스가 컨테이너 기본 부하 분산기를 성공적으로 프로비저닝했는지 확인해야 합니다.

인그레스의 상태를 검색합니다.

kubectl describe ingress neg-demo-ing

출력에는 ADDCREATE 이벤트가 포함됩니다.

Events:
Type     Reason   Age                From                     Message
----     ------   ----               ----                     -------
Normal   ADD      16m                loadbalancer-controller  default/neg-demo-ing
Normal   Service  4s                 loadbalancer-controller  default backend set to neg-demo-svc:32524
Normal   CREATE   2s                 loadbalancer-controller  ip: 192.0.2.0

부하 분산기 테스트

다음 섹션에서는 컨테이너 기본 부하 분산기의 기능을 테스트하는 방법을 설명합니다.

인그레스 IP 주소 방문

애플리케이션 부하 분산기가 구성될 때까지 몇 분 정도 기다립니다.

인그레스의 IP 주소를 방문하여 컨테이너 기본 부하 분산기가 작동하는지 확인할 수 있습니다.

인그레스 IP 주소를 얻으려면 다음 명령어를 실행하세요.

kubectl get ingress neg-demo-ing

명령어 결과의 ADDRESS 열에 인그레스의 IP 주소가 표시됩니다. 웹브라우저에서 이 IP 주소를 방문합니다.

백엔드 서비스 상태 확인

부하 분산기의 백엔드 서비스 상태를 확인할 수도 있습니다.

  1. 프로젝트에서 실행되는 백엔드 서비스의 목록을 가져옵니다.

    gcloud compute backend-services list
    

    neg-demo-svc와 같은 서비스 이름이 포함된 백엔드 서비스의 이름을 기록합니다.

  2. 백엔드 서비스 상태를 확인합니다.

    gcloud compute backend-services get-health BACKEND_SERVICE_NAME --global
    

    BACKEND_SERVICE_NAME을 백엔드 서비스 이름으로 바꿉니다.

인그레스 테스트

부하 분산기가 예상대로 작동하는지 테스트하는 또 다른 방법은 샘플 배포를 확장하고 테스트 요청을 인그레스에 전송하여 올바른 수의 복제본이 응답하는지 확인하는 것입니다.

  1. neg-demo-app 배포를 인스턴스 한 개에서 인스턴스 두 개로 확장합니다.

    kubectl scale deployment neg-demo-app --replicas 2
    

    이 명령어가 완료되는 데 몇 분이 소요될 수 있습니다.

  2. 출시가 완료되었는지 확인합니다.

    kubectl get deployment neg-demo-app
    

    출력에는 사용 가능한 복제본 2개가 포함되어야 합니다.

    NAME           DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
    neg-demo-app   2         2         2            2           26m
    
  3. 인그레스 IP 주소를 가져옵니다.

    kubectl describe ingress neg-demo-ing
    

    이 명령어가 404 오류를 반환하면 부하 분산기가 시작될 때까지 몇 분 정도 기다린 후 다시 시도합니다.

  4. 부하 분산기에서 고유 응답 수를 계산합니다.

    for i in `seq 1 100`; do \
      curl --connect-timeout 1 -s IP_ADDRESS && echo; \
    done  | sort | uniq -c
    

    IP_ADDRESS를 인그레스 IP 주소로 바꿉니다.

    출력은 다음과 비슷합니다.

    44 neg-demo-app-7f7dfd7bc6-dcn95
    56 neg-demo-app-7f7dfd7bc6-jrmzf
    

    이 출력에서 고유 응답 수는 복제본 수와 동일합니다. 이는 모든 백엔드 포드가 트래픽을 처리하고 있음을 나타냅니다.

삭제

이 페이지의 작업을 완료한 후에는 다음 단계에 따라 리소스를 제거하여 계정에서 원치 않는 요금이 부과되지 않도록 합니다.

클러스터 삭제

gcloud

gcloud container clusters delete neg-demo-cluster

콘솔

  1. Google Cloud 콘솔에서 Google Kubernetes Engine 페이지로 이동합니다.

    Google Kubernetes Engine으로 이동

  2. neg-demo-cluster를 선택하고 삭제를 클릭합니다.

  3. 확인 메시지가 나타나면 삭제를 클릭합니다.

다음 단계