backend/kubernetes in action

[kubernetes in action] 4-3 Using Replicasets instead of replicationController

seul chan 2020. 5. 28. 21:32

ch4. Replication and other controllers: deploying managed pods

4.3 Using Replicasets instead of replicationController

소챕터들

처음에는 ReplicationController가 pod를 replicating해주고 rescheduling 해주는 유일한 component였다. 나중에 비슷한 리소스인 ReplicaSet이 추가됨

ReplicationController는 결국 deprecated 될 예정

Kubernetes에서 처음 시작된 ReplicationController를 설명하는게 좋다고 생각하여 먼저 설명하였지만, 앞으로는 무조건 ReplicationController 대신 ReplicaSets을 사용하는게 좋음

Comparing a ReplicaSet to a ReplicationController

Replicasets은 ReplicationController와 동일하게 동작하지만, 더 express한 pod selector를 가진다.

ReplicaSet은 일치하는 label을 가진 pod 뿐만 아니라 value와 상관 없이 특정 label key를 가진 pod들을 사용할 수도 있다.

Defining a ReplicaSet

비슷하게 YAML을 만들어보지. kubia-replicaset.yaml

apiVersion: apps/v1beta2            1
kind: ReplicaSet                    1
metadata:
  name: kubia
spec:
  replicas: 3
  selector:
    matchLabels:                    2
      app: kubia                    2
  template:                         3
    metadata:                       3
      labels:                       3
        app: kubia                  3
    spec:                           3
      containers:                   3
      - name: kubia                 3
        image: luksa/kubia          3
    1. ReplicaSet은 v1 API 버전이 아니기 때문에 v1beta2를 사용
    1. ReplicationController에서 사용했던 것과 동일하지만 selector.matchLabels 형태로 사용

현재 (2020-05) 기준 ReplicaSet은 apps/v1 api version을 사용한다. 위 코드를 apiVersion: appds/v1로 변경하여야 작동한다.

Creating and examining a ReplicaSet

$ kubectl create -f kubia-replicaset.yaml
replicaset.apps/kubia created

$ kubectl get rs
NAME      DESIRED   CURRENT   READY     AGE
kubia     3         3         3         3s

Using the ReplicaSet's more expressive label selectors

ReplicationController에 비한 ReplicaSet의 주요 improvement는 더 expressive한 label selector를 사용할 수 있다는 것이다.

kubia-replicaest-matchexpressions.yaml을 만들고 기존의 selector를 다음과 같이 변경해보자.

selector:
   matchExpressions:
     - key: app                      1
       operator: In                  2
       values:                       2
         - kubia                     2

다음 4가지 operator 사용 가능

  • In
  • NotIn
  • Exists
  • DoesNotExist

Wrapping up ReplicaSets

다음과 같이 쉽게 제거 가능

$ kubectl delete rs kubia
replicaset.apps "kubia" deleted

물론 관리하는 pod들도 같이 사라진다.