In Kubernetes, effective state management is crucial for maintaining the stability and reliability of your clusters. Kubernetes relies on two key state concepts: Declared State and Observed State. These states are integral to the orchestration system’s operation, ensuring that applications run as intended. This guide will explore these concepts in depth, highlighting their key differences.
Table of Contents
Kubernetes States: Declared vs Observed States
Declared State (Desired State)
The declared state in Kubernetes, also known as, the desired state, represents the user’s intended configuration or specification for a specific Kubernetes resources. This state is defined declaratively using YAML or JSON manifests files.It encapsulates the desired attributes and configurations required for resources to function correctly.
The desired/declared state specifies characteristics such as the number of replicas, container images, resource limits, networking configurations, and other settings necessary for the resources to operate according to the user’s specifications.
Take for example, for a Deployment, the desired state may specify that three replicas of a web application should be running, using a specific container image and exposing a certain port.
In short, the declared or desired Kubernetes cluster state is essentially your blueprint of how you want your application to run in the cluster.
Observed State (Actual State)
On the other hand, Kubernetes cluster controllers dynamically determined the current state of Kubernetes resources, including their availability, health, and configuration etc. This is what is called observed or actual Kubernetes state.
The actual state reflects the real-time status and condition of resources within the Kubernetes cluster.
The Kubernetes control plane constantly monitors the cluster to ensure the observed state aligns with the desired state. This involves a continuous loop. For example:
- The control plane checks the desired state: It reads the deployment configurations, including the number of replicas and resource requests.
- The control plane observes the actual state: It checks how many pods are currently running for the deployment and their resource utilization.
- Discrepancy detected? Take action!: If the observed state deviates from the desired state, the control plane takes corrective measures. It might scale the deployment up or down, reschedule pods on healthy nodes, or initiate container restarts.
Importance of Resource States in Kubernetes
- Declarative Management: You define what you want, and Kubernetes takes care of the how. This simplifies application deployment and reduces configuration complexity.
- Self-Healing: Kubernetes automatically reconciles discrepancies in resource states, ensuring your applications remain available and resilient to failures.
- Predictability: By managing the desired state, you can predict the behavior of your applications and the overall cluster health.
- Consistency: Maintains a consistent environment even when changes or failures occur.
- Scalability: Facilitates the scaling of applications by managing the desired and actual number of instances.
- Reliability: Ensures that applications are running as expected.
- Recovery: Helps in the automatic recovery of applications from failures.
Conclusion
In summary, in Kubernetes, the declared state is the desired configuration of your cluster, while the observed state is the actual current condition. Managing these states effectively ensures that your applications run as intended.