컨트롤러는 특정 자원 또는 컴포넌트를 추적하며, 추적의 대상이 정상적으로 작동할 수 있도록 다양한 작업을 처리해주는 컴포넌트를 의미한다.
대표적으로, 아래와 같은 작업들을 컨트롤러가 수행한다.
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: replication-set-1
spec:
replicas: 1 # 구동하고 싶은 Pod의 수
selector:
matchLabels:
type: web
template:
metadata:
name: pod-1
labels:
type: web
spec:
containers:
- name: container-1
image: kai1114/hello
terminationGracePeriodSeconds: 0
ReplicaSet은 replicas에 지정된 수의 Pod가 항상 실행되도록 보장해주는 컨트롤러이다.
ReplicaSet에 의해서 관리 및 생성될 Pod에 대한 스펙은 template에 작성해준다.
Deployment는 운영 중인 서비스를 재배포할 때 도움을 주는 컴포넌트이다.
ReplicaSet의 버전을 관리해주는 역할도 한다.
Deployment는 다양한 배포 방식을 재공하는데, 각각 살펴보자.
기존에 존재하던 Pod들을 삭제하고, 업데이트할 버전의 Pod들을 새롭게 만드는 방식이다. 직관적이고, 빠르게 업데이트가 가능하지만 다운타임이 발생한다는 특징이 있다. 그래서 Dev 환경과 같은 다운 타임이 발생해도 괜찮은 상황에서 사용하기에 적합하다.
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-1
spec:
selector:
matchLabels:
type: app
replicas: 2
strategy:
type: Recreate
revisionHistoryLimit: 1
template:
metadata:
labels:
type: app
spec:
containers:
- name: container-1
image: kai1114/hello
terminationGracePeriodSeconds: 10
ReCreate방식으로 Deployment를 생성하기 위해서는 위와 같이 yml을 작성할 수 있다.
재배포를 위해서는 이미지의 버전을 조정하면 된다.
Rolling update는 동작하고 있는 N개의 Pod 중, 하나씩 업데이트할 버전의 Pod로 교체하는 형태로 점진적으로 배포하는 방식이다.
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-2
spec:
selector:
matchLabels:
type: app
replicas: 2
strategy:
type: RollingUpdate
minReadySeconds: 10
template:
metadata:
labels:
type: app
spec:
containers:
- name: container-2
image: kai1114/hello
terminationGracePeriodSeconds: 10
Rolling update 방식을 사용하려면, 위와 같이 yml을 작성하면 된다.
k8s에서 기본적으로 제공하는 Blue/Green 배포 옵션은 없고, ReplicaSet으로 구현할 수 있다.
기존에 돌고 있는 ReplicaSet을 Blue라고 생각하면, 새롭게 업데이트할 버전의 이미지가 명시된 ReplicaSet이 Green인 것이다.
트래픽은 ReplicaSet 앞 단에 붙일 Service를 통할 것이기 때문에 Green ReplicaSet이 배포가 완료되면, Service에서 Blue ReplicaSet이 아닌 Green ReplicaSet을 바라보도록 하면 Blue/Green 배포를 구현할 수 있다.
apiVersion: apps/v1
kind: Deployment
metadata:
name: replica-blue
spec:
replicas: 2
selector:
matchLabels:
ver: v1 # 라벨로 버전을 구분해서 Blue/Green 배포 구현
template:
metadata:
labels:
ver: v1
spec:
containers:
- name: container-3
image: kai1114/hello:v1
apiVersion: v1
kind: Service
metadata:
name: service-1
spec:
selector:
ver: v1 # "ver: v1" 라벨이 붙은 ReplicaSet을 바라본다.
ports:
- port: 8000
targetPort: 8000
protocol: TCP
위와 같이 ReplicaSet과 ReplicaSet을 바라보는 Service를 만들어준다.
apiVersion: apps/v1
kind: Deployment
metadata:
name: replica-green
spec:
replicas: 2
selector:
matchLabels:
ver: v2
template:
metadata:
labels:
ver: v2
spec:
containers:
- name: container-3
image: kai1114/hello:v2
그리고 새로운 버전(v2)를 배포하고자 한다면, 위와 같이 ReplicaSet을 만들고

Service의 selector를 v2로 변경하여 Service에서 바라보는 ReplicaSet을 변경하면, Blue/Green 배포가 가능해진다.