본문 바로가기
컨테이너/쿠버네티스

레플리카셋의 동작 원리

by ^..^v 2022. 1. 28.
728x90
반응형

라벨 셀렉터(Label Selector)를 이용해서 유지할 포드를 정의

레플리카셋은 spec.selector.matchLabels에 정의된 라벨을 통해 생성해야 하는 포드를 찾으며, spec.selector.matchLabels에 정의한 라벨을 가지는 포드의 개수가 replicas 항목에 정의된 숫자 보다 적으면 포드를 정의하는 포드 템플릿(template) 항목의 내용으로 포드를 생성

 

app: my-nginx-pods-label 라벨을 가지는 포드를 생성

/home/vagrant/nginx-pod-without-rs.yaml

apiVersion: v1                        
kind: Pod                             
metadata:                             
    name: my-nginx-pod
    labels:
        app: my-nginx-pods-label     ## app: my-nginx-pods-label 라벨을 가지는 포드를 정의
spec:                                 
    containers:
    - name: my-nginx-container
      image: nginx:latest
      ports:
      - containerPort: 80
## 포드 생성
root@master:/home/vagrant# kubectl apply -f nginx-pod-without-rs.yaml
pod/my-nginx-pod created


## 포드 생성 확인 (--show-labels 옵션을 이용해서 라벨을 함께 출력)
root@master:/home/vagrant# kubectl get pods --show-labels
NAME           READY   STATUS    RESTARTS   AGE   LABELS
my-nginx-pod   1/1     Running   0          34s   app=my-nginx-pods-label

 

app: my-nginx-pods-label 라벨의 포드를 3개 생성하는 레플리카셋을 생성

/home/vagrant/replicaset-nginx.yaml

apiVersion: apps/v1
kind: ReplicaSet
metadata:
    name: replicaset-nginx
spec:
    replicas: 3
    selector:
        matchLabels:
            app: my-nginx-pods-label
    template:
        metadata:
            name: my-nginx-pod
            labels:
                app: my-nginx-pods-label
        spec:
            containers:
            - name: my-nginx-container
              image: nginx:latest
              ports:
              - containerPort: 80
                protocol: TCP
## 레플리카셋 생성
root@master:/home/vagrant# kubectl apply -f replicaset-nginx.yaml
replicaset.apps/replicaset-nginx created


## 포드 생성 확인 ⇒ 두 개의 포드만 새롭게 생성
root@master:/home/vagrant# kubectl get pods --show-labels
NAME                     READY   STATUS    RESTARTS   AGE     LABELS
my-nginx-pod             1/1     Running   0          2m51s   app=my-nginx-pods-label
replicaset-nginx-q7kms   1/1     Running   0          12s     app=my-nginx-pods-label	⇐ 
replicaset-nginx-qh7sx   1/1     Running   0          12s     app=my-nginx-pods-label	⇐

 

수동으로 생성한 포드를 삭제

## my-nginx-pod 이름의 포드를 삭제
root@master:/home/vagrant# kubectl delete pods my-nginx-pod
pod "my-nginx-pod" deleted


## 포드 조회 ⇒ 레플리카셋이 새로운 포드를 생성
root@master:/home/vagrant# kubectl get pods --show-labels
NAME                     READY   STATUS    RESTARTS   AGE   LABELS
replicaset-nginx-q7kms   1/1     Running   0          80s   app=my-nginx-pods-label
replicaset-nginx-qh7sx   1/1     Running   0          80s   app=my-nginx-pods-label
replicaset-nginx-rw9tw   1/1     Running   0          4s    app=my-nginx-pods-label	⇐

 

레플리카셋이 생성한 포드의 라벨을 변경

root@master:/home/vagrant# kubectl edit pods replicaset-nginx-q7kms 

## 포드 라벨 삭제
root@master:/home/vagrant# kubectl edit pods replicaset-nginx-q7kms

