ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Kubernetes Deployment 알아보기
    Kubernetes 2024. 1. 9. 17:27
    728x90
    반응형

    - 목차

     

    함께 보면 좋은 글.

    https://westlife0615.tistory.com/412

     

    Kubernetes ReplicaSet 알아보기

    - 목차. 함께보면 좋은 글. https://westlife0615.tistory.com/210 Kubernetes Pod 이해하기 소개 Pod는 쿠버네티스의 가장 기본적인 구성요소입니다. 컨테이너 환경을 오케스트레이션하는 쿠버네티스 입장에서

    westlife0615.tistory.com

    https://westlife0615.tistory.com/407

     

    KinD 알아보기.

    - 목차 소개. KinD 는 "카인드" 라고 발음되며, 로컬 머신에서 쿠버네티스를 간편하게 실행할 수 있는 도구입니다. KinD 는 Kubernetes in Docker 의 약자이구요. Docker 를 활용하여 쿠버네티스 환경을 구성

    westlife0615.tistory.com

    소개.

    Deployment 은 ReplicaSet 을 관리하는 쿠버네티스 리소스입니다.

    간단하게 ReplicaSet 에 대해서 먼저 말씀드리면,

    ReplicaSet 은 쿠버네티스의 실질적인 실행 단위인 Pod 의 복제 갯수를 관리합니다.

    아래 이미지는 대략적인 Deployment - ReplicaSet - Pod 의 관계도인데요.

    아래 이미지의 경우라고 하면 ReplicaSet 1 와 ReplicaSet 2 의 replicas 는 모두 4로 설정됩니다.

    그래서 각 ReplicaSet 1, 2 는 4개의 Pod 를 유지합니다.

    하나의 Pod 가 종료되더라도 ReplicaSet 의 Reconsile Loop 에 의해서 결과적으로 4개를 유지하게 됩니다.

     

     

    그럼 Deployment 어떤 역할을 담당하게 될까요 ?

    Deployment 는 이름 그대로 새로운 Pod 들의 배포를 관리하게 됩니다.

    version 1 에서 version 2 로 업데이트를 해야하는 경우에 Deployment 를 변경하여 새로운 Pod v2 를 생성하게 합니다.

    이 과정에서 무중단 배포라는 성격의 배포를 수행합니다.

    그래서 새로운 ReplicaSet 인 ReplicaSet 2 를 생성하게 되고,

    ReplicaSet 2 이 안정화된다면 ReplicaSet 1 를 제거합니다.

    Version 1 과 Version 2 가 공존하게 하면서, Running 상태의 Pod 가 최소 1개 이상 구동되도록 유지합니다.

     

    즉, Deployment 는 ReplicaSet 를 관리하는 쿠버네티스의 리소스입니다.

     

     

    Deployment 생성하기.

    아래 yaml 파일은 nginx 이미지를 기반으로 생성한 Deployment yaml 파일입니다.

    cat <<EOF> /tmp/nginx-deployment.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-deployment
    spec:
      selector:
        matchLabels:
          app: nginx
      replicas: 2 # tells deployment to run 2 pods matching the template
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginx
            image: nginx:1.14.3
            ports:
            - containerPort: 80
            readinessProbe:
              httpGet:
                path: /
                port: 80
              initialDelaySeconds: 5
              periodSeconds: 5
            livenessProbe:
              httpGet:
                path: /
                port: 80
              initialDelaySeconds: 10
              periodSeconds: 10
    EOF
    kubectl apply -f /tmp/nginx-deployment.yaml

     

    apiVersion ?

    Deployment yaml 의 apiVersion 은 쿠버네티스 API Server 에게 어떤 API Group 과 버전을 사용하는지에 대한 명시입니다.

    "apps" API Group Version 1 를 사용한다는 명시이구요.

    쿠버네티스의 대표적인 리소스들이 apps/v1 에 속합니다.

    Deployment, ReplicaSet, StatefulSet, DaemonSet 의 apiVersion 은 "apps/v1" 을 사용하죠.

     

    metadata.name ?

    metadata.name 은 Deployment 의 이름을 설정하는 Key 입니다.

    아래와 같이 생성된 Deployment 를 kubectl 로 확인하게 되면 metadata.name 의 값이 Deployment 의 이름으로 설정된 모습을 확인할 수 있습니다.

    kubectl get deployment
    NAME               READY   UP-TO-DATE   AVAILABLE   AGE
    nginx-deployment   0/2     2            0           5s

     

     

    그리고 Deployment 는 ReplicaSet 를 관리한다고 말씀드렸죠?

    ReplicaSet 과 Pod 또한 Deployment 의 이름을 prefix 로 사용하여 자신들의 이름을 생성합니다.

    kubectl get replicaset
    NAME                          DESIRED   CURRENT   READY   AGE
    nginx-deployment-6595874d85   2         2         2       10s
    
    kubectl get pod       
    NAME                                READY   STATUS    RESTARTS   AGE
    nginx-deployment-6595874d85-57czk   1/1     Running   0          12s
    nginx-deployment-6595874d85-hkzz2   1/1     Running   0          12s

     

    spec.selector.matchLabels ?

    spec 에 관련한 설정값은 Deployment 를 제어하는 실질적인 설정입니다.

    그 중에서 spec.selector.matchLabels 는 관리해야하는 Pod 들의 Label 을 지칭합니다.

     

    만약 존재하지 않는 label 을 지칭하게 된다면 아래와 같은 경고글이 출력됩니다.

    The Deployment "nginx-deployment" is invalid: spec.template.metadata.labels: Invalid value: map[string]string{"app":"nginx"}: `selector` does not match template `labels`

     

     

    spec.replicas ?

    spec.replicas 는 Deployment 가 관리하는 ReplicaSet 의 replicas 설정입니다.

    Deployment 는 ReplicaSet 를 생성하고 관리하게 됩니다.

    Deployment 에 의해서 생성된 ReplicaSet 이 관리하게 될 Pod 의 수를 spec.replicas 을 통해서 설정합니다.

     

    spec.template ?

    spec.template 하위의 내용은 ReplicaSet 이 관리할 Pod 에 대한 설정입니다.

    어떤 이미지를 사용할 것인지, 생성된 컨테이너의 CPU, Memory, Network 등의 설정을 할 수 있으며,

    metadata 로써 label 을 지정할 수 있습니다.

     

     

    새로운 이미지로 변경하기.

    일반적으로 Container 가 사용할 이미지를 변경하는 것이 Deployment 의 큰 역할입니다.

    이 과정에서 새로운 ReplicaSet 과 기존의 ReplicaSet 를 컨트롤하면서 무중단 배포를 적용합니다.

     

    아래와 같이 새로운 이미지를 적용합니다.

    nginx:1.14.2 --> nginx:1.14.1 로 이미지를 변경하였습니다.

    cat <<EOF> /tmp/nginx-deployment.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-deployment
    spec:
      selector:
        matchLabels:
          app: nginx
      replicas: 2 # tells deployment to run 2 pods matching the template
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginx
            image: nginx:1.14.1
            ports:
            - containerPort: 80
    EOF

     

     

    아래에서 볼 수 있듯이, 새로운 ReplicaSet 이 생성되며,

    kubectl get replicaset
    NAME                          DESIRED   CURRENT   READY   AGE
    nginx-deployment-6595874d85   2         2         2       13m
    nginx-deployment-97964f88     1         1         0       8s

     

    Pod 또한 생성과 삭제 과정이 진행됩니다.

    nginx-deployment-6595874d85-57czk   1/1     Running             0          13m
    nginx-deployment-97964f88-mhq24     0/1     ContainerCreating   0          1s
    nginx-deployment-97964f88-qcc4l     1/1     Running             0          12s

     

    Deployment 의 배포 과정은 아래와 같습니다.

     

     

     

    Rollback.

    새로운 버전의 ReplicaSet 이 문제가 발생하는 경우가 있습니다.

    저희 경우에는 존재하지 않는 Image 를 설정하여 "Error: ErrImagePull" 상태를 만들어보았는데요.

     

    아래의 명령어를 통해서 Deployment 변경 사항을 취소할 수 있습니다.

    kubectl rollout undo deployment nginx-deployment

     

     

     

    반응형
Designed by Tistory.