Maintaining Desired State
Kubernetes controllers continually check the desired state specified by each resource against the actual state of the cluster, and make any necessary adjustments to keep them in sync. This process is called the reconciliation loop, because it loops forever, trying to reconcile the actual state with the desired state.
For example, when you first create the demo
Deployment, there is no demo
Pod running. So Kubernetes will start the required Pod immediately. If it were ever to stop, Kubernetes will start it again, so long as the Deployment still exists.
Let’s verify that right now by stopping the Pod manually. First, check that the Pod is indeed running:
kubectl get pods --selector app=demo
NAME READY STATUS RESTARTS AGE
demo-54df94b7b7-qgtc6 1/1 Running 1 22h
Now, run the following command to stop the Pod:
kubectl delete pods --selector app=demo
pod "demo-54df94b7b7-qgtc6" deleted
List the Pods again:
kubectl get pods --selector app=demo
NAME READY STATUS RESTARTS AGE
demo-54df94b7b7-hrspp 1/1 Running 0 5s
demo-54df94b7b7-qgtc6 0/1 Terminating 1 22h
You can see the original Pod shutting down (its status is Terminating
), but it’s already been replaced by a new Pod, which is only five seconds old. That’s the reconciliation loop at work.
You told Kubernetes, by means of the Deployment you created, that the demo
Pod must always be running. It takes you at your word, and even if you delete the Pod yourself, Kubernetes assumes you must have made a mistake, and helpfully starts a new Pod to replace it for you.
Once you’ve finished experimenting with the Deployment, shut it down and clean up using the following command:
kubectl delete all --selector app=demo
pod "demo-54df94b7b7-hrspp" deleted
service "demo" deleted
deployment.apps "demo" deleted