🔹Kubernetes

[Kubernetes] Controller 소개

terranbin 2024. 3. 7. 20:56
728x90
SMALL
  • 정의
    • Pod 개수 보장
    • 실행 안정적 유지

ReplicationController

 

  • 생성 명령어
kubectl create rc-exam --image=nginx --replicas=3 --selector=app-webui

 

  • 갯수를 살펴봐서 많으면 pod 를 죽이고, 부족하면 새로 생성해준다
kubectl edit rc rc-nginx 
# 'replicas: '부분을 변경, 실행 중인 pod 개수 변경 (3->4)
# 다른 부분 (예시, image: nginx:1.15) 변경은 적용되지 않는다
# # 변경 사항을 적용하려면, kubectl delete 로 삭제하라.
# # 다시 생성될 것이다
kubectl scale rc rc-nginx —replicas=2
# 4개가 강제적으로 2개로 줄어듬
kubectl delete rc rc-nginx
# pod 만 삭제하는 것은 의미 없으므로, Replication Controller 자체를 삭제

 


ReplicationSet

  • 풍부한 Selector 지원
selector:
  matchLabels:
    component: redis
  matchExpressions:
    - {key: version, operator: Exists}
  • 예시 yaml file
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: rs-nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: webui
  template:
    metadata:
      name: nginx-pod
      labels:
        app: webui
    spec:
      containers:
      - name: nginx-container
        image: nginx:1.14
  • 삭제 시 relicaset 만 삭제하려 할 경우
kubectl delete rs rs-nginx --cascade=false

Deployment

  • ReplicaSet 을 컨트롤 해서 Pod 수 조절

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app-deploy
spec:
  selector:
    matchLabels:
      app: webui
  replicas: 3
  template:
    metadata:
      labels:
        app: webui
    spec:
      containers:
      - name: web
        image: nginx:1.14
        ports:
        - containerPort: 80
  • deploy
#파일 생성 뒤 
kubectl create -f
#파일 수정 뒤 
kubectl apply -f
  • Rolling Update
kubectl set image deployment <deploy_name> <container_name>=<new_version_image>

kubectl set image deployment app-deploy web=nginx:1.15 --record
#version update 시킴
#record 작성 중요
  • RollBack
kubectl rollout history deployment <deploy_name>
kubectl rollout history deployment app-deploy
# ...
# deployment.apps/app-deploy
# REVISION  CHANGE-CAUSE
# 1         <none>

# Revision Rollback
kubectl rollout undo deploy <deploy_name>
kubectl rollout undo deployment app-deploy --to-revision=3

# Status Check
kubectl rollout status deployment app-deploy

# 정지 및 재시작
kubectl rollout pause deployment app-deploy
kubectl rollout resume deployment app-deploy

DaemonSet

  • 전체 노드에서 Pod 가 한 개씩 실행되도록 보장
  • 로그 수집기, 모니터링 에이전트와 같은 프로그램 실행 시 적용
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: daemonset-nginx
spec:
  selector:
    matchLabels:
      app: webui
  template:
    metadata:
      name: nginx-pod
      labels:
        app: webui
    spec:
      containers:
      - name: nginx-container
        image: nginx:1.14

 


StatefulSet

  • Pod 상태를 유지해주는 Controller
    • pod 의 이름, pod 의 볼륨(스토리지)
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: sf-nginx
spec:
  replicas: 3
  serviceName: sf-service
  podManagementPolicy: Parallel
  selector:
    matchLabels:
      app: webui
  template:
    metadata:
      name: nginx-pod
      labels:
        app: webui
    spec:
      containers:
      - name: nginx-container
        image: nginx:1.14

 

  • scale up, scale down
 kubectl scale statefulset sf-nginx --replicas=4
 kubectl scale statefulset sf-nginx --replicas=2

 


Job

  • Kubernetes 는 Pod 를 Running 중인 상태로 유지시키려 한다
  • Batch 처리하는 Pod 는 작업이 완료되면 종료됨
  • Batch 처리에 적합한 컨트롤러로, Pod 의 성공적 완료 보장
    • 비정상 종료 시 다시 실행
    • 정상 종료 시 완료
apiVersion: apps/v1
kind: Job
metadata:
  name: job-example
spec:
  template:
    spec:
      containers:
      - name: centos-container
        image: centos:7
        command: ["bash"]
        args:
        - "-c"
        - "echo 'Hello World'; sleep 50; echo 'Bye'"
    restartPolicy: Never
LIST