1 of 49

Kyverno

1

Kyverno Workshop �KCD Bengaluru

April 9, 2022

2 of 49

Kyverno workshop hosts

2

Vyankatesh Kudtarkar�

Maintainer�Nirmata

@VyankateshKudt1

Prateek Pandey

Maintainer

Nirmata

@imPrateek14

Shuting Zhao�

Maintainer Nirmata

@ShutingZhao2

3 of 49

Topics

  • Why Policies?
  • Why Kyverno?
  • Kyverno architecture
  • Kyverno policies
  • Kyverno Features & demos
  • Kyverno CLI & demos
  • Sample policies library
  • Summary and Q&A

3

4 of 49

Why Policies?

5 of 49

5

Policies are a contract

Developers

Security

Operations

6 of 49

What policies can do for you

  1. Improve security
  2. Eliminate misconfigurations
  3. Separation of concerns across roles
  4. Reduce configuration complexity
  5. Promote best practices
  6. Self-service and automation

6

7 of 49

7

8 of 49

Why Kyverno?

9 of 49

9

Kyverno means “Govern” in Greek

10 of 49

What Kyverno does

  1. Kyverno is a Kubernetes-native policy engine that helps you define policies using Kubernetes compliant manifests.
  2. Kyverno uses the Kubernetes admission webhook to validate, mutate, and generate Kubernetes resources, and verify images.
  3. A CLI is available for CI/CD pipelines.
  4. Using Kyverno, a central platform team can define policies and ensure the configurations are compliant with their security and best practices standards.
  5. Kyverno does not require learning a new programming language to define policies – it uses declarative manifests like Kubernetes.
  6. Creating and managing Kyverno policies is easy using existing tools!

11 of 49

Why Kyverno?

  1. Make K8s policies easy to write and manage
  2. Validate (audit or enforce), Mutate, Generate and ImageVerify
  3. Make policy results easy to process
  4. Support all Kubernetes types including Custom Resources
  5. Use Kubernetes patterns and practices �e.g. labels and selectors, annotations, events, ownerReferences, pod controllers, etc.

11

Kyverno simplifies K8s policy management!

12 of 49

Kubernetes Policy Management Tools Compared �OPA with Gatekeeper vs. Kyverno -- by Viktor Farcic

13 of 49

Common Use Cases

  • Pod and workload security
  • Best practice configurations
  • Fine-grained RBAC
  • Multi-tenancy
  • Auto-Labeling resources
  • Sidecar (/certificate) injection with mounts, etc.
  • Image Signing and Supply Chain Security

13

new

14 of 49

Kyverno Architecture

15 of 49

Kyverno Architecture

15

16 of 49

Kyverno Artifacts

16

17 of 49

Kyverno Policies

18 of 49

A Kyverno Policy

18

19 of 49

A Kyverno Policy

19

20 of 49

Validate Policy

  • Overlays with patterns specify desired state
  • Matches all defined fields
  • Patterns
    • * : zero or more
    • ? : any one
  • Operators
    • >, <, >=, <=, !, |(or)

20

21 of 49

Mutate Policy

  • JSON Patch (RFC 6902)
    • Use for precise updates
  • StrategicMergePatch
    • Use for describing intent
    • Anchors for conditional logic
      • “If-then-else”
      • “if-not-defined”

21

22 of 49

Generate Policy

22

  • Triggers when a new resource is created or based on label and metadata changes
  • Useful in creating defaults for a namespace
  • Clones existing resources or copies in-line data
  • Can optionally keep data in-sync across namespaces

23 of 49

Image Verification Policy

  • Native Cosign 1.0 support!
  • Match images using wildcards
  • Verify multiple signatures
  • Optional signature registry

23

24 of 49

�Features

&

Demos

25 of 49

Demo

Validate Policies

26 of 49

Demo - Kyverno Pod Security Policies

26

$ kustomize build https://github.com/kyverno/policies/pod-security | kubectl apply -f -

27 of 49

Policy Reporter

  • View policy results from Kyverno, Falco, kube-bench
  • Send notifications to Loki, ES, Teams, Discord, Slack

27

28 of 49

Policy Metrics

Prometheus metrics:

  1. Policies and Rule Counts
  2. Policy and Rule Execution
  3. Policy Rule Execution Latency
  4. Admission Review Latency
  5. Admission Requests Counts
  6. Policy Change Counts

28

29 of 49

Grafana Dashboard

29

30 of 49

Variables

  • Variables make policies data driven
  • Kyverno supports 2 types of data substitution:

30

    • References: reference data in the current resource
    • Variables: JMESPath expressions

31 of 49

Variables using JMESPath expressions

Built-in data:

  • Admission review request
  • User Information
  • Image information

31

{{request.object.namespace}}

{{request.user.namespace}}

{{roles}}

{{clusterRoles}}

{{images.<name>.<registry | name | tag }}

32 of 49

Data Sources

  • Data sources allow policies to be augmented with run-time data for reusability and complex logic
  • Kyverno supports three types of data sources:
    1. ConfigMaps
    2. API Server Lookups
    3. OCI Registries

33 of 49

External Data: Config Maps

  • Declare ConfigMap in the rule context
  • Reference ConfigMap data in policy rules

33

34 of 49

External Data: API Server Lookups

  • Use data from existing resources
  • Combine with JMESPath for easy processing
  • Test using kubectl �and jp

34

35 of 49

External Data: OCI Registries

  • Used for querying image signatures and attestations
  • Supports private registries
  • Supports multiple registries

35

36 of 49

Demo

Mutate Policies

37 of 49

Demo

Generate Policies

38 of 49

Demo - Generate Policies

38

-

  • Generate Clone: Clones existing resources or copies in-line data
  • Generate Data: To create defaults resources for matching resource
  • Multitenancy: Namespace-as-a-service

39 of 49

Demo

ImageVerify Policies

40 of 49

�Kyverno CLI

41 of 49

Kyverno CLI

41

  • Test policies locally or against clusters
  • Validate Kubernetes configurations for violations before they are deployed to a cluster
  • CLI can also be used to test policies to a cluster without installing Kyverno

42 of 49

Apply command

  • Perform a dry run on one or more policies with a given set of input resource.
  • The input resources can either be resource manifests or Kubernetes cluster

43 of 49

Test Command

  • Test multiple policy resources from a Git repository or local folders
  • Test command support Validating and Mutating policy.

43

44 of 49

�Sample Policies

45 of 49

Summary

46 of 49

Summary

  1. Kyverno is a Kubernetes native policy engine
  2. Kyverno secures and automates Kubernetes configurations
  3. Kyverno operates as an admission controller and as a CLI
  4. Kyverno can handle complex policies and is production ready with HA and security
  5. Kyverno is simple to get started with and works well with other Kubernetes tools and processes

46

47 of 49

Join the Kyverno Community

  • The Kyverno docs & samples: https://kyverno.io
  • Slack Channel:
  • Monthly community meetings
  • Weekly contributor meetings

47

Join https://groups.google.com/g/kyverno

48 of 49

Get Kyverno Certified!

  • Free training and certification

49 of 49

Thanks!

https://kyverno.io/

@kyverno