클라우드,가상화/K8S

k8s - ReplicaSet

aiemag 2023. 7. 2. 16:13
반응형

 

ReplicaSet?

Workload API중의 하나로 ReplicaSet은 Pod의 Replica를 생성하고 지정한 Pod 수를 유지하는 Resource

 

 

ReplicaSet 생성

Pod를 Replica 수 3으로 확장시킨 ReplicaSet Manifest

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: sample-rs
spec:
  replicas: 3
  selector:
    matchLabels:
      app: sample-app
  template:
    metadata:
      labels:
        app: sample-app
    spec:
      containers:
      - name: nginx-container
        image: nginx:1.16

 

 

※ 기동 중인 세 개의 Pod를 확인할 수 있음. 실제 Pod가 각각 다른 Node에 흩어져 생성되기 때문에 특정 Node에 장애가 발생해도 서비스에 미치는 영향을 최소화할 수 있음.

[root@k8s-master replicaset]# k apply -f sample-rs.yaml
replicaset.apps/sample-rs created

[root@k8s-master replicaset]# k get replicasets -o wide
NAME        DESIRED   CURRENT   READY   AGE   CONTAINERS        IMAGES       SELECTOR
sample-rs   3         3         3       23s   nginx-container   nginx:1.16   app=sample-app

[root@k8s-master replicaset]# k get pods -l app=sample-app -o wide
NAME              READY   STATUS    RESTARTS   AGE   IP                NODE          NOMINATED NODE   READINESS GATES
sample-rs-dvjc4   1/1     Running   0          26m   192.168.194.102   k8s-worker1   <none>           <none>
sample-rs-mb2tf   1/1     Running   0          26m   192.168.194.103   k8s-worker1   <none>           <none>
sample-rs-mxs2j   1/1     Running   0          26m   192.168.194.101   k8s-worker1   <none>           <none>

 

Pod 정지와 자동화된 복구

ReplicaSet에서는 Node나 Pod에 장애가 발생했을 때 지정한 Pod 수를 유지하기 위해 다른 Node에서 Pod를  가동시켜 주기 때문에 장애 시에도 많은 영향을 받지 않음.

 

자동화된 복구 테스트

[root@k8s-master replicaset]# k get pods -o wide
NAME              READY   STATUS    RESTARTS   AGE   IP                NODE          NOMINATED NODE   READINESS GATES
sample-rs-dvjc4   1/1     Running   0          45m   192.168.194.102   k8s-worker1   <none>           <none>
sample-rs-mb2tf   1/1     Running   0          45m   192.168.194.103   k8s-worker1   <none>           <none>
sample-rs-mxs2j   1/1     Running   0          45m   192.168.194.101   k8s-worker1   <none>           <none>

[root@k8s-master replicaset]# kubectl delete pod sample-rs-dvjc4
pod "sample-rs-dvjc4" deleted

[root@k8s-master replicaset]# k get pods -o wide
NAME              READY   STATUS    RESTARTS   AGE   IP                NODE          NOMINATED NODE   READINESS GATES
sample-rs-8bhmr   1/1     Running   0          8s    192.168.194.104   k8s-worker1   <none>           <none>
sample-rs-mb2tf   1/1     Running   0          46m   192.168.194.103   k8s-worker1   <none>           <none>
sample-rs-mxs2j   1/1     Running   0          46m   192.168.194.101   k8s-worker1   <none>           <none>

 

ReplicaSet의 POd 수 증감 이력 확인

[root@k8s-master replicaset]# kubectl describe replicaset sample-rs
Name:         sample-rs
Namespace:    default
Selector:     app=sample-app
Labels:       <none>
Annotations:  Replicas:  3 current / 3 desired
Pods Status:  3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=sample-app
  Containers:
   nginx-container:
    Image:        nginx:1.16
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age   From                   Message
  ----    ------            ----  ----                   -------
  Normal  SuccessfulCreate  47m   replicaset-controller  Created pod: sample-rs-mxs2j
  Normal  SuccessfulCreate  47m   replicaset-controller  Created pod: sample-rs-dvjc4
  Normal  SuccessfulCreate  47m   replicaset-controller  Created pod: sample-rs-mb2tf
  Normal  SuccessfulCreate  98s   replicaset-controller  Created pod: sample-rs-8bhmr

 

 

ReplicaSet과 Label

Replicaset은 kubernetes가 Pod를 모니터링하여 Pod 수를 조정.

모니터링은 특정 label을 가진 Pod 수를 계산하는 형태로 이루어짐.

