Shared PID, IPC, and volumes across Pod containers
What you'll learn
- Explain the namespace sharing options in a Pod spec
- Identify when to share the PID namespace (shareProcessNamespace)
- Use IPC namespace sharing for SystemV IPC or POSIX message queues
- Configure shared volumes across containers in a Pod
Prerequisites
Verified against Kubernetes 1.34.x · kubeadm 1.34.x · kubectl 1.34.x · etcd 3.6.x · CoreDNS 1.11.x · containerd 1.7.x / 2.x · 2026-08-16
Containers in a Pod share a sandbox that holds several Linux namespaces. The network namespace is always shared. The IPC namespace is shared by default. The PID namespace is per-container unless explicitly shared. Volumes are always available to declare at the Pod level. This lesson covers the sharing options and when each is the right choice.
The Linux namespace model
A Linux namespace is a kernel primitive that isolates a set of resources so that processes in different namespaces see different instances of those resources. Kubernetes uses namespaces extensively:
- Mount namespace: each container has its own; the Pod’s volumes are mounted into the container’s namespace.
- PID namespace: each container has its own by default; processes in container A don’t see processes in container B.
- Network namespace: shared across all containers in the Pod.
- IPC namespace: shared across all containers by default.
- UTS namespace: shared across all containers by default (the Pod’s hostname is the same for every container).
- User namespace: per-container (Linux 5.x); maps UIDs inside the container to UIDs on the host.
- Cgroup namespace: per-container.
The Pod’s sandbox holds the namespaces. Each container is started inside the sandbox via the CRI and inherits the namespaces the container is configured to inherit.
flowchart LR
Sandbox --> MountA["Mount ns A"]
Sandbox --> MountB["Mount ns B"]
Sandbox --> Net["Network ns (shared)"]
Sandbox --> IPC["IPC ns (shared)"]
Sandbox --> UTS["UTS ns (shared)"]
Sandbox --> PID1["PID ns A"]
Sandbox --> PID2["PID ns B"]
The defaults:
| Namespace | Default sharing | Override field |
|---|---|---|
| Mount | per-container | none (per-container is required) |
| PID | per-container | spec.shareProcessNamespace: true |
| Network | shared | spec.hostNetwork: true (breaks sharing) |
| IPC | shared | none (always shared) |
| UTS | shared | none (always shared) |
| User | per-container | spec.hostUsers: false (1.28+) |
| Cgroup | per-container | none |
shareProcessNamespace — sharing the PID namespace
spec:
shareProcessNamespace: true
Setting this to true makes all containers in the Pod share
the same PID namespace. Container A can see container B’s
processes in ps, send signals, etc.
Use cases:
- A sidecar that monitors the main container’s processes (e.g., a process exporter that walks the process tree).
- A debug sidecar that needs to attach to the main
container’s processes (rare; usually
kubectl debugis better). - Coordinated startup/shutdown where one container needs to signal the other.
kubectl exec web-7c8 -c sidecar -- ps aux
# sees processes from both containers
kubectl exec web-7c8 -c sidecar -- kill -TERM 1
# can send signals to processes in the main container
Risks:
- A bug in one container can
killanother container’s processes. The signal goes to PID 1 in the namespace, which is the main container’s PID 1 by default. - A compromised sidecar can read the main container’s
environment (via
/proc/<pid>/environ).
IPC namespace sharing
The IPC namespace is shared by default; there is no opt-in. Containers in a Pod can use System V IPC (semaphores, shared memory) and POSIX message queues to communicate.
// Container A creates a shared memory segment
shmget(key, size, IPC_CREAT);
// Container B attaches to the same segment
shmat(shmid, NULL, 0);
Use cases:
- High-performance shared memory between processes (rare in Kubernetes; usually a sign the workload should be in one container).
- Coordination via semaphores across containers.
In practice, IPC sharing is rarely the right choice for Kubernetes sidecars. Network namespace sharing covers most needs.
Shared volumes across containers
spec:
containers:
- name: app
volumeMounts:
- name: shared-data
mountPath: /data
- name: sidecar
volumeMounts:
- name: shared-data
mountPath: /var/log/app
volumes:
- name: shared-data
emptyDir: {}
Volumes declared in spec.volumes are mounted into any
container that lists them in spec.containers[*].volumeMounts.
The mount path can differ across containers, but the
underlying volume is shared.
The standard pattern:
- Log sidecar: the main container writes logs to a
shared
emptyDir; a sidecar tails the log file and ships it to a log aggregator. - Config hot-reload: a config sidecar watches a ConfigMap mounted as a volume and signals the main container on change.
- Cache warming: an init container pre-fills a shared
emptyDir; the main container reads the cache at startup. - Service mesh / proxy: a proxy sidecar reads the main container’s traffic (via shared network namespace) and writes access logs to a shared volume.
flowchart LR
App[Main container] -->|"writes /var/log/app.log"| Vol["emptyDir /shared-data"]
Vol -->|"reads via tail -f"| Side[Sidecar]
Side --> Log[Log aggregator]
hostPID, hostIPC, hostUsers — escaping the sandbox
Three flags lift the Pod out of the sandbox isolation:
spec:
hostPID: true
hostIPC: true
hostUsers: false # 1.28+
hostPID: true: the Pod shares the host’s PID namespace. The Pod can see all processes on the node.hostIPC: true: the Pod shares the host’s IPC namespace. The Pod can access System V IPC primitives of host processes.hostUsers: false(the default in 1.28+): the Pod’s containers run in a user namespace where the container’s root user maps to a non-root host user. Setting totruebreaks this — the container’s root user is the host’s root.
Use cases:
hostPID: process monitoring agents (e.g., node exporter) that need to walk the host’s process tree.hostIPC: rarely used; some database engines require it.hostUsers: false(default): every Pod. The mapping is what makes running as non-root in the container safe.
The sharing decision matrix
| Sharing | Default | Field | Use case |
|---|---|---|---|
| Network | shared | n/a | Always shared — same IP, localhost |
| IPC | shared | n/a | Always shared — rarely used in practice |
| UTS | shared | n/a | Always shared — same hostname |
| PID | per-container | shareProcessNamespace | Process monitors; debugging |
| User | per-container | hostUsers (1.28+) | Default is user namespace remap |
| Mount | per-container | none | Volumes are bind-mounted per container |
Production discipline: share only what the sidecar pattern needs. Most sidecars work over the network namespace alone; volume sharing for log shipping or config hot-reload; PID sharing for the rare process monitor.
Cross-course references
- The Linux course part
I-Linux-Foundationscovers Linux namespaces (man 7 namespaces); the Pod’s namespace sharing is the same primitive at the cluster level. - The Docker course part
XXXI-Docker-Networkingcovers container networking; Pod-level namespace sharing extends this to multi-container Pods. - The Ansible course part
XXXV-Ansible-Scriptingcovers process management discipline; PID sharing in Pods is the same idea.
Quiz
Knowledge check · 4 questions
Q1. Which namespaces are shared by default across containers in a Pod, without any explicit configuration?
Q2. If two containers in a Pod both mount an `emptyDir` volume, writes from one container are immediately visible to the other.
Q3. An operator proposes running a process-exporter sidecar with `hostPID: true` so it can monitor all processes on the node. Walk through the security implications and propose alternatives.
Cluster has Pod Security Standards enforced at the `baseline` level. The operator wants a custom process exporter that walks `/proc` on the node to gather per-process CPU and memory stats. The proposal: a DaemonSet with one Pod per node, each with `hostPID: true`.
Q4. When is sharing the PID namespace (`shareProcessNamespace: true`) the right choice for a sidecar?
Passing score: 75%. Answers are checked in this browser.
Production discipline
- Share only what the sidecar needs. Most sidecars work over localhost and shared volumes.
- Avoid
shareProcessNamespaceunless required. A compromised sidecar with shared PIDs can signal the main container or read its environment via/proc/<pid>/environ. - Never use
hostPID: truefor application Pods. It is reserved for node agents that genuinely need host-wide process visibility. - Use shared
emptyDirfor log shipping and config hot-reload. The standard sidecar pattern. - Inspect the Pod spec for namespace sharing during
security reviews.
hostPID,hostIPC,hostUsers: trueare red flags for application Pods.