스터디/DOIK

K8S Operator 패턴 - DOIK 스터디 2주차

시스템 엔지니어 2022. 6. 1. 22:15

https://youtu.be/sL2dVvDq32E

 

 

오퍼레이터 (extension): 쿠버네티스 동작을 이용하여 애플리케이션 운영을 자동화

오퍼레이터 사용전 선행 학습

  • 쿠버네티스 컨트롤 루프(컨트롤러 컴퍼넌트)
  • 애플리케이션 아키텍처와 동작이해

컨트롤 루프

  • control plane
    • 종료되지 않는 루프를 실행하면서 상태를 제어
    • 쿠버네티스 컴퍼넌트인 컨트롤러는 클러스터 상태를 의도한 상태로 유지
  • 행위
    • 1. 모니터링
    • 2. 상태 차이 발견
    • 3. 액션

주의사항

  • 오퍼레이터를 사용하려먼, 쿠버네티스 이해보다 애플리케이션 아키텍처와 동작 이해가 더 중요

용어

  • CRD Custom Resource Definition : 오퍼레이터로 사용할 상태 관리용 객체들의 Spec 을 정의
    • 어플리케이션 의도한 상태는 CRD로 정의
  • CR Custom Resource : CRD의 Spec 를 지키는 객체들의 실제 상태 데이터 조합
  • CC Custom Controller : CR의 상태를 기준으로 현재의 상태를 규정한 상태로 처리하기 위한 컨트롤 루프
  • 오퍼레이터는 Helm 기능을 포함하며, 배포 업그레이드 운영까지 확장해서 관리
  • CRD 구성 및 배포, Custom Controller 연계 및 실행 등을 모두 합쳐 놓은 것 == Operator

CR & CRD 실습

Docs  

CRD 생성

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition        # 사용자 정의 리소스(CRD) 생성
metadata:
  # name must match the spec fields below, and be in the form: <plural>.< group>
  name: crontabs.stable.example.com   # <NAMES>.<GROUP> 으로 정의
spec:
  # group name to use for REST API: /apis/<group>/<version>
  group: stable.example.com           # apiVersion 그룹 이름(<GROUP>) 을 지정
  # list of versions supported by this CustomResourceDefinition
  versions:                           # CRD 버전 정의
    - name: v1
      # Each version can be enabled/disabled by Served flag.
      served: true
      # One and only one version must be marked as the storage version.
      storage: true
      schema:
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              properties:
                cronSpec:
                  type: string
                image:
                  type: string
                replicas:
                  type: integer
  # either Namespaced or Cluster
  scope: Namespaced                  # Cluster 레벨 리소스인지 vs 네임스페이스 레벨 리소스인지 지정
  names:
    # plural name to be used in the URL: /apis/<group>/<version>/<plural>
    plural: crontabs                 # 복수 이름
    # singular name to be used as an alias on the CLI and for display
    singular: crontab                # 단수 이름
    # kind is normally the CamelCased singular type. Your resource manifests use this.
    kind: CronTab                    # Kind 이름
    # shortNames allow shorter string to match your resource on the CLI
    shortNames:                      # 축약 이름
    - ct

CR 생성

apiVersion: "stable.example.com/v1"
kind: CronTab
metadata:
  name: my-new-cron-object
spec:
  cronSpec: "* * * * */5"
  image: my-awesome-cron-image

현재는 리소스만 생성한 상태이고 어떤 동작을 수행해야 하는지 아무런 정보가 없음

실제 동작수행하기 위해서는 Custom Controller 의 도움이 필요

Operator 패턴이란 Custom Resource + Custom Controller 조합으로 특정 애플리케이션이나 서비스의 생성과 삭제를 관리하는 패턴을 말함

Operator 패턴을 통해 쿠버네티스 코어 API에 포함되지 않은 애플리케이션을 마치 쿠버네티스 Native 리소스처럼 동작하게끔 만들 수 있음

 

CRD 관리의 중요성

CRD 삭제

리소스 정보 확인 시 에러 발생