<p>I created a pod with <code>kubectl create -f pod.xml</code> and <code>kubectl apply -f pod.xml</code> using the below yaml and I don't see any difference, a pod gets created with both the commands. The <a href="https://kubernetes.io/docs/concepts/overview/object-management-kubectl/overview/" rel="noreferrer">K8S document</a>, mentions imperative and declarative commands. But, still the create and apply behave the same way.</p>

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

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

<p>There is a subtle difference between <code>kubectl create</code> and <code>kubectl apply</code> commands.</p>

<p>The <code>kubectl create</code> 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.</p>

<pre><code>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
</code></pre>

<p>2) The <code>kubectl apply</code> command applies the configuration to a resource. If the resource is not there then it will be created. The <code>kubectl apply</code> 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.</p>

<pre><code>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
</code></pre>

<p>In the <code>kubectl create</code>, we specify a certain action, in this case <code>create</code> and so it is <strong>imperative</strong>. In the <code>kubectl apply</code> command we specify the target state of the system and don't specify a certain action and so <strong>declarative</strong>. 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.</p>

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

<p>It took me some time to get around it, but it makes sense now.</p>
