본문 바로가기

Kubernetes

[Kubernetes] ReplicationController란?

반응형

Controller란

 - Pod의 개수를 보장

Controller 종류

 ReplicationController

  - 요구하는 Pod의 개수를 보장하며 파드 집합의 실행을 항상 안정적으로 유지하는 것을 목표

    . 요구하는 Pod의 개수가 부족하면 template를 이용해 Pod를 추가

    . 요구하는 Pod 수 보다 많으면 최근에 생성된 Pod를 삭제

  - 기본구성

    . selector

    . replicas

    . template

 

apiVersoin: v1
kind: ReplicationController
metadata:
  name: <RC_이름>
spec:
  replicas: <배포갯수>
  selector:
    key: value
  template:
    <컨테이너 템플릿>

※ Pod 템플릿은 selector의 키: value label을 포함해야 한다.

 

apiVersion: v1
kind: ReplicationController
metadata:
  name: rc-nginx
spec:
  replicas: 3
  selector:
    app: webui
  template:
    metadata:
      name: nginx-pod
      labels:
        app: webui
    spec:
      containers:
      - name: nginx-container
        image: nginx:1.14

 

rc-nginx.yaml를 통해 ReplicationController pod 실행

# kubectl create -f rc-nginx.yaml
replicationcontroller/rc-nginx created

kubectl get pods -o wide
NAME             READY   STATUS    RESTARTS   AGE   IP          NODE                NOMINATED NODE   READINESS GATES
rc-nginx-s4gmk   1/1     Running   0          95s   10.36.0.1   node1.example.com   <none>           <none>
rc-nginx-xpqtw   1/1     Running   0          95s   10.36.0.2   node1.example.com   <none>           <none>
rc-nginx-zgsst   1/1     Running   0          95s   10.44.0.1   node2.example.com   <none>           <none>

kubectl get replicationcontrollers
NAME       DESIRED   CURRENT   READY   AGE
rc-nginx   3         3         3       2m2s

kubectl get rc
NAME       DESIRED   CURRENT   READY   AGE
rc-nginx   3         3         3       2m26s

 

selector: app=webui 를 가진 3개의 pod 생성

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  2m40s  replication-controller  Created pod: rc-nginx-zgsst
  Normal  SuccessfulCreate  2m40s  replication-controller  Created pod: rc-nginx-xpqtw
  Normal  SuccessfulCreate  2m40s  replication-controller  Created pod: rc-nginx-s4gmk

 

3개의 pod를 구성중인 상태에서 app=webui 로 설정된 redis.yaml을 구성하여 실행

kubectl run redis --image=redis --dry-run
W1205 22:18:21.207618    5358 helpers.go:663] --dry-run is deprecated and can be replaced with --dry-run=client.
pod/redis created (dry run)

kubectl run redis --image=redis --labels=app=webui --dry-run 
W1205 22:18:59.078405    5548 helpers.go:663] --dry-run is deprecated and can be replaced with --dry-run=client.
pod/redis created (dry run)

kubectl run redis --image=redis --labels=app=webui --dry-run -o yaml > redis.yaml
W1205 22:19:29.933593    5674 helpers.go:663] --dry-run is deprecated and can be replaced with --dry-run=client.

 

아래와 같이 yaml 파일 수정

redis.yaml
apiVersion: v1
kind: Pod
metadata:
  labels:
    app: webui
  name: redis
spec:
  containers:
  - image: redis
    name: redis

 

redis.yaml 실행 후 pod 정보를 보면 replicas 가 3개로 설정이 되었기 때문에 pod가 생성되지 않음

kubectl get pod --show-labels 
NAME             READY   STATUS    RESTARTS   AGE    LABELS
rc-nginx-s4gmk   1/1     Running   0          6m6s   app=webui
rc-nginx-xpqtw   1/1     Running   0          6m6s   app=webui
rc-nginx-zgsst   1/1     Running   0          6m6s   app=webui

kubectl create -f redis.yaml 
pod/redis created

 kubectl get pods -o wide
