본문 바로가기

리눅스

[kubernetes] ReplicationController

728x90

kubernetes ReplicationController

레플리케이션 컨트롤러 생성

- 레이블 셀렉터

- 파드 템플릿

- 복제본 수

nginx-rc.yaml 편집

vim nginx-rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx-rc
spec:
  replicas: 3
  selector:
    app: webserver
    env: prod
  template:
    metadata:
      name: nginx-pod
      labels:
        app: webserver
        env: prod
    spec:
      containers:
        - name: nginx-container
          image: nginx:latest
          ports:
            - containerPort: 8080

kubectl create 명령

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

레플리케이션 컨트롤러(replicationcontrollers)와 파드(pods) 확인

레플리케이션 컨트롤러(replicationcontrollers) 확인

kubectl get rc --show-labels -o wide
$ kubectl get rc --show-labels -o wide
NAME       DESIRED   CURRENT   READY   AGE     CONTAINERS        IMAGES         SELECTOR                 LABELS
nginx-rc   3         3         3       5m25s   nginx-container   nginx:latest   app=webserver,env=prod   app=webserver,env=prod
$ kubectl describe rc nginx-rc
$ kubectl describe rc nginx-rc
Name:         nginx-rc
Namespace:    default
Selector:     app=webserver,env=prod
Labels:       app=webserver
              env=prod
Annotations:  <none>
Replicas:     3 current / 3 desired
Pods Status:  3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=webserver
           env=prod
  Containers:
   nginx-container:
    Image:        nginx:latest
    Port:         8080/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age   From                    Message
  ----    ------            ----  ----                    -------
  Normal  SuccessfulCreate  9m6s  replication-controller  Created pod: nginx-rc-hhxrf
  Normal  SuccessfulCreate  9m6s  replication-controller  Created pod: nginx-rc-nfgsr
  Normal  SuccessfulCreate  9m6s  replication-controller  Created pod: nginx-rc-8qp9m

 파드(pods) 확인

kubectl get pods --show-labels -o wide
$ kubectl get pods --show-labels -o wide
NAME             READY   STATUS    RESTARTS   AGE     IP              NODE       NOMINATED NODE   READINESS GATES   LABELS
nginx-rc-8qp9m   1/1     Running   0          7m26s   10.244.120.83   minikube   <none>           <none>            app=webserver,env=prod
nginx-rc-hhxrf   1/1     Running   0          7m26s   10.244.120.84   minikube   <none>           <none>            app=webserver,env=prod
nginx-rc-nfgsr   1/1     Running   0          7m26s   10.244.120.82   minikube   <none>           <none>            app=webserver,env=prod
kubectl describe pods nginx-rc
$ kubectl describe pods nginx-rc
Name:         nginx-rc-8qp9m
Namespace:    default
Priority:     0
Node:         minikube/192.168.39.107
Start Time:   Wed, 21 Sep 2022 10:06:25 +0900
Labels:       app=webserver
              env=prod
Annotations:  cni.projectcalico.org/containerID: 1f463142d1c481f280851d20a4be38b07b23b39520a9490090be10e62aebf964
              cni.projectcalico.org/podIP: 10.244.120.83/32
              cni.projectcalico.org/podIPs: 10.244.120.83/32
Status:       Running
IP:           10.244.120.83
IPs:
  IP:           10.244.120.83
