【问题标题】:Unable to install curl command in kubernetes pod无法在 kubernetes pod 中安装 curl 命令
【发布时间】:2021-09-28 12:50:34
【问题描述】:

我正在使用以下命令在其中一个 pod 中安装 curl:

$ apt-get install curl

但是,我得到以下错误:

E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?

当我试图删除文件 /var/lib/dpkg/lock-frontend

$ rm /var/lib/dpkg/lock-frontend
rm: remove write-protected regular empty file '/var/lib/dpkg/lock-frontend'? y
rm: cannot remove '/var/lib/dpkg/lock-frontend': Permission denied

我也不能使用 sudo 命令:

$ sudo rm /var/lib/dpkg/lock-frontend
bash: sudo: command not found

请帮忙,因为我是 Kubernetes 和 Pods 的新手。

【问题讨论】:

  • 这很可能是预期的行为,因为一旦容器运行,您不应该在容器内安装任何东西。您尝试在其上安装 curl 的容器的映像可能仅包含完成其工作所需的最低限度。另外,Pod 是短暂的,所以当它们重新启动时,一切(除非持久化)都会丢失。为什么要安装 curl?如果您总是需要它,您可以考虑更改图像以便从一开始就提供它。
  • 完全同意@AndD。如果您需要curl,只需使用图像即可。例如我经常使用这个hub.docker.com/r/curlimages/curl 并且它完成了它的工作。

标签: linux docker curl apt-get


【解决方案1】:

首先这是一个反模式。容器是无状态的,部署后不会更改。

您无法运行apt-get 的原因是您正在使用非特权用户运行容器,您可以通过命令提示符中的“$”符号看出这一点。此外,sudo 未安装,因此无法使用。

如果您真的想在容器中使用 curl,您有两种选择:

  • 扩展您的 docker 映像,并在构建时而不是运行时安装 curl
  • 以 root 身份运行您的容器,并按照您尝试的方式从交互式会话中安装 curl(不鼓励)。

我建议您使用的第一种方法的示例涉及如下 Dockerfile:

FROM the-image-name-you-are-currently-using

RUN apt-get update; apt-get install -y curl

然后,您需要使用(从您的 Dockerfile 所在的同一目录)构建这个新图像:

docker build -t your-new-image-name:version .

使用docker push 将图像推送到注册表后,您可以更新您的Pod 以使用your-new-image-name:version 作为图像。

【讨论】:

  • 我使用的是 Kubernetes pod YAML 文件,如何在 YAML 文件中实现。
  • @PixziumMedia 为什么需要在你的容器中安装 curl?有什么理由不能将现有图像与 curl 一起使用?上面的答案不是关于 kubernetes pod,而是创建内部 curl 图像的正确方法。
猜你喜欢
  • 1970-01-01
  • 2016-04-08
  • 2014-02-13
  • 2021-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-27
  • 1970-01-01
相关资源
最近更新 更多