Skip to content

Lab 02 — Gatekeeper Policies

Item Details
Lab ID K8S-COMPLIANCE-LAB-02
Difficulty Intermediate to Advanced
Estimated Time 5–6 Hours
Environment Kubernetes Training Cluster
Platform Kubernetes, OPA Gatekeeper, Helm, kubectl
Cost Free
Primary Role Kubernetes Security Engineer
Supporting Roles DevSecOps Engineer, Cloud Security Engineer, Platform Engineer, Compliance Analyst
Module Kubernetes Benchmarks & Compliance
Previous Lab Lab 01 — CIS Kubernetes Benchmark Assessment
Next Lab Lab 03 — Kyverno Policies

CloudNova Technologies has successfully completed a CIS Kubernetes Benchmark assessment.

Although the cluster is reasonably hardened, security engineers discover an operational challenge.

Developers are still able to deploy workloads that violate organisational security standards.

Examples include:

  • Privileged containers
  • Root containers
  • Missing resource limits
  • Containers without security contexts
  • Images from public registries
  • Missing labels
  • HostPath volumes
  • Host networking
  • Dangerous Linux capabilities

The security team currently identifies these issues only after deployments have already reached production.

The CISO has therefore mandated preventive governance, ensuring that non-compliant workloads are rejected before they are created.

CloudNova Technologies has selected Open Policy Agent (OPA) Gatekeeper as the enterprise policy engine.

Your mission is to deploy Gatekeeper, create enterprise policies, validate compliance, and demonstrate how Kubernetes admission control prevents insecure workloads from entering the cluster.


By completing this lab you will learn how to:

  • Understand OPA Gatekeeper
  • Understand Constraint Templates
  • Understand Constraints
  • Deploy Gatekeeper
  • Validate Gatekeeper installation
  • Create governance policies
  • Enforce required labels
  • Prevent privileged containers
  • Prevent root containers
  • Block HostPath volumes
  • Restrict Linux capabilities
  • Enforce resource requests and limits
  • Audit existing workloads
  • Review Gatekeeper violations
  • Produce enterprise governance reports

Developer
kubectl apply
Kubernetes API Server
OPA Gatekeeper
┌────┴───────────────┐
▼ ▼
Constraint Constraint
Template Policy
Policy Decision
┌────┴─────┐
▼ ▼
ALLOW DENY

Developer
Deployment YAML
Admission Request
Gatekeeper
Constraint Evaluation
Compliant?
┌────┴─────┐
▼ ▼
YES NO
│ │
▼ ▼
Deploy Reject

By the end of this lab you will have:

  • Installed Gatekeeper
  • Verified admission webhooks
  • Created Constraint Templates
  • Created Constraints
  • Tested policy enforcement
  • Audited existing workloads
  • Blocked insecure deployments
  • Reviewed violations
  • Produced governance evidence

Before beginning ensure you have:

  • Kubernetes Cluster
  • kubectl
  • Helm
  • Cluster Administrator permissions
  • Linux or Windows terminal
  • Internet connectivity

Tool Purpose
Kubernetes Cluster
Gatekeeper Policy Enforcement
Helm Installation
kubectl Administration
jq JSON Parsing
VS Code Policy Development

lab-02-gatekeeper-policies/
├── templates/
│ ├── k8srequiredlabels.yaml
│ ├── k8srequiredresources.yaml
│ ├── k8snonroot.yaml
│ ├── k8snoprivileged.yaml
│ ├── k8snohostpath.yaml
│ └── k8snocapabilities.yaml
├── constraints/
│ ├── required-labels.yaml
│ ├── required-resources.yaml
│ ├── non-root.yaml
│ ├── no-privileged.yaml
│ ├── no-hostpath.yaml
│ └── capability-restrictions.yaml
├── manifests/
│ ├── compliant-pod.yaml
│ ├── privileged-pod.yaml
│ ├── root-user.yaml
│ ├── missing-labels.yaml
│ └── hostpath-pod.yaml
├── reports/
│ ├── violations.md
│ ├── audit-results.md
│ ├── remediation.md
│ └── governance-report.md
└── evidence/

Verify connectivity.

Terminal window
kubectl cluster-info

Confirm:

  • Cluster reachable
  • API Server available
  • Administrator permissions

Add repository.

Terminal window
helm repo add gatekeeper https://open-policy-agent.github.io/gatekeeper/charts

Update repositories.

Terminal window
helm repo update

Install.

Terminal window
helm install gatekeeper gatekeeper/gatekeeper \
--namespace gatekeeper-system \
--create-namespace

Review Pods.

Terminal window
kubectl get pods \
-n gatekeeper-system

Expected:

  • Controller Manager
  • Audit Controller

Terminal window
kubectl get validatingwebhookconfigurations

Confirm.

Gatekeeper webhook exists.


Terminal window
kubectl get crds | grep gatekeeper

Review:

  • ConstraintTemplates
  • Constraints

Task 06 — Create Required Labels Template

Section titled “Task 06 — Create Required Labels Template”

Create.

templates/k8srequiredlabels.yaml

Purpose.

Every workload must contain:

  • app
  • owner
  • environment

Apply.

