In this lesson, we walk through the lab on validating and mutating admission controllers. You’ll gain hands-on experience with namespace creation, TLS secret management, deploying webhook servers, and testing pod security contexts.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.


Step 1: Create the Namespace
First, create a namespace named “webhook-demo” to serve as the environment for the webhook operations.Step 2: Create a TLS Secret
For secure webhook communication, create a TLS secret named “webhook-server-tls” in the “webhook-demo” namespace. Ensure you substitute the correct file paths for the certificate and key:Step 3: Deploy the Webhook Server
Review the webhook server deployment definition provided in the filewebhook-deployment.yaml:
Step 4: Create the Webhook Service
Create a service for the webhook server using the configuration inwebhook-service.yaml:
Step 5: Apply the Mutating Webhook Configuration
Next, apply the mutating webhook configuration defined inwebhook-configuration.yaml. This file includes rules under the “rules” section to intercept “CREATE” operations for pods.
When applying the webhook configuration, you might receive a deprecation warning indicating that admissionregistration.k8s.io/v1beta1 is deprecated in favor of v1. Despite the warning, the configuration will still perform its intended function by denying pod creation requests that attempt to run as root when no security context is provided.
true is applied, and the user ID is defaulted to 1234 unless overridden.
Step 6: Test the Webhook by Deploying Pods
Pod with Default Security Context
The next stage involves deploying a pod that does not specify any security context so that the webhook can mutate the configuration. The YAML filepod-with-defaults.yaml contains the following configuration:
runAsNonRoot: true and runAsUser: 1234.
Pod with Explicit Override
Next, deploy a pod that explicitly sets its security context to allow running as root. Check thepod-with-override.yaml file where the security context is defined with runAsNonRoot set to false:
Pod with Conflicting Security Context
Finally, deploy a pod with a conflicting security configuration in thepod-with-conflict.yaml file. This configuration attempts to set runAsNonRoot: true while also specifying runAsUser: 0, creating a conflict.
The admission webhook should reject the conflicting pod creation request, displaying an error message similar to the following:Error from server: error when creating “pod-with-conflict.yaml”: admission webhook “webhook-server.webhook-demo.svc” denied the request: runAsNonRoot specified, but runAsUser set to 0 (the root user)
This comprehensive walkthrough demonstrates how to validate and mutate admission controllers effectively. By following these steps, you ensure that your Kubernetes environment enforces the desired security configurations.