Generate Kubernetes manifest for each environment with kustomize

kubernetes

Kustomize is a tool that generates k8s manifests and is integrated into kubectl. Unlike Helm and ksonnet, it does not use templates, but uses the original manifest as the source, except for kusomization.yaml, so there is no need to rememberthe tool’s own notation and convert.

KubernetesのパッケージマネージャーHelmを使う - sambaiz-net

ksonnetでkubernetesのmanifestを環境ごとに生成/applyする - sambaiz-net

In kustomization.yaml, you can describe the target resources, common namespaces, labels, prefixes, patches, etc.

$ tree
.
|-- deployment.yaml
`-- kustomization.yaml

$ cat kustomization.yaml
resources:
- deployment.yaml
commonLabels:
  foo: bar

$ cat deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:1.14.2
        name: nginx
        ports:
        - containerPort: 80

You can output the yaml after applying kustomize to stdout with kubectl kustomize, and apply it directly with kubectl apply -k. The output shows that the foo:bar label is added from commonLabels.

$ kubectl kustomize .
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx
    foo: bar
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
      foo: bar
  template:
    metadata:
      labels:
        app: nginx
        foo: bar
    spec:
      containers:
      - image: nginx:1.14.2
        name: nginx
        ports:
        - containerPort: 80

Next, let’s generate manifest for each environment. Put kustomization.yaml in each directory and reference the original directory as bases.

$ tree
.
|-- base
|   |-- deployment.yaml
|   `-- kustomization.yaml
|-- dev
|   `-- kustomization.yaml
`-- prd
    |-- increase_replicas.yaml
    `-- kustomization.yaml

$ cat prd/increase_replicas.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 10

$ cat prd/kustomization.yaml
bases:
- ../base
namePrefix: prd-
patchesStrategicMerge:
- increase_replicas.yaml

In addition to namePrefix, the foo:bar label is added to the base, and the replicas is changed to 10 by the patch.

$ kubectl kustomize prd
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx
    foo: bar
  name: prd-nginx-deployment
spec:
  replicas: 10
  selector:
    matchLabels:
      app: nginx
      foo: bar
  template:
    metadata:
      labels:
        app: nginx
        foo: bar
    spec:
      containers:
      - image: nginx:1.14.2
        name: nginx
        ports:
        - containerPort: 80

Describe resources required exclusively in environments with Kustomize’s Component - sambaiz-net