Ssup2 Blog logo Ssup2 Blog

Operator SDK User Guide에 소개된 Memcached Operator 예제를 통해 Operator SDK와 Memcached Operator를 분석한다.

1. Operator SDK, Ansible Operator

1.1. Ansible Operator Component

1.2. Ansible Operator HA

2. Memcached Helm Operator

2.1. 개발 환경

개발 환경은 다음과 같다.

2.2. Operator SDK 설치

# mkdir -p ~/operator-sdk
# cd ~/operator-sdk
# RELEASE_VERSION=v0.8.0
# curl -OJL https://github.com/operator-framework/operator-sdk/releases/download/${RELEASE_VERSION}/operator-sdk-${RELEASE_VERSION}-x86_64-linux-gnu
# chmod +x operator-sdk-${RELEASE_VERSION}-x86_64-linux-gnu && sudo cp operator-sdk-${RELEASE_VERSION}-x86_64-linux-gnu /usr/local/bin/operator-sdk && rm operator-sdk-${RELEASE_VERSION}-x86_64-linux-gnu
# operator-sdk
An SDK for building operators with ease

Usage:
  operator-sdk [command]
...
[Shell 1] Operator SDK 설치

Kubernetes Operator SDK CLI를 설치하고 동작을 확인한다.

2.3. Project 생성

# operator-sdk new example-k8s-operator-ansible --api-version=cache.example.com/v1alpha1 --kind=Memcached --type=ansible
# cd example-k8s-operator-ansible && ls
build  deploy  molecule  roles  watches.yaml
[Shell 2] Project 생성

2.4. Memcached Spec 설정

---
size: 1
[파일 1] roles/memcached/defaults/main.yml
---
- name: start memcached
  k8s:
    definition:
      kind: Deployment
      apiVersion: apps/v1
      metadata:
        name: '-memcached'
        namespace: ''
      spec:
        replicas: ""
        selector:
          matchLabels:
            app: memcached
        template:
          metadata:
            labels:
              app: memcached
          spec:
            containers:
            - name: memcached
              command:
              - memcached
              - -m=64
              - -o
              - modern
              - -v
              image: "docker.io/memcached:1.4.36-alpine"
              ports:
                - containerPort: 11211
[파일 2] roles/memcached/tasks/main.yml

2.5. Memcached CRD 생성

# kubectl create -f deploy/crds/cache_v1alpha1_memcached_crd.yaml
[Shell 3] Memcached CRD 생성

2.5. Memcached Operator 구동

# operator-sdk build supsup5642/memcached-operator:v0.0.1
# docker push supsup5642/memcached-operator:v0.0.1
[Shell 4] Memcached Operator Image 생성 및 Push
# sed -i 's||supsup5642/memcached-operator:v0.0.1|g' deploy/operator.yaml
# sed -i 's||Always|g' deploy/operator.yaml
# kubectl create -f deploy/service_account.yaml
# kubectl create -f deploy/role.yaml
# kubectl create -f deploy/role_binding.yaml
# kubectl create -f deploy/operator.yaml
[Shell 5] Memcached Operator 구동

2.6. Memcached CR 생성을 통한 Memcached 구동

# kubectl create -f deploy/crds/cache_v1alpha1_memcached_cr.yaml
[Shell 6] Memcached 구동
# kubectl get pod
NAME                                                       READY   STATUS    RESTARTS   AGE
example-k8s-operator-ansible-b67c8d8b4-xgrqr               2/2     Running   0          9m31s
example-memcached-memcached-5d489fcf76-8q26l               1/1     Running   0          116s
example-memcached-memcached-5d489fcf76-cd4ph               1/1     Running   0          116s
example-memcached-memcached-5d489fcf76-sc7g5               1/1     Running   0          117s
[Shell 7] Nginx 구동 확인

3. 참조