Operator Pattern & CRD
운영자의 지식을 코드로 — K8s API를 확장하여 복잡한 앱을 자동 관리
1. CRD (Custom Resource Definition)
1.1 K8s API 확장
K8s의 기본 리소스(Pod, Service)만으로 부족할 때, 나만의 리소스를 정의할 수 있음.
# CRD 정의 — "PostgresCluster"라는 새로운 리소스 타입
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: postgresclusters.db.example.com
spec:
group: db.example.com
names:
kind: PostgresCluster
plural: postgresclusters
shortNames: ["pgc"]
scope: Namespaced
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
replicas:
type: integer
version:
type: string
# Custom Resource 생성 — kubectl로 관리 가능
apiVersion: db.example.com/v1
kind: PostgresCluster
metadata:
name: my-db
spec:
replicas: 3
version: "16"
kubectl get postgresclusters # 또는 kubectl get pgc
kubectl describe pgc my-db
2. Operator 패턴
2.1 CRD + Custom Controller = Operator
flowchart LR
CR["Custom Resource<br/>(원하는 상태)"]
Controller["Custom Controller<br/>(Reconciliation Loop)"]
K8sAPI["K8s API<br/>(Pod, Service, PVC)"]
CR -->|"Watch"| Controller
Controller -->|"생성/수정/삭제"| K8sAPI
K8sAPI -->|"현재 상태"| Controller
Reconciliation Loop:
- Custom Resource 변경 감지
- 현재 상태와 원하는 상태 비교
- 차이가 있으면 K8s 리소스를 조정 (Pod 추가, Service 업데이트 등)
- 반복
2.2 왜 Operator인가
DB 클러스터를 K8s에서 운영한다면:
- 초기 배포: Primary + Replica 구성
- 장애 복구: Primary 다운 → Replica 승격 → 새 Replica 추가
- 백업: 주기적 백업 실행
- 업그레이드: 롤링 업그레이드 (순서 중요)
이 운영 지식을 코드로 만든 것이 Operator.
3. 대표 Operator
| Operator | 관리 대상 | 역할 |
|---|---|---|
| Prometheus Operator | Prometheus, Alertmanager | 모니터링 스택 자동 설정 |
| Cert-Manager | TLS 인증서 | Let's Encrypt 인증서 자동 발급/갱신 |
| PostgreSQL Operator (Zalando/Crunchy) | PostgreSQL | 클러스터 배포, 복제, 백업, 페일오버 |
| Rook-Ceph | Ceph 스토리지 | Ceph를 K8s 네이티브로 관리 |
| Strimzi | Apache Kafka | Kafka 클러스터 관리 |
| MySQL Operator (Oracle) | MySQL | InnoDB Cluster 관리 |
# Operator Hub에서 검색
# https://operatorhub.io/
4. Operator 개발
| 도구 | 방식 | 언어 |
|---|---|---|
| Operator SDK | Go, Ansible, Helm 기반 | Go (권장), Ansible, Helm |
| Kubebuilder | Go 기반 프레임워크 | Go |
| KOPF | Python Kubernetes Operator Framework | Python |
| Metacontroller | 선언적 컨트롤러 | Webhook (언어 무관) |
Operator 성숙도 모델
| Level | 이름 | 능력 |
|---|---|---|
| 1 | Basic Install | Helm/Kustomize로 배포 |
| 2 | Seamless Upgrades | 롤링 업그레이드 자동화 |
| 3 | Full Lifecycle | 백업/복원, 장애 복구 자동화 |
| 4 | Deep Insights | 메트릭, 알림, 분석 |
| 5 | Auto Pilot | 자동 스케일링, 자동 튜닝, 이상 탐지 |
다음 글
→ #15 보안 심화 — OPA, Falco, Supply Chain