In Kubernetes service object is responsible for providing network access to a set of pods, which runs same images/applications, leading similar functionality.
Sometimes we may want to list/check pods running behind a service in OpenShift cluster. Here are steps one can follow to do so :- Let’s start by listing the end-points for a service.
$oc get endpoints portal-service
NAME ENDPOINTS AGE
portal-service 10.128.6.25:8080,10.128.6.26:8080 278d
We notice that portal-service has 2 endpoints. Now, let’s try to get the pod names for these endpoints.
$oc get endpoints portal-service -o jsonpath='{.subsets[*].addresses[*].ip}' | awk -v OFS="\n" '{$1=$1}1'
10.128.6.25
10.128.6.26
We got POD IP’s from previous command, let’s get pod name from those IP’s. We can do so by passing output of previous command using xargs (The xargs command executes commands provided via standard input. It takes the input and converts it into a command argument for another command.)
Let’s get POD running behind service.
$oc get endpoints portal-service -o jsonpath='{.subsets[*].addresses[*].ip}' | awk -v OFS="\n" '{$1=$1}1' | xargs -I % kubectl get pods --no-headers --field-selector=status.podIP=%
portal-deployment-587cbdff99-xwqfl 1/1 Running 0 76d
portal-deployment-587cbdff99-nzlrd 1/1 Running 0 76d
OR Just POD names running behind service.
oc get endpoints portal-service -o jsonpath='{.subsets[*].addresses[*].ip}' | awk -v OFS="\n" '{$1=$1}1' | xargs -I % kubectl get pods -o name --no-headers --field-selector=status.podIP=%
pod/portal-deployment-587cbdff99-xwqfl
pod/portal-deployment-587cbdff99-nzlrd
OR get pods in single line
oc get endpoints portal-service -o jsonpath='{.subsets[*].addresses[*].ip}' | awk -v OFS="\n" '{$1=$1}1' | xargs -I % kubectl get pods -o name --no-headers --field-selector=status.podIP=% |tr '\n' ' '
pod/portal-deployment-587cbdff99-xwqfl pod/portal-deployment-587cbdff99-nzlrd
Note: Above commands are using oc
utility, replacing with kubectl
should also work in any other implementation of K8 cluster not just OpenShift.
Thank you.