To use this feature, you (or a cluster administrator) will need to enable the PerPodPIDLimit feature gate for all relevant components in your cluster.
See Enable Or Disable Feature Gates for more information.
This page shows how to set a process ID (PID) limit for a Pod. A per-pod PID limit caps the number of processes that all containers in the Pod can create together. This protects the node from fork bombs and runaway processes in a single Pod.
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 1.38.To check the version, enter kubectl version.
The PerPodPIDLimit
feature gate
must be enabled on the API server and on the kubelet.
Nodes must use cgroup v2. The kubelet rejects Pods that set a PID limit on nodes that use cgroup v1.
If the feature gate is enabled on only some nodes, for example during a rolling upgrade, the scheduler places Pods that set a PID limit only on nodes with the feature gate enabled. This uses Node Declared Features, which is enabled by default. The scheduler does not check the cgroup version.
cgroupsPerQOS: false, it does not create Pod-level
cgroups and the PID limit is not enforced, even though the Pod is admitted and
runs.podPidsLimit, the effective limit is
min(node podPidsLimit, pod pids limit). If the node's podPidsLimit is lower
than the limit you set for the Pod, the node's limit applies. If the node's
podPidsLimit is -1 (no node limit), the Pod's limit is applied directly.To set a PID limit for a Pod, include pids under the Pod-level
spec.resources.limits field. You cannot set pids in a container's
resources, and you cannot set requests.pids.
Here is a manifest for a Pod with a PID limit of 1024:
apiVersion: v1
kind: Pod
metadata:
name: pid-limit-demo
spec:
resources:
limits:
pids: "1024"
containers:
- name: demo-container
image: registry.k8s.io/pause:3.10
Create the Pod:
kubectl apply -f https://k8s.io/examples/pods/resource/pod-level-pid-limit.yaml
Verify that the Pod is running:
kubectl get pod pid-limit-demo
View the Pod's specification:
kubectl get pod pid-limit-demo --output=yaml
The output shows the PID limit:
spec:
containers:
- name: demo-container
# ...
resources:
limits:
pids: "1024"
The kubelet sets the limit in the pids.max file of the Pod's cgroup.
Get the Pod's UID and the node it runs on:
kubectl get pod pid-limit-demo -o jsonpath='{.metadata.uid}{"\n"}{.spec.nodeName}{"\n"}'
Then, on that node, read pids.max from the Pod's cgroup. The cgroup name
contains the Pod's UID. With the systemd cgroup driver, the dashes in the UID
are replaced with underscores. The following command finds the cgroup for
either cgroup driver:
# Replace <pod-uid> with the UID from the previous command
POD_UID=<pod-uid>
find /sys/fs/cgroup -type d \( -name "*pod${POD_UID}*" -o -name "*pod${POD_UID//-/_}*" \) \
-exec cat {}/pids.max \; -quit
The output shows the effective limit:
1024
The PID limit is set on the Pod-level cgroup, not on each container's cgroup.
The path depends on the Pod's QoS class and the kubelet's cgroup driver. For
example, for a BestEffort Pod:
systemd cgroup driver (dashes in the UID replaced with underscores):
/sys/fs/cgroup/kubepods.slice/kubepods-besteffort.slice/kubepods-besteffort-pod<pod-uid>.slice/pids.maxcgroupfs cgroup driver:
/sys/fs/cgroup/kubepods/besteffort/pod<pod-uid>/pids.maxIf the Pod's limit is higher than the node's podPidsLimit, the kubelet applies
the node's limit and emits a PIDLimitCapped warning event. To check for it:
kubectl get events --field-selector involvedObject.name=pid-limit-demo,reason=PIDLimitCapped
If the limit was capped, the event message shows both values, for example:
Requested PID limit 8192 exceeds node podPidsLimit; capped to 4096
Delete the Pod:
kubectl delete pod pid-limit-demo
--pod-max-pids flag or the podPidsLimit
configuration field.