Tools/K8S

Kubernetes ReplicaSet

칼쵸쵸 2023. 7. 1. 17:40

레플리카셋

- 파드에 레플리카를 생성하고 지정한 파드수를 유지하는 리소스

- 노드나 파드에 장애가 발생했을 때 지정한 파드 수를 유지하기 위해 다른 노드에서 파드를 기동시켜 주기 때문에 장애 시에도 많은 영향을 받지 않음 (high Availability, LoadBalance를 수행함)

- 모니터링은 특정 레이블을 가진 파드 수를 계산하는 형태로 이루어짐

 

(yaml 형식)

- template에 명시된 형태로 pod를 자동 생성함

- replicas로 유지할 pod의 숫자를 정함

- 특정 레이블을 가진 레플리카 수가 부족한 경우 매니 페스트에 기술된 spec.template으로 파드를 생성하고 수가 많을 경우 해당 레이블을 가진 파드 중에서 하나를 삭제함

( 따라서 해당 모니터링을 위해 selector.matchLabels과 template.metadata.Lables의 값이 같아야 됨, 다를 시 에러 발생)

 

 * 단순히 일치, 불일치(=,!=) 뿐만 아니라 matchExpression을 활용한 집합성 기준 (in, notin, exists)도 지원함

 

Practice

1. There is an issue with the file, so try to fix it. and Create a ReplicaSet 

apiVersion: v1
kind: ReplicaSet
metadata:
  name: replicaset-1
spec:
  replicas: 2
  selector:
    matchLabels:
      tier: frontend
  template:
    metadata:
      labels:
        tier: frontend
    spec:
      containers:
      - name: nginx
        image: nginx

1) ReplicaSet을 사용하기 위한 버전이 다르다

controlplane ~ ✖ kubectl explain replicaset
GROUP:      apps
KIND:       ReplicaSet
VERSION:    v1

v1이 아니라 apps/v1으로 확인됨, 수정 후 생성

controlplane ~ ➜  kubectl apply -f /root/replicaset-definition-1.yaml
replicaset.apps/replicaset-1 created

 

2. There is an issue with the file 2

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: replicaset-2
spec:
  replicas: 2
  selector:
    matchLabels:
      tier: frontend
  template:
    metadata:
      labels:
        tier: nginx
    spec:
      containers:
      - name: nginx
        image: nginx

 

1) selector.matchLables와 template.metadata.lables가 일치하지 않음 (모니터링 불가)

template.metadata.lables를 frontend로 수정

 

3. Fix the original replica set new-replica-set to use the correct busybox image.

kubectl get replicasets 

controlplane ~ ➜  kubectl get replicasets
NAME              DESIRED   CURRENT   READY   AGE
new-replica-set   4         4         0       7s

레플리카셋 1개 , 요구하는 pod의 개수 4개 , 현재 ready개수 0개 (실행 안 된 상태)

 

1) 파드의 상태 확인

controlplane ~ ➜  kubectl get pods
NAME                    READY   STATUS             RESTARTS   AGE
new-replica-set-bc2nz   0/1     ImagePullBackOff   0          88s
new-replica-set-r22l5   0/1     ImagePullBackOff   0          87s
new-replica-set-srmx7   0/1     ImagePullBackOff   0          88s
new-replica-set-nlq6n   0/1     ImagePullBackOff   0          88s

이미지가 없어서 실행을 못하는 상태

 

2) 레플리카셋 수정하여 파드의 이미지를 정확히 기술

controlplane ~ ✖ kubectl edit rs new-replica-set

replicaset.apps/new-replica-set edited

 

3) 레플리카셋으로 기존에 만들어진 파드들을 삭제해야 정상동작하는 파드들이 재생성됨

controlplane ~ ➜  kubectl delete pod new-replica-set-bc2nz new-replica-set-r22l5 new-replica-set-srmx7 new-replica-set-nlq6n
pod "new-replica-set-bc2nz" deleted
pod "new-replica-set-r22l5" deleted
pod "new-replica-set-srmx7" deleted
pod "new-replica-set-nlq6n" deleted

controlplane ~ ➜  kubectl get rs
NAME              DESIRED   CURRENT   READY   AGE
new-replica-set   4         4         4       8m49s

 

4. Scale the ReplicaSet to 5 PODs.

kubectl scale rs new-replica-set --replicas=5

controlplane ~ ✖ kubectl scale rs new-replica-set --replicas=5
replicaset.apps/new-replica-set scaled

controlplane ~ ➜  kubectl get rs
NAME              DESIRED   CURRENT   READY   AGE
new-replica-set   5         5         5       11m

 

'Tools > K8S' 카테고리의 다른 글

Kubernetes Secret  (0) 2023.07.21
Kubernetes Deployment  (0) 2023.07.01
Kubernetes Config  (1) 2023.06.17
Kubernetes Scheduling  (0) 2023.03.20
Kubernetes namespace  (0) 2023.03.19