쿠버네티스/기초

컨트롤러 (기초) ReplicaSet, Controller

라리음 2022. 8. 5. 16:02

이 글은 인프런의 대세는 쿠버네티스 강의를 보며 정리한 글입니다.

 

 

 

 

Controller의 기능 몇가지

 

1. Auto Healing (죽은 pod를 다른 노드에 같은 pod로 생성)

2. Auto Scaling (트래픽 분산을 시켜 pod가 안죽게 만듬)

3. Software Update (버전 업데이트와 장애가 생겼을 때 롤백)

4. Job (일시적인 작업을 할 때 pod을 생성해 작업 후 pod를 죽임 <효율적>

 

 

Controller와 Pod의 연결부분은 label과 selector임

 

 

 

컨트롤러 몇가지

 

1. Replication Controller (Deprecated)

2. ReplicaSet (Replaced)

(Template, Rep)

 

 

 

 

1. Template

 

 

selector에 라벨의 내용으로 pod와 연결해야 하며 pod가 죽었을 때 재생성도 해줌

 

업데이트를 하고 pod를 죽이면 새로운 pod를 생성할때 업데이트 된 속성을 지닌 pod를 생성

 

 

 

2. Replicas

 

replicas에서 정의한 갯수 만큼 pod를 생성 (scale out)

in and out이 가능

 

 

 

3. Selector

 

ReplicaSet에만 있음.

 

matchLabels

- key와 value를 보고 pod와 연결

 

matchExpressions

- key와 value 정확히 같지 않아도 pod를 선택 가능함 (포함만 하면 된다 이런식)

operator에는 exists, DoesNotExist, In, NotIn과 같은 속성으로 자유롭게 설정 가능

 

 

 

 

 

 

 

 

 

 

실습

 

 

 

 

1. Template, Replicas


1-1) Pod 생성

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

 

1-2) ReplicaSet 생성

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



- replicas의 수를 변경하면 그만큼의 수를 유지하려 함

 

컨트롤러를 지우면 기본적으로 연결된 파드들도 사라짐

- 옵션을 이용해서 그러한 속성을 없앨 수 도있음.

 

 

 

 

 

2. Updating Controller


ReplicationController -> ReplicaSet

ReplicationController

apiVersion: v1
kind: ReplicationController
metadata:
  name: replication1
spec:
  replicas: 2
  selector:
    cascade: "false"
  template:
    metadata:
      labels:
        cascade: "false"
    spec:
      containers:
      - name: container
        image: kubetm/app:v1

kubectl

kubectl delete replicationcontrollers replication1 --cascade=false

ReplicaSet

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: replica2
spec:
  replicas: 2
  selector:
    matchLabels:
      cascade: "false"
  template:
    metadata:
      labels:
        cascade: "false"
    spec:
      containers:
      - name: container
        image: kubetm/app:v1



3. Selector


3-1) ReplicaSet

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



Pod

apiVersion: v1
kind: Pod
metadata:
  name: pod-node-affinity1
spec:
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIngnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
  	       - {key: AZ-01, operator: Exists}
  containers:
  - name: container
    image: kubetm/init

Tip


MatchExpressions