Kubernetes
kubectl
CURL
command-line
DevOps

How to run curl with kubectl (exec or a temporary curl pod)

Codemia Knowledge Hub ·

kubectl cannot send HTTP requests itself, but it can start a process inside a pod that does. Either run curl in an existing container with kubectl exec, or launch a short-lived pod from an image that ships curl (such as curlimages/curl) with kubectl run --rm -it.

Running curl inside an existing pod

Use kubectl exec when the target container already has curl installed:

bash
1kubectl get pods -n payments
2
3kubectl exec -n payments -it api-7c89d9f7f7-9x2lm -- \
4  curl -sS http://localhost:8080/healthz

This connects to the pod through the Kubernetes API, starts curl inside the container, and sends the request from the pod's network location rather than from your laptop. Because the traffic originates inside the container, it is the way to check endpoints that only listen on localhost.

For a pod with more than one container, pick the container with -c:

bash
kubectl exec -n payments -it api-7c89d9f7f7-9x2lm -c app -- \
  curl -sS http://localhost:8080/ready

Starting a temporary curl pod

Many production images are deliberately minimal and do not include curl. Start a disposable pod from a debugging image instead:

bash
1kubectl run curl-debug \
2  -n payments \
3  --rm -it \
4  --restart=Never \
5  --image=curlimages/curl:8.7.1 -- sh

From the shell, query cluster services:

bash
curl -sS http://api.payments.svc.cluster.local:8080/healthz
curl -sS http://api:8080/metrics | head

This is cleaner than adding troubleshooting tools to an application image, and it lets you test service discovery, DNS and network policies from the same namespace as the workload. Keep --rm and --restart=Never so the pod is removed after the session; otherwise these helper pods pile up.

Targeting services, pods and external URLs

From inside the cluster, curl can reach:

  • Pod-local endpoints such as http://localhost:8080
  • Service names such as http://api:8080
  • Fully qualified service DNS names such as http://api.payments.svc.cluster.local:8080
  • Public URLs, if egress is allowed

A short name like api resolves only within the current namespace, so use the full service DNS name when calling across namespaces. Headers for authenticated APIs work as in any Linux shell:

bash
1TOKEN="$(kubectl get secret api-token -n payments -o jsonpath='{.data.token}' | base64 --decode)"
2
3kubectl exec -n payments deploy/api -c app -- \
4  curl -sS \
5    -H "Authorization: Bearer ${TOKEN}" \
6    -H "Accept: application/json" \
7    http://localhost:8080/v1/orders

To check routing and policy assumptions, call a service from another namespace:

bash
1kubectl run netcheck \
2  -n reporting \
3  --rm -it \
4  --restart=Never \
5  --image=curlimages/curl:8.7.1 -- \
6  curl -sS http://api.payments.svc.cluster.local:8080/healthz

If this fails while the same request works from the payments namespace, the cause is often a network policy or a namespace-scoped DNS assumption.

Flags that help when debugging

bash
kubectl exec -n payments deploy/api -c app -- \
  curl -v --connect-timeout 3 --max-time 10 http://localhost:8080/healthz
  • -v prints request and response details
  • --connect-timeout limits how long the TCP connect phase can hang
  • --max-time stops a stuck request from blocking your terminal
  • -k skips certificate validation for temporary HTTPS debugging, but should not be your default

Piping into jq helps with JSON APIs, but with kubectl exec every tool you call (curl, jq, sh) must exist inside the container. The command does not use binaries from your local machine, so a missing one fails immediately.

Port-forwarding tests a different path

If you just want to run curl from your laptop against an in-cluster service, kubectl port-forward may be simpler. It exercises a different network path, though, so it does not tell you whether in-cluster connectivity works.


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