Controlled By:  ReplicationController/nginx-rc
Containers:
  nginx-container:
    Container ID:   docker://f17d7daa8c5b1908d0e0310c855dbe80ea02197eeb9797d7d85e49be04cbb34b
    Image:          nginx:latest
    Image ID:       docker-pullable://nginx@sha256:0b970013351304af46f322da1263516b188318682b2ab1091862497591189ff1
    Port:           8080/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Wed, 21 Sep 2022 10:07:20 +0900
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-jmfvw (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  kube-api-access-jmfvw:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age    From               Message
  ----    ------     ----   ----               -------
  Normal  Scheduled  10m    default-scheduler  Successfully assigned default/nginx-rc-8qp9m to minikube
  Normal  Pulling    9m49s  kubelet            Pulling image "nginx:latest"
  Normal  Pulled     9m36s  kubelet            Successfully pulled image "nginx:latest" in 12.455791938s
  Normal  Created    9m35s  kubelet            Created container nginx-container
  Normal  Started    9m28s  kubelet            Started container nginx-container
  
  ...

파드의 레이블 변경

kubectl label pod nginx-rc-nfgsr app=myapp --overwrite
$ kubectl label pod nginx-rc-nfgsr app=myapp --overwrite
pod/nginx-rc-nfgsr labeled

nginx-rc-nfgsr 파드는 레플리케이션 컨트롤러에서 제외되고 새로운 파트(nginx-rc-64l7f)를 생성한다

$ kubectl get pods --show-labels -o wide
NAME             READY   STATUS    RESTARTS   AGE   IP              NODE       NOMINATED NODE   READINESS GATES   LABELS
nginx-rc-64l7f   1/1     Running   0          49s   10.244.120.85   minikube   <none>           <none>            app=webserver,env=prod
nginx-rc-8qp9m   1/1     Running   0          14m   10.244.120.83   minikube   <none>           <none>            app=webserver,env=prod
nginx-rc-hhxrf   1/1     Running   0          14m   10.244.120.84   minikube   <none>           <none>            app=webserver,env=prod
nginx-rc-nfgsr   1/1     Running   0          14m   10.244.120.82   minikube   <none>           <none>            app=myapp,env=prod

복제수 변경

방법1)

kubectl scale 명령 사용

kubectl scale rc nginx-rc --replicas
$ kubectl scale rc nginx-rc --replicas=5
replicationcontroller/nginx-rc scaled

방법2)

kubectl edit 명령 사용

kubectl edit rc nginx-rc
$ kubectl edit rc nginx-rc
# 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-09-21T01:06:22Z"
  generation: 1
  labels:
    app: webserver
    env: prod
  name: nginx-rc
  namespace: default
  resourceVersion: "59325"
  uid: 4c6c4b33-bf99-48f8-939c-bdfa6fcde0a9
spec:
  replicas: 5
  selector:
    app: webserver
    env: prod
  template:
    metadata:
      creationTimestamp: null
...
---
replicationcontroller/nginx-rc edited

방법3)

nginx-rc.yaml 편집 후 적용(kubectl replace)

vim nginx-rc.yaml
$ vim nginx-rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx-rc
spec:
  replicas: 10
  selector:
    app: webserver
    env: prod
...
kubectl replace -f nginx-rc.yaml
$ kubectl replace -f nginx-rc.yaml
replicationcontroller/nginx-rc replaced

방법4)

nginx-rc.yaml 편집 후 적용(kubectl apply)

kubectl apply -f nginx-rc.yaml
$ kubectl apply -f nginx-rc.yaml
Warning: resource replicationcontrollers/nginx-rc is missing the kubectl.kubernetes.io/last-applied-configuration annotation which is required by kubectl apply. kubectl apply should only be used on resources created declaratively by either kubectl create --save-config or kubectl apply. The missing annotation will be patched automatically.
replicationcontroller/nginx-rc configured

kubectl 명령의 watch 옵션으로 변화를 모니터링할 수 있음

kubectl get rc --show-labels -o wide --watch
kubectl get pods --show-labels -o wide --watch

로그 확인

kubectl logs rc/nginx-rc
$ kubectl logs rc/nginx-rc
Found 5 pods, using pod/nginx-rc-hhxrf
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/09/21 01:07:29 [notice] 1#1: using the "epoll" event method
2022/09/21 01:07:29 [notice] 1#1: nginx/1.23.1
2022/09/21 01:07:29 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2022/09/21 01:07:29 [notice] 1#1: OS: Linux 5.10.57
2022/09/21 01:07:29 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/09/21 01:07:29 [notice] 1#1: start worker processes
2022/09/21 01:07:29 [notice] 1#1: start worker process 30
2022/09/21 01:07:29 [notice] 1#1: start worker process 31
2022/09/21 01:07:29 [notice] 1#1: start worker process 32
2022/09/21 01:07:29 [notice] 1#1: start worker process 33

 

** oh-my-zsh plugin : kubectl, kube-ps1

vim ~/.zshrc
...
plugins=(
     git
     kubectl
     kube-ps1
 )
 ...
 
 export PROMPT='$(kube_ps1)'$PROMPT
source ~/.zshrc

 

728x90