From aaf7e99434e92ad05bdd36a16c5a7efa2e4334a7 Mon Sep 17 00:00:00 2001 From: "wei.wang" Date: Sat, 30 Apr 2022 03:16:38 +0800 Subject: [PATCH] [zh]Add content/zh/docs/tasks/debug/debug-application/get-shell-running-container.md [zh]Add content/zh/docs/tasks/debug/debug-application/get-shell-running-container.md [zh]Add content/zh/docs/tasks/debug/debug-application/get-shell-running-container.md --- .../get-shell-running-container.md | 211 ++++++++++++++++++ 1 file changed, 211 insertions(+) create mode 100644 content/zh/docs/tasks/debug/debug-application/get-shell-running-container.md diff --git a/content/zh/docs/tasks/debug/debug-application/get-shell-running-container.md b/content/zh/docs/tasks/debug/debug-application/get-shell-running-container.md new file mode 100644 index 0000000000..e67d6b1859 --- /dev/null +++ b/content/zh/docs/tasks/debug/debug-application/get-shell-running-container.md @@ -0,0 +1,211 @@ +--- +title: 获取正在运行容器的 Shell +content_type: task +--- + + + + +本文介绍怎样使用 `kubectl exec` 命令获取正在运行容器的 Shell。 + +## {{% heading "prerequisites" %}} + +{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}} + + + + + +## 获取容器的 Shell + + + +在本练习中,你将创建包含一个容器的 Pod。容器运行 nginx 镜像。下面是 Pod 的配置文件: + +{{< codenew file="application/shell-demo.yaml" >}} + + + +创建 Pod: + +```shell +kubectl create -f https://k8s.io/examples/application/shell-demo.yaml +``` + + + +检查容器是否运行正常: + +```shell +kubectl get pod shell-demo +``` + + + +获取正在运行容器的 Shell: + +```shell +kubectl exec -it shell-demo -- /bin/bash +``` +{{< note >}} + + +双破折号 "--" 用于将要传递给命令的参数与 kubectl 的参数分开。 +{{< /note >}} + + + +在 shell 中,打印根目录: + +```shell +root@shell-demo:/# ls / +``` + + + +在 shell 中,实验其他命令。下面是一些示例: + +```shell +root@shell-demo:/# ls / +root@shell-demo:/# cat /proc/mounts +root@shell-demo:/# cat /proc/1/maps +root@shell-demo:/# apt-get update +root@shell-demo:/# apt-get install -y tcpdump +root@shell-demo:/# tcpdump +root@shell-demo:/# apt-get install -y lsof +root@shell-demo:/# lsof +root@shell-demo:/# apt-get install -y procps +root@shell-demo:/# ps aux +root@shell-demo:/# ps aux | grep nginx +``` + + + +## 编写 nginx 的根页面 + + + +在看一下 Pod 的配置文件。该 Pod 有个 `emptyDir` 卷,容器将该卷挂载到了 `/usr/share/nginx/html`。 + + + +在 shell 中,在 `/usr/share/nginx/html` 目录创建一个 `index.html` 文件: + +```shell +root@shell-demo:/# echo Hello shell demo > /usr/share/nginx/html/index.html +``` + + + +在 shell 中,向 nginx 服务器发送 GET 请求: + +```shell +root@shell-demo:/# apt-get update +root@shell-demo:/# apt-get install curl +root@shell-demo:/# curl localhost +``` + + + +输出结果显示了你在 `index.html` 中写入的文本。 + +```shell +Hello shell demo +``` + + + +当用完 shell 后,输入 `exit` 退出。 + + + +## 在容器中运行单个命令 + + + +在普通的命令窗口(而不是 shell)中,打印环境运行容器中的变量: + +```shell +kubectl exec shell-demo env +``` + + + +实验运行其他命令。下面是一些示例: + +```shell +kubectl exec shell-demo ps aux +kubectl exec shell-demo ls / +kubectl exec shell-demo cat /proc/1/mounts +``` + + + + + +## 当 Pod 包含多个容器时打开 shell + + + +如果 Pod 有多个容器,`--container` 或者 `-c` 可以在 `kubectl exec` 命令中指定容器。 +例如,您有个名为 my-pod 的容器,该 Pod 有两个容器分别为 main-app 和 healper-app。 +下面的命令将会打开一个 shell 访问 main-app 容器。 + +```shell +kubectl exec -it my-pod --container main-app -- /bin/bash +``` + +## {{% heading "whatsnext" %}} + +* [kubectl exec](/docs/reference/generated/kubectl/kubectl-commands/#exec)