Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between 'kubectl apply' and 'kubectl create'

I created a pod with kubectl create -f pod.xml and kubectl apply -f pod.xml using the below yaml and I don't see any difference, a pod gets created with both the commands. The K8S document, mentions imperative and declarative commands. But, still the create and apply behave the same way.

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  containers:
  - name: myapp-container
    image: busybox
    command: ['sh', '-c', 'echo Hello Kubernetes! && sleep 3600']

What's the difference? Also, how is kubectl apply declarative and kubectl create imperative? Both of them take one or multiple yaml files with the object details in it.

like image 769
Praveen Sripati Avatar asked Sep 25 '18 14:09

Praveen Sripati


People also ask

What is kubectl create?

kubectl create is for imperative management. In this approach, you tell the Kubernetes API what you want to create, replace, or delete. In simpler terms, create produces a new object (previously nonexistent or deleted). If a resource already exists, it will raise an error.

What does kubectl create command do?

The kubectl create service command creates the configuration for the Service and saves it to /tmp/srv. yaml . The kubectl create --edit command opens the configuration file for editing before it creates the object.

What is the opposite of kubectl apply?

It's really important to remember that technically there isn't an inverse to kubectl apply . This is because of how k8s works, it converges desired state against current state. By running apply , you are telling the cluster to "make it look like this".

Does kubectl apply overwrite?

kubectl apply .. will use various heuristics to selectively update the values specified within the resource. kubectl replace ... will replace / overwrite the entire object with the values specified. This should be preferred as you're avoiding the complexity of the selective heuristic update.


1 Answers

There is a subtle difference between kubectl create and kubectl apply commands.

The kubectl create command creates a new resource. So, if the command is run again it will throw an error as resource names should be unique in a namespace.

kubectl get pods
No resources found.

kubectl create -f pod.xml 
pod/myapp-pod created

kubectl create -f pod.xml 
Error from server (AlreadyExists): error when creating "pod.xml": pods "myapp-pod" already exists

2) The kubectl apply command applies the configuration to a resource. If the resource is not there then it will be created. The kubectl apply command can be run the second time as it simply applies the configuration as shown below. In this case, the configuration hasn't changed. So, the pod hasn't changed.

kubectl delete pod/myapp-pod
pod "myapp-pod" deleted

kubectl apply -f pod.xml 
pod/myapp-pod created

kubectl apply -f pod.xml 
pod/myapp-pod unchanged

In the kubectl create, we specify a certain action, in this case create and so it is imperative. In the kubectl apply command we specify the target state of the system and don't specify a certain action and so declarative. We let the system decide what action to take. If the resource is not there it will create it, if the resource is there then it will apply the configuration to the existing resource.

From an execution perspective, there is no difference when a resource is created for the first time between kubectl create and kubectl apply as shown above. But, the second time the kubectl create will throw an error.

It took me some time to get around it, but it makes sense now.

like image 109
Praveen Sripati Avatar answered Oct 24 '22 17:10

Praveen Sripati