Container and Kubernetes security basics
Small boxes, shared kernel, and the settings that keep them safe
A beginner tour of container security: images and registries, what namespaces and cgroups do, least-privilege container settings, and the Kubernetes controls (RBAC, network policies, secrets, Pod Security Standards, admission control) that go around them.
beginner level, 16 minutes, 19 slides.
Container and Kubernetes security basics
Welcome. A beginner tour of container security: images and registries, what namespaces and cgroups do, least-privilege container settings, and the Kubernetes controls (RBAC, network policies, secrets, Pod Security Standards, admission control) that go around them. This talk runs about 16 minutes.
Containers are convenient, not magic
NIST describes containers as a form of operating system virtualization combined with application software packaging. They provide a portable, reusable and automatable way to package and run applications.
A container is a way to package an application together with what it needs, so it runs the same on a laptop, a test server or the cloud. NIST describes them as a form of operating system virtualization combined with application packaging. That convenience is why they are everywhere. It also means one mistake in how you build or run them gets copied everywhere too. In this talk we look at the safe habits, first for single containers and then for Kubernetes, the system that runs many of them. No prior experience is needed.
Image, container, registry
| Term | Plain meaning |
|---|---|
| Image | The packaged, read-only recipe: your app plus the files it needs |
| Container | A running instance started from an image |
| Registry | A server that stores and hands out images |
- The Open Container Initiative (OCI) creates open industry standards around container formats and runtimes.
- It currently has three specifications: runtime, image and distribution.
- Standards mean images built with one tool can run on another.
Three words to learn first. An image is the packaged recipe, read only. A container is a running copy started from that image, like a cake baked from a recipe. A registry is the shop shelf where images are stored and downloaded from. The Open Container Initiative writes open standards for all this, and its site says it currently has three specifications: a runtime specification, an image specification and a distribution specification. Because the formats are standard, different tools can build, store and run the same image. Notice that security can go wrong at any of the three steps.
Namespaces and cgroups, at a glance
Namespaces
- Docker: the first and most straightforward form of isolation
- Processes in a container cannot see processes in another container or the host
- Each container gets its own network stack
Control groups (cgroups)
- Implement resource accounting and limiting
- Help each container get its fair share of memory, CPU and disk I/O
- Stop one container from exhausting a resource and bringing the system down
Define: kernel: The kernel is the core of the operating system. Containers on one machine share it, which is why isolation settings matter.
Containers get their separation from two features of the Linux kernel. Namespaces give each container its own view of the system. Docker's security page calls them the first and most straightforward form of isolation: processes inside cannot see processes in other containers or on the host, and each gets its own network stack. Control groups, or cgroups, count and limit resources, so one noisy container cannot eat all the memory or processor time. Think of namespaces as separate rooms, and cgroups as a fair-share meter on the electricity. The kernel underneath is still shared.
Where the layers sit
Our own simplified picture. Each layer needs its own controls.
- Cloud account and network: Who can reach the cluster at all
- Cluster and its control plane: RBAC, admission, policies
- Node: host operating system and runtime: Shared kernel
- Pod and container settings: Non-root, read-only, no extras
- Your application and data: The thing being protected
A weakness in an outer layer can undo careful work in an inner one, so we will touch each layer in turn.
Here is the map for the rest of the talk, drawn as layers with the outermost on top and the thing we protect at the bottom. The outer layers are your cloud account and network, then the cluster and its control plane, then the node, which is the machine running the containers with a shared kernel. Inside that sit your pods and container settings, and finally the application and its data. This picture is our own simplification. The point to notice is that no single layer is enough. Each one gets its own controls, and we will visit them one by one.
Pull by digest, not by hope
Kubernetes documents two ways to name an image version: a tag or a digest. Tags can be moved to point to different images. Digests are hashes of the image's content and are immutable.
A pod pinned to a digest. The registry name and digest are placeholders.
apiVersion: v1
kind: Pod
metadata:
name: api
spec:
containers:
- name: api
image: registry.example.com/shop/api@sha256:<digest-from-your-registry>- A tag such as 1.4.2 is a label someone can re-point.
- A digest names exactly one version of the image.
When you tell Kubernetes which image to run, you can name it with a tag or with a digest. The Kubernetes documentation says tags can be moved to point to different images, while a digest is a hash of the image's content and is immutable. Imagine a tag as a sticky note on a box that anyone can move to a different box, whereas a digest is the box's fingerprint. In the example, the digest is a placeholder you would copy from your own registry. Pinning to a digest means what you tested is exactly what runs.
Scan images before they ship
The Kubernetes security checklist says image scanning should be completed before deploying a container image to a cluster, and is usually done as part of the deployment process in a CI/CD pipeline.
A scan step sits before deploy. Our own simple pipeline.
- Build: Create the image
- Scan: Look for known vulnerabilities
- Registry: Store what passed
- Deploy: Run in the cluster
Define: CI/CD pipeline: An automated sequence that builds, tests and ships software every time the code changes.
Scanning an image means checking its contents against lists of known vulnerabilities. The Kubernetes security checklist says this should be completed before deploying the image to a cluster, and is usually done as part of the deployment process in a CI/CD pipeline. That is just an automated line that builds, tests and ships your software. Look at the flow: build, scan, registry, deploy. If the scan fails, the image never reaches the shelf. It is like a safety inspection at the factory door rather than after the product is in customers' homes.
Least privilege inside the container
A defensive container security context. Image name is a placeholder.
spec:
containers:
- name: api
image: registry.example.com/shop/api:1.4.2
securityContext:
runAsNonRoot: true
readOnlyRootFilesystem: true
allowPrivilegeEscalation: false
capabilities:
drop: ["ALL"]| Setting | What the docs say |
|---|---|
| runAsNonRoot | Containers must be required to run as non-root users (Restricted profile) |
| readOnlyRootFilesystem | Mounts the container's root filesystem as read-only |
| allowPrivilegeEscalation | Controls whether a process can gain more privileges than its parent process |
| capabilities drop ALL | Restricted profile: containers must drop ALL capabilities (only NET_BIND_SERVICE may be added back) |
This is the single most useful snippet in the talk. Run as non-root means the program does not start as the all-powerful root user. A read-only root filesystem means an intruder cannot write new files into the container. Disallowing privilege escalation stops a process gaining more rights than its parent. And dropping all capabilities removes the special kernel powers that Linux splits root into. The Pod Security Standards say the Restricted profile requires dropping all of them, with only the network bind capability allowed back. Think of giving a contractor one key, not the master key.
Kubernetes in one slide
- A Pod is the smallest deployable unit you can create and manage in Kubernetes.
- A Pod is a group of one or more containers with shared storage and network resources.
- The one-container-per-Pod model is the most common use case.
- Namespaces provide a mechanism for isolating groups of resources within a single cluster.
Define: cluster: A set of machines, called nodes, managed together by Kubernetes. Our own plain definition.
Kubernetes is the system that decides where containers run and keeps them running. Its smallest unit is the Pod, a wrapper around one or more containers that share storage and network. Most of the time a Pod holds a single container. A namespace is a way of dividing one cluster into separate areas, like folders on a shared drive, and the docs describe it as a mechanism for isolating groups of resources within a single cluster. A cluster is simply the set of machines managed together. Notice that namespaces scope and group resources, so they are one building block rather than the whole answer.
RBAC: who may do what
Role-based access control (RBAC) in Kubernetes grants permissions through roles. Permissions are purely additive: there are no deny rules. A Role sets permissions within one namespace; a ClusterRole is not namespaced.
Read-only access to pods in one namespace, bound to one example user.
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: demo
name: pod-reader
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
namespace: demo
name: read-pods
subjects:
- kind: User
name: jane@example.com
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io- Good practice: assign permissions at the namespace level where possible, using RoleBindings rather than ClusterRoleBindings.
- Avoid wildcard permissions when possible, especially to all resources.
Try it: Audit Kubernetes RBAC for risky permissions with rbac-tool
RBAC decides who can do what to which things in the cluster. A Role lists allowed actions, called verbs, on resources, such as get, list and watch on pods. A RoleBinding hands that Role to a person or a service account. The docs stress that permissions are purely additive: there are no deny rules, so anything you grant stays granted. The good practice pages say to give minimal rights, prefer namespace-level RoleBindings over cluster-wide ones, and avoid wildcards. In the example, Jane can only look at pods in the demo namespace, nothing else.
Network policies: close the doors between pods
Our own example: only the paths the app needs stay open.
- By default, a pod is non-isolated for ingress and egress: all connections are allowed.
- A pod becomes isolated for ingress when a NetworkPolicy selects it and lists Ingress.
- Then only connections allowed by a NetworkPolicy that applies to it are permitted.
By default, pods in a cluster can talk to each other freely. The Network Policies page says a pod is non-isolated for ingress, meaning incoming traffic, and egress, meaning outgoing traffic, until a policy selects it. Once a policy selects a pod, only the connections that policy allows get through. Look at the diagram. The web pods may reach the API, the API may reach the database, but the web pods are blocked from the database directly. It is like a building where every room has a locked door and only specific corridors have keys.
Default deny, then allow
A default-deny ingress policy for one namespace.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-ingress
namespace: demo
spec:
podSelector: {}
policyTypes:
- Ingress- An empty podSelector selects every pod in the namespace.
- No ingress rules are listed, so no inbound connections are allowed.
- Add further policies to open only the paths each pod needs.
Test the paths: After adding a policy, confirm the allowed paths still work and the blocked ones fail. Our own advice.
This small policy is the usual starting point. The empty pod selector means every pod in the namespace, and because it lists the Ingress policy type with no rules, no incoming connections are allowed to any of them. From there you add separate policies that open exactly the paths each application needs. That is the same least privilege idea as before, applied to the network. One practical tip: after adding a policy, test both directions. Check that the allowed paths still work and the blocked ones really fail, because a policy you never tested is only a hope.
Secrets: handle with care
From the Kubernetes docs: Kubernetes Secrets are, by default, stored unencrypted in the API server's underlying data store (etcd). Anyone with API access can retrieve or modify a Secret, and so can anyone with access to etcd.
- Anyone authorized to create a Pod in a namespace can use that access to read any Secret in that namespace, including indirectly through a Deployment.
- The docs list steps to take, starting with Encryption at Rest for Secrets.
- Keeping secrets out of images and code is the point of the Secret object.
Define: etcd: The key-value database where Kubernetes keeps cluster data, including Secrets.
A Secret is a Kubernetes object holding a small amount of sensitive data like a password or token, so you do not bake it into your image or code. But the docs add a caution: by default Secrets are stored unencrypted in etcd, the cluster's database. Anyone with API access can read them, and so can anyone who can create a pod in the same namespace, even indirectly through a Deployment. So protect access with RBAC, and follow the documented steps, which begin with turning on encryption at rest. Treat the Secret object as a safer place, not a safe by itself.
Pod Security Standards
| Profile | Description from the docs |
|---|---|
| Privileged | Unrestricted policy, providing the widest possible level of permissions; allows known privilege escalations |
| Baseline | Minimally restrictive policy which prevents known privilege escalations |
| Restricted | Heavily restricted policy, following current Pod hardening best practices |
- Pod Security Admission can enforce these levels per namespace using labels.
- Modes: enforce rejects a violating pod, audit adds an audit annotation, warn shows a user-facing warning.
The Pod Security Standards give three ready-made levels for how much a pod may do. Privileged has almost no limits and is meant for trusted system workloads. Baseline blocks the known ways to escalate privileges while allowing a normal default pod. Restricted follows current hardening best practice, at the expense of some compatibility. The built-in Pod Security Admission controller applies a level per namespace with labels, in three modes. Enforce rejects a violating pod, audit records it, and warn just tells the user. A sensible path is to start with warn, fix what it shows, then enforce.
Admission control: the last gate
Admission runs after authentication and authorization, before the object is saved.
- Request: kubectl or a pipeline
- Authenticated: Who are you?
- Authorized: RBAC says yes
- Admission: Validate or mutate
- Stored: Saved to the cluster
- An admission controller intercepts requests to the API server prior to persistence, after the request is authenticated and authorized.
- Controllers can be validating, mutating, or both. Mutating ones may modify the object; validating ones may not.
- If any controller rejects the request, the whole request is rejected.
Try it: Build a Kubernetes validating admission webhook from scratch (and fail closed)
Admission control is the last gate before something is saved in the cluster. Following the Kubernetes reference: an admission controller is a piece of code that intercepts requests to the API server before the object is persisted, but after the request is authenticated and authorized. Some controllers validate, some mutate, meaning they change the object, and some do both. If any one rejects the request, the whole request is rejected. This is where rules like only images from our registry or no privileged pods get enforced, like a security guard checking every delivery at the loading dock.
Check your understanding
Check yourself: Which image reference names exactly one version of an image?
Answer: A digest. Kubernetes docs say digests are hashes of the image content and are immutable, while tags can be moved to point to different images.
Check yourself: In Kubernetes RBAC, how do you take away a permission someone has been granted?
Answer: Remove the grant, because RBAC has no deny rules. RBAC permissions are purely additive and there are no deny rules, so access is removed by removing or narrowing the grant.
Check yourself: By default, what can a pod that no NetworkPolicy selects do?
Answer: Send and receive all connections. The docs say a pod is non-isolated for ingress and egress by default, so all connections are allowed until a policy selects it.
Three quick questions. Pause and answer before you reveal each one. The first is about naming images safely, the second about how RBAC permissions work, and the third about what happens by default on the network. If you were unsure on any of them, go back to that slide, because these are the foundations for everything else in the cluster. The labs after the recap let you practise on a real local cluster, which is the fastest way to make these ideas stick.
Recap
- Containers share a kernel; namespaces and cgroups isolate and limit them.
- Pin images by digest and scan them before deploy.
- Run non-root, read-only, no privilege escalation, drop all capabilities.
- RBAC: least privilege, namespace-scoped, no wildcards.
- Network policies: default deny, then allow what is needed.
- Pod Security Standards and admission control enforce the rules.
To check a cluster against community guidance, the CIS Kubernetes Benchmark offers secure configuration guidelines developed through a community consensus process.
To recap. Containers share the host kernel, and namespaces and cgroups provide isolation and limits. Pin images by digest and scan them before deploy. Run containers as non-root, with a read-only filesystem, no privilege escalation and all capabilities dropped. Give RBAC the minimum, scoped to a namespace, with no wildcards. Start networks from default deny. Use Pod Security Standards and admission control to enforce the rules automatically. When you want a checklist, the CIS Kubernetes Benchmark offers secure configuration guidelines. Now try the labs and build a cluster you can safely break.
Practise this
- Install kubectl and kind, and bring up a local Kubernetes cluster you can break
- Lint a Dockerfile for security with hadolint — catch the mistakes before the image exists
- Audit Kubernetes RBAC for risky permissions with rbac-tool
- Build a Kubernetes validating admission webhook from scratch (and fail closed)
Slides only get you so far. Pick one of these hands-on labs and do it today while it is fresh. Every lab is free.
Sources
- NIST SP 800-190, Application Container Security Guide
- Open Container Initiative: About
- Docker Docs: Docker security
- Kubernetes Docs: Images
- Kubernetes Docs: Security checklist
- Kubernetes Docs: Configure a Security Context for a Pod or Container
- Kubernetes Docs: Pod Security Standards
- Kubernetes Docs: Pods
- Kubernetes Docs: Namespaces
- Kubernetes Docs: Using RBAC Authorization
- Kubernetes Docs: Role Based Access Control Good Practices
- Kubernetes Docs: Network Policies
- Kubernetes Docs: Secrets
- Kubernetes Docs: Pod Security Admission
- Kubernetes Docs: Admission Controllers Reference
- CIS Kubernetes Benchmarks
Every claim in this deck came from these pages, fetched and checked for this release. Read the originals.
Hands-on labs for this deck
- Install kubectl and kind, and bring up a local Kubernetes cluster you can break
- Lint a Dockerfile for security with hadolint — catch the mistakes before the image exists
- Audit Kubernetes RBAC for risky permissions with rbac-tool
- Build a Kubernetes validating admission webhook from scratch (and fail closed)
Open the interactive deck or see all slide decks.
Related
- Install kubectl and kind, and bring up a local Kubernetes cluster you can break
- Lint a Dockerfile for security with hadolint — catch the mistakes before the image exists
- Audit Kubernetes RBAC for risky permissions with rbac-tool
- Build a Kubernetes validating admission webhook from scratch (and fail closed)
- All slide decks
- What is Common Vulnerabilities and Exposures?
- What is Role-based access control?
- Free cyber labs