K8s - Controller

OneDayDev·2023년 1월 21일
0

K8s

목록 보기
3/12
post-thumbnail

Intro

공식 문서
쿠버네티스는 크게 Object와 Object를 관리하는 Controller로 나눌 수 있다. Object에는 Pod, Service, Volume, Namespace 등이 있고 컨트롤러에는 Replicaset, Deployment, StatefulSet, DaemonSet, Job 등이 있다.
공식 문서에서는 Controller를 온도 조절 장치에 비유한다. 사용자가 온도를 설정하면 온도 조절 장치(Controller)는 사용자의 요구 온도(Desired state)에 맞게 현재 온도(current state)로 만든다.
Controller Pattern에 대해서는 공식 문서를 읽어보자.
이번 글에서는 Controller 종류와 특징에 대해 작성해보려한다.

Controller

Replication controller

  • 쿠버네티스 초기부터 있었던 기본적인 컨트롤러
  • 현재는 Replication대신 Deployment을 사용하길 권장함

ReplicaSet

  • ReplicaSet의 목적은 언제든 정해진 갯수의 Pod를 안정적으로 유지하는 것
  • 자세한 내용은 문서를 참고하자
  • Replicaset와 Pod는 느슨하게 결합되어 있다. 일반적인 replicaset 삭제(kubectl delete replicaset 에서 replicaset을 삭제하면 pod도 함께 삭제가 되겠지만 --cascade=orphan 옵션을 사용하면 pod를 유지하며 replicaset만 삭제할 수 있다.
  • pod의 레이블 변경해 replicaset의 관리에서 분리해 디버깅하는 용도 등으로 활용할 수도 있다.
  • rolling-update 옵션이 없다(Replication과 Deployment에는 있다)

Deployments

  • Note: Deployment가 가진 ReplicaSets를 직접 관리하지 말자.
  • 상태가 없는 앱을 배포할 때 사용하는 가장 기본적인 컨트롤러
  • ReplicaSet을 관리하면서 앱을 배포, 업데이트, 롤백 등을 할 수 있다.
  • kubectl scale 명령으로 파드 개수 변경할 수 있다.
    (ex. kubectl scale deploy deployment-name --replicas=5)
  • 배포를 멈췄다가(pause) 다시 시작(restart) 가능 그리고 재시작(restart)
    (ex. kubectl rollout pause/resume/restart deploy/deployment-name)

DaemonSet

  • 클러스터 전체 노드에 특정 파드를 실행할 때 사용하는 컨트롤러
  • 클러스터 안에 새로운 노드가 추가되었을 때 데몬세트가 자동으로 해당 노드에 파드를 실행시킨다.
  • 보통 저장소, 로그 수집기, 모니터링 등으로 활용
  • update 방법으로 OnDelete로 설정할 수 있다. 이 경우 업데이트 후 이전 파드를 직접 지워야 업데이트된 파드가 실행된다(참고로 updateStragegy.type 필드 값은 RollingUpdate와 OnDelete 두 가지가 있고, RollingUpdate가 default)

StatefulSets

  • 말 그대로 상태가 있는 파드들을 관리하는 Controller
  • Deployment같이 pod를 관리하지만 각 파드들에 밀접한 관계를 유지시켜준다.
  • 기본 동작은 순서대로 파드를 관리하는 것이지만, spec.podManagementPolicy 필드를 이용해 변경할 수 있다.

Using StatefulSets

  • Stable, unique network identifiers.
  • Stable, persistent storage.
  • Ordered, graceful deployment and scaling. (여기서 graceful은 프로세스를 강제 종료하는 게 아닌 작업을 마무리하고 정상적으로 종료하는 것을 뜻한다.)
  • Ordered, automated rolling updates.

Limitations

  • The storage for a given Pod must either be provisioned by a PersistentVolume Provisioner based on the requested storage class, or pre-provisioned by an admin.
  • Deleting and/or scaling a StatefulSet down will not delete the volumes associated with the StatefulSet. This is done to ensure data safety, which is generally more valuable than an automatic purge of all related StatefulSet resources.
  • StatefulSets currently require a Headless Service to be responsible for the network identity of the Pods. You are responsible for creating this Service.
    StatefulSets do not provide any guarantees on the termination of pods when a StatefulSet is deleted. To achieve ordered and graceful termination of the pods in the StatefulSet, it is possible to scale the StatefulSet down to 0 prior to deletion.
  • When using Rolling Updates with the default Pod Management Policy (OrderedReady), it's possible to get into a broken state that requires manual intervention to repair.
    Components

Job

  • 하나 이상의 파드를 명시된 숫자 만큼이 정상적으로 종료될 때까지 계속 실행한다.
  • 잡 시작을 지연시키는 옵션 등 참고할 만한 내용이 많으니 문서를 참고하자.

CronJob

  • 시간 기준으로 Job을 다룬다.
  • cron 명령어에서 사용하는 옵션 형식을 그대로 사용한다.

Automatic Clean-up for Finished Jobs (참고)

마치며

이번 글에서는 Controller에 대해 알아봤습니다.
간단하게 설명만 작성했기에 링크 걸어둔 문서를 참고하시면서 생성, 변경, 삭제 등의 여러 실습을 하시는 것을 추천드립니다.

profile
안녕하세요. Django 백엔드 개발하고 있습니다.

0개의 댓글