mingming

kubernetes controller - ReplicaSet 본문

kubernetes

kubernetes controller - ReplicaSet

mingming_96 2023. 9. 3. 22:55

ReplicaSet

- ReplicationController 와 같은 역할을 하는컨트롤러

- ReplicationController 보다 풍부한 Selctor를지원 

 

  selector:
    matchLabels:
      component: redis
    matchExpressions:
    - {key: tier, operator: In, values: [cache]}
    - {key: environment, operator: NotIn, values: [dev]}

  

- matchExpression 연산자

  In: key와 values를 지정하여 key, value가 일치하는 Pod만 연결

  NotIn: key와 일치하고 value는 일치하지 않는 Pod에 연결

  Exists: key에 맞는 label의 Pod를 연결

  DoesNotExist: key와 다른 label의 Pod를 연결 

 

ReplicationController 

spec:
  replicas: 3
  selector:
    app: webui
    version: "2.1"
  template:

 ReplicaSet

spec:
  replicas: 3
  selector:
    matchLabels:
      app: webui
    matchExressions:
    - {key: version, operator: In, value: ["2.1"]}
  template:

위 두 개의 yaml파일은 같은 내용을 담고있습니다. 

 

nginx-rc.yaml

apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx-rc
spec:
  replicas: 3
  selector:
    app: webui
  template:
    metadata:
      name: nginx-pod
      labels:
        app: webui
    spec:
      containers:
      - name: nginx-container
        image: nginx:1.14

 

nginx-rs.yaml

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: nginx-rs
spec:
  replicas: 3
  selector:
    matchLabels:
      app: webui
  template:
    metadata:
      name: nginx-pod
      labels:
        app: webui
    spec:
      containers:
      - name: nginx-container
        image: nginx:1.14

작성한 ReplicaSet.yaml 을 배포해줍니다. 

kubectl apply -f nginx-rs.yaml

 

개수 조절

kubectl scale rs nginx-rs --replicas=2

풍부한 Selector 지원하는 것을 빼면 기본적으로 ReplicationController와 매우 유사합니다. 

 

Pod는 유지하면서 ReplicaSet 만 삭제하기 

kubectl delete rs nginx-rs --cascade=false

ReplicaSet은 삭제되었지만 Pod는 여전히 동작중입니다. 

 

EXAMPLE

1. 다음의 조건으로 ReplicaSet을 사용하는 rc-lab.yaml 파일을 생성하고 동작시킵니다.

- labels(name: apache, app:main, rel:stable)를 가지는 httpd:2.2 버전의 Pod를 2개 운영합니다.

rs name: rs-mainui

container: httpd:2.2

 

rc-lab.yaml

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: rs-mainui
spec:
  replicas: 2
  selector:
    matchLabels:
      app: main
  template:
    metadata:
      name: http-pod
      labels:
        name: apache
        app: main
        rel: stable
    spec:
      containers:
      - name: apache
        image: httpd:2.2

 

2. 동작되는 httpd:2.2 버전의 컨테이너를 1개로 축소하는 명령을 적고 실행하세요 

kubectl scale rs rs-mainui --replicas=1