I am creating deployments using Kubernetes API from my server. The deployment pod has two containers - one is the main and the other is a sidecar container that checks the health of the pod and calls the server when it becomes healthy.
I am using this endpoint to get the deployment. It has deployment status property with the following structure as mention here.
I couldn't understand the fields availableReplicas
, readyReplicas
, replicas
, unavailableReplicas
and updatedReplicas
.
I checked docs of Kubernetes and these SO questions too - https://stackoverflow.com/q/51085740/11642727 and https://stackoverflow.com/q/39606728/11642727 but could not infer the difference between of a pod being ready, running and available. Could somebody please explain the difference between these terms and states?
A different kinds of replicas
in the Deployment's Status can be described as follows:
Replicas
- describes how many pods this deployment should have. It is copied from the spec. This happens asynchronously, so in a very brief interval, you could read a Deployment where the spec.replicas
is not equal to status.replicas
.
availableReplicas
- means how many pods are ready for at least some time (minReadySeconds
). This prevents flapping of state.
unavailableReplicas
- is the total number of pods that should be there, minus the number of pods that has to be created, or ones that are not available yet (e.g. are failing, or are not ready for minReadySeconds
).
updatedReplicas
- the number of pods reachable by deployment, that match the spec template.
readyReplicas
- the number of pods that are reachable from deployment through all the replicas.
Let's use the official example of a Deployment that creates a ReplicaSet to bring up three nginx Pods:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
The Deployment creates three replicated Pods, indicated by the .spec.replicas
field.
Create the Deployment by running the following command:
kubectl apply -f https://k8s.io/examples/controllers/nginx-deployment.yaml
Run kubectl get deployments
to check if the Deployment was created.
If the Deployment is still being created, the output is similar to the following:
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 0/3 0 0 1s
When you inspect the Deployments in your cluster, the following fields are displayed:
NAME
- lists the names of the Deployments in the namespace.
READY
- displays how many replicas of the application are available to your users. It follows the pattern ready/desired.
UP-TO-DATE
- displays the number of replicas that have been updated to achieve the desired state.
AVAILABLE
- displays how many replicas of the application are available to your users.
AGE
- displays the amount of time that the application has been running.
Run the kubectl get deployments
again a few seconds later. The output is similar to this:
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 3/3 3 3 18s
To see the ReplicaSet (rs
) created by the Deployment, run kubectl get rs
. The output is similar to this:
NAME DESIRED CURRENT READY AGE
nginx-deployment-75675f5897 3 3 3 18s
ReplicaSet output shows the following fields:
NAME
- lists the names of the ReplicaSets in the namespace.
DESIRED
- displays the desired number of replicas of the application, which you define when you create the Deployment. This is the desired state.
CURRENT
- displays how many replicas are currently running.
READY
displays how many replicas of the application are available to your users.
AGE
- displays the amount of time that the application has been running.
As you can see there is no actual difference between availableReplicas
and readyReplicas
as both of those fields displays how many replicas of the application are available to your users.
And when it comes to the Pod Lifecycle it is important to see the difference between Pod phase, Container states and Pod conditions which all have different meanings. I strongly recommend going through the linked docs in order to get a solid understanding behind them.