# 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: Pod
metadata:
  annotations:
    cni.projectcalico.org/podIP: 10.233.99.17/32
    cni.projectcalico.org/podIPs: 10.233.99.17/32
  creationTimestamp: "2022-01-06T06:01:09Z"
  generateName: replicaset-nginx-
  #  labels:                       ⇐ 포드의 라벨을 삭제 (주석 처리)
  #    app: my-nginx-pods-label
  name: replicaset-nginx-q7kms
  namespace: default
  ownerReferences:
  - apiVersion: apps/v1
    blockOwnerDeletion: true
    controller: true
    kind: ReplicaSet
    name: replicaset-nginx
    uid: d784a19e-9c74-4b01-b4db-8656f5ced2d0
  resourceVersion: "76376"
  selfLink: /api/v1/namespaces/default/pods/replicaset-nginx-q7kms
  uid: e00d6cd6-f0e6-4b93-8d75-e1dfeb3c1d92
spec:
  containers:
  - image: nginx:latest
    imagePullPolicy: Always
    name: my-nginx-container
    ports:
    - containerPort: 80
      protocol: TCP
         :
         

## 포드 조회 ⇒ 레플리카셋에서 정의한 라벨과 다른 라벨의 포드는 관리 대상으로 간주하지 않음
root@master:/home/vagrant# kubectl get pods --show-labels
NAME                    READY  STATUS    RESTARTS  AGE    LABELS
replicaset-nginx-q7kms  1/1    Running   0         8m58s  <none>                   ⇐ 관리 대상으로 간주하지 않음
replicaset-nginx-qh7sx  1/1    Running   0         8m58s  app=my-nginx-pods-label
replicaset-nginx-rw9tw  1/1    Running   0         7m42s  app=my-nginx-pods-label
replicaset-nginx-xsbpq  1/1    Running   0         44s    app=my-nginx-pods-label  ⇐ 새로운 포드 생성

 

레플리카셋 삭제 → 라벨이 일치하지 않는 포드는 삭제되지 않음 (레플리카의 관리 대상이 아님)

## 레플리카셋 조회
root@master:/home/vagrant# kubectl get replicasets -o wide
NAME               DESIRED   CURRENT   READY   AGE   CONTAINERS           IMAGES         SELECTOR
replicaset-nginx   3         3         3       11m   my-nginx-container   nginx:latest   app=my-nginx-pods-label


## 포드 조회
root@master:/home/vagrant# kubectl get pods --show-labels
NAME                     READY   STATUS    RESTARTS   AGE     LABELS
replicaset-nginx-q7kms   1/1     Running   0          11m     <none>
replicaset-nginx-qh7sx   1/1     Running   0          11m     app=my-nginx-pods-label
replicaset-nginx-rw9tw   1/1     Running   0          10m     app=my-nginx-pods-label
replicaset-nginx-xsbpq   1/1     Running   0          3m10s   app=my-nginx-pods-label


## 레플리카셋 삭제
root@master:/home/vagrant# kubectl delete replicasets replicaset-nginx
replicaset.apps "replicaset-nginx" deleted


## 레플리카셋 삭제 확인
root@master:/home/vagrant# kubectl get replicasets -o wide
No resources found in default namespace.


## 포드 조회 ⇒ 라벨이 삭제된 포드는 레플리카셋에 의해 삭제되지 않는 것을 확인
root@master:/home/vagrant# kubectl get pods --show-labels
NAME                     READY   STATUS    RESTARTS   AGE   LABELS
replicaset-nginx-q7kms   1/1     Running   0          12m   <none> 	⇐

 

라벨이 삭제된 포드를 직접 삭제

## 포드 조회
root@master:/home/vagrant# kubectl get pods --show-labels
NAME                     READY   STATUS    RESTARTS   AGE   LABELS
replicaset-nginx-q7kms   1/1     Running   0          12m   <none>


## 포드 삭제
root@master:/home/vagrant# kubectl delete pods replicaset-nginx-q7kms
pod "replicaset-nginx-q7kms" deleted


## 포드 삭제 확인
root@master:/home/vagrant# kubectl get pods
No resources found in default namespace.
728x90
반응형

댓글