Lab 02 — Gatekeeper Policies
Mission Information
Section titled “Mission Information”| 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 |
Mission Scenario
Section titled “Mission Scenario”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.
Learning Objectives
Section titled “Learning Objectives”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
Enterprise Governance Architecture
Section titled “Enterprise Governance Architecture”Developer
│
kubectl apply
│
▼
Kubernetes API Server
│
▼
OPA Gatekeeper
│
┌────┴───────────────┐
▼ ▼
Constraint ConstraintTemplate Policy
│
▼
Policy Decision
│
┌────┴─────┐
▼ ▼
ALLOW DENYEnterprise Policy Workflow
Section titled “Enterprise Policy Workflow”Developer
│
Deployment YAML
│
▼
Admission Request
│
▼
Gatekeeper
│
▼
Constraint Evaluation
│
▼
Compliant?
│
┌────┴─────┐
▼ ▼
YES NO
│ │
▼ ▼
Deploy RejectLab Outcomes
Section titled “Lab Outcomes”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
Prerequisites
Section titled “Prerequisites”Before beginning ensure you have:
- Kubernetes Cluster
- kubectl
- Helm
- Cluster Administrator permissions
- Linux or Windows terminal
- Internet connectivity
Tools Used
Section titled “Tools Used”| Tool | Purpose |
|---|---|
| Kubernetes | Cluster |
| Gatekeeper | Policy Enforcement |
| Helm | Installation |
| kubectl | Administration |
| jq | JSON Parsing |
| VS Code | Policy Development |
Recommended Lab Structure
Section titled “Recommended Lab Structure”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/Task 01 — Verify Cluster Access
Section titled “Task 01 — Verify Cluster Access”Verify connectivity.
kubectl cluster-infoConfirm:
- Cluster reachable
- API Server available
- Administrator permissions
Task 02 — Install Gatekeeper
Section titled “Task 02 — Install Gatekeeper”Add repository.
helm repo add gatekeeper https://open-policy-agent.github.io/gatekeeper/chartsUpdate repositories.
helm repo updateInstall.
helm install gatekeeper gatekeeper/gatekeeper \--namespace gatekeeper-system \--create-namespaceTask 03 — Verify Installation
Section titled “Task 03 — Verify Installation”Review Pods.
kubectl get pods \-n gatekeeper-systemExpected:
- Controller Manager
- Audit Controller
Task 04 — Verify Webhooks
Section titled “Task 04 — Verify Webhooks”kubectl get validatingwebhookconfigurationsConfirm.
Gatekeeper webhook exists.
Task 05 — Review Gatekeeper CRDs
Section titled “Task 05 — Review Gatekeeper CRDs”kubectl get crds | grep gatekeeperReview:
- ConstraintTemplates
- Constraints
Task 06 — Create Required Labels Template
Section titled “Task 06 — Create Required Labels Template”Create.
templates/k8srequiredlabels.yamlPurpose.
Every workload must contain:
- app
- owner
- environment
Apply.
kubectl apply \-f templates/k8srequiredlabels.yamlTask 07 — Create Required Labels Constraint
Section titled “Task 07 — Create Required Labels Constraint”Create.
constraints/required-labels.yamlApply.
kubectl apply \-f constraints/required-labels.yamlTask 08 — Test Missing Labels
Section titled “Task 08 — Test Missing Labels”Deploy.
manifests/missing-labels.yamlExpected.
Admission DeniedReview.
Violation message.
Task 09 — Create Non-Root Template
Section titled “Task 09 — Create Non-Root Template”Create.
templates/k8snonroot.yamlPurpose.
Reject.
runAsUser: 0Apply.
Task 10 — Create Non-Root Constraint
Section titled “Task 10 — Create Non-Root Constraint”Apply.
kubectl apply \-f constraints/non-root.yamlTask 11 — Test Root Container
Section titled “Task 11 — Test Root Container”Deploy.
root-user.yamlExpected.
Rejected.
Task 12 — Create Privileged Container Template
Section titled “Task 12 — Create Privileged Container Template”Reject.
privileged: trueTask 13 — Create Privileged Constraint
Section titled “Task 13 — Create Privileged Constraint”Apply.
kubectl apply \-f constraints/no-privileged.yamlTask 14 — Test Privileged Container
Section titled “Task 14 — Test Privileged Container”Deploy.
privileged-pod.yamlExpected.
Rejected.
Task 15 — Block HostPath Volumes
Section titled “Task 15 — Block HostPath Volumes”Create template.
Reject.
hostPath:Apply.
Constraint.
Task 16 — Test HostPath
Section titled “Task 16 — Test HostPath”Deploy.
hostpath-pod.yamlExpected.
Rejected.
Task 17 — Restrict Linux Capabilities
Section titled “Task 17 — Restrict Linux Capabilities”Create template.
Reject.
- SYS_ADMIN
- NET_ADMIN
- SYS_MODULE
Apply.
Constraint.
Task 18 — Test Linux Capabilities
Section titled “Task 18 — Test Linux Capabilities”Deploy.
Container requesting.
SYS_ADMINExpected.
Rejected.
Task 19 — Enforce Resource Requests
Section titled “Task 19 — Enforce Resource Requests”Require.
resources:Requests.
Limits.
Apply.
Task 20 — Test Missing Resources
Section titled “Task 20 — Test Missing Resources”Deploy Pod.
Without.
Requests.
Expected.
Rejected.
Task 21 — Audit Existing Workloads
Section titled “Task 21 — Audit Existing Workloads”Run.
kubectl get constraintsReview audit status.
Task 22 — Review Constraint Violations
Section titled “Task 22 — Review Constraint Violations”kubectl describe constraintReview.
Violations.
Task 23 — Review Audit Logs
Section titled “Task 23 — Review Audit Logs”Review.
kubectl logs \deployment/gatekeeper-audit \-n gatekeeper-systemAnalyse.
- Violations
- Namespace
- Resource
- Policy
Task 24 — Deploy Compliant Workload
Section titled “Task 24 — Deploy Compliant Workload”Deploy.
compliant-pod.yamlExpected.
Allowed.
Task 25 — Review Admission Events
Section titled “Task 25 — Review Admission Events”kubectl get events -AReview.
Policy enforcement.
Task 26 — Build Governance Matrix
Section titled “Task 26 — Build Governance Matrix”Create.
Policy
Constraint
Risk
OwnerTask 27 — Produce Compliance Report
Section titled “Task 27 — Produce Compliance Report”Document.
- Policies
- Violations
- Exceptions
- Recommendations
Task 28 — Evidence Collection
Section titled “Task 28 — Evidence Collection”Collect.
- Templates
- Constraints
- Violations
- Audit Results
- Admission Events
- Screenshots
- Reports
Task 29 — Cleanup
Section titled “Task 29 — Cleanup”Delete.
Test resources.
Retain:
- Templates
- Constraints
- Reports
Enterprise Governance Checklist
Section titled “Enterprise Governance Checklist”| 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 | ☐ |
Risk Classification
Section titled “Risk Classification”Critical
Section titled “Critical”- Privileged Containers Allowed
- Root Containers Allowed
- HostPath Access Allowed
- Gatekeeper Disabled
- Missing Resource Limits
- Missing Labels
- Dangerous Linux Capabilities
- Policy Bypass
Medium
Section titled “Medium”- Weak Documentation
- Missing Owners
- Manual Reviews
- Naming Standards
- Labels Formatting
- Metadata Improvements
Skills Developed
Section titled “Skills Developed”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
Knowledge Check
Section titled “Knowledge Check”Question 1
Section titled “Question 1”What is the primary purpose of OPA Gatekeeper?
Answer: To enforce Kubernetes admission policies that prevent non-compliant resources from being created.
Question 2
Section titled “Question 2”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.
Question 3
Section titled “Question 3”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.
Question 4
Section titled “Question 4”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.
Question 5
Section titled “Question 5”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.
Lab Summary
Section titled “Lab Summary”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.
What’s Next?
Section titled “What’s Next?”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.