iBetter Books
교안 슬라이드
수정

Deployment 개념과 구조

Kubernetes에는 여러 종류의 오브젝트가 있지만, 실제 운영에서 가장 자주 만나는 것은 Deployment입니다. Pod를 직접 관리하는 대신 Deployment에 "이 애플리케이션을 몇 개 실행해"라고 선언하면, Kubernetes가 그 상태를 알아서 유지해줍니다.

내부 구조를 보면 세 개의 계층으로 이루어져 있습니다.

Deployment
  └── ReplicaSet
        ├── Pod (app=my-app)
        ├── Pod (app=my-app)
        └── Pod (app=my-app)

Deployment가 전체 배포 전략을 관리합니다. ReplicaSet은 Deployment의 지시를 받아 Pod 수를 실제로 유지합니다. Pod는 컨테이너가 실행되는 최소 단위입니다. 평소에는 ReplicaSet과 Pod를 직접 건드릴 일이 없습니다. Deployment만 다루면 됩니다.

Deployment YAML은 다음과 같습니다.

apiVersion: apps/v1kind: Deploymentmetadata:  name: my-appspec:  replicas: 3  selector:    matchLabels:      app: my-app  template:    metadata:      labels:        app: my-app    spec:      containers:      - name: app        image: nginx:alpine        ports:        - containerPort: 80

여기서 한 가지 규칙을 반드시 기억해야 합니다. selector.matchLabelstemplate.metadata.labels의 값이 반드시 일치해야 합니다.

이유는 간단합니다. ReplicaSet이 "어떤 Pod가 내 관리 대상인지"를 라벨(label)로 구분하기 때문입니다. selector.matchLabels는 "나는 이 라벨이 붙은 Pod를 관리한다"는 선언이고, template.metadata.labels는 "내가 만드는 Pod에 이 라벨을 붙인다"는 선언입니다. 둘이 다르면 Kubernetes는 오류를 냅니다. 직접 만든 Pod도 라벨만 일치하면 Deployment의 관리 대상으로 편입됩니다. 라벨은 Kubernetes 오브젝트들이 서로를 찾는 핵심 메커니즘입니다.

이 YAML 파일을 deployment.yaml로 저장하고 kubectl apply -f deployment.yaml을 실행하면, Kubernetes는 지정된 replicas 수만큼 Pod를 생성하고 유지합니다.