bash 在具有 root 访问权限的 kubernetes pod 上执行命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42793382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:30:45  来源:igfitidea点击:

Exec commands on kubernetes pods with root access

bashdockerkubernetes

提问by biz dev

I have one pod running with name 'jenkins-app-2843651954-4zqdp'. I want to install few softwares temporarily on this pod. How can I do this?

我有一个名为“jenkins-app-2843651954-4zqdp”的吊舱。我想在这个 pod 上临时安装几个软件。我怎样才能做到这一点?

I am trying this- kubectl exec -it jenkins-app-2843651954-4zqdp -- /bin/bashand then running apt-get install commands but since the user I am accessing with doesn't have sudo access I am not able to run commands

我正在尝试此操作kubectl exec -it jenkins-app-2843651954-4zqdp -- /bin/bash,然后运行 ​​apt-get install 命令,但由于我正在访问的用户没有 sudo 访问权限,因此我无法运行命令

回答by Janos Lenart

  • Use kubectl describe pod ...to find the node running your Pod and the container ID (docker://...)
  • SSH into the node
  • run docker exec -u root ID -- /bin/bash
  • 使用kubectl describe pod ...查找运行你盘上的节点和容器ID( docker://...
  • SSH 进入节点
  • docker exec -u root ID -- /bin/bash

回答by jordanwilson230

There are some plugins for kubectl that may help you achieve this: https://github.com/jordanwilson230/kubectl-plugins

有一些 kubectl 插件可以帮助您实现这一目标:https: //github.com/jordanwilson230/kubectl-plugins

One of the plugins called, 'ssh', will allow you to exec as root user by running (for example) kubectl ssh -u root -p nginx-0

名为“ssh”的插件之一将允许您通过运行(例如)以 root 用户身份执行 kubectl ssh -u root -p nginx-0

回答by MCI

  • docker container lsto find container ID
  • docker exec -it -u root ID /bin/bash
  • docker container ls查找容器 ID
  • docker exec -it -u root ID /bin/bash

回答by Abdennour TOUMI

For my case, I was in need for root access (or sudo) to container to give the chownpermission to a specific mount path.

就我而言,我需要对容器进行 root 访问(或 sudo)以chown授予特定挂载路径的权限。

I cannot SSH to machine because I designed my infrastructure to be fully automated with Terraform without any manual access.

我无法通过 SSH 连接到机器,因为我将基础架构设计为使用 Terraform 完全自动化,无需任何手动访问。

Instead, I found that initContainersdoes the job:

相反,我发现这样initContainers做可以:

  initContainers:
    - name: volume-prewarming
      image: busybox
      command: ["sh", "-c", "chown -R 1000:0 {{ .Values.persistence.mountPath }}"]
      volumeMounts:
      - name: {{ .Chart.Name }}
        mountPath: {{ .Values.persistence.mountPath }}

I've also created a whole course about Production grade running kubernetes on AWS using EKS

我还创建了一个关于使用 EKS 在 AWS 上运行 Kubernetes 的生产级的完整课程