Kubernetes - Replication Controller, ReplicaSet

Sungjin·2022년 3월 16일
0

Kubernetes

목록 보기
9/11
post-thumbnail

Replication Controller, ReplicaSet - Kubernetes


이 글은 김태민님의 대세는 쿠버네티스 강의를 참고하여 정리하였습니다!

출처 : https://www.inflearn.com/course/%EC%BF%A0%EB%B2%84%EB%84%A4%ED%8B%B0%EC%8A%A4-%EA%B8%B0%EC%B4%88/dashboard


🔍 테스트해 볼 내용

  1. Template, Replicas 구성

  2. Selector 구성

    도커 이미지는 김태민님께서 만들어두신 이미지를 사용합니다.


    🚀 Controller

    먼저, Controller를 사용하는 이유에 대하여 알아봅시다.

    가장 중요한 이유는 Service를 관리하고 운영하는데 도움을 주기 때문입니다.

  • Auto Scaling
    • Pod가 장애로 다운될 시에, 이 Pod를 다른 노드에 생성
  • Software Update
    • 여러 Pod의 버전을 업그레이드 할 때 Controller를 통해 쉽게 할 수 있고, 도중 문제가 생기면 롤백 가능
  • Job
    • 일시적인 작업을 해야하는 경우 Controller가 필요한 순간에만 Pod를 만들어서 해당 작업을 이행하고 삭제.

👍 추가로, Replication Controller 는 ReplicaSet의 노후화된 버전이라고 합니다!


🚀 Template, Replicas

Template

  • ControllerPodLabelSelector로 연결
  • Controller를 만들 때 Template으로 Pod를 지정
  • Pod가 다운되면 Template을 통해 Pod를 새로 만들 수 있읍.

Replicas

  • Controller 생성 시, 숫자를 정할 수 있음.
  • 정해진 숫자만큼 Pod의 수를 유지.

이제, 실습해보도록 합시다!

pod1.yaml

apiVersion: v1
kind: Pod
metadata:
  name: pod-1
  labels:
    type: web
spec:
  containers:
  - name: container
    image: kubetm/app:v1
  terminationGracePeriodSeconds: 0

rs1.yaml

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: replica1
spec:
  replicas: 1
  selector:
    matchLabels:
      type: web
  template:
    metadata:
      name: pod1
      labels:
        type: web
    spec:
      containers:
      - name: container
        image: kubetm/app:v1
      terminationGracePeriodSeconds: 0

이제 ReplicaSet에 연결된 Pod를 확인해 봅시다.

pod1이 잘 연결 된것을 볼 수 있습니다.

이제, replicas의 수를 늘려보도록 하겠습니다!

kubectl scale rs/replica1 --replicas=2

새로운 Pod가 생성된 것을 확인할 수 있습니다!

이제 Pod의 Version을 업데이트 하기 위해 ReplicaSet의 Template의 Version을 바꾼 후, 기존의 Pod를 삭제해보도록 하겠습니다.

kubectl edit rs/replica1 -o yaml

kubectl delete pods --all

Pod들의 Version이 수정된 것을 확인할 수 있습니다!


🚀 Selector

ReplicaSetSelector에는 Keylabels형태인 Selector 만 아닌,

matchExpressions라는 Selector로 이미 존재하는 Object를 좀 더 세세하게 정해줄 수 있다고 합니다.

하지만, ReplicaSet은 위에서 말했다 시피 Template을 따로 정해주기 때문에 Selector라는 기능 자체를 잘 사용하지는 않다고 합니다.

따라서 별도의 실습없이 yaml파일만 정의하는 법을 간단히 확인해봅시다!

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: replica1
spec:
  replicas: 1
  selector:
    matchLabels:
      type: web
      ver: v1
    matchExpressions:
    - {key: type, operator: In, values: [web]}
    - {key: ver, operator: Exists}
  template:
    metadata:
      labels:
        type: web
        ver: v1
        location: dev
    spec:
      containers:
      - name: container
        image: kubetm/app:v1
      terminationGracePeriodSeconds: 0

저런 식으로, matchExpressions를 정의해주면 된다고 합니다!


이상으로 마치겠습니다. 🙋🏻‍♂️

profile
WEB STUDY & etc.. HELLO!

0개의 댓글