카테고리 없음

Imperative and Declarative

필유아사 2022. 5. 6. 01:37

명령 및 선언(Imperative and Declarative)


명령형 접근 방식에서는 작업을 수행할 대상과 방법을 지정해야 합니다.
예를 들어 작업이 샌드위치를 ​​준비하는 것이라면 필요한 항목과 단계별 지침을 설명합니다.

선언적 접근 방식에서는 무엇을 할 것인지 지정해야 하지만 어떻게 할 것인지 지정해야 하는 것은 아닙니다.
이 경우 지침 없이 샌드위치에 필요한 항목만 설명합니다.

 

apply vs create 비교

  kubectl apply kubectl create
1. It directly updates in the current live source with only the attributes that are given in the file. It creates resources from the file provided. It shows an error if the resource has already been created.
2. The file used in apply can be an incomplete spec. The file used in create should be complete.
3. apply works only on some properties of the resources. create works on every property of the resources..
4. You can apply a file that changes only an annotation without specifying any other properties of the resource. If you use the same file with a replace command, the command will fail due to the missing information.

- 작동방식 비교

For this example, let’s use the YAML file below to create a Kubernetes pod.

yaml
→ cat sample-pod.yml

---
apiVersion: v1
kind: Pod
metadata:
 name: kubectl-create-vs-apply-demo
 labels:
  app: front-end
  rel: dev
spec:
 containers:
 - name: httpd
  image: docker.io/httpd
  imagePullPolicy: IfNotPresent
  ports:
   - containerPort: 80

Now create the pod using the imperative approach with kubectl create:

shell

→ kubectl create -f sample-pod.yml
pod/kubectl-create-vs-apply-demo created

Let’s verify the pod status along with labels:

shell


→ kubectl get pods --show-labels

NAMEREADYSTATUSRESTARTSAGELABELS
kubectl-create-vs-apply-demo 1/1 Running 0 98s app=front-end,rel=dev

Now we’ll edit the YAML file and add an extra label to it (environment: local):

yaml
→ cat sample-pod.yml

---
apiVersion: v1
kind: Pod
metadata:
 name: kubectl-create-vs-apply-demo
 labels:
  app: front-end
  rel: dev
  environment: local
spec:
 containers:
 - name: httpd
  image: docker.io/httpd
  imagePullPolicy: IfNotPresent
  ports:
   - containerPort: 80

Once again, use the imperative approach to apply the changes:

shell

→ kubectl create -f sample-pod.yml
Error from server (AlreadyExists): error when creating "sample-pod.yml": pods "kubectl-create-vs-apply-demo" already exists

Note the error message saying the resource kubectl-create-vs-apply-demo already exists.

Now we’ll do the same operation using the declarative approach with kubectl apply:

shell

→ kubectl apply -f sample-pod.yml
pod/kubectl-create-vs-apply-demo configured

This time, the resource was configured. Let’s verify the changes we made.