본문 바로가기
개발공부 개발새발/Kubernetes

kubernetes ) ReplicaSet Service Deployment

by 휴일이 2025. 6. 6.

복제 컨트롤러

파드의 앱이 다운됐을 때를 대비해서 파드를 복제하거나, 또는 하나의 파드가 내려갔을 경우 그 파드를 대체하기 위해 파드를 복제한다.

  • 사용자가 늘어날 경우를 대비해 로드밸런싱과 스케일링도 가능.

복제 컨트롤러와 복제본 세트

복제 컨트롤러 Replication controller

  • 옛날 기술, 복제본 세트로 대체되고 있음

복제본 세트 Replica Set

  • 복제를 사용하는 권장 방법.
  • 레플리카 셋에 관리할 파드에 대한 정보를 직접 작성해도 되지만 이미 실행 중인 파드의 라벨을 맞춰서 관리도 가능하다.

복제 컨트롤러

apiVersion: v1
kind: ReplicationController
metadata:
  name: myapp-rc
  labels:
    app: myapp
    type: front-end
spec:
  template:
    metadata:
      name: myapp-pod
      labels:
        app: myapp
        type: Pod
    spec:
      containers:
      - name: nginx-container
        image: nginx

  replicas: 3

레플리카셋

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: myapp-replicaset
  labels:
    app: myapp
    type: front-end

spec:
  template:
    metadata:
      name: myapp-pod
      labels:
        app: myapp
        type: front-end
    spec:
      containers:
      - name: nginx-container
        image: nginx

  replicas: 3
  selector:
    matchLabels:
      type: front-end

명령어

kubectl create -f replicaset-definition.yml

kubectl get replicaset

# 이건 파드도 같이 삭제되니 주의
kubectl delete replicaset myapp-replicaset 

스케일링하는 레플리카 수 변경하기

# yml 파일의 replicas 값 수정해서 실행되는 레플리카셋 변경하기
kubectl replce -f replicaset-definition.yml

# yml 파일을 수정하지 않고 명령어로 변경하기
kubectl scale --replicas=6 -f replicaset-definition.yml

# type , name 으로 (라벨)로 수정
# replicaset - type , myapp-replicaset - name
kubectl sclae --replicas=6 replicaset myapp-replicaset

Deployments

다수의 파드(웹앱)가 레플리카셋으로 관리되어 올라간다. deployment 는 그 위 계층이다.

  • deployment 에 속한 파드들의 업데이트 , 변경, 취소, 배포 등을 한꺼번에 관리한다.

만드는 법 : 배포 정의 파일 만들기

apiVersion: apps/v1
kind: Deployment # Replicaset 과 다른 점은 이것뿐
metadata:
  name: myapp-replicaset
  labels:
    app: myapp
    type: front-end

spec:
  template:
    metadata:
      name: myapp-pod
      labels:
        app: myapp
        type: front-end
    spec:
      containers:
      - name: nginx-container
        image: nginx

  replicas: 3
  selector:
    matchLabels:
      type: front-end

실행

kubectl create -f deployment.yml

kubectl get deployments

kubectl get all # 모든 것 볼 수 있음.

설명

deployment 는 replicaSet 과 pod 를 묶어서 관리할 수 있게 해주는 것이다.

  • 상태를 선언하면 쿠버가 알아서 그 상태를 유지해준다(파드 갯수 등등)
  • 롤링 업데이트 : 컨테이너 이미지를 변경하면 순차적으로 pod 를 바꿔치기 해준다.
  • 문제가 생기면 이전 버전으로 자동 롤백 가능.
  • 오토스케일링 연동으로 필요에 따라 pod 수를 자동 조절 가능.
  • deployment 상태를 kubectl get deployment 로 실시간 확인 가능.

아키텍처

사용자 → kubectl apply -f deployment.yaml
             ↓
      Kubernetes API Server
             ↓
    Deployment Controller (Control Plane 구성요소)
             ↓
    ReplicaSet Controller (개수 유지 책임)
             ↓
        N개 Pod 생성
             ↓
    kubelet (각 노드에서 Pod 실행)

 

 

#Create an NGINX Pod
kubectl run nginx --image=nginx

#Generate POD Manifest YAML file (-o yaml). Don't create it(--dry-run)
kubectl run nginx --image=nginx --dry-run=client -o yaml

#Create a deployment
kubectl create deployment --image=nginx nginx

#Generate Deployment YAML file (-o yaml). Don't create it(--dry-run)
kubectl create deployment --image=nginx nginx --dry-run=client -o yaml

#Generate Deployment YAML file (-o yaml). Don’t create it(–dry-run) and save it to a file.
kubectl create deployment --image=nginx nginx --dry-run=client -o yaml > nginx-deployment.yaml

#Make necessary changes to the file (for example, adding more replicas) and then create the deployment.
kubectl create -f nginx-deployment.yaml

OR

#In k8s version 1.19+, we can specify the --replicas option to create a deployment with 4 replicas.
kubectl create deployment --image=nginx nginx --replicas=4 --dry-run=client -o yaml > nginx-deployment.yaml

서비스

앱 안팎의 요소와 통신을 가능하게 한다.

  • 가령 사용자 ↔ 프론트엔드 앱 그리고 프론트엔드 앱 ↔ 백엔드 앱, 백엔드 ↔ DB
  • 나는 파드에 ssh 로 직접 엑세스하는 게 아니라 웹서버로 엑세스하고싶따.. 그럴 때 서비스를 통한다.
    • 서비스를 통해 노드+포트로 접속하여 웹서버에 접속한다.

종류

  1. 노드 포트
    • 서비스가 내부 파드를 노드의 포트에 엑세스할 수 있도록 함.
  2. ClusterIP
    • 클러스터 내에서 가상 IP 를 만들어 다른 서비스 간의 통신을 가능하게 한다.
    • 백엔드 ↔ 프론트엔드
  3. 로드밸런서
    • 클라우드에게 우리 앱을 위해 로드밸런서를 프로비전하기
    • 다양한 웹 서버에 로드 배포

Node Port

 

yml

apiVersion: v1
kind: Service
metadata:
  name: myapp-service

spec:
  type: NodePort
  ports:
   - targetPort: 80
     port: 80
     nodePort: 30008
  selector:
    # 이건 pod 의 labels 를 따온다고 생각하면 됨. 해당 라벨이 붙은 파드에 적용한다는 것.
    app: myapp
    type: front-end
  • 저 라벨이 붙은 파드에 전부 붙기 때문에 파드가 스케일아웃되더라도 걱정 ㄴㄴ
  • 기본 랜덤 알고리즘
  • 여러 노드에 있는 파드여도 같은 포트에 붙기 가능

Cluster IP

백엔드 서버의 파드가 많아지더라도 백엔드 서버와 통신하는 IP는 하나로 고정되어야 함 그걸 위해 존재

apiVersion: v1
kind: Service
metadata:
  name: back-end

spec:
  type: ClusterIP
  ports:
   # 백엔드 노출 포트
   - targetPort: 80
   # 서비스 포트
     port: 80
     
  # 이걸로 어느 파드에 접근할지 정함
  selector:
    app: my-app
    type: back-end

Load Balancer

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  type: LoadBalancer
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  • 외부에서 접근 가능한 퍼블릭 IP를 갖는 로드밸런서를 만든다.(aws 등)
  • 이 ip로 들어오는 트래픽을 뒷단의 pod 로 분산한다.
    • 내부적으로 클라우드 api 를 호출해서 실제 L4/L7 로드밸런서를 생성한다.

→ 클라우드 환경 아니면 사용 안 됨

728x90