Slide 1

Slide 1 text

Kustomizing your Kubernetes Deployments Cloud Native Computing Switzerland Meetup, 22 November 2018 David Schweikert @dschweikert AdNovum Informatik AG

Slide 2

Slide 2 text

“Configuration management” in Kubernetes?

Slide 3

Slide 3 text

We don’t need it! $ kubectl apply -f /

Slide 4

Slide 4 text

But… how to parametrize?

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

Parametrization is important! § Minimize differences between environments § Test what you deploy in prod! § Avoid code duplication

Slide 7

Slide 7 text

Me in 2017: !

Slide 8

Slide 8 text

Me in 2017: ! § OpenShift Templates § Helm § Self-made? Jinja2? § Even thinking of using Ansible…

Slide 9

Slide 9 text

Me in 2018: ! § Kustomize

Slide 10

Slide 10 text

What are the options?

Slide 11

Slide 11 text

Ansible Forge Helm K8comp KPM KY Kapitan Kdeploy Kedge Kenv Kexpand Kit-Deploymentizer Kompose Konfd Kontemplate Ksonnet Ktmpl Kubecfg Kubegen Kubernetes-deploy Kubetpl Kustomize Mortar OpenShift templates Psykube Spread Terraform …

Slide 12

Slide 12 text

Why Kustomize?

Slide 13

Slide 13 text

Reason #1: Embrace Kubernetes API Object Descriptions (YAML files)

Slide 14

Slide 14 text

kind: Service apiVersion: v1 metadata: name: my-service spec: selector: app: MyApp ports: - protocol: TCP port: 80 targetPort: 9376 my-service.yaml:

Slide 15

Slide 15 text

local params = std.extVar("__ksonnet/params").components.demo; local k = import "k.libsonnet"; local service = k.core.v1.service; local servicePort = k.core.v1.service.mixin.spec.portsType; local targetPort = params.containerPort; local labels = {app: params.name}; local appService = service .new( params.name, labels, servicePort.new(params.servicePort, targetPort)) .withType(params.type); k.core.v1.list.new([appService]) Ksonnet:

Slide 16

Slide 16 text

keep your YAML files the way they are

Slide 17

Slide 17 text

Reason #2: Keep using kubectl

Slide 18

Slide 18 text

$ kustomize build . | kubectl apply -f - Typical workflow:

Slide 19

Slide 19 text

Helm: § forget about kubectl, now you need to always use “helm install”, “helm ls”, “helm status”

Slide 20

Slide 20 text

Reason #3: Declarative and Templates-free

Slide 21

Slide 21 text

YAML can be painful sometimes

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Templated YAML is much worse…

Slide 24

Slide 24 text

apiVersion: v1 kind: Service metadata: name: {{ template "grafana.fullname" . }} labels: app: {{ template "grafana.name" . }} chart: {{ template "grafana.chart" . }} release: {{ .Release.Name }} heritage: {{ .Release.Service }} {{- if .Values.service.labels }} {{ toYaml .Values.service.labels | indent 4 }} {{- end }} {{- with .Values.service.annotations }} annotations: {{ toYaml . | indent 4 }} {{- end }} spec: {{- if (or (eq .Values.service.type "ClusterIP") (empty .Values.service.type)) }} type: ClusterIP {{- if .Values.service.clusterIP }} clusterIP: {{ .Values.service.clusterIP }} {{end}} {{- else if eq .Values.service.type "LoadBalancer" }} …

Slide 25

Slide 25 text

Kustomize’s cure: § No templating at all!

Slide 26

Slide 26 text

Kustomize’s cure: § Overlays § Transformations § Generators § Patches

Slide 27

Slide 27 text

Overlays

Slide 28

Slide 28 text

Overlays myApp | ├── base │ ├── deployment.yaml │ ├── kustomization.yaml │ └── service.yaml | ├── development │ ├── ingress.yaml │ └── kustomization.yaml | └── production ├── ingress.yaml └── kustomization.yaml resources: - deployment.yaml - service.yaml

Slide 29

Slide 29 text

Overlays myApp | ├── base │ ├── deployment.yaml │ ├── kustomization.yaml │ └── service.yaml | ├── development │ ├── ingress.yaml │ └── kustomization.yaml | └── production ├── ingress.yaml └── kustomization.yaml bases: - ../base resources: - ingress.yaml bases: - ../base resources: - ingress.yaml

Slide 30

Slide 30 text

$ kustomize build development apiVersion: v1 kind: Service metadata: … --- apiVersion: apps/v1 kind: Deployment metadata: … --- apiVersion: apps/v1 kind: Ingress metadata: …

Slide 31

Slide 31 text

Transformations

Slide 32

Slide 32 text

Transformations myApp| ├── base │ ├── deployment.yaml │ └── kustomization.yaml | ├── development │ └── kustomization.yaml bases: - ../base namePrefix: dev- § All resource names are now prefixed with “dev-”

Slide 33

Slide 33 text

Transformations It’s what makes kustomize so powerful: § Because it knows Kubernetes semantics § A single line, use-case specific (e.g. namePrefix) causes big changes § All references are preserved

Slide 34

Slide 34 text

Generators kustomization.yaml: configMapGenerator: - name: myconfig files: - configs/configfile - configs/another_configfile § generates: myconfig-b62k6t7g8f (and fixes all references to it) § b62k6t7g8f is a hash of the contents

Slide 35

Slide 35 text

Generators kustomization.yaml: configMapGenerator: - name: myconfig files: - configs/configfile - configs/another_configfile § generates: myconfig-b62k6t7g8f (and fixes all references to it) § b62k6t7g8f is a hash of the contents !!!

Slide 36

Slide 36 text

Patches

Slide 37

Slide 37 text

Patches myApp| ├── base │ ├── deployment.yaml │ └── kustomization.yaml | ├── development | ├── deployment.patch.yaml │ └── kustomization.yaml bases: - ../base patches: - deployment.patch.yaml

Slide 38

Slide 38 text

Patches myApp| ├── base │ ├── deployment.yaml │ └── kustomization.yaml | ├── development | ├── deployment.patch.yaml │ └── kustomization.yaml apiVersion: apps/v1 kind: Deployment metadata: name: myapp spec: replicas: 1

Slide 39

Slide 39 text

Patches Same syntax as “kubectl patch” § Strategic merge patches § JSON patches (RFC 6902)

Slide 40

Slide 40 text

Challenges

Slide 41

Slide 41 text

Challenges § Documentation is (currently) not super great

Slide 42

Slide 42 text

Challenges § Things that Kustomize doesn’t know about § OpenShift objects § CRDs

Slide 43

Slide 43 text

Challenges § Things that Kustomize doesn’t know about § OpenShift objects § CRDs It is now possible to extend Kustomize knowledge about Kubernetes objects see also: https://github.com/adnovum/kustomize-openshift

Slide 44

Slide 44 text

Summary When to use kustomize § It’s the perfect tool to parametrize your own application When not to use kustomize § Packaging an application for the general public (use Helm for that)

Slide 45

Slide 45 text

Questions? More about this topic: § Declarative application management in Kubernetes August 2017, by Brian Grant § Introducing kustomize; Template-free Configuration Customization for Kubernetes May 2018, by Jeff Regan and Phil Wittrock Contacting me: § david@schweikert.ch, @dschweikert