Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Kubernetes - Security Context

Back


Security Context


Declarative Manifests



Set capabilities for a Container

apiVersion: v1
kind: Pod
metadata:
  name: demo-capabilities
spec:
  containers:
    - name: demo
      image: busybox:1.28
      command: ["sh", "-c", "sleep 1h"]
      securityContext:
        capabilities:
          add: ["NET_ADMIN", "SYS_TIME"]

Set the Seccomp Profile

securityContext:
  seccompProfile:
    type: RuntimeDefault
---
securityContext:
  seccompProfile:
    type: Localhost
    localhostProfile: my-profiles/profile-allow.json

Lab: Security Context

# demo-sc.yaml
apiVersion: v1
kind: Pod
metadata:
  name: demo-sc
spec:
  securityContext:
    runAsUser: 1000
    runAsGroup: 3000
    fsGroup: 2000
    supplementalGroups: [4000]
  volumes:
    - name: sec-ctx-vol
      emptyDir: {}
  containers:
    - name: sec-ctx-demo
      image: ubuntu
      command: ["sh", "-c", "sleep 1h"]
      volumeMounts:
        - name: sec-ctx-vol
          mountPath: /data/demo
      securityContext:
        allowPrivilegeEscalation: false
kubectl apply -f demo-sc.yaml
# pod/demo-sc created

kubectl exec -it demo-sc -- sh
id
# uid=1000 gid=3000 groups=2000,3000,4000