获取正在运行容器的 Shell

    你必须拥有一个 Kubernetes 的集群,同时你的 Kubernetes 集群必须带有 kubectl 命令行工具。 如果你还没有集群,你可以通过 构建一 个你自己的集群,或者你可以使用下面任意一个 Kubernetes 工具构建:

    要获知版本信息,请输入 kubectl version.

    获取容器的 Shell

    在本练习中,你将创建包含一个容器的 Pod。容器运行 nginx 镜像。下面是 Pod 的配置文件:

    application/shell-demo.yaml

    创建 Pod:

    1. kubectl create -f https://k8s.io/examples/application/shell-demo.yaml
    1. kubectl get pod shell-demo

    获取正在运行容器的 Shell:

      在 shell 中,打印根目录:

      在 shell 中,实验其他命令。下面是一些示例:

      1. root@shell-demo:/# ls /
      2. root@shell-demo:/# cat /proc/mounts
      3. root@shell-demo:/# cat /proc/1/maps
      4. root@shell-demo:/# apt-get update
      5. root@shell-demo:/# apt-get install -y tcpdump
      6. root@shell-demo:/# apt-get install -y lsof
      7. root@shell-demo:/# lsof
      8. root@shell-demo:/# apt-get install -y procps
      9. root@shell-demo:/# ps aux

      在 shell 中,在 /usr/share/nginx/html 目录创建一个 index.html 文件:

      1. root@shell-demo:/# echo Hello shell demo > /usr/share/nginx/html/index.html

      在 shell 中,向 nginx 服务器发送 GET 请求:

      1. root@shell-demo:/# apt-get install curl
      2. root@shell-demo:/# curl localhost

      输出结果显示了你在 index.html 中写入的文本。

      当用完 shell 后,输入 exit 退出。

      在容器中运行单个命令

      在普通的命令窗口(而不是 shell)中,打印环境运行容器中的变量:

      1. kubectl exec shell-demo env
      1. kubectl exec shell-demo ps aux
      2. kubectl exec shell-demo ls /
      3. kubectl exec shell-demo cat /proc/1/mounts

      如果 Pod 有多个容器,--container 或者 -c 可以在 命令中指定容器。 例如,您有个名为 my-pod 的容器,该 Pod 有两个容器分别为 main-app 和 healper-app。 下面的命令将会打开一个 shell 访问 main-app 容器。

      1. kubectl exec -it my-pod --container main-app -- /bin/bash

      接下来