Slide 1

Slide 1 text

Introduction to kustomize Kubernetes Meetup Tokyo #12, Jul 11, 2018

Slide 2

Slide 2 text

@spesnova SRE at Mercari, Inc. / Kubernetes Tokyo Community Organizer

Slide 3

Slide 3 text

Agenda

Slide 4

Slide 4 text

1. Basics 2. Features 3. Keys

Slide 5

Slide 5 text

Tested with kustomize v1.0.3

Slide 6

Slide 6 text

Basics

Slide 7

Slide 7 text

What is kustomize?

Slide 8

Slide 8 text

kustomize is a command line tool

Slide 9

Slide 9 text

kustomize is a CLI for managing k8s style object with declarative way

Slide 10

Slide 10 text

Let’s learn a basic usage!

Slide 11

Slide 11 text

Basics / Hello World

Slide 12

Slide 12 text

• 3 environments (dev, stg, prod) • 1 deployment resource • different replicas by environments Example Requirements

Slide 13

Slide 13 text

hello-world/ ├── base │ ├── deployment.yaml │ └── kustomization.yaml └── overlays ├── production │ ├── replica_count.yaml │ └── kustomization.yaml └── staging ├── replica_count.yaml └── kustomization.yaml File Structure

Slide 14

Slide 14 text

hello-world/ ├── base │ ├── deployment.yaml │ └── kustomization.yaml └── overlays ├── production │ ├── replica_count.yaml │ └── kustomization.yaml └── staging ├── replica_count.yaml └── kustomization.yaml File Structure

Slide 15

Slide 15 text

hello-world/ ├── base │ ├── deployment.yaml │ └── kustomization.yaml └── overlays ├── production │ ├── replica_count.yaml │ └── kustomization.yaml └── staging ├── replica_count.yaml └── kustomization.yaml File Structure

Slide 16

Slide 16 text

hello-world/ ├── base │ ├── deployment.yaml │ └── kustomization.yaml └── overlays ├── production │ ├── replica_count.yaml │ └── kustomization.yaml └── staging ├── replica_count.yaml └── kustomization.yaml File Structure

Slide 17

Slide 17 text

hello-world/ ├── base │ ├── deployment.yaml │ └── kustomization.yaml └── overlays ├── production │ ├── replica_count.yaml │ └── kustomization.yaml └── staging ├── replica_count.yaml └── kustomization.yaml Base

Slide 18

Slide 18 text

# hello-world/base/deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: hello-world spec: replicas: 1 selector: matchLabels: app: hello-world template: .. Base

Slide 19

Slide 19 text

# hello-world/base/kustomization.yaml resources: - deployment.yaml Base

Slide 20

Slide 20 text

hello-world/ ├── base │ ├── deployment.yaml │ └── kustomization.yaml └── overlays ├── production │ ├── replica_count.yaml │ └── kustomization.yaml └── staging ├── replica_count.yaml └── kustomization.yaml Staging

Slide 21

Slide 21 text

# hello-world/staging/replica_count.yaml apiVersion: apps/v1 kind: Deployment metadata: name: hello-world spec: replicas: 3 Staging

Slide 22

Slide 22 text

# hello-world/staging/kustomization.yaml bases: - ../../base patches: - replica_count.yaml Staging

Slide 23

Slide 23 text

$ kustomize build -h Print current configuration per contents of kustomization.yaml Usage: kustomize build [path] [flags] $ kustomize build

Slide 24

Slide 24 text

$ kustomize build hello-world/overlays/staging/ Print staging configuration

Slide 25

Slide 25 text

apiVersion: apps/v1 kind: Deployment metadata: name: hello-world spec: replicas: 3 selector: matchLabels: app: hello-world template: .. Print staging configuration

Slide 26

Slide 26 text

template-free customization

Slide 27

Slide 27 text

overlay customization

Slide 28

Slide 28 text

base deployment (replicas 1)

Slide 29

Slide 29 text

staging deployment (replicas: 3)

Slide 30

Slide 30 text

overlayed staging deployment (replicas 3)

Slide 31

Slide 31 text

hello-world/ ├── base │ ├── deployment.yaml │ └── kustomization.yaml └── overlays ├── production │ ├── replica_count.yaml │ └── kustomization.yaml └── staging ├── replica_count.yaml └── kustomization.yaml Production

Slide 32

Slide 32 text

# hello-world/production/replica_count.yaml apiVersion: apps/v1 kind: Deployment metadata: name: hello-world spec: replicas: 7 Production

Slide 33

Slide 33 text

# hello-world/production/kustomization.yaml bases: - ../../base patches: - replica_count.yaml Production

Slide 34

Slide 34 text

$ kustomize build hello-world/overlays/production/ Print production configuration

