Kubernetes - What exactly is imperative Vs Declarative

2/15/2020

I am seeing multiple and different explanations for imperative Vs Declarative for Kubernetes - something like Imperative means when we use yaml files to create the resources to describe the state and declarative vice versa.

what is the real and clear difference between these two. I would really appreciate if you can put the group of commands fall under the same - like Create under imperative way etc ..

-- Nag
kubernetes

2 Answers

2/15/2020

"Imperative" is a command - like "create 42 widgets".

"Declarative" is a statement of the desired end result - like "I want 42 widgets to exist".

Typically, your yaml file will be declarative in nature: it will say that you want 42 widgets to exist. You'll give that to Kubernetes, and it will execute the steps necessary to end up with having 42 widgets.

"Create" is itself an imperative command, but what you're creating is a Kubernetes cluster. What the cluster should look like is determined by the declarations in the yaml file.

-- user12904511
Source: StackOverflow

2/16/2020

Imperative

Official docs on Managing Kubernetes Objects Using Imperative Commands.

Kubernetes objects can quickly be created, updated, and deleted directly using imperative commands built into the kubectl command-line tool.

kubectl run nginx  --generator=run-pod/v1 --image=nginx

kubectl create service nodeport <myservicename>

kubectl delete pod

Declarartive

Kubernetes objects can be created, updated, and deleted by storing multiple object configuration files in a directory and using kubectl apply to recursively create and update those objects as needed. This method retains writes made to live objects without merging the changes back into the object configuration files. kubectl diff also gives you a preview of what changes apply will make.

Official docs on Declarative Management of Kubernetes Objects Using Configuration Files.

Official docs on Declarative Management of Kubernetes Objects Using Kustomize

Define what you want in an yaml file and use kubectl apply

kubectl apply -f app.yaml

kubectl apply -f <directory>/

kubectl apply -f https://k8s.io/examples/application/simple_deployment.yaml
-- Arghya Sadhu
Source: StackOverflow