sangheee / issues

2 stars 0 forks source link

kubernetes operator 읽기 #7

Open sangheee opened 2 years ago

sangheee commented 2 years ago

쿠버네티스 오퍼레이터는 특정한 도메인 지식을 쿠버네티스 확장 기능으로 인코딩하여 애플리케이션 라이프사이클을 자동화하고 관리합니다. 쿠버네티스 오퍼레이터는 애플리케이션 수동 관리라는 까다로운 태스크를 제거함으로써 이러한 프로세스를 확장 및 반복 가능하도록 만들고 표준화합니다. Operators make it easier to automate the lifecycle of complex, stateful programs by adding application-specific skills to a k8s cluster. Operators understand their application's internal state, so they can coordinate installation and upgrades, act to repair failures, and automate repetitive or intricately choreographed maintenance work.

오퍼레이터 프레임워크 오퍼레이터 개발 가속화를 지원하는 오픈소스 프로젝트

  • operator SDK: 개발자가 쿠버네티스 API 복잡성에 관한 지식이 없어도 전문성을 바탕으로 오퍼레이터를 구축하도록 지원합니다.
    • operator lifecycle manager: 쿠버네티스 클러스터 전반에서 실행 중인 모든 오퍼레이터의 설치, 업데이트 및 라이프사이클 관리를 감독합니다.
  • operator metering: 전문 서비스를 제공하는 오퍼레이터에 대한 사용량 보고를 지원합니다.
sangheee commented 2 years ago

The Operator Framework

Operator SDK

Helm Operator


sangheee commented 2 years ago

Go-based Operator using Operator SDK

Create a new project

$ mkdir memcached-operator
$ cd memcached-operator
$ operator-sdk init --domain example.com --repo github.com/example/memcached-operator

Create a new API and Controller

# 난 여기서 $WORKSPACE/memcached-operator/bin/controller-gen not found error가 발생해서 골좀 썩였다
# 이유는 $GOBIN이 적절하게 설정되어 있지 않아서였다 ㅡㅡ!
# 아무튼 api/${version}/${lowercase_kind}_types.go controller/${lowercase_kind}_controller.go 가 각각 생성된다.
$ operator-sdk create api --group cache --version v1alpha1 --kind Memcached --resource --controller
Writing kustomize manifests for you to edit...
Writing scaffold for you to edit...
api/v1alpha1/memcached_types.go
controllers/memcached_controller.go
Update dependencies:
$ go mod tidy
Running make:
$ make generate
go: creating new go.mod: module tmp
Downloading sigs.k8s.io/controller-tools/cmd/controller-gen@v0.8.0
[...]
/Users/user/workspace/memcached-operator/bin/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."
Next: implement your new API and generate the manifests (e.g. CRDs,CRs) with:
$ make manifests

$ ll
total 384
drwxr-xr-x  15 user  staff    480  4 17 22:31 .
drwxr-xr-x  52 user  staff   1664  4 17 22:29 ..
-rw-------   1 user  staff    129  4 17 22:29 .dockerignore
-rw-------   1 user  staff    367  4 17 22:29 .gitignore
-rw-------   1 user  staff    776  4 17 22:29 Dockerfile
-rw-------   1 user  staff   9860  4 17 22:29 Makefile
-rw-------   1 user  staff    448  4 17 22:31 PROJECT
drwx------   3 user  staff     96  4 17 22:31 api
drwxr-xr-x   3 user  staff     96  4 17 22:31 bin
drwx------  10 user  staff    320  4 17 22:31 config
drwx------   4 user  staff    128  4 17 22:31 controllers
-rw-------   1 user  staff   3413  4 17 22:31 go.mod
-rw-r--r--   1 user  staff  94793  4 17 22:29 go.sum
drwx------   3 user  staff     96  4 17 22:29 hack
-rw-------   1 user  staff   3192  4 17 22:31 main.go

Manager

Defining the Go Types

--resource로 생성한 *_types.go file 에는 Custom Resource에 해당하는 Go type definitions이 있다 *_types.go에는 두 가지 struct가 있다.

  1. The spec object must include all possible configuration values that may be specified for resources of this type. spec은 아래 항목들을 포함한다.:
    • Operator code 내에서 참조될 variables name
    • CR에 지정될 field 이름
  2. The status object must include all possible values that the Operator may set to convey the state of the CR. status는 아래 항목들을 포함한다.:
    • Operator code 내에서 참조될 variables name
    • CR을 묘사할 field name

It is the Operator’s responsibility to determine how to use the values.

After modifying the *_types.go file always run the following command to update the generated code for that resource type:

make generate

The above makefile target will invoke the controller-gen utility to update the api/${version}/zz_generated.deepcopy.go file to ensure our API’s Go type definitions implement the runtime.Object interface that all Kind types must implement.

Generating CRD manifests

Once the API is defined with spec/status fields and CRD validation markers, the CRD manifests can be generated and updated with the following command:

make manifests

This makefile target will invoke controller-gen to generate the CRD manifests at config/crd/bases/${group}.${domain}_${kind}.yaml.

Controller

controller to watch for changes to CRs and react accordingly. CRD go type과 마찬가지로 --controller flag를 통해 생성된 *_controller.go가 controller go type을 나타낸다. controller는 특정 resource를 reconciling 할 책임이 있다.

Resources watched by the Controller

deploy

make deploy