Slide 35

Slide 35 text

apiVersion: apps/v1 kind: Deployment metadata: name: hello-world spec: replicas: 7 selector: matchLabels: app: hello-world template: .. Print production configuration

Slide 36

Slide 36 text

$ kustomize build [PATH] | kubectl apply -f - Apply printed configuration

Slide 37

Slide 37 text

Basics / Motivation

Slide 38

Slide 38 text

Declarative specification is the recommended way

Slide 39

Slide 39 text

However…

Slide 40

Slide 40 text

It’s difficult to use only current kubectl to follow declarative way…

Slide 41

Slide 41 text

Then…

Slide 42

Slide 42 text

• Helm • Ksonnet • Kapitan • Forge • Ktmpl • etc… Another Tools are required

Slide 43

Slide 43 text

1. I have to learn new tools… 2. I have to learn new DSL… (complicated!) 3. I have to teach new concepts to teams… Drawbacks of those tools

Slide 44

Slide 44 text

Features

Slide 45

Slide 45 text

Features / Name Prefix

Slide 46

Slide 46 text

# overlays/production/kustomization.yaml namePrefix: prod- bases: - ../../base patches: - replica_count.yaml Name Prefix

Slide 47

Slide 47 text

$ kustomize build hello-world/overlays/production/ Name Prefix

Slide 48

Slide 48 text

apiVersion: apps/v1 kind: Deployment metadata: name: prod-hello-world spec: replicas: 7 selector: matchLabels: app: hello-world template: .. Name Prefix

Slide 49

Slide 49 text

Features / Common Labels

Slide 50

Slide 50 text

# base/kustomization.yaml commonLabels: owner: spesnova resources: - deployment.yaml Common Labels

Slide 51

Slide 51 text

$ kustomize build hello-world/overlays/production/ Common Labels

Slide 52

Slide 52 text

apiVersion: apps/v1 kind: Deployment metadata: name: hello-world labels: owner: spesnova spec: replicas: 7 selector: matchLabels: app: hello-world template: .. Common Labels

Slide 53

Slide 53 text

Features / Common Annotattion

Slide 54

Slide 54 text

# base/kustomization.yaml commonAnnotations: description: This is Hello World App resources: - deployment.yaml Common Annotations

Slide 55

Slide 55 text

$ kustomize build hello-world/overlays/production/ Common Annotations

Slide 56

Slide 56 text

apiVersion: apps/v1 kind: Deployment metadata: name: hello-world annotations: description: This is Hello World App spec: replicas: 7 selector: matchLabels: app: hello-world template: … Common Annotations

Slide 57

Slide 57 text

Features / ConfigMap Generator

Slide 58

Slide 58 text

# base/kustomization.yaml resources: - deployment.yaml configMapGenerator: - name: hello-config files: - hello.config ConfigMap Generator

Slide 59

Slide 59 text

# hello.config name=hello-world region=tokyo ConfigMap Generator

Slide 60

Slide 60 text

$ kustomize build hello-world/overlays/production/ ConfigMap Generator

Slide 61

Slide 61 text

apiVersion: v1 data: hello.config: | name=hello-world region=tokyo kind: ConfigMap metadata: creationTimestamp: null name: hello-config-4g5t58m8t5 --- apiVersion: apps/v1 kind: Deployment … ConfigMap Generator

Slide 62

Slide 62 text

apiVersion: v1 data: hello.config: | name=hello-world region=tokyo kind: ConfigMap metadata: creationTimestamp: null name: hello-config-4g5t58m8t5 --- apiVersion: apps/v1 kind: Deployment … Hash suffix

Slide 63

Slide 63 text

# hello.config name=hello-world region=london Hash suffix

Slide 64

Slide 64 text

apiVersion: v1 data: hello.config: | name=hello-world region=tokyo kind: ConfigMap metadata: creationTimestamp: null name: hello-config-bdmmkghm2m --- apiVersion: apps/v1 kind: Deployment … Hash suffix

Slide 65

Slide 65 text

Features / Secrets Generator (skip)

Slide 66

Slide 66 text

Features / Diff

Slide 67

Slide 67 text

$ kustomize diff hello-world/overlays/production/ $ kustomize diff

Slide 68

Slide 68 text

@@ -3,7 +3,7 @@ metadata: name: hello-world spec: - replicas: 1 + replicas: 7 selector: matchLabels: app: hello-world $ kustomize diff

Slide 69

Slide 69 text

Features / Substitute (skip)

Slide 70

Slide 70 text

Workflows / Bespoke config

Slide 71

Slide 71 text

Bespoke config

Slide 72

Slide 72 text

Workflows / Off-the-shelf config

Slide 73

Slide 73 text

Off-the-shelf config

Slide 74

