본문 바로가기
Server-side 개발 & 트러블 슈팅/🚢 Kubernetes (쿠버네티스)

[minikube] 쿠버네티스 레플리카셋(replicaset)을 이용한 무중단 베포 가이드 (고가용성 유지)

by 코딩하는 동현 2025. 4. 30.

ReplicaSet: Pod 수명 관리를 책임지는 컨트롤러

1. ReplicaSet이란?

Kubernetes에서 ReplicaSet은 특정 수의 Pod 복제본이 항상 실행되도록 보장해주는 **컨트롤러(controller)**이다. 만약 어떤 Pod가 종료되거나 장애로 인해 사라지더라도, ReplicaSet은 새로운 Pod를 자동으로 생성하여 원하는 개수를 유지한다.

이는 특히 무중단 서비스 제공고가용성(HA, High Availability) 확보에 필수적인 요소다.


2. 왜 필요한가?

Kubernetes에서는 Pod 자체는 일시적인 존재다. 다음과 같은 상황을 생각해보자.

  • Pod가 예기치 않게 종료되면 수동으로 다시 띄워야 할까?
  • 서비스 확장을 위해 Pod 수를 늘리고 싶다면 어떻게 해야 할까?

이러한 반복적이고 수동적인 작업을 자동화하기 위해 ReplicaSet이 등장했다. 아래는 ReplicaSet의 주요 역할이다:

  • 원하는 수의 Pod 유지
  • Pod 자동 재생성
  • Selector 기반으로 기존 Pod와 연결

3. 구조 및 동작 원리

ReplicaSet은 다음 요소들로 구성된다:

  • replicas: 유지할 Pod의 수
  • selector: 어떤 Pod를 관리할지 결정
  • template: 관리할 Pod의 정의 (종종 새로 생성할 때 사용)

동작 흐름:

  1. 사용자가 replicas: 3을 정의하면,
  2. ReplicaSet은 selector 조건에 맞는 Pod를 찾는다.
  3. 부족한 수만큼 새 Pod를 생성하거나, 초과된 Pod를 제거한다.

4. YAML 문법 규칙

다음은 기본적인 ReplicaSet 정의 파일이다:

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: my-replicaset
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx-container
          image: nginx:1.25
          ports:
            - containerPort: 80

 

주요 문법 설명

필드 설명
apiVersion: apps/v1 ReplicaSet이 속한 API 그룹
kind: ReplicaSet 리소스 종류
metadata.name ReplicaSet 이름
spec.replicas 유지할 Pod 수
spec.selector 어떤 Pod를 관리할지 정의 (label 기반)
spec.template Pod 생성 시 사용할 템플릿

 

5. 실습: ReplicaSet 적용해보기

 

pods.yaml, service.yaml 파일이 기존에 있고 pod와 service 가동중이라고 가정하겠습니다.

 

도커 엔진 기반 minikube는 항상 아래와 같이 포트포워딩을 해줘야한다.

kubectl port-forward service/fleetman-webapp 8080:80 & kubectl port-forward service/fleetman-queue 8161:8161 &

 

1단계. 기존 : pod가 만약에 정지 된다면?

현재 webapp 서비스 상태 조회

kubectl describe svc fleetman-webapp

 

 

webapp pod를 모의로 중지 시킨다.

kubectl delete po webapp-release-0-5

 

kubectl get all을 하면 webapp-release-0-5 파드가 없어진것을 볼 수 있다. (전/후 사진)

 

 

pod가 삭제됐으므로 webapp 서비스에 접속이 안되는것을 확인 할 수 있다.

 

이것을 해결하기 위해서 레플리카셋을 사용할 수 있다.

 

2단계. 레플리카셋 작성 후 실행

1. pods.yaml, service.yaml 파일을 수정한다.

 

pods.yaml

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: webapp
spec:
  selector: 
    matchLabels: 
      app: webapp
  replicas: 1
  template: # templates for the pods
    metadata:
      labels:
        app: webapp
    spec:
      containers:
      - name: webapp
        image: richardchesterwood/k8s-fleetman-webapp-angular:release0-5-arm64
---
apiVersion: v1
kind: Pod
metadata:
  name: queue
  labels:
    app: queue
    release: "0"
spec:
  containers:
  - name: queue
    image: richardchesterwood/k8s-fleetman-queue:release1-arm64

 

 

service.yaml

apiVersion: v1
kind: Service
metadata:
  name: fleetman-webapp

spec:
  # This defines which pods are going to be represented by this Service
  # The service becomes a network endpoint for either other services
  # or maybe external users to connect to (eg browser)
  selector:
    app: webapp
  ports:
    - name: http
      port: 80
      nodePort: 30080

  type: NodePort

---
apiVersion: v1
kind: Service
metadata:
  name: fleetman-queue

spec:
  # This defines which pods are going to be represented by this Service
  # The service becomes a network endpoint for either other services
  # or maybe external users to connect to (eg browser)
  selector: 
    app: queue
  ports:
    - name: http
      port: 8161
      nodePort: 30010

  type: NodePort

 

 

3. 적용

kubectl apply -f 파일이름

 

 

4. 레플리카셋 조회

아래 명령어로 레플리카셋과 서비스를 조회할수 있다.

pod를 전부 레플리카 셋으로 대체한다.

 

kubectl get all

 

 

kubectl describe replicaset webapp

 

 

ReplicaSet vs ReplicationController

항목 ReplicaSet ReplicationController
등장 시기 Kubernetes 1.2 이후 초기 버전부터 존재
Selector 문법 matchLabels, matchExpressions 모두 지원 단순 labels만 지원
템플릿 변경 시 대응 유연함 제한적
추천 여부 ✅ 사용 권장 ❌ 구버전, 사용 지양

 

6. 레플리카 자동 복구 테스트

 

1. 레플리카셋 조회

kubectl get all

 

 

2. 레플리카셋의 pod에 붙은 고유 번호를 확인하고, 그 대상을 임의로 중시시킨다.

kubectl delete po webapp-zg888

 

기존 pod가 삭제되자마자 새로운 pod가 생겨나고 대체된 것을 볼 수 있다.

zg888 -> 7rmtx

 

도커 엔진 기반 minikube는 항상 아래와 같이 포트포워딩을 해줘야한다.

kubectl port-forward service/fleetman-webapp 8080:80 & kubectl port-forward service/fleetman-queue 8161:8161 &

 

임의로 삭제돼도 서비스가 정상적으로 진행된다.

 

 

3. 레플리카셋의 갯수를 늘려서 무중단 서비스를 구현해낼 수 있다.

 

pods.yaml 수정

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: webapp
spec:
  selector: 
    matchLabels: 
      app: webapp
  replicas: 2

조회시 레플리카셋이 2개로 유지돼있는것을 확인할 수 있고, 보통 인프라에서는 다른 워커 노드에 배치된다.

 

7. 실제 환경에서는 ReplicaSet 단독으로 쓰지 않는다

실제 서비스 환경에서는 ReplicaSet보다 Deployment를 더 많이 사용한다. 왜냐하면 Deployment는 내부적으로 ReplicaSet을 생성하면서도, 롤링 업데이트, 롤백 등 더 강력한 기능을 제공하기 때문이다.

반응형

댓글