Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Require basic best practices of Kubernetes depl...

Avatar for Jacky Hung Jacky Hung
November 22, 2021

Require basic best practices of Kubernetes deployments using OPA Gatekeeper with Rancher

Whit is OPA Gatekeeper and how to simply getting started with it to enforce some basic best practices and audit violations.

Avatar for Jacky Hung

Jacky Hung

November 22, 2021
Tweet

More Decks by Jacky Hung

Other Decks in Technology

Transcript

  1. Outline • Problem • OPA Gatekeeper ◦ Admission controllers ◦

    Dynamic admission control ◦ Gatekeeper ◦ OPA ◦ Rego ◦ Constraint templates and constraints • Practice with Rancher ◦ Installing OPA Gatekeeper with Rancher v2.6 ◦ Applying a constraint template ◦ Applying a constraint ◦ Verification • Other OPA use cases
  2. If you want to enforce best practices to your org

    like this... • General policies ◦ All images must be from approved repositories ◦ All pods must have resource limits ◦ All ingress hostnames must be globally unique ◦ Disallow NodePort services ◦ Disallow “latest” container image tag ◦ Require speicfic labels or annotations ◦ Require container probles • Pod security policies ◦ Disallow running as root ◦ Disallow privileged containers ◦ Allow only specific hostPath volumes
  3. Deprecation of PodSecurityPolicy • Kubernetes 1.21 starts the deprecation process

    for PodSecurityPolicy. • The current plan is to remove PSP from Kubernetes in the 1.25 release. • PSP has some serious usability problems that can’t be addressed without making breaking changes. • New "PSP Replacement Policy." for the feature. https://kubernetes.io/blog/2021/04/06/podsecuritypolicy-deprecation-past-present-and-future/
  4. Dynamic admission control • MutatingAdmissionWebhook • ValidatingAdmissionWebhook • Avoiding deadlocks

    in self-hosted webhooks • Avoiding operating on the kube-system namespace https://kubernetes.io/docs/reference/access-authn-authz/extensible-admission-controllers/
  5. Gatekeeper • An extensible, parameterized policy library. • Native Kubernetes

    CRDs for instantiating the policy library (aka “constraints”). • Native Kubernetes CRDs for extending the policy library (aka “constraint templates”). • Audit functionality.
  6. Open Policy Agent (OPA) • Pronounced “oh-pa”. • General-purpose policy

    engine. • OPA’s high-level declarative policy language: Rego. • Can enforce policies in microservices, Kubernetes, CI/CD pipelines, API gateways, and more. https://www.openpolicyagent.org/docs/latest/
  7. Rego • It’s declarative. • Rego Playground. • There are

    a lot of out-of-the-box templates for common use cases. https://www.openpolicyagent.org/docs/latest/#rego https://www.openpolicyagent.org/docs/latest/policy-language/
  8. Enforcement action • deny • dryrun • warn (available in

    Gatekeeper v3.4+ with Kubernetes v1.19+) https://open-policy-agent.github.io/gatekeeper/website/docs/violations
  9. Audit • Reading Audit Results ◦ Prometheus Metrics ▪ gatekeeper_audit_last_run_time

    ▪ gatekeeper_violations ◦ Constraint Status ◦ Audit Logs • Configuring Audit ◦ --constraint-violations-limit=20 ◦ --audit-interval=300
  10. Rancher • Provisioning and managing Kubernetes clusters with web UI.

    • Integrate OPA Gatekeeper starting from v2.5.
  11. Example: Disallow “latest” image tags #1 • The constraint template

    • The constraint • Verification examples: 1, 2, 3