Slide 74 text

Keys

Slide 75

Slide 75 text

Keys / Overlay vs Template

Slide 76

Slide 76 text

1. Can only override parameterized config 2. DSL is too complicated for human 3. Most tools can not read DSL Drawbacks of Templating

Slide 77

Slide 77 text

1. I’m using official Redis Helm chart 2. I want to add annotation 3. Annotations are not defined in the chart… 4. …Fork? Example

Slide 78

Slide 78 text

With kustomize You can override any part of config with kustomize

Slide 79

Slide 79 text

Keys / Single source of truth

Slide 80

Slide 80 text

1. There is a config file “hello.config” 2. Copy contents of the file 3. Paste it into configMap 4. … I have 2 config sources… Before kustomize

Slide 81

Slide 81 text

# base/kustomization.yaml resources: - deployment.yaml configMapGenerator: - name: hello-config files: - hello.config ConfigMap Generator

Slide 82

Slide 82 text

# hello.config name=hello-world region=tokyo ConfigMap Generator

Slide 83

Slide 83 text

$ kustomize build hello-world/overlays/production/ ConfigMap Generator

Slide 84

Slide 84 text

apiVersion: v1 data: hello.config: | name=hello-world region=tokyo kind: ConfigMap metadata: creationTimestamp: null name: hello-config-4g5t58m8t5 --- apiVersion: apps/v1 kind: Deployment … ConfigMap Generator

Slide 85

Slide 85 text

1. There is a config file “hello.config” 2. Run “kustomize build” 3. kustomize generates configMap 4. The config source is only “hello.config” After kustomize

Slide 86

Slide 86 text

Keys / Rolling ConfigMap Update

Slide 87

Slide 87 text

1. Update contents of existing configMap 2. Deployment itself is not changed… 3. Deployment still reads old configMap… Updating existing configMap

Slide 88

Slide 88 text

apiVersion: v1 data: hello.config: | name=hello-world region=tokyo kind: ConfigMap metadata: creationTimestamp: null name: hello-config-4g5t58m8t5 --- apiVersion: apps/v1 kind: Deployment … Hash suffix

Slide 89

Slide 89 text

1. Update contents of configMap 2. kustomize prints new configMap 3. Update configMap name in deployment 4. Deployment reads new configMap Rolling ConfigMap Update

Slide 90

Slide 90 text

Keys / Teaching native k8s APIs

Slide 91

Slide 91 text

kustomize exposes and teaches native k8s APIs, rather than hiding them. IUUQTHJUIVCDPNLVCFSOFUFTTJHTLVTUPNJ[FCMPCNBTUFSEPDTHMPTTBSZNE

Slide 92

Slide 92 text

Same as kubernetes manifest Using Native Kubernetes API

Slide 93

Slide 93 text

1. Lower learning cost 2. Deeper understanding about Kubernetes Using Native Kubernetes API

Slide 94

Slide 94 text

Keys / Rollback

Slide 95

Slide 95 text

$ git checkout XXXXXX $ kustomize build [PATH] | kubectl apply -f - Rollback

Slide 96

Slide 96 text

kustomize rollback is very good for GitOps. However, I also like heroku style rollback such as “helm status”, “helm history”, “helm rollback”. Helm provides us logical group of k8s resources as “application”. kustomize doesn’t. Rollback

Slide 97

Slide 97 text

Kubernetes Application proposal KEP Related issue https://github.com/kubernetes/community/pull/1629

Slide 98

Slide 98 text

Keys / might be moved to kubectl

Slide 99

Slide 99 text

Kustomize was initially developed as its own cli, however once it has matured, it should be published as a subcommand of kubectl or as a statically linked plugin. IUUQTHJUIVCDPNLVCFSOFUFTDPNNVOJUZCMPCNBTUFSLFQTTJHDMJLVTUPNJ[FNEJNQMFNFOUBUJPOEFUBJMTOPUFTDPOTUSBJOUTPQUJPOBM

Slide 100

Slide 100 text

Keys / See design doc!

Slide 101

Slide 101 text

https://github.com/kubernetes/community/blob/master/contributors/ design-proposals/architecture/declarative-application- management.md IUUQTHJUIVCDPNLVCFSOFUFTDPNNVOJUZCMPCNBTUFSLFQTTJHDMJLVTUPNJ[FNEJNQMFNFOUBUJPOEFUBJMTOPUFTDPOTUSBJOUTPQUJPOBM It’s awesome!

Slide 102

Slide 102 text

It’s awesome! If kustomize looks easy to use for you, I think it comes from good design!

Slide 103

Slide 103 text

Questions

Slide 104

Slide 104 text

Can I delete labels with overlay? As far as I know, you can not for now

Slide 105

Slide 105 text

End