개발강의정리/DevOps

[데브옵스를 위한 쿠버네티스 마스터] 쿠버네티스 핵심 개념-디플로이먼트

nineDeveloper 2020. 12. 1.
728x90

Deployment

디플로이먼트

https://kubernetes.io/docs/concepts/workloads/controllers/deployment

  • 애플리케이션을 다운 타입 없이 업데이트 가능하도록 도와주는 리소스!
  • 레플리카셋과 레플리케이션컨트롤러 상위에 배포되는 리소스

  • 모든 포드를 업데이트하는 방법
    • 잠깐의 다운 타임 발생 (새로운 포드를 실행시키고 작업이 완료되면 오래된 포드를 삭제)
    • 롤링 업데이트

디플로이먼트 작성 요령

  • 포드의 metadata 부분과 spec 부분을 그대로 옮김
  • Deployment의 spec.template에는 배포할 포드를 설정
  • replicas에는 이 포드를 몇 개를 배포할지 명시
  • label은 디플로이먼트가 배포한 포드를 관리하는데 사용됨

pod.yaml

apiVersion: v1
kind: Pod
metadata:
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.14.2
    ports:
    - containerPort: 80

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
        labels:
          app: nginx
      spec:
        containers:
        - name: nginx
          image: nginx:1.14.2
          ports:
          - containerPort: 80

디플로이먼트 스케일링

  • kubectl edit deploy <deploy name> 명령을 사용해 yaml 파일을 직접 수정하여 replicas 수를 조정
  • kubectl scale deploy <deploy name> --replicas=<number> 명령을 사용해 replicas 수 조정

연습문제

  • jenkins 디플로이먼트를 deploy-jenkins를 생성하라

jenkins-deploy.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: deploy-jenkins
  labels:
    app: jenkins-test
spec:
  replicas: 3
  selector:
    matchLabels:
      app: jenkins-test
  template:
    metadata:
      labels:
        app: jenkins-test
    spec:
      containers:
      - name: jenkins
        image: jenkins
        ports:
        - containerPort: 9080
$ kubectl create -f jenkins-deploy.yaml
deployment.apps/deploy-jenkins created
  • jenkins 디플로이먼트로 배포되는 앱을 app: jenkins-test로 레이블링하라
$ kubectl get all
NAME                                  READY   STATUS    RESTARTS   AGE
pod/deploy-jenkins-7444f88f6f-89cmf   1/1     Running   0          110s
pod/deploy-jenkins-7444f88f6f-js24q   1/1     Running   0          110s
pod/deploy-jenkins-7444f88f6f-mjgd5   1/1     Running   0          110s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   14m

NAME                             READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/deploy-jenkins   1/3     3            1           8s

NAME                                        DESIRED   CURRENT   READY   AGE
replicaset.apps/deploy-jenkins-7444f88f6f   3         3         1       8s
  • 디플로이먼트로 배포된 포드를 하나 삭제하고 이후 생성되는 포드를 관찰하라
$ kubectl delete pod deploy-jenkins-7444f88f6f-89cmf
pod "deploy-jenkins-7444f88f6f-89cmf" deleted

$ kubectl get pod
NAME                              READY   STATUS    RESTARTS   AGE
deploy-jenkins-7444f88f6f-js24q   1/1     Running   0          3m57s
deploy-jenkins-7444f88f6f-mjgd5   1/1     Running   0          3m57s
deploy-jenkins-7444f88f6f-w6snb   1/1     Running   0          10s

$ kubectl describe rs deploy-jenkins-7444f88f6f
Name:           deploy-jenkins-7444f88f6f
Namespace:      default
Selector:       app=jenkins-test,pod-template-hash=7444f88f6f
Labels:         app=jenkins-test
                pod-template-hash=7444f88f6f
Annotations:    deployment.kubernetes.io/desired-replicas: 3
                deployment.kubernetes.io/max-replicas: 4
                deployment.kubernetes.io/revision: 1
Controlled By:  Deployment/deploy-jenkins
Replicas:       3 current / 3 desired
Pods Status:    3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=jenkins-test
           pod-template-hash=7444f88f6f
  Containers:
   jenkins:
    Image:        jenkins
    Port:         9080/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age    From                   Message
  ----    ------            ----   ----                   -------
  Normal  SuccessfulCreate  5m19s  replicaset-controller  Created pod: deploy-jenkins-7444f88f6f-mjgd5
  Normal  SuccessfulCreate  5m19s  replicaset-controller  Created pod: deploy-jenkins-7444f88f6f-89cmf
  Normal  SuccessfulCreate  5m19s  replicaset-controller  Created pod: deploy-jenkins-7444f88f6f-js24q
  Normal  SuccessfulCreate  92s    replicaset-controller  Created pod: deploy-jenkins-7444f88f6f-w6snb
  • 새로 생성된 포드의 레이블을 바꾸어 Deployment의 관리 영역에서 벗어나게 하라
$ kubectl label pod deploy-jenkins-7444f88f6f-js24q app-
pod/deploy-jenkins-7444f88f6f-js24q labeled

$ kubectl get pod --show-labels
NAME                              READY   STATUS              RESTARTS   AGE     LABELS
deploy-jenkins-7444f88f6f-74zjk   0/1     ContainerCreating   0          4s      app=jenkins-test,pod-template-hash=7444f88f6f
deploy-jenkins-7444f88f6f-js24q   1/1     Running             0          6m28s   pod-template-hash=7444f88f6f
deploy-jenkins-7444f88f6f-mjgd5   1/1     Running             0          6m28s   app=jenkins-test,pod-template-hash=7444f88f6f
deploy-jenkins-7444f88f6f-w6snb   1/1     Running             0          2m41s   app=jenkins-test,pod-template-hash=7444f88f6f
  • Scale 명령을 사용해 레플리카 수를 5개로 정의한다
$ kubectl scale deploy deploy-jenkins --replicas=5
deployment.apps/deploy-jenkins scaled

$ kubectl get pod -l app
NAME                              READY   STATUS    RESTARTS   AGE
deploy-jenkins-7444f88f6f-74zjk   1/1     Running   0          104s
deploy-jenkins-7444f88f6f-khnw4   1/1     Running   0          17s
deploy-jenkins-7444f88f6f-mjgd5   1/1     Running   0          8m8s
deploy-jenkins-7444f88f6f-w6snb   1/1     Running   0          4m21s
deploy-jenkins-7444f88f6f-wk7f8   1/1     Running   0          17s
  • edit 기능을 사용하여 10로 스케일링하라
$ kubectl edit deploy deploy-jenkins
728x90

댓글

💲 추천 글