안녕하세요 이웃님들 ^.^
좋은 아침 입니다.
오늘은 Kubernetis 컨트롤러 중 하나인 Deployment의 사용 방법에 대해서 알아 보겠습니다.
Deployment는 Pod를 생성하기 위한 상위 개념의 Workload resource이며, 아래 그림과 같이 ReplicaSet과 Pod에 대한 선언적 업데이트를 제공합니다. 선언적 업데이트란 말은 Pod의 버전을 업데이트 하거나 할때 유용한 기능을 제공을 합니다. 업데이트의 방법은 아래와 같은 종류가 있습니다.
- RollingUpdate : 업데이트 시 Pod들을 정해진 규칙에 따라 순차적으로 생성/삭제
- Recreate: 업데이트 시 기존의 Pod를이 모두 삭제되고 새로운 Pod들이 생성됨
그리고, RollingUpdate시에는 다음과 같은 정보를 이용하여 프로세스를 제어할 수 있습니다.
- Max Unavailable : 업데이트 프로세스 중에 사용할 수 없는 최대 파드의 수를 지정 (Default : 25%)
- Max Surge : 의도한 파드의 수에 대해 (추가로) 생성할 수 있는 최대 파드의 수 (Default : 25%)
그럼 이제 Deployment를 생성하고 삭제 하고 변경 하는 방법을 알아보겠습니다.
1. Deployment yaml 파일을 아래와 같이 vi를 통해 생성을 해보겠습니다.
ubuntu@ip-172-31-31-84:~/dockertext2/chap07$ vi test-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx-deployment
labels:
app: my-nginx
tier: frontend
spec:
replicas: 3
strategy:
type: RollingUpdate
selector:
matchLabels:
app: my-nginx
template:
metadata:
labels:
app: my-nginx
spec:
containers:
- image: nginx:1.19.3
name: my-nginx
ports:
- containerPort: 80
위에서 Pod의 수는 3개를 유지하고, 업데이트 방식은 RollingUpdate로 설정을 하였습니다.
2.아래와 같이 생성을 하고 조회를 해보겠습니다
ubuntu@ip-172-31-31-84:~/dockertext2/chap07$ kubectl apply -f test-deployment.yaml
deployment.apps/my-nginx-deployment created
ubuntu@ip-172-31-31-84:~/dockertext2/chap07$ kubectl get all
NAME READY STATUS RESTARTS AGE
pod/my-nginx-deployment-55985c7fcf-hqsvp 1/1 Running 0 9s
pod/my-nginx-deployment-55985c7fcf-mj2vb 1/1 Running 0 10s
pod/my-nginx-deployment-55985c7fcf-pg9vl 1/1 Running 0 8s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/my-nginx-deployment 3/3 3 3 10s
NAME DESIRED CURRENT READY AGE
replicaset.apps/my-nginx-deployment-55985c7fcf 3 3 3 10s
위에서 보듯이 deployment를 생성을 하면 deplyment에 설정이 되어 있는 replicaset과 pod들이 위 처럼 자동 생성이 됩니다.
3. Pod들의 Lebel을 조회를 해보면 아래와 같습니다. Deployment에 설정된 Lable로 모두 생성된 것을 확인 할수 있습니다.
ubuntu@ip-172-31-31-84:~/dockertext2/chap07$ kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
my-nginx-deployment-55985c7fcf-hqsvp 1/1 Running 0 3m8s app=my-nginx,pod-template-hash=55985c7fcf
my-nginx-deployment-55985c7fcf-mj2vb 1/1 Running 0 3m9s app=my-nginx,pod-template-hash=55985c7fcf
my-nginx-deployment-55985c7fcf-pg9vl 1/1 Running 0 3m7s app=my-nginx,pod-template-hash=55985c7fcf
4. 생성된 Pod중 하나를 삭제를 하고 다시 조회를 합니다.
ubuntu@ip-172-31-31-84:~/dockertext2/chap07$ kubectl get pod
NAME READY STATUS RESTARTS AGE
my-nginx-deployment-55985c7fcf-hqsvp 1/1 Running 0 8m16s
my-nginx-deployment-55985c7fcf-mj2vb 1/1 Running 0 8m17s
my-nginx-deployment-55985c7fcf-pg9vl 1/1 Running 0 8m15s
ubuntu@ip-172-31-31-84:~/dockertext2/chap07$
ubuntu@ip-172-31-31-84:~/dockertext2/chap07$ kubectl delete pod my-nginx-deployment-55985c7fcf-hqsvp
pod "my-nginx-deployment-55985c7fcf-hqsvp" deleted
ubuntu@ip-172-31-31-84:~/dockertext2/chap07$
ubuntu@ip-172-31-31-84:~/dockertext2/chap07$
ubuntu@ip-172-31-31-84:~/dockertext2/chap07$ kubectl get pod
NAME READY STATUS RESTARTS AGE
my-nginx-deployment-55985c7fcf-h4r2j 1/1 Running 0 3s
my-nginx-deployment-55985c7fcf-mj2vb 1/1 Running 0 8m33s
my-nginx-deployment-55985c7fcf-pg9vl 1/1 Running 0 8m31s
hqsvp 레이블을 가지고 있는 Pod를 삭제를 하면 h4r2j라는 신규 Pod가 생겨 나는것을 확인 할수 있습니다.
다시 말해 Replicaset이 제대로 동작을 하고 있다는 애기지요 ^.^
5. 그럼 이제 명령어로 replicaset의 수량을 변경해 보겠습니다.
아래 명령을 사용하여 replicaset=4로 변경을 합니다. 다시 말해 기존 Pod 3개에서 4개로 변경을 하는 과정 입니다.
ubuntu@ip-172-31-31-84:~/dockertext2/chap07$ kubectl scale deployment my-nginx-deployment --replicas=4
deployment.apps/my-nginx-deployment scaled
ubuntu@ip-172-31-31-84:~/dockertext2/chap07$
ubuntu@ip-172-31-31-84:~/dockertext2/chap07$ kubectl get pod
NAME READY STATUS RESTARTS AGE
my-nginx-deployment-55985c7fcf-h4r2j 1/1 Running 0 3m2s
my-nginx-deployment-55985c7fcf-l4ctq 1/1 Running 0 5s
my-nginx-deployment-55985c7fcf-mj2vb 1/1 Running 0 11m
my-nginx-deployment-55985c7fcf-pg9vl 1/1 Running 0 11m
위에서 보듯이 신규 Pod가 하나더 생긴걸 확인 할수 있습니다. 이 방법은 YAML을 수정하고 다시 apply를 하는 과정과 동일 합니다. 그리고 kubectl edit명령으로 수정이 가능합니다.
아래 명령으로 모든 리소스를 조회를 해보면 replica 숫자가 같이 변경된 것을 확인할수 있습니다.
ubuntu@ip-172-31-31-84:~/dockertext2/chap07$ kubectl get all
NAME READY STATUS RESTARTS AGE
pod/my-nginx-deployment-55985c7fcf-h4r2j 1/1 Running 0 4m40s
pod/my-nginx-deployment-55985c7fcf-l4ctq 1/1 Running 0 103s
pod/my-nginx-deployment-55985c7fcf-mj2vb 1/1 Running 0 13m
pod/my-nginx-deployment-55985c7fcf-pg9vl 1/1 Running 0 13m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 22h
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/my-nginx-deployment 4/4 4 4 13m
NAME DESIRED CURRENT READY AGE
replicaset.apps/my-nginx-deployment-55985c7fcf 4 4 4 13m
replicaset.apps/nginx-replicaset 0 0 0 29m
6. 그럼 이제 생성된 Deployment를 삭제를 해보겠습니다.
ubuntu@ip-172-31-31-84:~/dockertext2/chap07$ kubectl delete deployments my-nginx-deployment
deployment.apps "my-nginx-deployment" deleted
ubuntu@ip-172-31-31-84:~/dockertext2/chap07$
ubuntu@ip-172-31-31-84:~/dockertext2/chap07$
ubuntu@ip-172-31-31-84:~/dockertext2/chap07$ kubectl get all
'쿠버네티스' 카테고리의 다른 글
[쿠버네티스] Kubernetis Volume 사용하기 (128) | 2024.03.19 |
---|---|
[쿠버네티스] Replicaset을 이용해 Pod 관리 하기 (64) | 2024.03.17 |
[쿠버네티스] Kubernetis Pod 구동 하는 방법 (62) | 2024.03.13 |
[쿠버네티스] 쿠버네티스 서비스 객체 사용 하기 (143) | 2024.03.07 |
[쿠버네티스] curl로 API 접근하기 (31) | 2024.03.05 |