目的——我想达到什么目的?

我想从运行 kubernetes 节点的容器内部访问 systemctl(ami:运行 debian stretch)

工作设置:

  • Node AMI : kope.io/k8s-1.10-debian-jessie-amd64-hvm-ebs-2018-08-17
  • 节点目录挂载在容器中以使 systemctl 工作:
  • /var/run/dbus
  • /run/systemd
  • /bin/systemctl
  • /etc/systemd/system

  • 不工作设置:
  • Node AMI : kope.io/k8s-1.11-debian-stretch-amd64-hvm-ebs-2018-08-17
  • 节点目录挂载在容器中以使 systemctl 工作:
  • /var/run/dbus
  • /run/systemd
  • /bin/systemctl
  • /etc/systemd/system

  • 调试以解决问题

    使用不支持 debian-stretchsystemctl 镜像调试此问题,其挂载与 debian-jessie 相同

    1)我首先通过在其中安装上述卷来启动 nginx 部署
    kubectl apply -f https://k8s.io/examples/application/deployment.yaml
    
    kubectl exec -it nginx-deployment /bin/bash
    
    root@nginx-deployment-788f65877d-pzzrn:/# systemctl
    systemctl: error while loading shared libraries: libsystemd-shared-
    232.so: cannot open shared object file: No such file or directory
    

    2) 由于上述问题显示未找到文件 libsystemd-shared-232.so。我通过查看节点找到了实际路径。
    admin@ip-10-0-20-11:~$ sudo find / -iname 'libsystemd-shared-232.so'
    /lib/systemd/libsystemd-shared-232.so
    

    3)在nginx pod中挂载/lib/systemd并再次运行systemctl
     kubectl exec -it nginx-deployment /bin/bash
    
     root@nginx-deployment-587d866f54-ghfll:/# systemctl
     systemctl: error while loading shared libraries: libcap.so.2:cannot
     open shared object file: No such file or directory
    

    4) 现在 systemctl 失败了,出现了一个新的遗漏错误
    root@nginx-deployment-587d866f54-ghfll:/# systemctl
    systemctl: error while loading shared libraries: libcap.so.2: cannot
    open shared object file: No such file or directory
    

    5)为了解决上述错误,我再次在节点中搜索 libcap.so.2 ,在下面的路径中找到了它。
    admin@ip-10-0-20-11:~$ sudo find / -iname 'libcap.so.2'
    /lib/x86_64-linux-gnu/libcap.so.2
    

    6) 看到上面的目录没有挂载在我的 pod 中。我在 nginx pod 中安装了以下路径。
    /lib/x86_64-linux-gnu mounted in the nginx pod(deployment)
    

    7)添加上述mount后nginx pod上不来。得到以下错误:
    $ k logs nginx-deployment-f9c5ff956-b9wn5
    standard_init_linux.go:178: exec user process caused "no such file
    or directory"
    

    请建议如何进一步调试。以及使 systemctl 在 debian 拉伸(stretch)环境中的容器内工作所需的所有挂载。

    任何进一步进行调试的指针都可能会有所帮助。

    最佳答案

    您只需在容器中安装 systemd 即可,而不是从主机挂载一些库文件。

    $ apt-get -y install systemd
    

    现在,这不一定会使 systemctl 运行。您将需要 systemd 在您的容器中运行,该容器由系统上的 /sbin/init 生成。 /sbin/init 需要以 root 身份运行,因此本质上,您必须在 Kubernetes 上的 pod 或容器 security context 中使用 privileged 标志运行它。现在,在容器中运行 systemd 是不安全和 there is a long history 的,Docker 人员主要反对它(安全性),而 Red Hat 人员表示需要它。

    尽管如此,Red Hat 的人想出了一种方法来 make it work without the unprivileged flag 。你需要:
  • /run 作为 tmpfs 安装在容器中。
  • /sys/fs/cgroup 挂载为只读就可以了。
  • /sys/fs/cgroup/systemd/ 挂载为读/写。
  • 用于 STOPSIGNAL SIGRTMIN+3

  • 在 Kubernetes 中,您需要一个 emptyDir 来挂载 tmpfs 。其他的可以作为主机卷挂载。

    关于docker - 使 systemctl 在 debian 拉伸(stretch)镜像中的容器内工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54079586/

    10-15 23:24