|
| 1 | +--- |
| 2 | +title: Share Process Namespace between Containers in a Pod |
| 3 | +min-kubernetes-server-version: v1.10 |
| 4 | +approvers: |
| 5 | +- dawnchen |
| 6 | +- verb |
| 7 | +--- |
| 8 | + |
| 9 | +{% capture overview %} |
| 10 | + |
| 11 | +{% include feature-state-alpha.md %} |
| 12 | + |
| 13 | +This page shows how to configure process namespace sharing for a pod. When |
| 14 | +process namespace sharing is enabled, processes in a container are visible |
| 15 | +to all other containers in that pod. |
| 16 | + |
| 17 | +You can use this feature to configure cooperating containers, such as a log |
| 18 | +handler sidecar container, or to troubleshoot container images that don't |
| 19 | +include debugging utilities like a shell. |
| 20 | + |
| 21 | +{% endcapture %} |
| 22 | + |
| 23 | +{% capture prerequisites %} |
| 24 | + |
| 25 | +{% include task-tutorial-prereqs.md %} |
| 26 | + |
| 27 | +A special **alpha** feature gate `PodShareProcessNamespace` must be set to true |
| 28 | +across the system: `--feature-gates=PodShareProcessNamespace=true`. |
| 29 | + |
| 30 | +{% endcapture %} |
| 31 | + |
| 32 | +{% capture steps %} |
| 33 | + |
| 34 | +## Configure a Pod |
| 35 | + |
| 36 | +Process Namespace Sharing is enabled using the `ShareProcessNamespace` field of |
| 37 | +`v1.PodSpec`. For example: |
| 38 | + |
| 39 | +{% include code.html language="yaml" file="share-process-namespace.yaml" ghlink="/docs/tasks/configure-pod-container/share-process-namespace.yaml" %} |
| 40 | + |
| 41 | +1. Create the pod `nginx` on your cluster: |
| 42 | + |
| 43 | + $ kubectl create -f https://k8s.io/docs/tasks/configure-pod-container/share-process-namespace.yaml |
| 44 | + |
| 45 | +1. Attach to the `shell` container and run `ps`: |
| 46 | + |
| 47 | + $ kubectl attach -it nginx -c shell |
| 48 | + If you don't see a command prompt, try pressing enter. |
| 49 | + / # ps ax |
| 50 | + PID USER TIME COMMAND |
| 51 | + 1 root 0:00 /pause |
| 52 | + 8 root 0:00 nginx: master process nginx -g daemon off; |
| 53 | + 14 101 0:00 nginx: worker process |
| 54 | + 15 root 0:00 sh |
| 55 | + 21 root 0:00 ps ax |
| 56 | + |
| 57 | +You can signal processes in other containers. For example, send `SIGHUP` to |
| 58 | +nginx to restart the worker process. This requires the `SYS_PTRACE` capability. |
| 59 | + |
| 60 | + / # kill -HUP 8 |
| 61 | + / # ps ax |
| 62 | + PID USER TIME COMMAND |
| 63 | + 1 root 0:00 /pause |
| 64 | + 8 root 0:00 nginx: master process nginx -g daemon off; |
| 65 | + 15 root 0:00 sh |
| 66 | + 22 101 0:00 nginx: worker process |
| 67 | + 23 root 0:00 ps ax |
| 68 | + |
| 69 | +It's even possible to access another container image using the |
| 70 | +`/proc/$pid/root` link. |
| 71 | + |
| 72 | + / # head /proc/8/root/etc/nginx/nginx.conf |
| 73 | + |
| 74 | + user nginx; |
| 75 | + worker_processes 1; |
| 76 | + |
| 77 | + error_log /var/log/nginx/error.log warn; |
| 78 | + pid /var/run/nginx.pid; |
| 79 | + |
| 80 | + |
| 81 | + events { |
| 82 | + worker_connections 1024; |
| 83 | + |
| 84 | +{% endcapture %} |
| 85 | + |
| 86 | +{% capture discussion %} |
| 87 | + |
| 88 | +## Understanding Process Namespace Sharing |
| 89 | + |
| 90 | +Pods share many resources so it makes sense they would also share a process |
| 91 | +namespace. Some container images may expect to be isolated from other |
| 92 | +containers, though, so it's important to understand these differences: |
| 93 | + |
| 94 | +1. **The container process no longer has PID 1.** Some container images refuse |
| 95 | + to start without PID 1 (for example, containers using `systemd`) or run |
| 96 | + commands like `kill -HUP 1` to signal the container process. In pods with a |
| 97 | + shared process namespace, `kill -HUP 1` will signal the pod sandbox. |
| 98 | + (`/pause` in the above example.) |
| 99 | + |
| 100 | +1. **Processes are visible to other containers in the pod.** This includes all |
| 101 | + information visible in `/proc`, such as passwords that were passed as arguments |
| 102 | + or environment variables. These are protected only by regular Unix permissions. |
| 103 | + |
| 104 | +1. **Container filesystems are visible to other containers in the pod through the |
| 105 | + `/proc/$pid/root` link.** This makes debugging easier, but it also means |
| 106 | + that filesystem secrets are protected only by filesystem permissions. |
| 107 | + |
| 108 | +{% endcapture %} |
| 109 | + |
| 110 | +{% include templates/task.md %} |
0 commit comments