curl
Kubernetes
command-line
pod
DevOps

How to run curl from inside a Kubernetes pod

Codemia Knowledge Hub ·

Open a shell in the pod with kubectl exec -it <pod-name> -- /bin/bash (or /bin/sh on minimal images), install curl with the image's package manager if it is missing, and run your request. This is handy for debugging, testing connectivity between services and calling internal APIs.

You need a working cluster, kubectl configured for it, and a running pod.

1. Find the pod

List pods in all namespaces:

bash
kubectl get pods --all-namespaces

Or in one namespace:

bash
kubectl get pods -n <namespace>

2. Open a shell in the pod

bash
kubectl exec -it <pod-name> -- /bin/bash

If the image has no /bin/bash (Alpine or other minimal images), use /bin/sh:

bash
kubectl exec -it <pod-name> -- /bin/sh

3. Install curl if it is missing

Many images do not include curl. Check:

bash
which curl

On a Debian-based image, install it with:

bash
apt-get update
apt-get install -y curl

On Alpine:

bash
apk add --no-cache curl

4. Run curl

  • Fetch a URL:
bash
  curl http://example.com
  • Fetch headers only:
bash
  curl -I http://example.com
  • Send a POST request:
bash
  curl -X POST -d "param1=value1&param2=value2" http://example.com/resource
  • Send a custom header:
bash
  curl -H "Authorization: Bearer <token>" http://example.com

Testing connectivity and internal APIs

Check that another service, inside or outside the cluster, is reachable:

bash
curl http://<service-name>.<namespace>.svc.cluster.local

Call an internal service endpoint directly, which is useful while developing:

bash
curl -X GET http://<internal-service>:<port>/api/v1/resource

Keeping production pods clean and secure

  • Minimal images: Avoid installing extra packages in production pods. Use lightweight or sidecar containers for debugging instead.
  • Credentials: Do not put secrets directly in curl commands. Use environment variables or a secret manager.
  • Namespaces: Use the right namespace in service names to avoid connectivity problems.
TaskCommand/ActionNote
List Podskubectl get pods --all-namespacesUse namespace-specific filtering with -n <namespace>
Access Pod Terminalkubectl exec -it <pod-name> -- /bin/bashUse /bin/sh for minimal images
Check if curl is Installedwhich curlInstall via package manager if not present
Installing curl (Debian/Ubuntu)apt-get update && apt-get install -y curlRequired only if curl is absent
Fetch a URLcurl http://example.comBasic GET request
API Interaction Examplecurl -X GET http://<service>:<port>/api/v1Replace placeholders with actual service details
Connectivity Testingcurl http://<service>.<namespace>.svc.cluster.localUse service DNS names

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design