Cloud Engineering/Kubernetes ⚙️

[Kubernetes] Replication Controller (레플리케이션 컨트롤러)

minjiwoo 2023. 2. 11. 23:57
728x90

Replication Controller

쿠버네티스의 컨트롤러는 파드 복제본 (replicas)들의 숫자를 보장해주는 역할을 한다. 레플레케이션 컨트롤러는 이용가능한 상태의 pod을 원하는 수 만큼으로 관리해 주는데, pod가 원하는 수 보다 많은 경우 줄여주고, 원하는 수 보다 적은 경우 자동으로 늘려준다. 

레플리케이션 컨트롤러를 구성하는데 필요한 요소는 레이블 셀렉터, 파드 템플릿, 복제본의 수가 있다. 

레이블 셀렉터는 key :value 형식으로 설정되며, 생성 및 관리를 할 파드에 대해 지정하는 것이다. 

파드 템플릿은 파드를 어떤식으로 구성할 것인지 설정 내용에 대한 것이다. 이 파드 템플릿과 동일하게 복제본들이 만들어 진다. 

복제본의 수는 지정해 주지 않으면 디폴트 값이 1 이며, 지정해 주면 지정한 수대로 pod의 수가 유지 된다. 

 

ReplicationController Manifest File 예시 

rc.yaml 

apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx
spec:
  replicas: 3
  selector:
    app: nginx
  template:
    metadata:
      name: nginx
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80

레플리케이션 컨트롤러에 의해서 pod이 3개 생성되었다. 

 

생성된 pod들의 레이블 정보를 확인해보면 app=nginx로 설정이 되어있다. 

vagrant@kube-control1:~/work/mj$ kubectl get pods --show-labels
NAME          READY   STATUS    RESTARTS   AGE     LABELS
nginx-c28r4   1/1     Running   0          8m31s   app=nginx
nginx-dswl8   1/1     Running   0          8m31s   app=nginx
nginx-gghs2   1/1     Running   0          8m31s   app=nginx

 

레플리케이션 컨트롤러 상세정보 확인 명령어 

vagrant@kube-control1:~/work/mj$ kubectl describe replicationcontrollers/nginx

kubectl describe 명령어로 생성한 레플리케이션 컨트롤러의 상세 정보를 확인한다. 

Selector 가 app=nginx 레이블로 설정되었다.
Replicas 는 현재 총 3개이고 원하는 개수가 3 (desired)이다. 

 

레플리케이션 컨트롤러 목록과 상태를 확인하는 명령어 

vagrant@kube-control1:~/work/mj$ kubectl get replicationcontrollers
NAME    DESIRED   CURRENT   READY   AGE
nginx   3         3         3       6m27s

 

Pod 수평 스케일링 

1. 명령어로 스케일링하기 

$ kubectl scale replicationcontroller NAME --replicas=REPLICAS_NUMBER
$ kubectl scale replicationcontroller nginx --replicas=4

스케일링 되어 pod이 하나 더 늘어난 것을 확인할 수 있다. 

레플리케이션 컨트롤러의 정보를 확인해보면 DESIRED 값이 4로 변경되었다. 

vagrant@kube-control1:~/work/mj$ kubectl get replicationcontroller nginx
NAME    DESIRED   CURRENT   READY   AGE
nginx   4         4         4       14m

 

2. 오브젝트 수정하여 스케일링하기 

$ kubectl edit replicationcontroller NAME

$ kubectl edit replicationcontroller nginx

위의 명령어를 입력하면 vim editor에서 내용을 수정할 수 있다. 

spec 필드의 replicas 필드의 값을 4에서 5로 수정후 :wq 로 저장해준다. 

pod 개수가 5개로 스케일링 되었다. 

vagrant@kube-control1:~/work/mj$ kubectl get pods
NAME          READY   STATUS    RESTARTS   AGE
nginx-5frd4   1/1     Running   0          7m20s
nginx-c28r4   1/1     Running   0          18m
nginx-dswl8   1/1     Running   0          18m
nginx-g8mfd   1/1     Running   0          9s
nginx-gghs2   1/1     Running   0          18m

 

3. 오브젝트 파일 (Manifest File)을 수정하여 스케일링하기 

레플리케이션 컨트롤러를 생성하기 위해 작성한 오브젝트 파일을 vi/vim editor로 편집하고 변경된 사항을 적용시킨다.

편집하지 않고 기존의 복제본 개수 3개였던 설정파일을 다시 적용시켜보기로 한다. 

기존 설정파일 

apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx
spec:
  replicas: 3
  selector:
    app: nginx
  template:
    metadata:
      name: nginx
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80

 

replace -f 로 오브젝트 설정파일대로 레플리케이션 컨트롤러를 업데이트 해준다. 

vagrant@kube-control1:~/work/mj$ kubectl replace -f rc.yaml
replicationcontroller/nginx replaced

 

pod 의 목록을 확인하면 다음과 같다. 신기한 점은, pod 개수가 줄어드는 스케일링이 될때 삭제되는 pod은 최신에 생성된 순으로 먼저 삭제가 된다는 것이다 !

vagrant@kube-control1:~/work/mj$ kubectl get pods
NAME          READY   STATUS    RESTARTS   AGE
nginx-c28r4   1/1     Running   0          25m
nginx-dswl8   1/1     Running   0          25m
nginx-gghs2   1/1     Running   0          25m

 

Replication Controller 삭제하기 

$ kubectl delete replicationcontroller NAME

vagrant@kube-control1:~/work/mj$ kubectl delete replicationcontroller nginx
replicationcontroller "nginx" deleted

 

레플리케이션 컨트롤러를 삭제하면 레플리케이션 컨트롤러에 의해 생성된 pod들이 모두 삭제가 된다. 

vagrant@kube-control1:~/work/mj$ kubectl get pods
No resources found in default namespace.

 

자료 출처 

https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller/

 

ReplicationController

Note: A Deployment that configures a ReplicaSet is now the recommended way to set up replication. A ReplicationController ensures that a specified number of pod replicas are running at any one time. In other words, a ReplicationController makes sure that a

kubernetes.io

 

728x90