Imperative vs Declarative
Kubernetes is declarative: i.e. it focuses on the what is the desired result, rather than how to make the desired result.
Imperative Command
- Make 2 containers: depend on the current state, and thus the final result is affected by the initial state.
Declarative Command
- There should be 2 containers: only depend on the declarative statement, regardless of the inital/current state.
Reconciliation
Reconciliation: Managing the state of containers by consistently monitoring the bridge
- the difference between the desired and current state - and adjusting the configuration.
Value for developers
With reconciliation, developers gain value in 4 aspects:
-
Declarative
: All actions are performed declaratively. -
Idempotency
: Applying the same definition multiple times always yields the same result. -
Self-Healing
: Automatically recovering from unexpected inconsistencies in the current state. -
Consistency
: Aligning the state with the final specification regardless of the current state or operations in Kubernetes.
Reconciliation Example: Container Version Update
Assume the following situation:
- desired state: container built from image version
1.0.1
- current state: container is built from image version
1.0.0
If my kubernetes cluster do not have an image with version 1.0.1
, what would happen?
Because a bridge is monitored during reconciliation, the kubernetes cluster will try to create a new contiainer with image version 1.0.1
.
However, because there is no such image, kubernetes will fail to build the container, so the current container has image version as1.0.0
.
The bridge is still monitored, so kubernetes eventually enters an infinite loop.
Reconciliation Example: Restarting the container
Restarting containers is a bit ambiguous in Kubernetes because the concept of restarting is not easily defined in a declarative manner. While Kubernetes supports imperative commands that allow you to achieve restarts, it's not inherently designed for straightforward restarts.
If you want to restart a container, one approach is to remove the container, which effectively creates a bridge between the current state and the desired state. Alternatively, you can set the container count to 0 and then increase it again to achieve a restart.
Strength of Self-Healing
- Automatically corrects container states if they deviate from the desired state.
- Replace containers with new ones if processes terminate or containers become unhealthy.
- When utilized in an operational environment without user intervention, these features become incredibly powerful.
What should developers do?
Therefore, what developers should do is:
- Build applications that are easy for Kubernetes to observe.
- Define the desired final state accurately.
- Implement systematic version control or automated build and deployment processes.
- Develop applications that can handle sudden termination or restart without issues.