kubernetes/udemy

[Udemy] ReplicaSet

bbiyak2da 2025. 2. 12. 17:30

ReplicaSet

  • ReplicaSet은 지정된 수의 Pod 복제본이 항상 실행되도록 보장하는 컨트롤러
  • Pod가 실패하거나 삭제되면, ReplicaSet은 자동으로 새로운 Pod를 생성하여 원하는 상태를 유지
  • ReplicaSet을 사용하여 여러 개의 파드 복제본을 관리할 때, 서비스(Service)를 활용하여 이러한 파드에 대한 로드 밸런싱을 제공

 

ReplicaSet 생성

 

# replicaset-demo.yml 파일 작성

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: my-helloworld-rs
  labels:
    app: my-helloworld
spec:
  replicas: 6
  selector:
    matchLabels:
      app: my-helloworld
  template:
    metadata:
      labels:
        app: my-helloworld
    spec:
      containers:
      - name: my-helloworld-app
        image: stacksimplify/kube-helloworld:1.0.0

 

# replicaset 생성

kubectl create -f replicaset-demo.yml
replicaset.apps/my-helloworld-rs created

 

# replicaset 확인

kubectl get rs        
NAME               DESIRED   CURRENT   READY   AGE
my-helloworld-rs   6         6         6       2m4s
kubectl describe rs
Name:         my-helloworld-rs
Namespace:    default
Selector:     app=my-helloworld
Labels:       app=my-helloworld
Annotations:  <none>
Replicas:     6 current / 6 desired
Pods Status:  6 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=my-helloworld
  Containers:
   my-helloworld-app:
    Image:        stacksimplify/kube-helloworld:1.0.0
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age    From                   Message
  ----    ------            ----   ----                   -------
  Normal  SuccessfulCreate  3m41s  replicaset-controller  Created pod: my-helloworld-rs-kkklm
  Normal  SuccessfulCreate  3m41s  replicaset-controller  Created pod: my-helloworld-rs-tmbcr
  Normal  SuccessfulCreate  3m41s  replicaset-controller  Created pod: my-helloworld-rs-r55cx
  Normal  SuccessfulCreate  3m41s  replicaset-controller  Created pod: my-helloworld-rs-28v29
  Normal  SuccessfulCreate  3m41s  replicaset-controller  Created pod: my-helloworld-rs-wlx6b
  Normal  SuccessfulCreate  3m41s  replicaset-controller  Created pod: my-helloworld-rs-zzznq

 

replicaset을 6으로 지정하여, pod 6개를 확인할 수 있다

 

# pod 확인

kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
my-helloworld-rs-28v29   1/1     Running   0          13m
my-helloworld-rs-kkklm   1/1     Running   0          13m
my-helloworld-rs-r55cx   1/1     Running   0          13m
my-helloworld-rs-tmbcr   1/1     Running   0          13m
my-helloworld-rs-wlx6b   1/1     Running   0          13m
my-helloworld-rs-zzznq   1/1     Running   0          13m

 

 

ReplicaSet을 Service를 통해 노출

 

# replicaset을 Service를 통해 노출

# Expose ReplicaSet as a Service
kubectl expose rs <ReplicaSet-Name>  --type=LoadBalancer --port=80 --target-port=8080 --name=<Service-Name-To-Be-Created>
kubectl expose rs my-helloworld-rs  --type=LoadBalancer --port=80 --target-port=8080 --name=my-helloworld-rs-service

# Get Service Info
kubectl get service
kubectl get svc

 

# Service의 External IP를 통해, Pod 내 애플리케이션 접근

http://<External-IP-from-get-service-output>/hello

 

replicaset을 통해 6개의 pod가 생성되었고,

expose 명령어를 통해 Load Balancer(service)를 생성하여서

service의 외부 IP를 통해 접근 시, pod가 로드밸런싱 되는 것을 확인 가능

 

ReplicaSet의 High Availability

 

# pod 삭제

kubectl get pods
---
NAME                     READY   STATUS    RESTARTS   AGE
my-helloworld-rs-28v29   1/1     Running   0          17m
my-helloworld-rs-kkklm   1/1     Running   0          17m
my-helloworld-rs-r55cx   1/1     Running   0          17m
my-helloworld-rs-tmbcr   1/1     Running   0          17m
my-helloworld-rs-wlx6b   1/1     Running   0          17m
my-helloworld-rs-zzznq   1/1     Running   0          17m

 

여기서 한 개의 pod를 삭제해보자

kubectl delete pod my-helloworld-rs-28v29
pod "my-helloworld-rs-28v29" deleted

 

 

# pod 확인

kubectl get pods
---
NAME                     READY   STATUS    RESTARTS   AGE
my-helloworld-rs-kkklm   1/1     Running   0          17m
my-helloworld-rs-q7gpp   1/1     Running   0          6s
my-helloworld-rs-r55cx   1/1     Running   0          17m
my-helloworld-rs-tmbcr   1/1     Running   0          17m
my-helloworld-rs-wlx6b   1/1     Running   0          17m
my-helloworld-rs-zzznq   1/1     Running   0          17m
PS C:\Users\serahp\azure-aks-kubernetes-masterclass\03-Kubernetes-Fun

 

28v29가 삭제 되고, q7gpp라는 새로운 pod가 생성되어 파드 수가 6개로 유지되는 것을 확인 가능

이는 replicaset의 High Availability 기능 때문이다.

 

ReplicaSet의 Scalability

 

# replicaset-demo.yaml 파일 수정

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: my-helloworld-rs
  labels:
    app: my-helloworld
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-helloworld
  template:
    metadata:
      labels:
        app: my-helloworld
    spec:
      containers:
      - name: my-helloworld-app
        image: stacksimplify/kube-helloworld:1.0.0

 

replicas를 6 -> 3으로 수정해보자

 

# replicaset 수정

kubectl replace -f replicaset-demo.yml 
replicaset.apps/my-helloworld-rs replaced

 

# replicaset 및 pod 확인

kubectl get rs
NAME               DESIRED   CURRENT   READY   AGE
my-helloworld-rs   3         3         3       28m
kubectl get pods -o wide
NAME                     READY   STATUS    RESTARTS   AGE   IP             NODE                                NOMINATED NODE   READINESS GATES
my-helloworld-rs-r55cx   1/1     Running   0          26m   10.224.0.70    aks-agentpool-29221675-vmss000000   <none>           <none>
my-helloworld-rs-tmbcr   1/1     Running   0          26m   10.224.0.149   aks-agentpool-29221675-vmss000001   <none>           <none>
my-helloworld-rs-wlx6b   1/1     Running   0          26m   10.224.0.95    aks-agentpool-29221675-vmss000000   <none>           <none>

 

pod 수가 3으로 자동적으로 조정된 것을 볼 수 있다.

 

# clean

kubectl get rs   
NAME               DESIRED   CURRENT   READY   AGE
my-helloworld-rs   3         3         3       28m
kubectl delete rs my-helloworld-rs
replicaset.apps "my-helloworld-rs" delete

[참고 영상]

Udemy - Azure Kubernetes Service with Azure DevOps and Terraform

섹션 4 : Kubernetes Fundamentals with kubectl - Imperative Approach

25. Step-12 : Introduction ot Kubernetes Deployments

 

[참고 문서]

https://github.com/stacksimplify/azure-aks-kubernetes-masterclass/tree/master/03-Kubernetes-Fundamentals-with-kubectl/03-02-ReplicaSets-with-kubectl