This page describes some of the security features that are built into the Linux kernel that you can use in your Kubernetes workloads. To learn how to apply these features to your Pods and containers, refer to Configure a SecurityContext for a Pod or Container. You should already be familiar with Linux and with the basics of Kubernetes workloads.
When you deploy a workload in Kubernetes, use the Pod specification to restrict
that workload from running as the root user on the node. You can use the Pod
securityContext to define the specific Linux user and group for the processes in
the Pod, and explicitly restrict containers from running as root users. Setting
these values in the Pod manifest takes precedence over similar values in the
container image, which is especially useful if you're running images that you
don't own.
Configuring the kernel security features on this page provides fine-grained control over the actions that processes in your cluster can take, but managing these configurations can be challenging at scale. Running containers as non-root, or in user namespaces if you need root privileges, helps to reduce the chance that you'll need to enforce your configured kernel security capabilities.
Kubernetes lets you configure and use Linux kernel features to improve isolation and harden your containerized workloads. Common features include the following:
To configure settings for one of these features, the operating system that you choose for your nodes must enable the feature in the kernel. For example, Ubuntu 7.10 and later enable AppArmor by default. To learn whether your OS enables a specific feature, consult the OS documentation.
You use the securityContext field in your Pod specification to define the
constraints that apply to those processes. The securityContext field also
supports other security settings, such as specific Linux capabilities or file
access permissions using UIDs and GIDs. To learn more, refer to
Configure a SecurityContext for a Pod or Container.
Some of your workloads might need privileges to perform specific actions as the root user on your node's host machine. Linux uses capabilities to divide the available privileges into categories, so that processes can get the privileges required to perform specific actions without being granted all privileges. Each capability has a set of system calls (syscalls) that a process can make. seccomp lets you restrict these individual syscalls. It can be used to sandbox the privileges of a process, restricting the calls it is able to make from userspace into the kernel.
In Kubernetes, you use a container runtime on each node to run your containers. Example runtimes include CRI-O, Docker, or containerd. Each runtime allows only a subset of Linux capabilities by default. You can further limit the allowed syscalls individually by using a seccomp profile. Container runtimes usually include a default seccomp profile. Kubernetes lets you automatically apply seccomp profiles loaded onto a node to your Pods and containers.
allowPrivilegeEscalation setting for Pods and
containers. When set to false, this prevents processes from gaining new
capabilities and restricts unprivileged users from changing the applied seccomp
profile to a more permissive profile.To learn how to implement seccomp in Kubernetes, refer to Restrict a Container's Syscalls with seccomp or the Seccomp node reference
To learn more about seccomp, see Seccomp BPF in the Linux kernel documentation.
To use this feature, you (or a cluster administrator) will need to enable the SecurityProfileOCI feature gate for all relevant components in your cluster.
See Enable Or Disable Feature Gates for more information.
Instead of installing a seccomp profile on every node, you can publish the
profile to an OCI registry and reference it from the seccompProfile of a Pod
or container by setting type: OCI. The oci.ref field must be a fully
qualified reference that names the registry and is pinned by a sha256,
sha384 or sha512 digest, such as
registry.example.com/profiles/app@sha256:<digest>. Tags are not accepted, so
the profile that a workload uses changes only when you change the reference.
Before the kubelet starts the Pod, it asks the container runtime to pull the
profile, using the same credentials as for the Pod's container images. After
the Pod has started, the kubelet pulls a container's profile again right before
it creates that container, for example on a restart. If that pull fails, only
that container waits, and the rest of the Pod keeps running.
The container runtime merges the pulled profile with a base profile. A syscall
is allowed only if both the pulled profile and the base allow it, so a profile
from a registry can restrict a workload further, but it can never loosen its
base. The base is the runtime's default seccomp profile, or a different one that
the node administrator configured in the runtime, unless you set
oci.baseProfile to a Localhost profile on the node, which then takes the
place of the default. Setting oci.baseProfile to RuntimeDefault selects the
default explicitly.
To publish a profile, push it as an OCI artifact whose single layer is the
profile itself: a JSON seccomp profile in the format of the linux.seccomp
section of the
OCI runtime specification,
stored as is rather than in a tar archive. Set the config media type of the
artifact, or its artifactType together with an empty config, to
application/vnd.cncf.seccomp-profile.config.v1+json. This is the format that
Kubernetes recommends; which formats a container runtime accepts is up to the
runtime. Use the digest of the pushed artifact in oci.ref.
The scheduler places Pods that use type: OCI only on nodes whose kubelet has
the SecurityProfileOCI feature gate enabled and whose container runtime
supports pulling seccomp profiles. A Pod bypasses that check if it sets
nodeName, or if a client other than the scheduler binds it to a node through
the binding subresource. A kubelet of v1.38 or later rejects such a Pod if
the node cannot run it.
A kubelet older than v1.38 does not know type: OCI and runs the containers
without any seccomp profile, as Unconfined. The scheduler never places these
Pods on such a node, but a Pod that bypasses the scheduler still runs there.
The same applies to an ephemeral container with type: OCI that is added to a
Pod already running on such a node. Enable the feature gate only once every
kubelet in the cluster runs v1.38 or later.
As an additional safeguard, a cluster administrator can use a
ValidatingAdmissionPolicy
to reject Pods that set both spec.nodeName and a seccomp profile of
type: OCI. Such a policy does not cover the binding subresource, because a
binding request does not contain the Pod; use
RBAC to limit who can create
pods/binding.
Privileged containers cannot use type: OCI, whether the profile is set on the
container or inherited from the Pod, because container runtimes do not apply
seccomp profiles to privileged containers. This includes privileged ephemeral
containers, such as the one that kubectl debug --profile=sysadmin adds. While
the feature is alpha, the baseline and restricted
Pod Security Standards
do not allow type: OCI, so
Pod Security Admission
rejects such Pods in namespaces that enforce either of these levels.
seccomp is a low-level security configuration that you should only configure yourself if you require fine-grained control over Linux syscalls. Using seccomp, especially at scale, has the following risks:
Recommendation: Use the default seccomp profile that's bundled with your container runtime. If you need a more isolated environment, consider using a sandbox, such as gVisor. Sandboxes solve the preceding risks with custom seccomp profiles, but require more compute resources on your nodes and might have compatibility issues with GPUs and other specialized hardware.
You can use Linux policy-based mandatory access control (MAC) mechanisms, such as AppArmor and SELinux, to harden your Kubernetes workloads.
AppArmor is a Linux kernel security module that supplements the standard Linux user and group based permissions to confine programs to a limited set of resources. AppArmor can be configured for any application to reduce its potential attack surface and provide greater in-depth defense. It is configured through profiles tuned to allow the access needed by a specific program or container, such as Linux capabilities, network access, and file permissions. Each profile can be run in either enforcing mode, which blocks access to disallowed resources, or complain mode, which only reports violations.
AppArmor can help you to run a more secure deployment by restricting what containers are allowed to do, and/or provide better auditing through system logs. The container runtime that you use might ship with a default AppArmor profile, or you can use a custom profile.
To learn how to use AppArmor in Kubernetes, refer to Restrict a Container's Access to Resources with AppArmor.
SELinux is a Linux kernel security module that lets you restrict the access that a specific subject, such as a process, has to the files on your system. You define security policies that apply to subjects that have specific SELinux labels. When a process that has an SELinux label attempts to access a file, the SELinux server checks whether that process' security policy allows the access and makes an authorization decision.
In Kubernetes, you can set an SELinux label in the securityContext field of
your manifest. The specified labels are assigned to those processes. If you
have configured security policies that affect those labels, the host OS kernel
enforces these policies.
To learn how to use SELinux in Kubernetes, refer to Assign SELinux labels to a container.
The operating system on your Linux nodes usually includes one of either AppArmor or SELinux. Both mechanisms provide similar types of protection, but have differences such as the following:
The following table describes the use cases and scope of each security control. You can use all of these controls together to build a more hardened system.
| Security feature | Description | How to use | Example |
|---|---|---|---|
| seccomp | Restrict individual kernel calls in the userspace. Reduces the likelihood that a vulnerability that uses a restricted syscall would compromise the system. | Specify a loaded seccomp profile in the Pod or container specification to apply its constraints to the processes in the Pod. | Reject the unshare syscall, which was used in
CVE-2022-0185. |
| AppArmor | Restrict program access to specific resources. Reduces the attack surface of the program. Improves audit logging. | Specify a loaded AppArmor profile in the container specification. | Restrict a read-only program from writing to any file path in the system. |
| SELinux | Restrict access to resources such as files, applications, ports, and processes using labels and security policies. | Specify access restrictions for specific labels. Tag processes with those labels to enforce the access restrictions related to the label. | Restrict a container from accessing files outside its own filesystem. |
seccomp, AppArmor, and SELinux usually have a default configuration that offers basic protections. You can also create custom profiles and policies that meet the requirements of your workloads. Managing and distributing these custom configurations at scale might be challenging, especially if you use all three features together. To help you to manage these configurations at scale, use a tool like the Kubernetes Security Profiles Operator.
Kubernetes lets you specify that some trusted containers can run in privileged mode. Any container in a Pod can run in privileged mode to use operating system administrative capabilities that would otherwise be inaccessible. This is available for both Windows and Linux.
Privileged containers explicitly override some of the Linux kernel constraints that you might use in your workloads, as follows:
Unconfined seccomp profile,
overriding any seccomp profile that you specified in your manifest.unconfined_t domain.Any container in a Pod can enable Privileged mode if you set the
privileged: true field in the
securityContext
field for the container. Privileged containers override or undo many other hardening settings such as the applied seccomp profile, AppArmor profile, or
SELinux constraints. Privileged containers are given all Linux capabilities,
including capabilities that they don't require. For example, a root user in a
privileged container might be able to use the CAP_SYS_ADMIN and
CAP_NET_ADMIN capabilities on the node, bypassing the runtime seccomp
configuration and other restrictions.
In most cases, you should avoid using privileged containers, and instead grant
the specific capabilities required by your container using the capabilities
field in the securityContext field. Only use privileged mode if you have a
capability that you can't grant with the securityContext. This is useful for
containers that want to use operating system administrative capabilities such
as manipulating the network stack or accessing hardware devices.
In Kubernetes version 1.26 and later, you can also run Windows containers in a
similarly privileged mode by setting the windowsOptions.hostProcess flag on
the security context of the Pod spec. For details and instructions, see
Create a Windows HostProcess Pod.
runAsNonRoot: true.Additionally, you can run workloads in user namespaces by setting
hostUsers: false in your Pod manifest. This lets you run containers as root
users in the user namespace, but as non-root users in the host namespace on the
node. This is still in early stages of development and might not have the level
of support that you need. For instructions, refer to
Use a User Namespace With a Pod.