Replica 수가 부족한 경우 Manifest에 기술된 spec.template로 파드를 생성하고 Replica 수가 많을 경우 Label이 일치하는 Pod 중 하나를 삭제.

 

어떤 Label을 가진 Pod를 계산할지는 spec.selector에 지정

 

spec.selector와 spec.template.metadata.labels의 Label이 일치하지 않는 경우.

 

manifest

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: sample-rs-fail
spec:
  replicas: 3
  selector:
    matchLabels:
      app: sample-app
    template:
      metadata:
        labels:
          app: sample-app-fail
      spec:
        containers:
        - name: nginx-container
          image: nginx:1.16

Label이 일치하지 않는 경우 에러가 ㅂ라생하여 생성할 수 없게 됨.

[root@k8s-master replicaset]# k apply -f sample-rs-fail.yaml
error: error validating "sample-rs-fail.yaml": error validating data: ValidationError(ReplicaSet.spec.selector): unknown field "template" in io.k8s.apimachinery.pkg.apis.meta.v1.LabelSelector; if you choose to ignore these errors, turn validation off with --validate=false

 

 

같은 Label을 가진 Pod를 ReplicaSet 밖에서 생성할 경우.

apiVersion: v1
kind: Pod
metadata:
  name: sample-rs-pod
  labels:
    app: sample-app
spec:
  containers:
  - name: nginx-container
    image: nginx:1.17

 

Label이 중복된 경우 Replicaset은 Pod를 초과하여 생성한 것으로 판단하고 네 개의 Pod 중 하나를 정지.

※ 상황에 따라 기존 Pod가 삭제될 수 있으므로 주의.

 

[root@k8s-master replicaset]# k apply -f sample-rs.yaml
replicaset.apps/sample-rs unchanged

[root@k8s-master replicaset]# k get pods -L app
NAME              READY   STATUS    RESTARTS   AGE    APP
sample-rs-6hhp7   1/1     Running   0          2m2s   sample-app
sample-rs-gxs65   1/1     Running   0          2m2s   sample-app
sample-rs-xs2xf   1/1     Running   0          2m2s   sample-app

[root@k8s-master replicaset]# k apply -f sample-rs-pod.yaml
pod/sample-rs-pod created

[root@k8s-master replicaset]# k get pods -L app
NAME              READY   STATUS        RESTARTS   AGE     APP
sample-rs-6hhp7   1/1     Running       0          3m51s   sample-app
sample-rs-gxs65   1/1     Running       0          3m51s   sample-app
sample-rs-pod     0/1     Terminating   0          4s      sample-app
sample-rs-xs2xf   1/1     Running       0          3m51s   sample-app

[root@k8s-master replicaset]# k get pods -L app
NAME              READY   STATUS    RESTARTS   AGE    APP
sample-rs-6hhp7   1/1     Running   0          5m8s   sample-app
sample-rs-gxs65   1/1     Running   0          5m8s   sample-app
sample-rs-xs2xf   1/1     Running   0          5m8s   sample-app

 

 

ReplicaSet과 Scaling

ReplicaSet 설정을 변경하고 Pod 수를 변경하는 방법

1. Manifest를 수정하여 kubectl apply -f 명령어 실행

[root@k8s-master replicaset]# sed -i -e 's|replicas: 3|replicas: 4|' sample-rs.yaml
[root@k8s-master replicaset]# k apply -f sample-rs.yaml
replicaset.apps/sample-rs configured

[root@k8s-master replicaset]# k get pods -L app
NAME              READY   STATUS    RESTARTS   AGE   APP
sample-rs-6hhp7   1/1     Running   0          15m   sample-app
sample-rs-gxs65   1/1     Running   0          15m   sample-app
sample-rs-rcpl8   1/1     Running   0          25s   sample-app
sample-rs-xs2xf   1/1     Running   0          15m   sample-app

 

2. kubectl scale 명령을 사용하여 scale 처리

[root@k8s-master replicaset]# k scale replicaset sample-rs --replicas 5
replicaset.apps/sample-rs scaled

[root@k8s-master replicaset]# k get replicasets
NAME        DESIRED   CURRENT   READY   AGE
sample-rs   5         5         5       172m

 

반응형

'클라우드,가상화 > K8S' 카테고리의 다른 글

k8s - kubernetes 클러스터 네트워크와 서비스  (0) 2023.07.16
k8s - Deployment  (0) 2023.07.02
k8s - Pod  (0) 2023.07.02
k8s - Overview  (0) 2023.07.02
Minikube 설치  (0) 2023.05.20