Terminal window
kubectl apply \
-f templates/k8srequiredlabels.yaml

Task 07 — Create Required Labels Constraint

Section titled “Task 07 — Create Required Labels Constraint”

Create.

constraints/required-labels.yaml

Apply.

Terminal window
kubectl apply \
-f constraints/required-labels.yaml

Deploy.

manifests/missing-labels.yaml

Expected.

Admission Denied

Review.

Violation message.


Create.

templates/k8snonroot.yaml

Purpose.

Reject.

runAsUser: 0

Apply.


Apply.

Terminal window
kubectl apply \
-f constraints/non-root.yaml

Deploy.

root-user.yaml

Expected.

Rejected.


Task 12 — Create Privileged Container Template

Section titled “Task 12 — Create Privileged Container Template”

Reject.

privileged: true

Apply.

Terminal window
kubectl apply \
-f constraints/no-privileged.yaml

Deploy.

privileged-pod.yaml

Expected.

Rejected.


Create template.

Reject.

hostPath:

Apply.

Constraint.


Deploy.

hostpath-pod.yaml

Expected.

Rejected.


Create template.

Reject.

  • SYS_ADMIN
  • NET_ADMIN
  • SYS_MODULE

Apply.

Constraint.


Deploy.

Container requesting.

SYS_ADMIN

Expected.

Rejected.


Require.

resources:

Requests.

Limits.

Apply.


Deploy Pod.

Without.

Requests.

Expected.

Rejected.


Run.

Terminal window
kubectl get constraints

Review audit status.


Terminal window
kubectl describe constraint

Review.

Violations.


Review.

Terminal window
kubectl logs \
deployment/gatekeeper-audit \
-n gatekeeper-system

Analyse.

  • Violations
  • Namespace
  • Resource
  • Policy

Deploy.

compliant-pod.yaml

Expected.

Allowed.


Terminal window
kubectl get events -A

Review.

Policy enforcement.


Create.

Policy
Constraint
Risk
Owner

Document.

  • Policies
  • Violations
  • Exceptions
  • Recommendations

Collect.

  • Templates
  • Constraints
  • Violations
  • Audit Results
  • Admission Events
  • Screenshots
  • Reports

Delete.

Test resources.

Retain:

  • Templates
  • Constraints
  • Reports

Control Status
Gatekeeper Installed
Webhooks Active
Constraint Templates Created
Constraints Applied
Required Labels Enforced
Root Containers Blocked
Privileged Containers Blocked
HostPath Blocked
Linux Capabilities Restricted
Resource Limits Required
Audit Completed
Violations Reviewed
Governance Report Produced

  • Privileged Containers Allowed
  • Root Containers Allowed
  • HostPath Access Allowed
  • Gatekeeper Disabled
  • Missing Resource Limits
  • Missing Labels
  • Dangerous Linux Capabilities
  • Policy Bypass
  • Weak Documentation
  • Missing Owners
  • Manual Reviews
  • Naming Standards
  • Labels Formatting
  • Metadata Improvements

After completing this lab you will be able to:

  • Deploy OPA Gatekeeper
  • Build Constraint Templates
  • Create Constraints
  • Enforce Kubernetes governance
  • Prevent insecure deployments
  • Audit existing workloads
  • Analyse policy violations
  • Produce enterprise governance reports
  • Strengthen Kubernetes compliance posture

What is the primary purpose of OPA Gatekeeper?

Answer: To enforce Kubernetes admission policies that prevent non-compliant resources from being created.

What is the difference between a Constraint Template and a Constraint?

Answer: A Constraint Template defines the policy logic, while a Constraint applies that policy to Kubernetes resources with specific enforcement settings.

Why should privileged containers be restricted?

Answer: Privileged containers have elevated access to the host and significantly increase the risk of container escape and host compromise.

Can Gatekeeper audit existing resources as well as enforce new ones?

Answer: Yes. Gatekeeper can audit existing resources for policy violations while also enforcing policies on new admission requests.

Does Gatekeeper replace RBAC or Pod Security Admission?

Answer: No. Gatekeeper complements Kubernetes security controls by providing policy-based governance alongside RBAC, Pod Security Admission, network policies, and other security mechanisms.


In this lab, you deployed Open Policy Agent (OPA) Gatekeeper and implemented enterprise governance policies to enforce Kubernetes security standards before workloads were admitted into the cluster.

You created Constraint Templates and Constraints to require mandatory labels, prevent privileged and root containers, block HostPath volumes, restrict dangerous Linux capabilities, and enforce resource requests and limits. You validated policy enforcement by testing compliant and non-compliant workloads, reviewed audit results, analysed violations, and produced governance documentation.

This lab demonstrates how preventive policy enforcement helps organisations reduce configuration drift, strengthen compliance, and ensure that Kubernetes workloads consistently meet enterprise security requirements before reaching production.


Next Lab: Lab 03 — Kyverno Policies

In the next lab, you will deploy Kyverno and implement policy-as-code using native Kubernetes resources, enforce security controls, automatically mutate workloads, validate compliance, generate governance reports, and compare Kyverno’s capabilities with OPA Gatekeeper in enterprise Kubernetes environments.