1. Introduction

In this tutorial, we’ll see how to run curl from within a Kubernetes pod.

Depending on its image, a container in a Kubernetes pod may or may not have curl pre-installed. Of course, if it doesn’t have curl, it can’t run curl commands. But when it does, we can readily run curl in it using kubectl exec. We’ll look at both scenarios next.

2. Using kubectl exec

kubectl exec allows us to execute commands in a container. The command we intend to run must be available in the target container. But if the command isn’t available, we can install it.

2.1. When the Container Has curl Pre-Installed

Let’s run curl localhost in the default container of our pod named test:

$ kubectl exec -it test -- curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
...truncated...

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

We created the default container of the test pod from an nginx:bullseye Docker image. Since this image comes with curl pre-installed, we’re able to run curl inside the pod.

In the snippet above, we ran a curl command in the default container of our pod. But we can also run it in a specific container using -c:

$ kubectl exec -it test -c nginx-container -- curl localhost

2.2. When the Container Doesn’t Have curl

When we run curl localhost in a container created from an ubuntu:xenial Docker image, for example, we get an error:

$ kubectl exec -it test -c ubuntu -- curl localhost
OCI runtime exec failed: exec failed: unable to start container process: exec: "curl": executable file not found in $PATH: unknown
command terminated with exit code 126

As the error message says, the curl executable file was not found in the PATH environment variable. Basically, curl isn’t installed in the container. However, we can install it.

To install curl, let’s start a shell in the container:

$ kubectl exec -it test -c ubuntu -- bash
root@test:/#

Next, we’ll update apt, install curl, and run curl localhost in the shell we started:

# apt update && apt install curl -y && curl localhost
...truncated...
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
...truncated...

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

Now that we’ve installed curl, we didn’t get an error message.

We used apt in this snippet because the container runs on Ubuntu. If it were a different distro, we’d have used the package manager for that distro.

Instead of running apt update and apt install curl using kubectl exec, we can add it to the definition (Dockerfile) of a custom-built ubuntu:xenial image. This way, the container created from that custom image comes with curl pre-installed.

3. Conclusion

In this article, we learned how to run curl inside an existing Kubernetes pod using kubectl exec. We saw how to run curl in containers where it comes pre-loaded. We also looked into installing and running curl in containers that don’t have curl.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.