How to Run curl Command From Within a Kubernetes Pod?
Last Updated :
14 May, 2024
Ever needed to fetch data or test connectivity from within a Kubernetes pod? The curl command becomes your trusty companion in these situations. This guide explores different approaches to execute curl commands directly from within your Kubernetes pod, empowering you to diagnose issues and interact with external services effectively.
What are Kubernetes Pods?
A Kubernetes pod is the most basic unit for deploying applications. It encapsulates one or more containers that share storage (volumes) and network resources. Additionally, a pod specification defines how these containers will run.
What is kubectl?
kubectl is the command-line tool that acts as your interface for interacting with Kubernetes clusters. It empowers you to deploy and manage applications, inspect and modify cluster resources, and view logs for your applications running on Kubernetes.
Creating Kubernetes Pods: Imperative vs. Declarative Approaches
Imperative Approach
Imperative Approach: This approach utilizes the kubectl run command. It's a quick way to launch a pod, as seen in the following example:
kubectl run mynginx --image=nginx --port=80 --limits=memory=128Mi,cpu=500m
This command creates a pod named mynginx using the Nginx image, exposes it on port 80, and sets resource limits for memory and CPU.
Declarative Approach (Preferred)
This method offers a more structured and reusable approach. You define a YAML file (e.g., definition.yaml) specifying the desired pod configuration and then apply it using kubectl apply.
apiVersion: v1
kind: Pod
metadata:
name: mynginx
labels:
name: mynginx
spec:
containers:
- name: mynginx
image: nginx
resources:
limits:
memory: "128Mi"
cpu: "500m"
ports:
- containerPort: 80
Once you have the YAML file ready, use the following command to create the pod:
kubectl apply -f definition.yaml
Both approaches have the same output, the only difference is that declarative is reusable which makes it the preferred way.
Verifying Pod Creation
After creating the pod using either approach, you can confirm its existence using the kubectl get pods command.
kubectl get pods

This will display information about your running pods, including their name, status, and container details.
Accessing and Interacting with Pods
To execute commands within containers of a pod, you can access their shell. Here's how:
kubectl exec -it mynginx -- /bin/sh

This command opens an interactive shell session within the mynginx container. Now, you can run various commands inside the container, such as using curl to interact with web servers.
Alternative: Running Commands without Shell Access
One we run command, we get access to mynginx shell. Any further command will be executed inside this, we can run curl command here.
curl localhost
Default nginx pageSimilarly, we can try with different urls,
curl https://example.com

Or download a file
curl -O https://example.com/file.zip

Alternatively without directly accessing the shell, we can run commands on our pods.
kubectl exec -it mynginx -- [command-to-run]
kubectl exec -it mynginx -- curl localhost
Similar Reads
How to Run Shell Commands in Kubernetes Pods or Containers In Kubernetes, we create pods by adding an extra layer of information on containers. This Kubernetes in short is known as K8s, an open-source container orchestration tool developed by Google. It is used to orchestrate the containers for bringing Agility in software deployment through scaling, and ma
6 min read
How to Deploy Kubernetes on AWS? Kubernetes, the open-supply box orchestration platform, has emerged as the solution for dealing with containerized applications. When deploying Kubernetes in the cloud, Amazon Web Services (AWS) gives a robust and scalable environment. In this manual, we can walk you through the manner of deploying
7 min read
How to Add Node to Existing Kubernetes Cluster Kubernetes allows the management and orchestration of containerized deployments. We can use various cluster managing tools, but Kubeadm allows us to create and manage Kubernetes clusters from scratch. Kubernetes cluster can be autoscaled with the use of Cluster management services. In this article,
4 min read
Using Commands and Arguments in a Kubernetes Pod Kubernetes is open-source software that is used for the deployment and automation of software applications. It is a container orchestration platform. It means that we can manage microservices using containers.What is a Kubernetes pod?Kubernetes Pod is the smallest unit in the structure of Kubernetes
7 min read
Kubernetes Pods: How to Create and Manage Them Kubernetes is an open-source container orchestration system mainly used for automated software deployment, management, and scaling. Kubernetes is also known as K8s. Kubernetes was originally developed by Google, but it is now being maintained by the Cloud Native Computing Foundation. It was original
13 min read
How To Deploy Dashboard UI On A Kubernetes Cluster Locally ? Docker Desktop is easily the most popular containerization software for developers and teams. But Docker Desktop would not only allow you to play with containers but can enable you to use Kubernetes without downloading any external cluster like Minikube. Docker Desktop is a secure, out-of-the-box co
6 min read