Ansible playbook to list kubernetes pods with ip - kubernetes

I am trying to write get Kubernetes pods with IP using ansible script/playbook.
I followed the instruction from Ansible documentation where they have shown following example
name: Get a list of all pods from any namespace
k8s_facts:
kind: Pod
register: pod_list
But this is returning list without ip and node name.
Using kubectl it is possible to get ip and name detail by running following command :
kubectl get pods --output=wide --namespace=mynamespace
If I have to implement this what should I modify? Meanwhile, I tried passing "output" parameter but the result didn't change (no ip and name listed in the result)

I just did:
sudo kubectl get nodes -o wide | grep md | grep Ready | awk '{print $6}'
With the shell module, and save that output in a file.
I also try to find a module for that, but either way, this works.

Related

Find out all the pods that are using default service account

We have a k8s cluster with 10 workers. we run hundreds of pods in the cluster. we want to avoid running pods with default service account.
Need to find out the pods that are running with default service account. am able to find the number of pods using default service account with grep command but also need the pod name and the image it is using. Let us know your thoughts
In Case if you want to use just kubectl without jq :
needed to print both namespace and the pod name
kubectl get pods --all-namespaces -o jsonpath='{range .items[?(#.spec.serviceAccountName == "default")]}{.metadata.namespace} {.metadata.name}{"\n"}{end}' 2>/dev/null
i have added 2>/dev/null to avoid printing whole json template in case if no field was found
I used the below command to identify the pods from each namespace that is using default service account
kubectl get pods --all-namespaces -o json | jq '.items[] | select(.spec.serviceAccountName?=="default") | "\(.metadata.namespace) \(.metadata.name)"' | cut -d'"' -f2 | sort
if you are using k9s you can also :pod then e the pod to see which service account it is associated with

kubernetes-cli command to get dns of a kubernetes resource

I have gone through the doc mentioned here gitlink as well as doclink
But my job would be a whole lot easier if I could get the dns of a resource type by using any kubernetes command.
Also tried this commands-link
For example, i would like to get the dns name of a service db-service running in dev namespace inside svc.cluster.local
db-service.dev.svc.cluster.local
Any pointers ?
If you need to, you can query that in a pod:
How do I run a container from the command line in Kubernetes (like docker run)?
Retrieve the full name of a service in Kubernetes
using a pod which has some DNS utils
kubectl run tmp-shell --rm -i --tty --image tutum/dnsutils -- /bin/bash
you can then run
root#tmp-shell:/# nslookup db-service
Server: 10.2.32.10
Address: 10.2.32.10#53
Name: db-service.dev.svc.cluster.local
for a one-liner, see: https://serverfault.com/questions/929211/kubernetes-pod-dns-resolution
Try command
kubectl get svc
First column is internal DNS name.
If the type is LoadBalancer then EXTERNAL-IP column will display the external DNS name.

Not able to fetch ip address of pods using Kubectl and jsonpath

I'm trying to get the ip address of pods with particular label using jsonpath with the following command:
kubectl get pods -l app=validate -n {namespace_name} -o jsonpath={.status.podIP}
But this doesn't result into anything, even though the namespace and label names are correct. On the other hand, if I try to do:
kubectl get pod/pod_name -n {namespace_name} -o jsonpath={.status.podIP}
I'm able to get the pod IP address after that. But the problem is, since I'm trying to query all the pods created for a particular deployment, I want to fetch Ip addresses for all the pods under that particular label. I'm not sure what is wrong with the command.
If you have multiple Pods with the same label, you get a list of Pods. You have to adjust your jsonpath to -o jsonpath="{.items[*].status.podIP}" to get all the podIPs.
According to the official doc, you can add custom columns when querying a list of resources.
So you can do kubectl get pods -l app=validate -n {namespace_name} -o custom-columns=ip:.status.podIP

Kubernetes - Get value of a specific metadata when using describe command

When ruining kubectl describe service MyService command and can get the details of my kubernetes service as per below sample:
I am only interested to get the value of LoadBalancer Ingress metadata. Is there a way to retrieve this specific metadata using kubectl describe command?
I think it's better to use the get method and the go-template output :
kubectl get svc MyService -o go-template --template='{{range .status.loadBalancer.ingress}}{{.ip}}{{end}}'
Otherwise (but I don't recommend it), use bash tools like grep and cut
kubectl describe svc MyService | grep "LoadBalancer Ingress" | cut -d ':' -f2
How do you define "LoadBalancer Ingress metadata"? The IP address? If so, that information should appear under "IP:", right on top of LoadBalancer Ingress. But the LoadBalancer type service is cloud provider specific.
Do you actually have an IP address assigned to that specific load balancer?

How do I get the External IP of a Kubernetes service as a raw value?

I am running an application with GKE. It works fine but I can not figure out how to get the external IP of the service in a machine readable format.
So i am searching a gcloud or kubectl command that gives me only the external IP or a url of the format http://192.168.0.2:80 so that I can cut out the IP.
You can use the jsonpath output type to get the data directly without needing the additional jq to process the json:
kubectl get services \
--namespace ingress-nginx \
nginx-ingress-controller \
--output jsonpath='{.status.loadBalancer.ingress[0].ip}'
NOTE
Be sure to replace the namespace and service name, respectively, with yours.
Maybe not GKE as my clusters are on AWS, but I assume logic will be similar. When you kubectl get svc you can select output format and it will show more then just the "normal" get. For me, with ELB based services to het LB hostname it's enough to run ie. kubectl -n kube-system get svc cluster-nginx-ingress-controller -o json | jq .status.loadBalancer.ingress.hostname
In my case 'kubectl get services' returns array of items, but not just one service.
So then such jsonpath works fine to me:
kubectl get services -l component=controller,app=nginx-ingress -o jsonpath="{.items[0].status.loadBalancer.ingress[0].ip}"
...and yet another way... This will list all the "load-balancer" services
kubectl get services --all-namespaces -o json | jq -r '.items[] | { name: .metadata.name, ns: .metadata.namespace, ip: .status.loadBalancer?|.ingress[]?|.ip }'
Depending on the networkPlugin used by your cluster services/pods may be exposed directly on external-ip. But this will also find an Ingress controllers run in the cluster.
To get the external-ip on GCP i can use:
kubectl get services --namespace=<your-namespace> -o jsonpath="{.items[0].status.loadBalancer.ingress[0].ip}"
The answers above do not provide the output the user asked. The correct command would be:
kubectl -n $namespace get svc $ingressServiceName -o json | jq -r .status.loadBalancer.ingress[].hostname
All previous solutions don't work any more for me (on GCP).
To get the IP:
kubectl get ingress <YOUR_INGRESS_NAME> -o jsonpath="{.status.loadBalancer.ingress[0].ip}"
To get the host-name:
kubectl get ingress <YOUR_INGRESS_NAME> -o jsonpath="{.spec.rules[0].host}"
Type
minikube tunnel
or
kubectl cluster-info
You can get the public exposed IP of your relevant service.