NAME             READY   STATUS        RESTARTS   AGE     IP          NODE                NOMINATED NODE   READINESS GATES
rc-nginx-s4gmk   1/1     Running       0          7m13s   10.36.0.1   node1.example.com   <none>           <none>
rc-nginx-xpqtw   1/1     Running       0          7m13s   10.36.0.2   node1.example.com   <none>           <none>
rc-nginx-zgsst   1/1     Running       0          7m13s   10.44.0.1   node2.example.com   <none>           <none>
redis            0/1     Terminating   0          3s      <none>      node2.example.com   <none>           <none>

 

edit 에서 replicas 를 3 → 4개로 수정

kubectl edit rc rc-nginx

 Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
kind: ReplicationController
metadata:
  creationTimestamp: "2022-12-05T13:14:21Z"
  generation: 3
  labels:
    app: webui
  name: rc-nginx
  namespace: default
  resourceVersion: "62250"
  uid: 3797522a-2dad-4227-bc84-1ca622937b57
spec:
  replicas: 4
  selector:
    app: webui
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: webui
      name: nginx-pod
    spec:
      containers:
      - image: nginx:1.14
        imagePullPolicy: IfNotPresent
        name: nginx-container
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
status:
  availableReplicas: 2
  fullyLabeledReplicas: 2

 

4개의 pod 가 실행중인것을 확인

kubectl get pods -o wide
NAME             READY   STATUS    RESTARTS   AGE     IP          NODE                NOMINATED NODE   READINESS GATES
rc-nginx-h884d   1/1     Running   0          10s     10.44.0.2   node2.example.com   <none>           <none>
rc-nginx-s4gmk   1/1     Running   0          8m16s   10.36.0.1   node1.example.com   <none>           <none>
rc-nginx-xpqtw   1/1     Running   0          8m16s   10.36.0.2   node1.example.com   <none>           <none>
rc-nginx-zgsst   1/1     Running   0          8m16s   10.44.0.1   node2.example.com   <none>           <none>

 

scale을 통해 replicas 개수를 변경시 가장 최근의 pod가 삭제된것을 확인 가능

kubectl scale rc rc-nginx --replicas=2
replicationcontroller/rc-nginx scaled

kubectl get pods -o wide
NAME             READY   STATUS    RESTARTS   AGE   IP          NODE                NOMINATED NODE   READINESS GATES
rc-nginx-s4gmk   1/1     Running   0          9m    10.36.0.1   node1.example.com   <none>           <none>
rc-nginx-xpqtw   1/1     Running   0          9m    10.36.0.2   node1.example.com   <none>           <none>

 

위와 같은 구성에서 edit을 통해 nginx 버전을 1.15 버전으로 변경시 pod에 변화는 없다

kubectl edit rc rc-nginx

apiVersion: v1
kind: ReplicationController
metadata:
  creationTimestamp: "2022-12-05T13:14:21Z"
  generation: 4
  labels:
    app: webui
  name: rc-nginx
  namespace: default
  resourceVersion: "62924"
  uid: 3797522a-2dad-4227-bc84-1ca622937b57
spec:
  replicas: 2
  selector:
    app: webui
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: webui
      name: nginx-pod
    spec:
      containers:
      - image: nginx:1.15
        imagePullPolicy: IfNotPresent
        name: nginx-container
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30

 

pod delete 후 nginx 버전을 확인하면 nginx 버전이 변경된 것을 확인할 수 있다.

kubectl delete pod rc-nginx-s4gmk
pod "rc-nginx-s4gmk" deleted

kubectl get pods -o wide
NAME             READY   STATUS    RESTARTS   AGE     IP          NODE                NOMINATED NODE   READINESS GATES
rc-nginx-jq8rw   1/1     Running   0          5m28s   10.44.0.1   node2.example.com   <none>           <none>
rc-nginx-xpqtw   1/1     Running   0          21m     10.36.0.2   node1.example.com   <none>           <none>


kubectl describe pod rc-nginx-jq8rw
Containers:
  nginx-container:
    Container ID:   containerd://b1e328cd8200c021cb346689a4db68e82caf8359596798702930390b73e76f74
    Image:          nginx:1.15
    Image ID:       docker.io/library/nginx@sha256:23b4dcdf0d34d4a129755fc6f52e1c6e23bb34ea011b315d87e193033bcd1b68

 

 

[참고]

- https://www.youtube.com/watch?v=5X3t6VJH2vQ&list=PLApuRlvrZKohaBHvXAOhUD-RxD0uQ3z0c&index=17

 

반응형