Share Process Namespace between Containers in a Pod

FEATURE STATE: Kubernetes v1.17 [stable]

This page shows how to configure process namespace sharing for a pod. When process namespace sharing is enabled, processes in a container are visible to all other containers in that pod.

You can use this feature to configure cooperating containers, such as a log handler sidecar container, or to troubleshoot container images that don’t include debugging utilities like a shell.

Before you begin

You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. It is recommended to run this tutorial on a cluster with at least two nodes that are not acting as control plane hosts. If you do not already have a cluster, you can create one by using minikube or you can use one of these Kubernetes playgrounds:

Your Kubernetes server must be at or later than version v1.10. To check the version, enter kubectl version.

Configure a Pod

Process Namespace Sharing is enabled using the shareProcessNamespace field of v1.PodSpec. For example:

pods/share-process-namespace.yaml Share Process Namespace between Containers in a Pod - 图1

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: nginx
  5. spec:
  6. shareProcessNamespace: true
  7. containers:
  8. - name: nginx
  9. image: nginx
  10. - name: shell
  11. image: busybox
  12. securityContext:
  13. capabilities:
  14. add:
  15. - SYS_PTRACE
  16. stdin: true
  17. tty: true
  1. Create the pod nginx on your cluster:

    1. kubectl apply -f https://k8s.io/examples/pods/share-process-namespace.yaml
  2. Attach to the shell container and run ps:

    1. kubectl attach -it nginx -c shell

    If you don’t see a command prompt, try pressing enter.

    1. / # ps ax
    2. PID USER TIME COMMAND
    3. 1 root 0:00 /pause
    4. 8 root 0:00 nginx: master process nginx -g daemon off;
    5. 14 101 0:00 nginx: worker process
    6. 15 root 0:00 sh
    7. 21 root 0:00 ps ax

You can signal processes in other containers. For example, send SIGHUP to nginx to restart the worker process. This requires the SYS_PTRACE capability.

  1. / # kill -HUP 8
  2. / # ps ax
  3. PID USER TIME COMMAND
  4. 1 root 0:00 /pause
  5. 8 root 0:00 nginx: master process nginx -g daemon off;
  6. 15 root 0:00 sh
  7. 22 101 0:00 nginx: worker process
  8. 23 root 0:00 ps ax

It’s even possible to access another container image using the /proc/$pid/root link.

  1. / # head /proc/8/root/etc/nginx/nginx.conf
  2. user nginx;
  3. worker_processes 1;
  4. error_log /var/log/nginx/error.log warn;
  5. pid /var/run/nginx.pid;
  6. events {
  7. worker_connections 1024;

Understanding Process Namespace Sharing

Pods share many resources so it makes sense they would also share a process namespace. Some container images may expect to be isolated from other containers, though, so it’s important to understand these differences:

  1. The container process no longer has PID 1. Some container images refuse to start without PID 1 (for example, containers using systemd) or run commands like kill -HUP 1 to signal the container process. In pods with a shared process namespace, kill -HUP 1 will signal the pod sandbox. (/pause in the above example.)

  2. Processes are visible to other containers in the pod. This includes all information visible in /proc, such as passwords that were passed as arguments or environment variables. These are protected only by regular Unix permissions.

  3. Container filesystems are visible to other containers in the pod through the /proc/$pid/root link. This makes debugging easier, but it also means that filesystem secrets are protected only by filesystem permissions.