CLI를 이용한 kubectl 명령어의 apply 와 create 차이점은 apply 는 선언적(declaritive)이라는 데 반해 create 은 명령적(imperative)이라는 것이다.

 

Declarative: 목적지를 알려주고 step by step 으로 도달하기 위한 단계를 생략

Imperative: 무엇을 어떻게 목적지까지 갈지 명확한 가이드 존재

 

위 내용을 좀 더 자세히 설명하자면 다음과 같다.

 

보통, 리소스의 상태가 manifest 파일에 기록되면 kubectl apply로 해당 상태를 배포한다.

 

이에 반해, kubectl create 명령어는 바로 CLI를 통해 쿠버네티스 리소스를 생성한다.

kubectl create도 manifest 파일을 이용해서 리소스의 새 인스턴스를 생성할 수 있으나, 만약 이미 해당 인스턴스가 존재하고 있었다면 에러가 발생한다.

 

  • Example of kubectl apply

 

예를 들어서 아래 yml 파일은 2개의 nginx 리플리카를 배포한다고 기술하고 있다.

 

newdeployment.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: newdeployment
  labels:
    app: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80

 

kubectl apply로 해당 파일을 배포하면 metadata의 이름 newdeployment로 기술된 내용이 배포된다.

kubectl apply -f newdeployment.yml

result:

deployment/newdeployment created

 

kubectl get deployment 명령어로 배포된 내용을 확인한다.

NAME           READY   UP-TO-DATE   AVAILABLE   AGE
newdeployment   2/2     2            2           5m10s

해당 결과로 newdeployment라는 배포로 2개의 pod가 준비된 것을 확인할 수 있다.

 

 

  • Example of kubectl create

kubectl create을 사용하여 명령적으로 새 배포를 생성할 수 있다.

kubectl create deployment directdeployment --image=nginx
deployment.apps/directdeployment created

 

만약 방금 생성한 존재하는 배포이름과 동일하게 생성을 시도하면 이미 존재한다며 오류가 발생한다.

kubectl create deployment newdeployment --image=nginx
Error from server (AlreadyExists): deployments.apps "newdeployment" already exists

 

  • 차이점 정리

만약 replica 갯수를 2에서 3으로 증가시켜야 한다면, 위에 기술했던 yml 파일의 replica 갯수를 2에서 3으로 수정한 후,

kubectl apply로 배포하기만 하면 된다.

kubectl apply가 수정된 manifest파일을 참조해서 리소스를 알아서 업데이트하는 반면,

kubectl create 은 이미 존재하는 배포 이름이 아닌 오직 새 리소스를 생성할 때 명시적으로 사용할 수 있다.

 

 

  • 이외 해당 명령어의 부가적인 옵션

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands

 

Kubectl Reference Docs

 

kubernetes.io

 

추가 참조: https://kubernetes.io/docs/tasks/manage-kubernetes-objects/imperative-command/

+ Recent posts