【问题标题】:Why kubectl exec --username=root does not work?为什么 kubectl exec --username=root 不起作用?
【发布时间】:2021-05-04 15:24:33
【问题描述】:

我在kubernetes上部署了istio/bookinfo,想在微服务容器上安装压力注入故障。但是,当我使用

kubectl exec -it reviews-v1-f55d74d54-kpxr2 -c reviews --username=root -- /bin/bash

登录容器,显示用户仍然是默认用户。并且命令'apt-get'得到了

default@reviews-v2-6f4995984d-4752v:/$ apt-get update
Reading package lists... Done
E: List directory /var/lib/apt/lists/partial is missing. - Acquire (13: Permission denied)

我尝试使用“su root”,但我不知道答案。 我搜索了一些答案说我可以使用'docker exec',它可以工作但不方便,所以我想知道如何使用命令 kubectl exec 登录容器。

【问题讨论】:

    标签: kubernetes root kubectl


    【解决方案1】:

    不支持。

    源代码表明这是一个 TODO 功能:kubernetes/kubectl/pkg/cmd/exec/exec.go

    kubectl 解释的--username 标志:

    ➜  ~ kubectl options  | grep user    
      --user='': The name of the kubeconfig user to use
      --username='': Username for basic authentication to the API server
    

    正如您可能看到的,没有一个用户标志可以更改 exec 的用户/UID。

    exec 命令支持的所有标志:

    ➜  ~ kubectl exec --help
    [...]
    
    Options:
      -c, --container='': Container name. If omitted, the first container in the pod will be chosen
      -f, --filename=[]: to use to exec into the resource
          --pod-running-timeout=1m0s: The length of time (like 5s, 2m, or 3h, higher than zero) to wait until at least one
    pod is running
      -i, --stdin=false: Pass stdin to the container
      -t, --tty=false: Stdin is a TTY
    

    此外,apt-get update 最好在构建时运行,而不是在运行时运行。

    保持容器不可变是一种很好的做法。对于测试目的,您应该坚持使用 docker exec,因为没有其他已知的替代方案。

    另外,如果您有特定的问题要解决,请解释问题,而不是解决方案。 xyproblem

    【讨论】:

    • 谢谢你的回答,这很有帮助。
    猜你喜欢
    • 2011-08-02
    • 2022-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-02
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多