보람찬 코기의 개발자 블로그
article thumbnail
반응형

필자는 1Master 2Worker의 클러스터를 통하여 실습한 기록물입니다.

 

1. Yaml 작성

<shell />
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: replicas: 3 # 3개의 Pod를 유지하도록 replica를 설정 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:latest ports: - containerPort: 80

2. K8S에 배포하기

<bash />
$ kubectl apply -f nginx-deployment.yaml //yaml파일이 있는 경로에서 실행 //출력 deployment.apps/nginx-deployment created

3.  

4. Pod 조회하기

<bash />
$ kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx-deployment-585449566-7sjzn 1/1 Running 0 14s 192.168.1.108 aicomputing <none> <none> nginx-deployment-585449566-l9snj 1/1 Running 0 14s 192.168.1.183 worker2 <none> <none> nginx-deployment-585449566-tz9pq 1/1 Running 0 14s 192.168.1.109 aicomputing <none> <none> //두 대의 PC를 가지고 클러스터를 구축했기에 레플리카셋을 통한 pod가 스케줄링 되어 두 대의 자원에 할당된 모습이다. //replica에 맞는 3개의 Pod 생성 확인

5. 업데이트를 하는 방법

  • yaml 을 직접 수정하여 kubectl apply -f '파일이름'.yaml 로 업데이트
  • kubectl set image deployment/nginx-deployment nginx=nginx:1.21.3 로 업데이트 (이 커맨드는 nginx의 버전을 바꾸는 것)
  • recreate를 사용하는 방법
<bash />
$ kubectl rollout status deployment/nginx-deployment Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated... Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated... Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated... Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination... Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination... deployment "nginx-deployment" successfully rolled out //파일 업데이트 과정을 실시간으로 모니터링이 가능하다

6. 업데이트를 기록하는 방법

우리는 배포에 있어 업데이트를 진행했지만 무엇이 변경되었는지 log를 남기는 것이 좋은 방향이다

 

<bash />
$ kubectl set image deployment/nginx-deployment nginx=nginx:latest --record deployment.apps/nginx-deployment image updated $ kubectl rollout status deployment/nginx-deployment Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated... Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated... Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated... Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated... Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated... Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated... Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination... Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination... deployment "nginx-deployment" successfully rolled out $ kubectl get replicasets NAME DESIRED CURRENT READY AGE nginx-deployment-585449566 3 3 3 5m3s nginx-deployment-f9cfb46d4 0 0 0 4m //--record를 사용하면 이전 레플리카셋이 보존되고 언제든지 되돌릴수 있는 롤백기능을 사용할 수 있다 $ kubectl rollout history deployment nginx-deployment deployment.apps/nginx-deployment REVISION CHANGE-CAUSE 2 <none> 3 kubectl set image deployment/nginx-deployment nginx=nginx:latest --record=true //2번은 --record 옵션 없이 롤링업데이트를 한결과 <none>으로 남겨진체 리버전이 갱신되었고, //3번은 옵션이 적용되어 기록이 남아있다. $ kubectl rollout undo deployment nginx-deployment --to-revision=2 을 하면 언제든지 롤백이 가능하다(레플리카셋이 보존이 된 경우만 해당)

 

7. Rolling Update의 동작 과정 (무중단 업데이트)

  1. v2 버전의 ReplicaSet을 생성한다. 이때 replicas는 (maxSurge + maxUnavailable)이 되고 해당 수치 만큼 Pod를 생성한다.
  2. v1 버전의 ReplicaSet의 replicas가 (replicas - maxUnavailable)로 변경되고, 해당 수치 만큼 Pod를 제거한다.
  3. 배포가 진행되는 동안 v1 버전과 v2 버전에 트래픽이 분산된다.
  4. v2 버전의 replicas를 1 증가시키고, v1 버전의 replicas를 1 감소시킨다. (v2 버전의 replicas가 템플릿에 정의된 replicas와 일치할때까지 반복)
  5. v1 버전의 replicas를 0으로 변경한 후 남은 Pod를 삭제한다.

https://yoo11052.tistory.com/212?category=1033876

장점: 지속적인 서비스

단점: 큰 단위의 업데이트라면 일시적이지만 두개를 운영해야하기 때문에 리소스 및 용량 오버

8. Recreat 의 동작과정 (일시 중단 업데이트)

  1. v1 버전의 ReplicaSet의 replicas를 0으로 변경한다.
  2. v1 버전의 Pod가 제거 된다.
  3. v2 버전의 ReplicaSet이 생성된다.
  4. v2 버전의 Pod가 생성된다.

https://yoo11052.tistory.com/212?category=1033876

장점: 업데이트의 경우 요구되는 리소스량 증가랑 무관하여 기존 환경으로 업데이트

단점: 일시적 서비스 중단

 

 

반응형
profile

보람찬 코기의 개발자 블로그

@BoChan

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!