Execute CURL with kubectl
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
kubectl does not run HTTP requests by itself, but it can start a process inside a pod that does. In practice, that means you either execute curl in an existing container or launch a short-lived troubleshooting pod that already contains the tool.
Running curl Inside an Existing Pod
The most direct approach is kubectl exec. This works when the target container already has curl installed.
That command does three things:
- It connects to the pod over the Kubernetes API.
- It starts
curlinside the container process namespace. - It sends the request from the pod's network location, not from your laptop.
If the pod has more than one container, add -c to choose the right one:
This is especially useful for checking endpoints that only listen on localhost, because traffic originates inside the same container.
Starting a Temporary Debug Pod
Many production images are intentionally minimal and do not include curl. In that case, spin up a disposable pod with a debugging image.
Once the shell opens, you can query cluster services:
Using a temporary pod is cleaner than modifying an application image just to add troubleshooting tools. It also lets you test service discovery, DNS, and network policies from the same namespace as the workload.
Calling Services, Pods, and External Endpoints
Inside a cluster, curl can target several kinds of addresses:
- 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
For authenticated APIs, include headers the same way you would on any Linux shell:
A common debugging pattern is to hit a service from another namespace to confirm routing and RBAC assumptions:
If that request fails while the same service works from the payments namespace, the problem is often a network policy or a namespace-scoped DNS assumption.
Useful Flags When Debugging
curl becomes much more informative with a few extra flags:
- '
-vprints request and response details' - '
--connect-timeoutlimits how long the TCP connect phase can hang' - '
--max-timeprevents a stuck request from blocking your terminal' - '
-kcan bypass certificate validation for temporary HTTPS debugging, though it should not be your default'
For JSON APIs, pairing curl with jq is often helpful, but remember that both tools must exist inside the container if you are using kubectl exec.
Port-forwarding is a different tool
If the real goal is to run curl from your laptop against an in-cluster service, kubectl port-forward may be simpler than executing curl inside a pod. That approach tests a different network path, though, so it should not be confused with in-cluster connectivity debugging.
Common Pitfalls
The most common mistake is assuming kubectl exec gives you the pod network but still uses tools from your local machine. It does not. The command runs inside the container, so missing binaries such as curl or sh will cause errors immediately.
Another frequent issue is forgetting the namespace. A service name like api resolves only within the current namespace unless you use the full service DNS name. Multi-container pods are another source of confusion, because kubectl exec may default to the wrong container if you omit -c.
Finally, do not treat troubleshooting pods as permanent infrastructure. Use --rm and --restart=Never so the pod disappears after the session, otherwise these helpers tend to accumulate and make operational cleanup harder.
Summary
- Use
kubectl execwhen the target container already hascurl. - Use
kubectl run --rm -it --image=curlimages/curlwhen the application image is minimal. - Prefer service DNS names when you want to test Kubernetes networking instead of pod-local behavior.
- Add
-cfor multi-container pods and-nfor the correct namespace. - Use verbose and timeout flags to turn a hanging request into a useful diagnostic signal.

