Kubernetes

Kubectl Get Pod IP

Essentially, the “kubectl” command-line tool is vital for developers and system administrators when it comes to administering the Kubernetes clusters. One of the many useful commands that are provided by “kubectl” is “kubectl get pod IP” which allows you to obtain the IP addresses of running pods in your cluster and you can use them to manage your Kubernetes pods.

In this tutorial, we will look at how to use the “kubectl get pod” command in Kubernetes to get the IP address of a pod. To access a container that runs in a pod, you must first know the pod’s IP address. This is where the “kubectl get pod IP” command detail is explained step by step. Let’s get started!

Step 1: Launch the Minikube Server

In this step, we will start the Kubernetes minikube server so that we can run the minikube command on our system to start the local Kubernetes environment. We can start the minikube in our application by running the following minikube command.

~$ minikube start

When this command is executed, the minikube cluster runs in our system successfully as you can see in the attached output:

Text Description automatically generated

Step 2: Create a Pod YAML File

Now, we create a YAML file for the pod. We can deploy the pod with the help of this pod. You can modify or update the pod’s specifications before generating it by accessing the “pod1.yaml” file in nano. This file may contain a large amount of data such as the name of the pod, labels, containers, volumes, and other requirements. You can use nano to change, add, or remove any of this information as needed. We can create the nano file with a “.yaml” extension by running the following command:

~$ nano pod1.yaml

Step 3: Configuration of the YAML File

In this step, we can configure the YAML file after creating a nano file. We can now view the capabilities of our container by creating the YAML file. This file contains different information about the container. Please read this information carefully. This given information is almost the same for all users and is the default.

apiVersion: v1
kind
: Pod
metadata
:
name
: nginx1
spec
:
containers
:
- name
: nginx1
image
: nginx:1.14.2
ports
:
- containerPort
: 90

As we can see in the previously-attached text, different information appears in the configuration file. Here, the container kind is “pod”, the name of the pod is “nginx1”, and the container port is “90”.

To save your modifications, press “Ctrl+S” to save the file to the disc, followed by “Ctrl+X” to quit the editor. Let’s move toward the next step of our article which is how you can apply the updated specifications to the cluster.

Step 4: Apply the Updated specifications to the Cluster

In this step, we will see how we can update the specifications to the Kubernetes cluster that is saved in the YAML file.

~$ kubectl apply -f pod1.yaml

When this command is executed, Kubernetes examines the “pod1.yaml” file’s contents and generates a pod depending on the given specifications in the file. If the pod already exists, Kubernetes updates it to reflect the YAML file specs. If everything works as intended, you should get a notification which verifies that the pod is created or modified after running the command.

In the following output screenshot, the message is “pod/nginx1 created” which indicates that a new pod named “nginx1” is created in the Kubernetes cluster:

Here, we explain the previously-used command:

  • The “apply” command is the action that is taken by kubectl when you wish to create or update the resources in the cluster.
  • The “-f” argument is used to specify the file or files that contain the resource definitions to be created or changed.
  • The “pod1.yaml” file contains the pod definition.

Step 5: Display All Running Pods Information

In this step, we want to see a detailed information about all the pods that are currently running in our cluster. We use the following command to obtain an information on all currently active pods.

~$ kubectl get pod -o wide

Here is the output of the executed “get pod -o wide” command:

Graphical user interface, text Description automatically generated

The kubectl command-line program is used to interact with Kubernetes clusters. “Get” is a sub command that returns an information on Kubernetes objects such as pods, services, deployments, and more. In a Kubernetes cluster, a pod is an object that stands in for a single running container instance.

The output format for the “get” command is specified by the -o option. In this situation, we use the broad output format which gives us more information about the pods such as the node on which the pod is operating and its IP address along with all these node names, nominated notes, and gates which are also included as you can see in the previously-attached screenshot.

Step 6: Get the IP Address of the Pod

In this step, we get the IP address of the pod. We can retrieve the information which is related to the pod that is running in the Kubernetes cluster by running the following command:

~$ kubectl get pod nginx1 --template '{{.status.podIP}}'

Attached is the output:

Text Description automatically generated

When this command is executed, we can easily see the list of all pods along with their status and IP address. As seen in the previous command, we use the – – template option which allows us to specify a template that can be used to format the output of the “kubectl get pod”. This template extracts the “pod1” field from the status object of the ngnix pod. The –template option to the “kubectl get pod” command can be a helpful tool to extract specific information from Kubernetes pods in a structured and adjustable manner. In brackets, the expression returns only the status of the IP address except for all information. The IP address of our pod is 10.244.0.4 as you can see in the previous illustration.

Conclusion

With the help of this article, we can easily access the IP address of our Kubernetes cluster pod. Every Kubernetes cluster runs in a different system or application. The IP address of the Kubernetes cluster pod is assigned in any case. Every container in the pod is assigned by the same IP address.

Every step is explained with the help of screenshots. If you are new to this concept, you can solve your problem by just following all the mentioned steps. For better understanding, you can execute the given commands and make sure that your command result resembles the given attached screenshots.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content