Real-World Challenges When Operating Kubernetes
When I first set up a shared Kubernetes cluster for our dev team, I had a heart-stopping incident on a Friday evening. A junior developer accidentally deployed a test service to the staging namespace with privileged: true, using the :latest image tag from Docker Hub and leaving resources.limits completely empty. That container consumed all 32GB of RAM on a worker node in just 10 minutes. The result? Over 20 other pods on the same node were OOMKilled in a cascade.
On Monday morning, I audited the entire cluster and was shocked. Up to 40% of deployments were running as root inside their containers. Some pods were even mounting /var/run/docker.sock or sensitive hostPath volumes directly. If a web application had an RCE vulnerability, an attacker could easily break out of the container and take control of the entire physical node.
Our team had no shortage of well-written guidelines on Confluence. But whenever deadlines approached, developers just copy-pasted old YAML files to move faster. Verbal reminders or manual reviews were practically useless at that point.
Root Cause Analysis: Why RBAC Alone Cannot Block Misconfigurations
Many newcomers to Kubernetes confuse RBAC (Role-Based Access Control) with resource content validation. In reality, RBAC only answers the question: “Is User A allowed to create a Deployment in namespace X?”. Once permission is granted, the API Server accepts any syntactically valid fields inside that YAML file by default.
Kubernetes will not automatically reject a Pod running as root or pulling an untrusted image if you lack a validation layer at the Admission Control stage. This is the final checkpoint before an object is persisted to etcd. Leave this gate unguarded, and every misconfiguration slips straight into your cluster.
Infrastructure security needs to be consistent from the underlying servers up to the orchestration layer. For instance, when provisioning nodes or creating initial admin accounts, I often use the password generator at toolcraft.app/en/tools/security/password-generator — it runs 100% in the browser, so there is no risk of leaking data over the network. However, no matter how strong your node passwords are, if the cluster still lets privileged containers run unchecked, your defenses remain full of holes.
Common Approaches Used Today
To tighten configuration discipline without slowing down deployments, our team tried three approaches:
- Static Checks in the CI/CD Pipeline: Using Conftest or Checkov to scan YAML files as soon as a developer opens a Pull Request. This catches issues very early, but has a fatal flaw: anyone with permission to run
kubectl applydirectly from their local machine can bypass the CI/CD checks completely. - Using Pod Security Admission (PSA): A built-in feature since Kubernetes 1.25 that replaces the deprecated PodSecurityPolicy. PSA is lightweight and quickly enforces
baselineorrestrictedstandards per namespace. However, it only validates basic Pod attributes and falls short on custom rules like requiring anownerlabel, blocking the:latesttag, or restricting images to an internal registry. - Using a Policy Engine (OPA Gatekeeper or Kyverno): Deploying a Validating Admission Webhook directly inside the cluster. Every request sent to the API Server — whether from CI/CD or run manually via
kubectl— must pass through this filter.
The Best Approach: Automated Policy Enforcement with OPA and Gatekeeper
Among the options above, our team chose Open Policy Agent (OPA) paired with Gatekeeper for its flexibility when scaling across multiple teams. OPA uses the powerful declarative Rego language. Gatekeeper acts as the bridge, turning Rego policies into Kubernetes Custom Resource Definitions (CRDs) so DevOps engineers can manage them using familiar YAML files.
Step 1: Install Gatekeeper on Your Kubernetes Cluster
Installing via Helm is the cleanest approach and makes future upgrades effortless. Open your terminal and run:
helm repo add gatekeeper https://open-policy-agent.github.io/gatekeeper/charts
helm repo update
helm install gatekeeper gatekeeper/gatekeeper \
--namespace gatekeeper-system \
--create-namespace
Wait about a minute, then check the pod status. By default, you should see 3 gatekeeper-controller-manager pods and 1 gatekeeper-audit pod in the Running state:
kubectl get pods -n gatekeeper-system
Step 2: Create a ConstraintTemplate (Define the Validation Logic)
Gatekeeper splits a policy into two parts: a ConstraintTemplate (which holds the Rego logic) and a Constraint (which applies that logic to specific resources). The example below blocks any container using the :latest tag or missing a tag altogether.
Create a file named template-block-latest.yaml:
apiVersion: templates.gatekeeper.sh/v1
kind: ConstraintTemplate
metadata:
name: k8sblocklatesttag
spec:
crd:
spec:
names:
kind: K8sBlockLatestTag
targets:
- target: admission.k8s.gatekeeper.sh
rego: |
package k8sblocklatesttag
violation[{"msg": msg}] {
container := input.review.object.spec.containers[_]
endswith(container.image, ":latest")
msg := sprintf("Container '%v' is not allowed to use the :latest tag (%v)", [container.name, container.image])
}
violation[{"msg": msg}] {
container := input.review.object.spec.containers[_]
not contains(container.image, ":")
msg := sprintf("Container '%v' must specify an explicit image tag (%v)", [container.name, container.image])
}
Apply this template to the cluster (note: if you use a private registry with a port like localhost:5000/app without a tag, in production you should inspect the image name portion after the /):
kubectl apply -f template-block-latest.yaml
Step 3: Create a Constraint to Activate the Policy
Now that the template is ready, create constraint-block-latest.yaml to enforce the rule across all Pods. Be sure to exclude system namespaces like kube-system and gatekeeper-system so you don’t lock yourself out.
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sBlockLatestTag
metadata:
name: block-latest-image-tag
spec:
enforcementAction: dryrun
match:
kinds:
- apiGroups: [""]
kinds: ["Pod"]
excludedNamespaces:
- kube-system
- gatekeeper-system
Hard-earned lesson: when enabling a new policy on a live cluster, always start with enforcementAction: dryrun. This mode won’t block deployments right away; instead, it logs violating resources under status.violations after each audit pod scan cycle (every 60 seconds by default). Give the dev team 1–2 days to clean up existing manifests before switching to deny.
kubectl apply -f constraint-block-latest.yaml
# Check pods currently in violation in dryrun mode
kubectl describe k8sblocklatesttag block-latest-image-tag
Step 4: Switch to Enforcement Mode (deny) and Test
Change enforcementAction: dryrun to deny and re-apply. Now try creating a Pod using the nginx:latest image to see what happens:
kubectl run test-nginx --image=nginx:latest -n default
The API Server will reject it immediately:
Error from server (Forbidden): admission webhook "validation.gatekeeper.sh" denied the request: [block-latest-image-tag] Container 'test-nginx' is not allowed to use the :latest tag (nginx:latest)
Simply switch to a specific tag like nginx:1.25.3-alpine, and the Pod will be created normally.
Deployment Tips for DevOps Teams
Don’t write every Rego rule from scratch. The OPA community maintains the Gatekeeper Library with dozens of ready-to-use templates: blocking root privileges, requiring resources.limits, preventing duplicate Ingress hosts, and more. Just grab what you need and run conftest in your CI/CD pipeline using the exact same Rego rules. Developers will catch errors the moment they push code, rather than waiting for the cluster to reject their deployment.

