How to get the IP address of a Pod in Kubernetes

In Kubernetes, every Pod is assigned an IP address. But how do you find out a Pod’s IP address?

I was in a meeting the other day (yes, it was dreamy). We were discussing Pods and IP addresses in a new Kubernetes cluster.

But pretty quickly, we got deep into the weeds of port forwarding and routing and networking.

I realised that it’s really easy to get confused with Kubernetes networking – especially if you’ve not done much Linux and networking before!

So I thought about writing an article to answer this question:

How do you get a Pod’s IP address, both inside the cluster and outside of it?

We’ll try to answer this question, and also learn why the IP address isn’t perhaps sufficient to do what you want to do.

Why tho?

There are a few reasons why you might want to find out the IP address of a Pod in Kubernetes – perhaps you’re doing some work inside the cluster, or perhaps you’re deploying applications which need to know their own IP.

In this article, we’ll focus on finding it from outside the Pod (when you’re creating or configuring Pods), and then inside the Pod (when your application needs to know its IP address).

Find a Pod’s IP address from outside the Pod

So you want to find out the IP address of a Pod from your terminal? It’s quite straightforward.

You can get most information about the state of the Kubernetes cluster using the kubectl command, which fetches this info from the Kubernetes API.

Specifically, the command kubectl get pod will give you information about Pods.

Let’s have a look:

If you want a quick Kubernetes cluster to follow these instructions, I’m using k3s, which you can download and install locally, from https://k3s.io/.

Use kubectl to get a Pod’s IP address

  1. Create a Pod, either directly or through a Deployment.

    I’m creating an Nginx pod with this YAML:

    apiVersion: v1
    kind: Pod
    metadata:
      creationTimestamp: null
      labels:
        run: nginx
      name: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
      dnsPolicy: ClusterFirst
      restartPolicy: Always
    
  2. On your laptop or terminal, type kubectl get pod -o wide:

    kubectl get pod -o wide
    

    This will show you the pod’s name and its IP address.

    NAME    READY   STATUS    RESTARTS   AGE    IP           NODE           NOMINATED NODE   READINESS GATES
    nginx   1/1     Running   0          103s   10.42.0.13   toms-laptop   <none>           <none>
    
  3. If you just want to get the Pod IP address by itself, then use a template to format the output:

    kubectl get pod nginx --template '{{.status.podIP}}'
    

    This uses a “Go template” to format the information of the Pod, and return only the Pod’s IP address.

    10.42.0.13
    

If you have more than one container in a single Pod, the containers will all have the same IP address.

But wait… the IP address doesn’t seem to be reachable!

You’ve got the IP address, but it doesn’t seem to be reachable. Hmm, that’s weird… 🤔

Well:

The IP address of a Pod is normally accessible from inside a Kubernetes cluster only!

If you want another beginner’s guide to Kubernetes networking, then check out our article on How Pods communicate in Kubernetes.

Kubernetes creates a private network for your Pods (the exact behaviour depends on which networking plugin you’re using – see below).

So usually, you won’t be able to reach a Pod’s IP address directly from outside the cluster.

Exposing a Pod outside the cluster

If you want to allow users to access your application running inside a Pod, then you’ll need to expose it, by creating something like:

  • A NodePort Service

  • A LoadBalancer Service (if you’re running on a cloud provider)

  • An Ingress or Route

These are the building blocks of Kubernetes which can route traffic to your Pod from outside the cluster.

Now, let’s look at the other side of the coin. How can you find out the IP address of a Pod, from inside the Pod (without kubectl)?

Find a Pod’s IP address from inside the Pod

Finding the IP address of a Pod from inside the Pod itself, is a little different.

Why would you want to do this?

Perhaps you’re running some kind of application inside the Pod, which needs to know its own IP address.

We can use the Kubernetes Downward API for this.

Getting Pod info with the Downward API

The Kubernetes Downward API lets you access metadata about a Pod, inside the Pod definition itself.

You can use this data to populate an environment variable, which you can then access from inside the container.

Let’s take a look at an example!

Finding a Pod’s IP address from inside the Pod

  1. First, we need to define an environment variable inside the Pod. We want to populate the environment variable from a field called status.podIP.

    To use the Downward API to fetch this, use the valueFrom, fieldRef and fieldPath in your environment variable definitions.

    Take a look at this simple Pod definition which populates the IP address into the env var POD_IP:

    apiVersion: v1
    kind: Pod
    metadata:
      creationTimestamp: null
      labels:
        run: nginx
      name: nginx
    spec:
      containers:
      - env:
        - name: POD_IP
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        image: nginx
        name: nginx
        resources: {}
      dnsPolicy: ClusterFirst
      restartPolicy: Always
    

    If you’re using a Deployment, add the environment variable in the equivalent location in your Pod spec.

  2. Now run your Pod, or restart your Pod if it’s already running.

  3. Start a shell inside a Pod with kubectl exec:

    kubectl exec -it nginx -- /bin/sh
    
  4. Type env and you’ll see that the Pod’s IP address is now stored in an environment variable.

    I’ve added grep here – it will filter the output from env to show only lines that contain our environment variable, POD_IP:

    # env | grep POD_IP
    POD_IP=10.42.0.14
    
  5. So to print the Pod’s IP address from inside the Pod, you can now use:

    echo $POD_IP
    

Ta-da! But how is this useful to us?

How to use the Pod’s IP address in your application

How can you use this value that’s stored in an environment variable?

Well, we can now read this value from our application inside the Pod. Setting environment variables is a very common way to configure applications in containers.

Most programming languages have a way to read environment variables.

For example:

  • in a Java application you might use System.getenv("POD_IP");

  • while in Python, you might use os.environ.

But, here’s a note of caution:

Kubernetes networking varies a lot

Kubernetes is highly configurable. You can actually plug in many different network providers (using the Kubernetes Container Network Interface API).

So the network inside Kubernetes can behave differently, depending on which network provider you’re using in the cluster.

There’s OpenShift SDN, OVN-Kubernetes, Flannel, Calico, to name but a few of these many providers.

NetworkPolicy can restrict things, too

You can also add special network restrictions to Kubernetes, so that Pods are accessible to only some other Pods.

This is basically like creating a firewall between your Pods.

It’s called NetworkPolicy (see here for more information about it).

So if you’re struggling to reach another Pod by its IP address, NetworkPolicy might be the reason.

Wrapping up

If you want a really excellent deep-dive on how a Pod gets an IP address then check out:

Happy networking in Kubernetes!

Join the discussion

Got some thoughts on what you've just read? Want to know what other people think? Or is there anything technically wrong with the article? (We'd love to know so that we can correct it!) Join the conversation and leave a comment.

Comments are moderated.