'Is it possible to install curl into busybox in kubernetes pod

I am using busybox to detect my network problem in kubernetes v1.18 pods. I created the busybox like this:

apiVersion: v1
kind: Pod
metadata:
    name: busybox
    namespace: default
spec:
    containers:
    - name: busybox
    image: busybox:1.28
    command:
        - sleep
        - "3600"
    imagePullPolicy: IfNotPresent
    restartPolicy: Always

and login to find the kubernetes clusters network situation:

 kubectl exec -it busybox /bin/bash

What surprises me is that the busybox does not contain curl. Why does the busybox package not include the curl command? I am searching the internet and find the docs do not talk about how to add curl into busybox. I tried to install curl, but found no way to do this. Is there anyway to add curl package into busybox?



Solution 1:[1]

The short answer, is you cannot.

Why?

Because busybox does not have package manager like: yum, apk, or apt-get ..

Acutally you have two solutions:

1. Either use a modified busybox

You can use other busybox images like progrium/busybox which provides opkg-install as a package manager.

image: progrium/busybox

Then:

kubectl exec -it busybox -- opkg-install curl

2. Or if your concern to use a minimal image, you can use alpine

image: alpine:3.12

then:

kubectl exec -it alpine -- apk --update add curl

Solution 2:[2]

No. Consider alpine as a base image instead that includes BusyBox plus a package manager, or building (or finding) a custom image that has the tools you need pre-installed.

BusyBox is built as a single binary that contains implementations of many common Linux tools. The BusyBox documentation includes a listing of the included commands. You cannot "install" more commands into it without writing C code.

BusyBox does contain an implementation of wget, which might work for your purposes (wget -O- http://other-service).

Solution 3:[3]

BusyBox has a subset of wget. The usage patterns of curl are significantly more complex in your OS than the one that comes with Busybox.

To clarify what I mean, run the following in your OS:

$ wget --help | wc -l
207

while running wget's help inside Busybox container should give you a minimal subset package:

$ docker run --rm busybox wget --help 2>&1 | wc -l
20

In K8s, you could run the following:

$ kubectl run -i --tty --rm busybox --image=busybox -- sh
If you don't see a command prompt, try pressing enter.
/ # wget
BusyBox v1.33.1 (2021-06-07 17:33:50 UTC) multi-call binary.

Usage: wget [-cqS] [--spider] [-O FILE] [-o LOGFILE] [--header 'HEADER: VALUE'] [-Y on/off]
    [--no-check-certificate] [-P DIR] [-U AGENT] [-T SEC] URL...

Retrieve files via HTTP or FTP

    --spider    Only check URL existence: $? is 0 if exists
    --no-check-certificate  Don't validate the server's certificate
    -c      Continue retrieval of aborted transfer
    -q      Quiet
    -P DIR      Save to DIR (default .)
    -S          Show server response
    -T SEC      Network read timeout is SEC seconds
    -O FILE     Save to FILE ('-' for stdout)
    -o LOGFILE  Log messages to FILE
    -U STR      Use STR for User-Agent header
    -Y on/off   

If curl is something required for your use case, I wouldsuggest to use Alpine which is busybox + a minimal package manager and libc implementation such that you can trivially do apk add --no-cache curl and get real curl (or even apk add --no-cache wget to get the "real" wget instead of BusyBox's wget).

Solution 4:[4]

As @abdennour is suggesting, I'm no longer sticking with busybox anymore. Alpine is a very lightweight Linux container image as others suggest here in which you can literally install any UNIX-like tool handy to accomplish your troubleshooting task. In fact, I use this function within my dotfiles at .bashrc to spin a handy ephemeral ready-to-rock Alpine pod:

## This function takes an optional argument to run a pod within a Kubernetes NS, if it's not provided it fallsback to `default` NS.
function kalpinepod () { kubectl run -it --rm --restart=Never --image=alpine handytools -n ${1:-default} -- /bin/ash }

? kalpinepod kube-system
If you don't see a command prompt, try pressing enter.
/ # cat /etc/resolv.conf
search kube-system.svc.cluster.local svc.cluster.local cluster.local
nameserver 10.245.0.10
options ndots:5
/ # apk --update add curl openssl
fetch https://dl-cdn.alpinelinux.org/alpine/v3.13/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.13/community/x86_64/APKINDEX.tar.gz
(1/6) Installing ca-certificates (20191127-r5)
(2/6) Installing brotli-libs (1.0.9-r3)
(3/6) Installing nghttp2-libs (1.42.0-r1)
(4/6) Installing libcurl (7.74.0-r1)
(5/6) Installing curl (7.74.0-r1)
(6/6) Installing openssl (1.1.1j-r0)
Executing busybox-1.32.1-r3.trigger
Executing ca-certificates-20191127-r5.trigger
OK: 9 MiB in 20 packages

Solution 5:[5]

Radial has an overlay of busybox images adding cURL. docker pull radial/busyboxplus:curl

They also have a second images having cURL + Git. docker pull radial/busyboxplus:git

Solution 6:[6]

Or just copy a statically built curl into Busybox: https://github.com/moparisthebest/static-curl/releases

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abdennour TOUMI
Solution 2 David Maze
Solution 3 Muhammad Soliman
Solution 4
Solution 5 mpc-DT
Solution 6 user2716262