Home

imperative declarative

The imperative way is to use kubectl commands. //Create Objects

kubectl run --image=nginx nginx
kubectl create deployment --image=nginx nginx
kubectl expose deployment nginx --port 80

Update Objects

kubectl edit deployment nginx
kubectl scale deployment nginx --replicas=5
kubectl set image deployment  nginx nginx=nginx:1.8

These commands are only available in session history once executed

Editing the configuration file does not change the source object file. Instead, a better way would be to change the source object file and use the replace command

kubectl replace -f nginx.yaml

To completely delete the resources and recreate objects use:

kubectl replace --force -f nginx.yaml

The declarative way is to apply changes declared in a configuration file/manifest file/object file. We use "kubectl apply" commands Apply command is intelligent enough to create an object if it is not created.

Multiple configuration files can be passed using apply command.

kubectl apply -f /path/to/configuration/files

How kubectl apply works: Local file, last applied configuration and Kubernetes

When an object is created, a live configuration file is created inside Kubernetes. It has information that was written into the local configuration file and some extra information is added into it.

When kubetcl apply command is executed, an intermediary step is added wherein the configuration file is converted into json file and is stored as the last applied configuration.

The live configuration file is compared with the local configuration file. If any change is observed the live configuration file is updated. And then the last applied configuration file is updated to the latest changes.

The last configured configuration helps us figure out what configuration has been removed from the local file and that info is removed from the live configuration.

The last applied configuration is present inside the live configuration file under annotation, last-applied-configuration.