Imagine the following deployment definition in kubernetes:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
env: staging
spec:
...
I have two questions in particular:
1). The label env: staging
won’t be available in created pods. how can I access this data programmatically in client-go
?
2). When pod is created/updated, how can I found which deployment it belongs to?
2
Answers
You can get the
Deployment
using client-go. See the example Create, Update & Delete Deployment for operations on aDeployment
.When a
Deployment
is created, a ReplicaSet is created that manage thePods
.See the
ownerReferences
field of aPod
to see whatReplicaSet
manages it. This is described in How a ReplicaSet workshope you are enjoying your kubernetes journey !
In fact the label won’t be available in created pods but you can add it to the manifest, in the pod section:
you can check the pods’ labels by typing:
you can get the deployments’ labels by typing:
you can add a custom column in your "kubectl get po" command to display the value of each "app" labels when getting the pods:
and you can use multiple -L :
In general, the names of the pod begin by the name of their owner (deployment, replicaset, statefulset, job etc)
When you use a deployment to create a pod, you can be sure that between the deployment and the pod there is a replicaset (The deployment only manages the differents version of the replicaset, while the replicaset only ENSURES that the current number of actual replicas is matching the demanded number of replicas in the manifes, with labels selector ! )
So you in fact, checks the ownerReference filed of a pod, by typing:
can do the same with replicasets to get their deployments owner:
thats how you can quickly see withs kubectl who owns who
here is a little reading about owners and dependants: https://kubernetes.io/docs/concepts/overview/working-with-objects/owners-dependents/
hope this has helped you. bguess