Declarative vs Imperative
Let's see the difference between the declarative and the imperative method of performing updates.
We'll cover the following...
We’ve seen the following two ways to perform updates:
Declaratively: The declarative method is the preferred method and requires all updates to be performed through YAML configuration files.
Imperatively: The imperative method uses kubectl commands to perform updates and isn’t recommended for live production environments.
Why imperative method is not preferred?
Consider the following example to understand why we don't prefer scaling the applications imperatively.
We have a Deployment YAML file defining four replicas of a Pod that we deploy to our cluster. Everything is fine until demand increases and the app starts responding slowly. Somebody comes along and uses the kubectl scale
command to increase the number of replicas from four to eight. This fixes the slow response times, but the state of the cluster and the YAML file are no longer in sync — the cluster is running eight replicas, but the Deployment YAML file only defines four.
Sometime later, we need to push a new version of the app. To accomplish this, we open the YAML file, update the image version it references, save the changes, and re-post the file to the cluster. This successfully updates the version of the image, but it also decreases the number of replicas to four!
If demand is still high, the app will start responding slowly again, and we may think the problem is with the new version and not realize we accidentally reduced the number of replicas.
Reasons like this are why it’s considered a good practice to manage everything declaratively.