Skip to Content

2 ways to find the IP address of Pod in Kubernetes

Using kubectl command to find the ip address of Pod in Kubernetes cluster

To retrieve the IP address of a Pod in Kubernetes, you can use the kubectl command-line tool. Here’s how you can do it:

  1. List all Pods: First, if you need to find the specific name of the Pod whose IP address you want to retrieve, list all Pods in your cluster or within a specific namespace.
    • For all namespaces:
      kubectl get pods --all-namespaces
      
    • For a specific namespace:
      kubectl get pods -n <namespace>
      
  2. Get the Pod’s IP Address: Once you have identified the Pod, use the following command to get detailed information about the Pod, which includes its IP address.
    • For a Pod in the default namespace:
      kubectl get pod <pod-name> -o wide
      
    • For a Pod in a specific namespace:
      kubectl get pod <pod-name> -n <namespace> -o wide
      

The output of the kubectl get pod <pod-name> -o wide command includes several pieces of information about each Pod, including its IP address. Look for the IP address in the column labeled “IP”.

Using template option in kubectl command to find the ip address of Pod in Kubernetes cluster

This method provides a quick and easy way to find out the IP addresses of Pods running in your Kubernetes cluster.

This command will query the specified Pod in the given namespace and print out just the Pod’s IP address, making it a concise and efficient way to retrieve this information.

kubectl get pod <pod-name> -n <namespace> --template "{{.status.podIP}}"

When you use –template, you’re essentially telling kubectl to format the output of the command according to the Go template you’ve provided.

This can be particularly useful when you’re only interested in specific data fields from the larger set of information that a kubectl command returns.

In the context of the command you provided:

kubectl get pod <pod-name> -n <namespace> --template "{{.status.podIP}}"
  • –template “{{.status.podIP}}”: This part of the command instructs kubectl to parse the output and extract the .status.podIP field from the data structure that represents the Pod’s status. The {{ }} brackets denote the Go template syntax, and .status.podIP is the path to the Pod IP address field within the Pod’s status object. 

Using –template, you can tailor the kubectl output to show only the information you need, making it easier to integrate into scripts or simply to reduce the verbosity of the command-line output for easier human consumption.

10 Useful kubectl exec Examples

Mastering Kubernetes Pods: A Comprehensive Guide to Container Orchestration