Introduction to kustomize

Introduction to kustomize

A talk at Kubernetes Meetup Tokyo #12
https://k8sjp.connpass.com/event/90631/

32f2e5ddb187baa2abac66d7e8b283fe?s=128

Seigo Uchida

July 11, 2018
Tweet

Transcript

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

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

  3. Agenda

  4. 1. Basics 2. Features 3. Keys

  5. Tested with kustomize v1.0.3

  6. Basics

  7. What is kustomize?

  8. kustomize is a command line tool

  9. kustomize is a CLI for managing k8s style object with

    declarative way
  10. Let’s learn a basic usage!

  11. Basics / Hello World

  12. • 3 environments (dev, stg, prod) • 1 deployment resource

    • different replicas by environments Example Requirements
  13. hello-world/ ├── base │ ├── deployment.yaml │ └── kustomization.yaml └──

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

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

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

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

    overlays ├── production │ ├── replica_count.yaml │ └── kustomization.yaml └── staging ├── replica_count.yaml └── kustomization.yaml Base
  18. # hello-world/base/deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: hello-world spec:

    replicas: 1 selector: matchLabels: app: hello-world template: .. Base
  19. # hello-world/base/kustomization.yaml resources: - deployment.yaml Base

  20. hello-world/ ├── base │ ├── deployment.yaml │ └── kustomization.yaml └──

    overlays ├── production │ ├── replica_count.yaml │ └── kustomization.yaml └── staging ├── replica_count.yaml └── kustomization.yaml Staging
  21. # hello-world/staging/replica_count.yaml apiVersion: apps/v1 kind: Deployment metadata: name: hello-world spec:

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

  23. $ kustomize build -h Print current configuration per contents of

    kustomization.yaml Usage: kustomize build [path] [flags] $ kustomize build
  24. $ kustomize build hello-world/overlays/staging/ Print staging configuration

  25. apiVersion: apps/v1 kind: Deployment metadata: name: hello-world spec: replicas: 3

    selector: matchLabels: app: hello-world template: .. Print staging configuration
  26. template-free customization

  27. overlay customization

  28. base deployment (replicas 1)

  29. staging deployment (replicas: 3)

  30. overlayed staging deployment (replicas 3)

  31. hello-world/ ├── base │ ├── deployment.yaml │ └── kustomization.yaml └──

    overlays ├── production │ ├── replica_count.yaml │ └── kustomization.yaml └── staging ├── replica_count.yaml └── kustomization.yaml Production
  32. # hello-world/production/replica_count.yaml apiVersion: apps/v1 kind: Deployment metadata: name: hello-world spec:

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

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

  35. apiVersion: apps/v1 kind: Deployment metadata: name: hello-world spec: replicas: 7

    selector: matchLabels: app: hello-world template: .. Print production configuration
  36. $ kustomize build [PATH] | kubectl apply -f - Apply

    printed configuration
  37. Basics / Motivation

  38. Declarative specification is the recommended way

  39. However…

  40. It’s difficult to use only current kubectl to follow declarative

    way…
  41. Then…

  42. • Helm • Ksonnet • Kapitan • Forge • Ktmpl

    • etc… Another Tools are required
  43. 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
  44. Features

  45. Features / Name Prefix

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

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

  48. apiVersion: apps/v1 kind: Deployment metadata: name: prod-hello-world spec: replicas: 7

    selector: matchLabels: app: hello-world template: .. Name Prefix
  49. Features / Common Labels

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

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

  52. apiVersion: apps/v1 kind: Deployment metadata: name: hello-world labels: owner: spesnova

    spec: replicas: 7 selector: matchLabels: app: hello-world template: .. Common Labels
  53. Features / Common Annotattion

  54. # base/kustomization.yaml commonAnnotations: description: This is Hello World App resources:

    - deployment.yaml Common Annotations
  55. $ kustomize build hello-world/overlays/production/ Common Annotations

  56. 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
  57. Features / ConfigMap Generator

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

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

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

  61. 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
  62. 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
  63. # hello.config name=hello-world region=london Hash suffix

  64. 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
  65. Features / Secrets Generator (skip)

  66. Features / Diff

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

  68. @@ -3,7 +3,7 @@ metadata: name: hello-world spec: - replicas:

    1 + replicas: 7 selector: matchLabels: app: hello-world $ kustomize diff
  69. Features / Substitute (skip)

  70. Workflows / Bespoke config

  71. Bespoke config

  72. Workflows / Off-the-shelf config

  73. Off-the-shelf config

  74. Keys

  75. Keys / Overlay vs Template

  76. 1. Can only override parameterized config 2. DSL is too

    complicated for human 3. Most tools can not read DSL Drawbacks of Templating
  77. 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
  78. With kustomize You can override any part of config with

    kustomize
  79. Keys / Single source of truth

  80. 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
  81. # base/kustomization.yaml resources: - deployment.yaml configMapGenerator: - name: hello-config files:

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

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

  84. 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
  85. 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
  86. Keys / Rolling ConfigMap Update

  87. 1. Update contents of existing configMap 2. Deployment itself is

    not changed… 3. Deployment still reads old configMap… Updating existing configMap
  88. 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
  89. 1. Update contents of configMap 2. kustomize prints new configMap

    3. Update configMap name in deployment 4. Deployment reads new configMap Rolling ConfigMap Update
  90. Keys / Teaching native k8s APIs

  91. kustomize exposes and teaches native k8s APIs, rather than hiding

    them. IUUQTHJUIVCDPNLVCFSOFUFTTJHTLVTUPNJ[FCMPCNBTUFSEPDTHMPTTBSZNE
  92. Same as kubernetes manifest Using Native Kubernetes API

  93. 1. Lower learning cost 2. Deeper understanding about Kubernetes Using

    Native Kubernetes API
  94. Keys / Rollback

  95. $ git checkout XXXXXX $ kustomize build [PATH] | kubectl

    apply -f - Rollback
  96. 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
  97. Kubernetes Application proposal KEP Related issue https://github.com/kubernetes/community/pull/1629

  98. Keys / might be moved to kubectl

  99. 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
  100. Keys / See design doc!

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

  102. It’s awesome! If kustomize looks easy to use for you,

    I think it comes from good design!
  103. Questions

  104. Can I delete labels with overlay? As far as I

    know, you can not for now
  105. End