반응형

 

개요

쿠버네티스의 기본 단위는 Pod 이다. 
그리고 이러한 Pod 를 이용하여 rs, deploy, job .. 등등 많은 형태가 정의 된다.
쿠버네티스는 어떻게 리소스를 관리하고, 운영되는지 쿠버네티스의 핵심, 컨트롤러에 대해 정리하려 한다.
내용은 쿠버네티스 공식 docs 를 참고한다.
Kubernetes Documentation | Kubernetes

 

Kubernetes Documentation

Kubernetes is an open source container orchestration engine for automating deployment, scaling, and management of containerized applications. The open source project is hosted by the Cloud Native Computing Foundation.

kubernetes.io

 

 

K8S Controller

컨트롤러란 ?

컨트롤러는 쿠버네티스의 컴포넌트이며,
특정 리소스를 지속적으로 바라보며 생명주기에 따라 정해진 작업을 수행하는 주체
현재 상태 (current state) , 바라는 상태 (desired state) 에 의하여 상태를 계속 관리함 
rs, deploy, job, daemonset 등은 쿠버네티스 내장 컨트롤러로 kube-controller-manager 컴포넌트 안에서 동작

ReplicaSet

Pod 를 복제 하는 컨트롤러, 1개의 Pod 가 문제가 생기더라도 다른 Pod 를 이용하여 동일한 서비스 제공을 목적

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: frontend
  labels:
    app: guestbook
    tier: frontend
spec:
  # modify replicas according to your case
  replicas: 3
  selector:
    matchLabels:
      tier: frontend
  template:
    metadata:
      labels:
        tier: frontend
    spec:
      containers:
      - name: php-redis
        image: us-docker.pkg.dev/google-samples/containers/gke/gb-frontend:v5

공식문서의 기본 Example 을 참고하면 위와 같다.

타이틀 정보
replicas  복제할 Pod 의 개수를 정의,
desired state 로 ReplicaSet 은 해당 Pod 의 개수를 유지
selector.matchLabels 라벨링을 이용하여 복제 개수를 유지할 Pod 선택,
tier: frontend 라벨을 가진 파드 개수 유지
template 복제할 Pod 를 정의

 

kubectl apply -f https://kubernetes.io/examples/controllers/frontend.yaml

위 명령어를 통해 replicaset 을 생성한다.

kubectl get rs

NAME       DESIRED   CURRENT   READY   AGE
frontend   3         3         3       6s

replicaset 을 조회하면 위와 같이 표시됨

타이틀 정보
DESIRED 사용자가 원하는 Pod 의 개수, 위에서 명시한 replicas 
CURRENT 현재 생성 된 Pod 의 개수
READY 생성된 Pod 중 준비가 완료된 Pod (readiness Probe)
kubectl get pods

NAME             READY   STATUS    RESTARTS   AGE
frontend-gbgfx   1/1     Running   0          10m
frontend-rwz57   1/1     Running   0          10m
frontend-wkl7w   1/1     Running   0          10m

pod 를 조회하면 replicaset 의 이름 - xxxx 으로 Pod 가 생성된 것을 볼 수 있다.
이렇게 생성된 Pod 는 tier: frontend 라벨을 가지고 있으며, 해당 Pod 를 강제로 삭제하면 새로운 Pod 가 생성된다.
replicaset 의 역할을 일정 개수의 복제본을 유지시키기 때문에 Pod 의 개수를 체크하기 때문이다.

kubectl scale rs --replicas 4 frontend

이런식으로 frontend replicaset 의 replicas 개수를 조절할 수 있다.

Deployment

Deployment 리소스는 ReplicaSet 과 유사하지만 애플리케이션 업데이트 및 배포에 특화된 기능을 가짐

  • 롤링 업데이트를 지원, 롤링 시 Pod 비율 조절
  • 업데이트 히스토리를 저장하고 롤백 기능 제공
  • ReplicaSet 과 마찬가지로 Pod 개수 조절 가능
  • 배포 상태 확인 가능 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
타이틀 정보
replicas 유지할 Pod 의 개수 정의
selector.matchLabels 배포를 수행할 Pod 를 label 시스템을 이용하여 지정
strategy.type 배포전략 종류 선택, RollingUpdate, Recreate 등.
RollingUpdate 일 경우 maxUnavailable, maxSurge 사용 가

 

kubectl apply -f https://k8s.io/examples/controllers/nginx-deployment.yaml

위 명령어를 통해 deployment 을 생성한다.

kubectl get deployments

NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   0/3     0            0           1s

deployment 를 조회하면 위와 같음.

deployment 를 배포해보자. 
rollout은 여러개의 pod를 모두 죽이지않고 순차적으로 업데이트하는 방식이다.

kubectl rollout status deployment/nginx-deployment

Waiting for rollout to finish: 2 out of 3 new replicas have been updated...
deployment "nginx-deployment" successfully rolled out



kubectl get deployments

NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3/3     3            3           18s



kubectl get rs

NAME                          DESIRED   CURRENT   READY   AGE
nginx-deployment-75675f5897   3         3         3       18s



kubectl get pods --show-labels

NAME                                READY     STATUS    RESTARTS   AGE       LABELS
nginx-deployment-75675f5897-7ci7o   1/1       Running   0          18s       app=nginx,pod-template-hash=75675f5897
nginx-deployment-75675f5897-kzszj   1/1       Running   0          18s       app=nginx,pod-template-hash=75675f5897
nginx-deployment-75675f5897-qqcnn   1/1       Running   0          18s       app=nginx,pod-template-hash=75675f5897

배포가 완료되면 deployment, rs, pods 를 순차적으로 조회해보자.
위와 같이 모두 생성되는 것을 확인할 수 있다.

 

쿠버네티스 리소스 구성도

위의 컨트롤러 개념을 적용하면 쿠버네티스의 리소스 개념은 아래 도식과 같다.

반응형