Reputation: 18227
We have created deployments which in turns creates pod. I need to get the Pod names created by my deployment file.
kubectl get -f deployment.yml
The above command list the deployment names. If i type kubectl get pod
it displays the pod names which is having some random extra strings added with deployment names.
Even if i use the -o json
option still unable to get the pod names.
How to get all the pods created by deployment? Even a shell script way would be fine.
my deployment file looks below
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
ansible: test-2020-05-26-00-04-1590476651
podRef: pod-ansibles
name: rd-2020-05-26-00-04-1590476651
spec:
replicas: 2
selector:
matchLabels:
app: nginx
strategy:
type: Recreate
template:
metadata:
labels:
app: nginx
...
....
so is there any way that i can get all pods which has deployment label 'test-2020-05-26-00-04-1590476651' ?
when i tried 'kubectl get pod -l ansible=test-2020-05-26-00-04-1590476651'
. it shows
'No resources found in default namespace.'
seems it can only display deploy name 'kubectl get deploy -l ansible=test-2020-05-26-00-04-1590476651'
But i need all pod names which is under a deployment..
Upvotes: 3
Views: 6640
Reputation: 9041
All pods created by Kubernetes controller have its top owner's name in Pods' name. For pods created by a ReplicaSet, created by a Deployment it would look like:
PodName = ReplicaSetName-PodHash
ReplicaSet name in turn obey the same rule:
ReplicaSetName = DeploymentName-ReplicaSetHash
"Unfolded" Pod's name would look like:
PodName = DeploymentName-ReplicaSetHash-PodHash
Kubernetes requires that all resources of the same kind in the same namespace have different names.
So, to get the pods created by your deployment using kubectl we could use grep
and awk
to get just names:
kubectl get pods -n NamespaceName | grep "DeploymentName" | awk '{print $1}'
For ex:
kubectl get pods -n default | grep "rd-2020-05-26-00-04-1590476651" | awk '{print $1}'
# or just awk
kubectl get pods -n default | awk '/rd-2020-05-26-00-04-1590476651/{print $1}'
Upvotes: 2
Reputation: 1260
Usually, we label deployment's pods.
template:
metadata:
labels:
app: my-app
So, we can get all pod filter by label
kubectl get pod -l app=my-app
Upvotes: 6