I wanted to hit a command which searches pod with the service name and identify its pod's status as "Ready"
I tried some of the commands but it does not work and it does not search with service-name.
kubectl get svc | grep my-service | --output="jsonpath={.status.containerStatuses[*].ready}" | cut -d' ' -f2
I tried to use the loop also, but the script does not give the desired output.
Can you please help me figure out the exact command?
every service create a endpoints which contain the podIp and other info for service. you can just use that endpoints
to get you pods. . it will show you the ready pod for your my-service
.
use this command:
kubectl get endpoints -n <Name_space> <service_name> -o json | jq -r 'select(.subsets != null) | select(.subsets[].addresses != null) | .subsets[].addresses[].targetRef.name'
for you the command will be:
kubectl get endpoints my-service -o json | jq -r 'select(.subsets != null) | select(.subsets[].addresses != null) | .subsets[].addresses[].targetRef.name'
you can run the script for getting the pod status
#!/usr/bin/env bash
for podname in $(kubectl get endpoints my-service -o json | jq -r 'select(.subsets != null) | .subsets[].addresses[].targetRef.name')
do
kubectl get pods -n demo $podname -o json | jq -r ' select(.status.conditions[].type == "Ready") | .status.conditions[].type ' | grep -x Ready
done