Configure a Security Context for a Pod or Container

Specifiying privileges for a container or pod

Planted .

A security context defines privileges for and access control setting for a pod or container. For example:

  1. Running as a specific user
  2. Capabilities like SYS_TIME

This can be done by using the securityContext field in Pod spec.

Official doc has more examples.

Sample run as a user

apiVersion: v1
kind: Pod
metadata:
  name: multi-pod
spec:
  securityContext:
    runAsUser: 1001
  containers:
  -  image: ubuntu
     name: web
     command: ["sleep", "5000"]
     securityContext:
      runAsUser: 1002

  -  image: ubuntu
     name: sidecar
     command: ["sleep", "5000"]

Sample run with capabilitie

apiVersion: v1
kind: Pod
metadata:
  name: security-context-demo-4
spec:
  containers:
  - name: sec-ctx-4
    image: gcr.io/google-samples/hello-app:2.0
    securityContext:
      capabilities:
        add: ["NET_ADMIN", "SYS_TIME"]
Updated . Thoughts? Email me.