do I need to create overlay for each microservice container app

7/2/2021

I have around 20 container app and for each one I have deployment.yaml and now let's say I want to put different replicas for each one. What below image suggest that I need to match metadata:name.

Is this means I need to create 20 overlay.yaml each for one container app? Can I managed all app with SINGLE overlay file?

enter image description here

-- user584018
kubernetes
kustomize

1 Answer

7/7/2021

This can be resolved by using patches. You will be able to manage all deployments with one overlay file per environment where you can set explicitly number of replicas for each deployment.


Here's a simplified example of it:

I have two environments: dev and stage. Both have kustomization.yaml with patches for specific deploys and numbers of replicas (different for both envs).

tree command:

.
├── base
│   ├── app-1.yaml
│   ├── app-2.yaml
│   └── kustomization.yaml
└── overlays
    ├── dev
    │   └── kustomization.yaml
    └── stage
        └── kustomization.yaml

Deployment - app-1.yaml: (2nd is almost identical, different name and image)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: example-1
spec:
  template:
    spec:
      containers:
      - name: example-1
        image: example:1.0

Below a snippet from overlays/dev/kustomization.yaml:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
  - ../../base

patches:
  - patch: |-
      - op: replace
        path: /spec/replicas
        value: 2
    target:
      group: apps
      version: v1
      kind: Deployment
      name: example-1
  - patch: |-
      - op: replace
        path: /spec/replicas
        value: 3
    target:
      group: apps
      version: v1
      kind: Deployment
      name: example-2

Actual result:

$ kubectl kustomize overlays/dev
apiVersion: apps/v1
kind: Deployment
metadata:
  name: example-1
spec:
  replicas: 2
  template:
    spec:
      containers:
      - image: example:1.0
        name: example-1
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: example-2
spec:
  replicas: 3
  template:
    spec:
      containers:
      - image: example:2.0
        name: example-2

Useful links:

-- moonkotte
Source: StackOverflow