Kubernetes에서 Pod 컨트롤러: Deployment와 DaemonSet의 핵심 개념 및 실습

1. Pod 컨트롤러 개요

1.1 Pod 컨트롤러 정의

Pod 컨트롤러는 클러스터 내 Pod 자원의 상태를 관리하는 중간 계층입니다. 예상 상태와 실제 상태가 일치하도록 보장하며, Pod 오류 시 재시작 및 재생성을 담당합니다.

1.2 주요 기능

  • Pod 오류 시 재시작
  • 재시작 정책 실패 시 새 Pod 생성

1.3 컨트롤러 종류

  • ReplicaSet: 지정된 Pod 복사본 수 유지
  • Deployment: 무상태 애플리케이션 관리, 롤링 업데이트 지원
  • DaemonSet: 노드당 하나의 Pod 배포 (시스템 데몬용)
  • Job: 일회성 작업 처리

2. ReplicaSet 실습

2.1 전통적 클러스터링 한계

기존 방식은 수동 Nginx 설정을 통해 부하 분산을 관리해야 하며, 확장/축소 시 오류 발생 가능성 있음.

2.2 ReplicaSet 구성 요소

  • Selector: 관리 대상 Pod 라벨 매칭
  • Replicas: 유지할 Pod 복사본 수
  • Pod 템플릿: Pod 정의 내역

2.3 예제 YAML


apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: example-rs
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - name: nginx
        image: nginx:1.20
        ports:
        - containerPort: 80

3. Deployment 핵심 개념

3.1 Deployment 특징

  • ReplicaSet 상위 추상화
  • 롤링 업데이트 및 롤백 지원
  • 상태 추적 및 이력 관리

3.2 구성 요소

  • Selector: 관리 대상 Pod 라벨
  • Replicas: 목표 복사본 수
  • Pod 템플릿: Pod 정의 내역

3.3 예제 YAML


apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-deploy
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - name: web-app
        image: my-webapp:v1
        ports:
        - containerPort: 8080

4. DaemonSet 사용법

4.1 사용 시나리오

  • 모든 노드에 로그 수집기 배포
  • 네트워크 플러그인 설치
  • 메트릭 수집자 배포

4.2 예제 YAML


apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: log-collector
spec:
  selector:
    matchLabels:
      app: fluentd
  template:
    metadata:
      labels:
        app: fluentd
    spec:
      containers:
      - name: collector
        image: fluent/fluentd-kubernetes-daemonset:v1.14.6
        env:
        - name: FLUENT_ELASTICSEARCH_HOST
          value: "elasticsearch"

5. 고가용성 테스트

5.1 서비스 설정


apiVersion: v1
kind: Service
metadata:
  name: web-service
spec:
  selector:
    app: web
  ports:
  - port: 80
    targetPort: 8080

5.2 테스트 명령어


kubectl get pods
kubectl delete pod <pod-name>
curl http://<service-ip>

6. 스케일링 및 자동확장

6.1 수동 스케일링


kubectl scale deployment web-deploy --replicas=5

6.2 HPA 설정


apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: web-hpa
spec:
  minReplicas: 2
  maxReplicas: 10
  targetCPUUtilizationPercentage: 70

7. 업데이트 전략

7.1 Recreate 전략

기존 Pod를 모두 종료한 후 새 버전 Pod 생성

7.2 RollingUpdate 전략

  • maxSurge: 최대 허용 초과 Pod 수
  • maxUnavailable: 최소 가용 Pod 수

spec:
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%

태그: kubernetes deployment daemonset pod-controller ReplicaSet

10월 2일 03:08에 게시됨