π‘οΈ A collection of Kyverno policies for Kubernetes cluster governance, security, and compliance - deployed via GitOps
- π Security First: Enforce pod security standards, image signing, and zero-trust networking
- βοΈ Best Practices: Automated enforcement of Kubernetes best practices
- π Compliance Ready: Pre-built policies for PCI-DSS, HIPAA, and more
- π GitOps Native: Designed for ArgoCD and Flux deployment
- π§ͺ CI/CD Tested: Automated validation in your pipeline
- π Observable: Built-in monitoring and reporting
- π Multi-Environment: Dev, staging, and production configurations
- π¦ Modular: Pick and choose policies that fit your needs
Kyverno (Greek for "govern") is a CNCF project that makes Kubernetes policy management simple and declarative:
| Feature | Kyverno | Traditional Tools |
|---|---|---|
| Language | Native YAML/Kubernetes | Custom DSL (Rego, etc.) |
| Learning Curve | β Low - Uses K8s syntax | |
| Validation | β Validate resources | β Validate resources |
| Mutation | β Modify resources | |
| Generation | β Create resources | β Not supported |
| GitOps | β Native support | β Supported |
| Reporting | β Built-in reports |
- Features
- Why Kyverno?
- Overview
- Policy Statistics
- Quick Start
- Example Policies
- Repository Structure
- Policy Categories
- Prerequisites
- GitOps Deployment
- Policy Enforcement Modes
- Environment-Specific Configuration
- Testing Policies
- Monitoring and Reporting
- Policy Development Guidelines
- GitOps Workflow
- Best Practices for GitOps
- Contributing
- Useful Commands
- Resources
- Support
- Troubleshooting
- License
This repository contains Kyverno policies designed to enforce best practices, security standards, and compliance requirements across Kubernetes clusters. Kyverno is a policy engine designed specifically for Kubernetes, enabling declarative validation, mutation, and generation of resources.
| Category | Policies | Status |
|---|---|---|
| π Security | 15+ | β Active |
| βοΈ Best Practices | 20+ | β Active |
| π Compliance | 10+ | β Active |
| Total | 45+ | β Active |
# 1. Clone the repository
git clone https://github.com/your-org/kyverno-policies.git
cd kyverno-policies
# 2. Install Kyverno (if not already installed)
kubectl create -f https://github.com/kyverno/kyverno/releases/download/v1.10.0/install.yaml
# 3. Test policies locally
kyverno apply policies/ --resource tests/
# 4. Deploy via GitOps (see GitOps Deployment section)π Require Read-Only Root Filesystem
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: require-ro-rootfs
annotations:
policies.kyverno.io/title: Require Read-Only Root Filesystem
policies.kyverno.io/category: Security
policies.kyverno.io/severity: medium
spec:
validationFailureAction: Enforce
rules:
- name: validate-readOnlyRootFilesystem
match:
any:
- resources:
kinds:
- Pod
validate:
message: "Root filesystem must be read-only"
pattern:
spec:
containers:
- securityContext:
readOnlyRootFilesystem: trueβοΈ Require Resource Limits
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: require-resource-limits
annotations:
policies.kyverno.io/title: Require Resource Limits
policies.kyverno.io/category: Best Practices
spec:
validationFailureAction: Audit
rules:
- name: validate-resources
match:
any:
- resources:
kinds:
- Pod
validate:
message: "CPU and memory resource limits are required"
pattern:
spec:
containers:
- resources:
limits:
memory: "?*"
cpu: "?*"π Require Labels
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: require-labels
annotations:
policies.kyverno.io/title: Require Labels
policies.kyverno.io/category: Best Practices
spec:
validationFailureAction: Enforce
rules:
- name: check-for-labels
match:
any:
- resources:
kinds:
- Deployment
- Service
validate:
message: "Required labels are missing"
pattern:
metadata:
labels:
app: "?*"
env: "?*"
team: "?*".
βββ base/
β βββ kyverno/ # Kyverno installation (optional)
βββ policies/
β βββ kustomization.yaml
β βββ security/ # Security-focused policies
β βββ best-practices/ # General best practices
β βββ compliance/ # Compliance and regulatory policies
β βββ custom/ # Organization-specific policies
βββ overlays/
β βββ dev/ # Development environment overrides
β βββ staging/ # Staging environment overrides
β βββ production/ # Production environment overrides
βββ tests/ # Policy test cases
βββ README.md
- Container image validation and signing
- Pod security standards enforcement
- Network policy requirements
- Secret and ConfigMap management
- Privilege escalation prevention
- Resource limits and requests
- Label and annotation requirements
- Namespace governance
- Deployment strategies
- Health check requirements
- Industry-specific compliance (PCI-DSS, HIPAA, etc.)
- Audit logging requirements
- Data residency rules
- Retention policies
- Kubernetes cluster (v1.24+)
- Kyverno installed (v1.10+)
- GitOps tool (ArgoCD or Flux) configured
- Git repository access
This repository is designed to be deployed via GitOps using ArgoCD or Flux.
Create an Application manifest:
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: kyverno-policies
namespace: argocd
spec:
project: default
source:
repoURL: https://github.com/your-org/kyverno-policies
targetRevision: main
path: policies
destination:
server: https://kubernetes.default.svc
namespace: kyverno
syncPolicy:
automated:
prune: true
selfHeal: true
syncOptions:
- CreateNamespace=trueCreate a Kustomization:
apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
name: kyverno-policies
namespace: flux-system
spec:
interval: 10m
sourceRef:
kind: GitRepository
name: kyverno-policies
path: ./policies
prune: true
wait: true.
βββ policies/
β βββ kustomization.yaml
β βββ security/
β β βββ kustomization.yaml
β β βββ *.yaml
β βββ best-practices/
β β βββ kustomization.yaml
β β βββ *.yaml
β βββ compliance/
β βββ kustomization.yaml
β βββ *.yaml
βββ overlays/ # Environment-specific overrides
βββ dev/
βββ staging/
βββ production/
Policies can run in different validation failure actions:
- Enforce: Blocks resources that violate the policy
- Audit: Allows resources but generates policy violations
- Warn: Allows resources and displays warnings
To change enforcement mode, modify the validationFailureAction field in each policy.
Use overlays to customize policies per environment:
Development - Audit mode for most policies:
# overlays/dev/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../policies
patches:
- patch: |-
- op: replace
path: /spec/validationFailureAction
value: Audit
target:
kind: ClusterPolicyProduction - Enforce mode:
# overlays/production/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../policies
patches:
- patch: |-
- op: replace
path: /spec/validationFailureAction
value: Enforce
target:
kind: ClusterPolicyTest policies locally before committing:
# Use Kyverno CLI to test policies
kyverno apply policies/ --resource tests/
# Test specific policy
kyverno apply policies/security/require-ro-rootfs.yaml --resource tests/pod-examples/Example GitHub Actions workflow:
name: Validate Kyverno Policies
on: [pull_request]
jobs:
validate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Kyverno CLI
run: |
curl -LO https://github.com/kyverno/kyverno/releases/download/v1.10.0/kyverno-cli_v1.10.0_linux_x86_64.tar.gz
tar -xzf kyverno-cli_v1.10.0_linux_x86_64.tar.gz
sudo mv kyverno /usr/local/bin/
- name: Test Policies
run: kyverno apply policies/ --resource tests/
- name: Validate Manifests
run: kyverno validate policies/# .pre-commit-config.yaml
repos:
- repo: local
hooks:
- id: kyverno-validate
name: Validate Kyverno Policies
entry: kyverno validate
language: system
files: \.yaml$
pass_filenames: trueView policy reports:
# Cluster-wide policy reports
kubectl get clusterpolicyreport -A
# Namespaced policy reports
kubectl get policyreport -n <namespace>
# View detailed violations
kubectl get clusterpolicyreport -o yamlMonitor deployment status through your GitOps tool:
ArgoCD Dashboard: Access via https://argocd.yourdomain.com
- View sync status and health
- See policy drift detection
- Review sync history
Flux:
# Check if policies are up to date
flux get kustomizations
flux logs
# View alerts
flux get alertsKyverno exposes Prometheus metrics:
# ServiceMonitor for Prometheus Operator
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: kyverno-metrics
spec:
selector:
matchLabels:
app.kubernetes.io/name: kyverno
endpoints:
- port: metricsKey metrics to monitor:
kyverno_policy_rule_results_total- Policy execution resultskyverno_admission_requests_total- Admission request countkyverno_policy_execution_duration_seconds- Policy execution time
- Clear naming: Use descriptive names that indicate the policy's purpose
- Documentation: Include detailed descriptions and rationale
- Annotations: Add relevant metadata (severity, category, references)
- Testing: Provide test cases for both compliant and non-compliant resources
- Exclusions: Use namespace selectors to exclude system namespaces when appropriate
graph LR
A[π¨βπ» Developer] -->|1. Create Policy| B[πΏ Feature Branch]
B -->|2. Test Locally| C[π§ͺ Kyverno CLI]
C -->|3. Push & PR| D[π Pull Request]
D -->|4. CI Validation| E[β
GitHub Actions]
E -->|5. Review| F[π₯ Team Review]
F -->|6. Merge| G[π Main Branch]
G -->|7. Auto Sync| H[π GitOps Tool]
H -->|8. Deploy| I[βΈοΈ Kubernetes]
I -->|9. Monitor| J[π Policy Reports]
- Development: Create/modify policies in feature branch
- Testing: Run local tests using Kyverno CLI
- Pull Request: Submit PR, automated tests run via CI/CD
- Review: Team reviews policy changes and test results
- Merge: Merge to main branch
- Deployment: GitOps tool automatically syncs to clusters
- Monitor: Check policy reports and metrics
If a policy causes issues:
# Via ArgoCD
argocd app rollback kyverno-policies
# Via Flux
flux reconcile kustomization kyverno-policies --with-sourceOr revert the Git commit and let GitOps sync automatically.
- Fork the repository
- Create a feature branch (
git checkout -b feature/new-policy) - Add or modify policies
- Include test cases in
tests/directory - Test locally with Kyverno CLI
- Update documentation
- Commit changes (
git commit -m 'Add: policy description') - Push to branch (
git push origin feature/new-policy) - Submit a pull request
- Wait for CI checks and team review
- Once approved, merge triggers automatic deployment via GitOps
# List all installed policies
kubectl get clusterpolicy,policy -A
# Describe a specific policy
kubectl describe clusterpolicy <policy-name>
# View policy status
kubectl get clusterpolicy <policy-name> -o jsonpath='{.status}'
# Delete a policy
kubectl delete clusterpolicy <policy-name>ArgoCD:
# Check sync status
argocd app get kyverno-policies
# Sync manually
argocd app sync kyverno-policies
# View sync history
argocd app history kyverno-policies
# Rollback to previous version
argocd app rollback kyverno-policiesFlux:
# Check reconciliation status
flux get kustomizations
# Force reconciliation
flux reconcile kustomization kyverno-policies --with-source
# Suspend reconciliation
flux suspend kustomization kyverno-policies
# Resume reconciliation
flux resume kustomization kyverno-policies- Start with Audit Mode: Deploy new policies in audit mode first, monitor reports, then switch to enforce
- Use Overlays: Maintain environment-specific configurations using Kustomize overlays
- Version Control: Tag releases for easy rollback (
git tag v1.0.0) - Namespace Exclusions: Exclude critical namespaces (kube-system, kyverno, argocd, flux-system)
- Gradual Rollout: Deploy to dev β staging β production
- Monitor Policy Reports: Set up alerts for policy violations
- Document Exceptions: Clearly document any policy exclusions or exceptions
- Regular Reviews: Schedule periodic reviews of policies and violations
- Immutable Infrastructure: Never modify policies directly in clusters
- Backup Policies: Keep policies in version control (already done with Git!)
[Specify your license here]
For issues and questions:
Kyverno:
- Open an issue in this repository
- Kyverno Slack channel (#kyverno)
- Kyverno documentation
GitOps Tools:
- ArgoCD Slack (#argo-cd)
- Flux Slack (#flux)
- Check GitOps application status:
argocd app get kyverno-policiesorflux get kustomizations - Verify repository access and credentials
- Check for YAML syntax errors in policies
- Verify Kyverno is running:
kubectl get pods -n kyverno - Check policy status:
kubectl get clusterpolicy <policy-name> -o yaml - Review webhook configuration:
kubectl get validatingwebhookconfigurations
- Review policy complexity and optimize rules
- Consider using background scanning for expensive checks
- Monitor Kyverno resource usage
Documentation β’ Contributing β’ Code of Conduct β’ Security