디플로이먼트로 무상태 애플리케이션 실행하기
디플로이먼트로 무상태 애플리케이션 실행하기
쿠버네티스에서 애플리케이션을 띄울 때 가장 기본적인 방법은 Deployment 오브젝트를 사용하는 거예요. 이 페이지에서는 Deployment를 만들고, kubectl로 정보를 조회하고, 이미지를 바꿔가며 업데이트하고, 레플리카 수를 조절하고, 마지막에 삭제하는 흐름을 하나씩 따라가 볼게요.
출처: Run a Stateless Application Using a Deployment — Kubernetes 공식 문서
본문
이 튜토리얼의 목표
- nginx 디플로이먼트를 만들어요.
kubectl로 디플로이먼트 정보를 조회해요.- 디플로이먼트를 업데이트해요.
nginx 디플로이먼트 만들고 살펴보기
Kubernetes Deployment 오브젝트를 만들면 애플리케이션을 실행할 수 있어요. Deployment는 YAML 파일로 기술하는데, 아래 YAML은 nginx:1.14.2 도커 이미지를 실행하는 Deployment를 나타내요.
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.2
ports:
- containerPort: 80
-
YAML 파일을 바탕으로 Deployment를 만들어요.
kubectl apply -f https://k8s.io/examples/application/deployment.yaml -
Deployment 정보를 확인해요.
kubectl describe deployment nginx-deployment출력은 대략 이렇게 나와요.
Name: nginx-deployment Namespace: default CreationTimestamp: Tue, 30 Aug 2016 18:11:37 -0700 Labels: app=nginx Annotations: deployment.kubernetes.io/revision=1 Selector: app=nginx Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable StrategyType: RollingUpdate MinReadySeconds: 0 RollingUpdateStrategy: 1 max unavailable, 1 max surge Pod Template: Labels: app=nginx Containers: nginx: Image: nginx:1.14.2 Port: 80/TCP Environment: <none> Mounts: <none> Volumes: <none> Conditions: Type Status Reason ---- ------ ------ Available True MinimumReplicasAvailable Progressing True NewReplicaSetAvailable OldReplicaSets: <none> NewReplicaSet: nginx-deployment-1771418926 (2/2 replicas created) No events. -
디플로이먼트가 만든 Pod를 조회해요.
kubectl get pods -l app=nginx출력은 대략 이렇게 나와요.
NAME READY STATUS RESTARTS AGE nginx-deployment-1771418926-7o5ns 1/1 Running 0 16h nginx-deployment-1771418926-r18az 1/1 Running 0 16h -
Pod 하나의 상세 정보를 확인해요.
kubectl describe pod <pod-name>여기서
<pod-name>은 위에서 만든 Pod 중 하나의 이름이에요.
디플로이먼트 업데이트하기
새 YAML 파일을 적용하면 디플로이먼트를 업데이트할 수 있어요. 아래 YAML은 nginx 이미지를 1.16.1로 업데이트하도록 지정한 거예요.
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 2
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.16.1 # Update the version of nginx from 1.14.2 to 1.16.1
ports:
- containerPort: 80
-
새 YAML 파일을 적용해요.
kubectl apply -f https://k8s.io/examples/application/deployment-update.yaml -
디플로이먼트가 새 이름의 Pod를 만들고 이전 Pod를 지우는 과정을 지켜봐요.
kubectl get pods -l app=nginx
레플리카 수를 늘려 애플리케이션 확장하기
새 YAML 파일을 적용하면 Deployment의 Pod 수를 늘릴 수 있어요. 아래 YAML은 replicas를 4로 설정해서 Deployment가 Pod 4개를 갖도록 지정해요.
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 4 # Update the replicas from 2 to 4
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.16.1
ports:
- containerPort: 80
-
새 YAML 파일을 적용해요.
kubectl apply -f https://k8s.io/examples/application/deployment-scale.yaml -
Deployment에 Pod가 4개 있는지 확인해요.
kubectl get pods -l app=nginx출력은 대략 이렇게 나와요.
NAME READY STATUS RESTARTS AGE nginx-deployment-148880595-4zdqq 1/1 Running 0 25s nginx-deployment-148880595-6zgi1 1/1 Running 0 25s nginx-deployment-148880595-fxcez 1/1 Running 0 2m nginx-deployment-148880595-rwovn 1/1 Running 0 2m
축소를 포함해 더 자세한 확장 절차는 Scale a Deployment Manually 문서를 참고하세요.
디플로이먼트 삭제하기
이름으로 디플로이먼트를 삭제할 수 있어요.
kubectl delete deployment nginx-deployment
ReplicationController — 옛날 방식
복제된 애플리케이션을 만들 때 권장하는 방법은 Deployment를 쓰는 거예요. Deployment는 내부적으로 ReplicaSet을 사용하죠. Deployment와 ReplicaSet이 쿠버네티스에 추가되기 전에는 복제 애플리케이션을 ReplicationController로 구성했어요.
더 알아보기
- Deployment 객체에 대해 더 알아보기
- 다운타임 없이 Deployment 업데이트하기