Review Kubernetes container Linux capability reduction

Drop default privileges and allow only the Linux capabilities the application needs.

Description

A Linux container can inherit capabilities from the runtime. Adding one with capabilities.add leaves the existing defaults in place.

Potential impact

An application that is compromised can use every capability it still holds.

Remediation

Use drop: [ALL] under securityContext.capabilities and add back only what the application needs. Disable privileged mode, use a non-root user, and restrict privilege escalation. Test startup and normal operation, then verify the effective capabilities.

Examples

These Deployment excerpts omit the selector. Keep NET_BIND_SERVICE only if the image and its port configuration require it to bind a low port.

Before

yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  template:
    spec:
      containers:
        - name: payment
          image: nginx
          securityContext:
            capabilities:
              add:
                - NET_BIND_SERVICE

After

yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  template:
    spec:
      containers:
        - name: payment
          image: nginx
          securityContext:
            capabilities:
              drop:
                - ALL
              add:
                - NET_BIND_SERVICE

References