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

Introduction to kustomize

Introduction to kustomize

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

Seigo Uchida

July 11, 2018
Tweet

More Decks by Seigo Uchida

Other Decks in Technology

Transcript

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

    View full-size slide

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

    View full-size slide

  3. 1. Basics
    2. Features
    3. Keys

    View full-size slide

  4. Tested with kustomize v1.0.3

    View full-size slide

  5. What is kustomize?

    View full-size slide

  6. kustomize is a command line tool

    View full-size slide

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

    View full-size slide

  8. Let’s learn a basic usage!

    View full-size slide

  9. Basics / Hello World

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  24. template-free customization

    View full-size slide

  25. overlay customization

    View full-size slide

  26. base deployment (replicas 1)

    View full-size slide

  27. staging deployment (replicas: 3)

    View full-size slide

  28. overlayed staging deployment (replicas 3)

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  35. Basics / Motivation

    View full-size slide

  36. Declarative specification
    is the recommended way

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  39. 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

    View full-size slide

  40. Features / Name Prefix

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  44. Features / Common Labels

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  48. Features / Common Annotattion

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  51. 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

    View full-size slide

  52. Features / ConfigMap Generator

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  56. 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

    View full-size slide

  57. 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

    View full-size slide

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

    View full-size slide

  59. 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

    View full-size slide

  60. Features / Secrets Generator (skip)

    View full-size slide

  61. Features / Diff

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  64. Features / Substitute (skip)

    View full-size slide

  65. Workflows / Bespoke config

    View full-size slide

  66. Bespoke config

    View full-size slide

  67. Workflows / Off-the-shelf config

    View full-size slide

  68. Off-the-shelf config

    View full-size slide

  69. Keys / Overlay vs Template

    View full-size slide

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

    View full-size slide

  71. 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

    View full-size slide

  72. With kustomize
    You can override any part of config with kustomize

    View full-size slide

  73. Keys / Single source of truth

    View full-size slide

  74. 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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  78. 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

    View full-size slide

  79. 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

    View full-size slide

  80. Keys / Rolling ConfigMap Update

    View full-size slide

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

    View full-size slide

  82. 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

    View full-size slide

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

    View full-size slide

  84. Keys / Teaching native k8s APIs

    View full-size slide

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

    View full-size slide

  86. Same as kubernetes manifest
    Using Native Kubernetes API

    View full-size slide

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

    View full-size slide

  88. Keys / Rollback

    View full-size slide

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

    View full-size slide

  90. 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

    View full-size slide

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

    View full-size slide

  92. Keys / might be moved to kubectl

    View full-size slide

  93. 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

    View full-size slide

  94. Keys / See design doc!

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide