Kubernetes

쿠버네티스 기초 Replication Controller

Daniel007 2020. 3. 26. 00:29

Replication Controller는 노드가 클러스터에서 사라지거나 노드에있는 pod이 문제가 생겼을때 대체 pod을 생성 해준다.

Pod 리소스가 부족하다고 느껴지면 자동으로 scale out 기능도 가능 합니다.

pod에서 만들었던 방법이랑 비슷 하지만 kind에 ReplicationController, spec-template에 pod에서 사용할때 썼던 컨테이너들을 적어줬습니다.

apiVersion: v1
kind: ReplicationController
metadata:
  name: replication-1pod-2container
  labels:
    app: ubuntu_nginx
spec: # Replication Controller
  template:
    metadata: # string
      name: 1pod-2container
      labels: # dictionary
        app: ubuntu-nginx
    spec: # POD
      containers: # List/Array(여러개를 추가할수 있으므로)
        - name: nginx-container # - List(1 item)
          image: nginx
        - image: ubuntu
          name: ubuntu
          command: ["/bin/sh"]
          args: ["-c", "while true; do echo hello; sleep 10;done"]
          # Docker 에서는 command에 인자값을 줄때 Cmd를 사용했지만 Kubernetes에서는 arg를 사용한다.

yml파일 실행

KDS-2:yaml kimdaesung$ kubectl create -f replication_controller.yml 
replicationcontroller/replication-1pod-2container created

replication controller 확인 및 pod 확인

KDS-2:yaml kimdaesung$ kubectl get replicationcontrollers 
NAME                          DESIRED   CURRENT   READY   AGE
replication-1pod-2container   1         1         1       31m
KDS-2:yaml kimdaesung$ kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
1pod-2container                     2/2     Running   4          3d6h
redis-pod                           1/1     Running   2          3d5h
replication-1pod-2container-6bv7s   2/2     Running   0          31m

pod 삭제한 상태에서 어떻게 동작하는지 확인

KDS-2:yaml kimdaesung$ kubectl delete pod replication-1pod-2container-6bv7s
pod "replication-1pod-2container-6bv7s" deleted
KDS-2:yaml kimdaesung$ kubectl get pods
NAME                                READY   STATUS        RESTARTS   AGE
1pod-2container                     2/2     Running       4          3d6h
redis-pod                           1/1     Running       2          3d5h
replication-1pod-2container-6bv7s   2/2     Terminating   0          37m
replication-1pod-2container-9ntqv   2/2     Running       0          24s
KDS-2:yaml kimdaesung$ kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
1pod-2container                     2/2     Running   4          3d6h
redis-pod                           1/1     Running   2          3d5h
replication-1pod-2container-9ntqv   2/2     Running   0          35s

replication-1pod-2container-6bv7s가 지워지고 replication-1pod-2container-9ntqv 가 새로 생성된걸 알수 있다.

KDS-2:yaml kimdaesung$ kubectl describe rc replication-1pod-2container 
Name:         replication-1pod-2container
Namespace:    default
Selector:     app=ubuntu-nginx
Labels:       app=ubuntu_nginx
Annotations:  <none>
Replicas:     1 current / 1 desired #실제 / 원하는 pod의 수
Pods Status:  1 Running / 0 Waiting / 0 Succeeded / 0 Failed #pod 상태별 instance 수 
Pod Template:
  Labels:  app=ubuntu-nginx
  Containers:
   nginx-container:
    Image:        nginx
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
   ubuntu:
    Image:      ubuntu
    Port:       <none>
    Host Port:  <none>
    Command:
      /bin/sh
    Args:
      -c
      while true; do echo hello; sleep 10;done
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events: # Replication Contorller 이벤트 
  Type    Reason            Age   From                    Message
  ----    ------            ----  ----                    -------
  Normal  SuccessfulCreate  40m   replication-controller  Created pod: replication-1pod-2container-6bv7s
  Normal  SuccessfulCreate  4m2s  replication-controller  Created pod: replication-1pod-2container-9ntqv