How do I run curl command from within a Kubernetes pod
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
The curl command-line tool is widely used for transferring data with URLs, allowing interaction with web servers directly from the terminal. In Kubernetes, running a curl command from within a pod can be a crucial task for debugging, testing connectivity, or interacting with APIs. This guide provides a detailed walkthrough on how to execute curl commands from within a Kubernetes pod, with technical explanations and examples to clarify each step.
Prerequisites
Before proceeding, ensure you have the following:
- A functioning Kubernetes cluster.
kubectlinstalled on your machine and configured to interact with your cluster.- A running pod in the Kubernetes cluster.
- Basic knowledge of Kubernetes and
curl.
Step-by-step Guide
Step 1: Identify the Target Pod
First, you need to identify which pod you will use to execute the curl command. You can list all the running pods in the cluster using:
If you want to filter the pods by a specific namespace, use:
Step 2: Access the Pod's Terminal
Once you have identified the pod, you need to access its terminal. You can execute shell commands inside a Kubernetes pod with:
If the pod does not have /bin/bash (it might be an Alpine or a minimal image), you can use /bin/sh:
Step 3: Ensure curl is Installed
Not all container images include curl. Verify if curl is installed:
If curl is not installed, you must install it. For example, in a Debian-based image, run:
In an Alpine image, use:
Step 4: Running curl Commands
Now that you have access to the pod’s terminal and curl is installed, you can run curl commands. Here are a few examples:
- Fetching a URL:
- Fetching headers only:
- Making a POST request:
- Using a custom header:
Common Use Cases for curl within a Kubernetes Pod
1. Connectivity Testing
Verify connectivity with other services running inside or outside the cluster:
2. API Interaction
Interact directly with APIs. This is particularly useful when testing internal service endpoints in the development phase:
Best Practices
- Use Minimal Images: Don’t install unnecessary packages inside production pods. Use lightweight or sidecar containers for debugging.
- Security Consideration: Avoid exposing credentials in
curlcommands. Use environment variables or secret management tools for sensitive information. - Namespace Awareness: Be aware of the namespaces when interacting with services to avoid connectivity issues.
Summary Table
| Task | Command/Action | Note |
| List Pods | kubectl get pods --all-namespaces | Use namespace-specific filtering with -n <namespace> |
| Access Pod Terminal | kubectl exec -it <pod-name> -- /bin/bash | Use /bin/sh for minimal images |
Check if curl is Installed | which curl | Install via package manager if not present |
Installing curl (Debian/Ubuntu) | apt-get update && apt-get install -y curl | Required only if curl is absent |
| Fetch a URL | curl http://example.com | Basic GET request |
| API Interaction Example | curl -X GET http://<service>:<port>/api/v1 | Replace placeholders with actual service details |
| Connectivity Testing | curl http://<service>.<namespace>.svc.cluster.local | Use service DNS names |
Conclusion
Running curl commands within a Kubernetes pod is a powerful technique for interacting with services, testing connectivity, and debugging applications. By following this guide, you will be able to effectively utilize curl in your Kubernetes environment to enhance your development and operational workflows, while maintaining best practices for security and efficiency.
Related reading
- How do I run private docker images on Google Container Engine
- How do I save kubectl logs to a file on my host machine?
- How do I scale up my cluster on Google Container Engine / Kubernetes?
- How do I ssh into the VM for Minikube?
- How do I run/test my Flutter app on a real device?
- How do I safely replicate data to another data center
- How do I ssh to nodes in ACS Kubernetes cluster?
- How do I switch between contexts in K9s when the contexts have already been set up?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.