Beginner Lab
aws
Secure Kubernetes using OPA Gatekeeper
Explore how to enhance the security and compliance of our Kubernetes clusters using Open Policy Agent (OPA) and the OPA Gatekeeper project.
Overview
Join us as we explore how to enhance the security and compliance of our Kubernetes clusters using Open Policy Agent (OPA) and the OPA Gatekeeper project. OPA Gatekeeper provides a powerful way to enforce policies and to ensure that Kubernetes environments adhere to organizational and security standards.
Scenario
Your company has been hired by SuperTech who have encountered some recent security challenges with their Kubernetes clusters. The organization has struggled with misconfigured deployments and non-compliant resource settings, which has led to several security incidents. To address these issues, you are tasked with helping them implement a robust policy enforcement solution using OPA Gatekeeper to ensure the following:
- Non-Read-Only Filesystem: Ensure that all pods use a read-only filesystem to prevent unauthorized modifications.
- Privileged Escalation: Block pods where the AllowPrivilegeEscalation option in the security context is set to true, which can allow processes to gain additional privileges.
- Host Network Usage: Disallow the use of hostNetwork: true in pod specifications to prevent exposing the pod directly to the host network, reducing the risk of network-level attacks.
- Restrict HostPath Volumes: Restrict the use of hostPath volumes on a k8s pod.
Lab prerequisites
- Basic understanding of Kubernetes concepts and architecture
- Familiarity with Kubernetes manifests
- While not required, basic understanding of a programming language will be beneficial
Learning outcomes
- You will learn how to create and customize security policies using the Rego policy language.
- You will be able to able to apply policies to Kubernetes resources and ensure OPA Gatekeeper enforces them.
- You will learn how to apply best practices for policy management to improve cluster security and compliance.
Real-world context
Kubernetes is a powerful tool for managing large scale containerised applications, but this also makes it highly suspectible to configuration errors. Misconfigurations often arise from the intricate interactions between numerous components, evolving features, and default settings that may not prioritize security. As organizations integrate Kubernetes with other tools and environments, the risk of errors increases, especially if best practices are not well understood or consistently applied.
OPA Gatekeeper provides a vital layer of security for organizations to define and enforce policies consistently across their Kubernetes clusters, such as preventing containers from running with elevated privileges, restrict access to senstive namespaces and by ensuring pods are assigned appropriate resource limits.
KEY TAKEAWAY
Admission control is the only place a Kubernetes policy is actually enforced. Guidance in a wiki is advice, and a constraint evaluated when a pod is submitted is a control, because it rejects the workload rather than reporting it afterwards.
What this lab covers
You deploy OPA Gatekeeper into a cluster and write policies enforcing four specific requirements: read-only root filesystems, no privilege escalation, no host network, and restricted hostPath volumes.
Each of those maps directly to an escape path. Privilege escalation and host mounts are how a pod reaches the node, and host network removes the isolation between the workload and the machine, so this is the defensive counterpart to the RBAC and container escape labs.
It assumes familiarity with Kubernetes manifests and suits platform engineers as much as security teams, since policy that breaks deployments is policy that gets removed.
How admission control works
When a resource is submitted to the API server, admission webhooks are consulted before it is persisted. Gatekeeper registers as a validating webhook, evaluates the object against policy, and returns an allow or deny decision with a message.
Policy is expressed in two layers. A constraint template defines the rule and its parameters in Rego, and a constraint instantiates that template against specific resource kinds and scopes. The separation exists so one rule can be applied several ways without being rewritten.
The evaluation point is what matters. A scanner that finds a privileged pod running has found an incident, and an admission policy that rejects it has prevented one, which is the difference between reporting and enforcing.
Rolling policy out without breaking things
Start in audit mode. Gatekeeper can evaluate and record violations without rejecting anything, which tells you exactly what would have broken before it does, and that list is nearly always longer than expected.
Exempt deliberately and narrowly. Some workloads genuinely need host access, monitoring agents and storage drivers among them, and the correct response is a documented exemption for those namespaces rather than a weakened rule for everyone.
Communicate the rejection clearly. A deny message that names the policy and explains the fix turns policy from an obstacle into guidance, and it is the difference between engineers working with the control and working around it.
Where this fits alongside other controls
Pod Security Standards cover much of the same ground with less flexibility and considerably less setup. If the built-in levels meet your needs, they are the simpler choice, and Gatekeeper earns its place when you need rules the standards do not express.
Policy engines also handle requirements beyond security, such as mandatory labels, approved registries and resource limits. That breadth is why platform teams frequently adopt them for governance and inherit the security benefit.
None of this replaces RBAC. Admission control governs what workloads may look like, and RBAC governs who may submit them, so the two answer different questions and a cluster needs both.
Applying this at work
- Deploy a policy engine in audit mode first and review what would be rejected before enforcing anything.
- Start with the four rules in this lab, which map directly to container escape paths.
- Document exemptions per namespace for workloads that genuinely require host access, rather than weakening the rule globally.
- Write deny messages that name the policy and explain the fix.
- Check whether Pod Security Standards already meet your requirements, since they are simpler where they suffice.
Frequently asked questions
What is the difference between a constraint template and a constraint?
The template defines the rule and its parameters in Rego. The constraint instantiates that template against specific resource kinds and scopes. The separation lets one rule be applied in several ways without rewriting it.
Should I use OPA Gatekeeper or Pod Security Standards?
Pod Security Standards are simpler and cover the common workload restrictions. A policy engine is worth adopting when you need rules the standards cannot express, or when you also want governance rules such as approved registries and mandatory labels.
How do I roll out policy without breaking deployments?
Run in audit mode first so violations are recorded without being rejected. That produces the list of what would break, which is almost always longer than expected, and lets you handle exemptions before enforcement.
Do I need my own cluster to practice this?
No. The lab runs in a live environment that Pwned Labs provisions for you, so there is nothing to set up and no risk to your own infrastructure.
Which certification covers this in depth?
The Kubernetes Attack and Defense Bootcamp, Professional Edition (KRTP) covers Kubernetes attack and defense end to end, including RBAC, workload restrictions, admission control and the route from a cluster into the cloud account behind it.
Cloud Security Training To Protect Your Business
Pwned Labs for Business gives your team access to dedicated business content, including labs and cyber ranges.
We also offer in-person or remote workshops, and our cloud penetration services are helping businesses become more secure!