(01)Install Kyverno
1kubectl create namespace kyverno2kubectl apply -f https://github.com/kyverno/kyverno/releases/download/v1.12.0/install.yamlKubernetes policy engine — validate, mutate, and generate resources
What is this?
Kyverno enforces policies on Kubernetes resources — block bad configs before they reach the cluster.
Where to create or edit the main configuration — paths below match the setup steps.
policy.yamlLocation: Kubernetes cluster — kubectl apply
Admission policies — validate, mutate, generate
Step 01
1kubectl create namespace kyverno2kubectl apply -f https://github.com/kyverno/kyverno/releases/download/v1.12.0/install.yamlStep 02
1cat > require-labels.yaml << 'EOF'2apiVersion: kyverno.io/v13kind: ClusterPolicy4metadata:5 name: require-app-label6spec:7 validationFailureAction: Enforce8 rules:9 - name: check-app-label10 match:11 any:12 - resources:13 kinds: [Pod]14 validate:15 message: "label 'app' is required"16 pattern:17 metadata:18 labels:19 app: "?*"20EOF21kubectl apply -f require-labels.yamlStep 03
1kubectl get pods -n kyverno2kubectl get clusterpolicyStep 04
1kubectl get clusterpolicy2kubectl describe clusterpolicy require-app-label3# Test with a pod missing label:4kubectl run test --image=nginx --dry-run=server -o yaml | kubectl apply -f -1 YAML template for Kyverno. Copy and deploy after setup.
1 ready-to-copy template. Expand one, copy the YAML, then run the deploy commands.
deployment.yml
Deployment with resource requests/limits and local image
1apiVersion: apps/v12kind: Deployment3metadata:4 labels:5 app: api-deploy6 name: api-deploy7spec:8 replicas: 19 selector:10 matchLabels:11 app: api-deploy12 template:13 metadata:14 labels:15 app: api-deploy16 spec:17 containers:18 - image: api:local19 name: api20 imagePullPolicy: IfNotPresent21 ports:22 - containerPort: 300023 resources:24 limits:25 memory: "512Mi"26 cpu: "500m"27 requests:28 memory: "256Mi"29 cpu: "250m"services.yml
NodePort Service exposing the API on port 32000
1apiVersion: v12kind: Service3metadata:4 labels:5 app: api-deploy6 name: api-deploy-service7spec:8 type: NodePort9 selector:10 app: api-deploy11 ports:12 - port: 300013 protocol: TCP14 targetPort: 300015 nodePort: 32000Step 01
1# Build local image2docker build -t api:local .3 4# For Minikube / Kind — load image into cluster5minikube image load api:local6# OR: kind load docker-image api:local1kubectl apply -f deployment.yml2kubectl apply -f services.yml3kubectl get pods,svc4curl http://localhost:32000 # or minikube service api-deploy-service