Let's suppose a K8s cluster exists with a deployment with 3 replicas (shouldn't be relevant, but let's suppose it is an AKS cluster).
The container runs .NET 6 code with the K8s .NET client library. We can read the host pod id, but can we read the host pod replica number?
For example, let's suppose the 3 replicas are hosted in pods named podA, podB, podC. Then podB crashes. K8s will restart the replica but with a different pod name, let's say podD. How can I find, programmatically, that the just started pod is exactly the resuscitated podB?
CodePudding user response:
First of all, When a pod crashes, k8s doesn't necessarily change the name of the pod, Because as you said, it's restarted, not recreated. Restart will appear and number will increase incrementally in RESTARTS
column of kubectl get pods -n <namespace>
command output. If every restart would create a new pod, there would not have been necessary to have RESTARTS
column in the first place.
Second, in your example, there is no way to assign PodB
's totally random name to PodD
in deployment. Because k8s assigning them to deployment pods randomly. If you want to accomplish this, you should use StatefulSet
instead of Deployment
Because each pod in a StatefulSet has a unique and stable network identity.
As k8s document points ;
Like a Deployment, a StatefulSet manages Pods that are based on an identical container spec. Unlike a Deployment, a StatefulSet maintains a sticky identity for each of their Pods. These pods are created from the same spec, but are not interchangeable: each has a persistent identifier that it maintains across any rescheduling.
Reference : https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/