【问题标题】:Is it possible to install curl into busybox in kubernetes pod是否可以将 curl 安装到 kubernetes pod 中的busybox中
【发布时间】:2022-05-03 08:02:56
【问题描述】:

我正在使用busybox 来检测我在kubernetes v1.18 pod 中的网络问题。我这样创建了busybox:

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

并登录查看kubernetes集群网络情况:

 kubectl exec -it busybox /bin/bash

让我吃惊的是busybox不包含curl。为什么busybox包不包含curl命令?我正在搜索互联网,发现文档没有谈论如何将 curl 添加到busybox。我试图安装 curl,但没有办法做到这一点。有没有办法将 curl 包添加到busybox?

【问题讨论】:

    标签: kubernetes busybox


    【解决方案1】:

    简短的回答是,你不能。

    为什么?

    因为busybox没有包管理器,比如:yum、apk或apt-get ..

    其实你有两个解决方案:

    1.要么使用修改过的busybox

    您可以使用其他busybox 映像,例如progrium/busybox,它提供opkg-install 作为包管理器。

    image: progrium/busybox
    

    然后:

    kubectl exec -it busybox -- opkg-install curl
    

    2。或者,如果您担心使用最小图像,您可以使用 alpine

    image: alpine:3.12
    

    然后:

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

    【讨论】:

      【解决方案2】:

      没有。将alpine 视为基本映像,而不是包含 BusyBox 和包管理器,或者构建(或查找)具有预安装所需工具的自定义映像。

      BusyBox 构建为包含许多常见 Linux 工具实现的单个二进制文件。 BusyBox documentation 包含所包含命令的列表。如果不编写 C 代码,就无法在其中“安装”更多命令。

      BusyBox 确实包含 wget 的实现,它可能适用于您的目的 (wget -O- http://other-service)。

      【讨论】:

        【解决方案3】:

        BusyBox 有一个wget 的子集。在您的操作系统中,curl 的使用模式比 Busybox 附带的要复杂得多。

        为了澄清我的意思,请在您的操作系统中运行以下命令:

        $ wget --help | wc -l
        207
        

        在 Busybox 容器中运行 wget 的帮助时,应该会为您提供一个最小的子集包:

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

        在 K8s 中,您可以运行以下命令:

        $ 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   
        

        如果您的用例需要curl,我建议使用Alpine,即busybox + 一个最小的包管理器和libc 实现,这样您就可以轻松地执行apk add --no-cache curl 并获得真正的@ 987654330@(或者甚至apk add --no-cache wget 来获得“真正的” wget 而不是 BusyBox 的wget)。

        【讨论】:

          【解决方案4】:

          正如@abdennour 所建议的那样,我不再坚持使用busybox。正如其他人在此处建议的那样,Alpine 是一个非常轻量级的 Linux 容器映像,您可以在其中安装任何方便的类 UNIX 工具来完成故障排除任务。事实上,我在 .bashrc 的 dotfiles 中使用这个函数来旋转一个方便的临时准备摇滚 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
          

          【讨论】:

            【解决方案5】:

            Radial 覆盖了 busybox images 添加 cURL。 docker pull radial/busyboxplus:curl

            他们还有第二张带有 cURL + Git 的图片。 docker pull radial/busyboxplus:git

            【讨论】:

              【解决方案6】:

              或者只是将静态构建的 curl 复制到 Busybox: https://github.com/moparisthebest/static-curl/releases

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2021-09-28
                • 1970-01-01
                • 1970-01-01
                • 2018-01-23
                • 1970-01-01
                • 2021-07-17
                • 2017-03-08
                • 1970-01-01
                相关资源
                最近更新 更多