Controller란?
Pod 의 개수를 보장
Replication Controller (가장 BASIC한 Controller)
- 요구하는 Pod의 개수를 보장하며 파드 집합의 실행을 항상 안정적으로 유지하는 것을 목표
- 요구하는 Pod의 개수가 부족하면 template을 이용해 Pod cnrk
- 요구하는 Pod 수보다 많으면 최근에 생성된 Pod 삭제
- 기본 구성 : selector를 replicas 갯수만큼 만들려고 보장해준다
- selector
- replicas
- template
- 구식의 기술로 최근에는 Replica Set으로 대체되고 있다.
# 실습
[master ~]$ cat > rc-nginx.yaml
apiVersion: v1
kind: ReplicationController
metadata: # ReplicationController의 metadata
name: rc-nginx # 컨테이너 이름
spec:
replicas: 3
selector:
app: webui
template:
metadata: # pod의 metadata
name: nginx-pod
labels:
app: webui
spec:
containers:
- name: nginx-container
image: nginx:1.14
[master ~]$ kubectl create -f rc-nginx.yaml
replicationcontroller/rc-nginx created
[master ~]$ kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
rc-nginx-4zzw6 1/1 Running 0 14s 10.5.1.4 node2 <none> <none>
rc-nginx-qkdvx 1/1 Running 0 14s 10.5.1.5 node2 <none> <none>
rc-nginx-rn4dp 1/1 Running 0 14s 10.5.1.3 node2 <none> <none>
# 해시값으로 random한 이름의 Pod가 생성된다.
# RC 확인
[master ~]$ kubectl get replicationcontrollers
NAME DESIRED CURRENT READY AGE
rc-nginx 3 3 3 4m32s
[master ~]$ kubectl get rc
NAME DESIRED CURRENT READY AGE
rc-nginx 3 3 3 12m
# 좀 더 자세히 확인
[master ~]$ kubectl describe rc rc-nginx
Name: rc-nginx
Namespace: default
Selector: app=webui
Labels: app=webui
Annotations: <none>
Replicas: 3 current / 3 desired
Pods Status: 3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
Labels: app=webui
Containers:
nginx-container:
Image: nginx:1.14
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 12m replication-controller Created pod: rc-nginx-rn4dp
Normal SuccessfulCreate 12m replication-controller Created pod: rc-nginx-4zzw6
Normal SuccessfulCreate 12m replication-controller Created pod: rc-nginx-qkdvx
# Yaml 로 만들어보기
[master ~]$ kubectl run redis --image=redis --labels=app=webui --dry-run
W0219 08:38:04.821497 17456 helpers.go:692] --dry-run is deprecated and can be replaced with --dry-run=client.
pod/redis created (dry run)
[master ~]$ kubectl run redis --image=redis --labels=app=webui --dry-run -o yaml > redis.yaml
W0219 08:40:17.236640 17703 helpers.go:692] --dry-run is deprecated and can be replaced with --dry-run=client.
[master ~]$ cat redis.yaml
apiVersion: v1
kind: Pod
metadata:
labels: # 단지 여기만 추가되는 것!
app: webui
name: redis
spec:
containers:
- image: redis
name: redis
[master ~]$ kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
rc-nginx-4zzw6 1/1 Running 0 27m app=webui
rc-nginx-qkdvx 1/1 Running 0 27m app=webui
rc-nginx-rn4dp 1/1 Running 0 27m app=webui
# Scale-out
- Kubenetes는 Horizontal Scale-out을 해준다.
[master ~]$ kubectl edit rc rc-nginx
replicationcontroller/rc-nginx edited
[master ~]$ kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
rc-nginx-4zzw6 1/1 Running 0 65m app=webui
rc-nginx-qkdvx 1/1 Running 0 65m app=webui
rc-nginx-rn4dp 1/1 Running 0 65m app=webui
rc-nginx-svjqk 1/1 Running 0 4s app=webui
밑에 Template 영역은 Pod Template이다.
👉image: nginx:1.15 로 바꿔주면 변화가 있는 가? No!
Controller는 selector만 본다. 이미 app=webui가 이미 2개가 동작중이기 때문에 아무런 영향이 없다.
# 삭제하고 다시 확인해보기
[master ~]$ kubectl delete pod rc-nginx-4zzw6
pod "rc-nginx-4zzw6" deleted
[master ~]$ kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
rc-nginx-qkdvx 1/1 Running 0 75m app=webui
rc-nginx-svwxv 1/1 Running 0 12s app=webui
[master ~]$ kubectl describe pod rc-nginx-svwxv
Name: rc-nginx-svwxv
Namespace: default
Priority: 0
Service Account: default
Node: node2/192.168.0.12
Start Time: Mon, 19 Feb 2024 09:38:07 +0000
Labels: app=webui
Annotations: <none>
Status: Running
IP: 10.5.1.7
IPs:
IP: 10.5.1.7
Controlled By: ReplicationController/rc-nginx
Containers:
nginx-container:
Container ID: containerd://abf400960952b3251d7645d9c81ea0310e130719f62ef17ac345b31381cd7a78
Image: nginx:1.15 # 1.15버전 된 것 확인가능
Image ID: docker.io/library/nginx@sha256:23b4dcdf0d34d4a129755fc6f52e1c6e23bb34ea011b315d87e193033bcd1b68
Port: <none>
Host Port: <none>
State: Running
Started: Mon, 19 Feb 2024 09:38:13 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-rt4ng (ro)
# 서비스 동작 중에 1.14 -> 1.15 버전으로 바뀌었다 = 롤링 업데이트 (비즈니스 연속성)
# 명령어로 scale-down
[master ~]$ kubectl scale rc rc-nginx --replicas=2
replicationcontroller/rc-nginx scaled
[master ~]$ kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
rc-nginx-4zzw6 1/1 Running 0 68m app=webui
rc-nginx-qkdvx 1/1 Running 0 68m app=webui
반응형
'Container > Kubernetes' 카테고리의 다른 글
[K8S] Deployment - RollingUpdate (0) | 2024.02.21 |
---|---|
[K8S] ReplicaSet (0) | 2024.02.21 |
[K8S] Pod 환경 변수 설정 (0) | 2024.02.19 |
[K8S] Pod Resource 할당 (0) | 2024.02.19 |
[K8S] pause container / Static container (0) | 2024.02.19 |