Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reuse common configuration across different kubernetes manifests?

Tags:

kubernetes

Assume I have this manifest:

apiVersion: batch/v1
kind: Job
metadata:
  name: initialize-assets-fixtures
spec:
  template:
    spec:
      initContainers:
      - name: wait-for-minio
        image: bitnami/minio-client
        env:
          - name: MINIO_SERVER_ACCESS_KEY
            valueFrom:
              secretKeyRef:
                name: minio
                key: access-key
          - name: MINIO_SERVER_SECRET_KEY
            valueFrom:
              secretKeyRef:
                name: minio
                key: secret-key
          - name: MINIO_SERVER_HOST
            value: minio
          - name: MINIO_SERVER_PORT_NUMBER
            value: "9000"
          - name: MINIO_ALIAS
            value: minio
        command: 
          - /bin/sh
          - -c 
          - |
            mc config host add ${MINIO_ALIAS} http://${MINIO_SERVER_HOST}:${MINIO_SERVER_PORT_NUMBER} ${MINIO_SERVER_ACCESS_KEY} ${MINIO_SERVER_SECRET_KEY}
      containers:
      - name: initialize-assets-fixtures
        image: bitnami/minio
        env:
          - name: MINIO_SERVER_ACCESS_KEY
            valueFrom:
              secretKeyRef:
                name: minio
                key: access-key
          - name: MINIO_SERVER_SECRET_KEY
            valueFrom:
              secretKeyRef:
                name: minio
                key: secret-key
          - name: MINIO_SERVER_HOST
            value: minio
          - name: MINIO_SERVER_PORT_NUMBER
            value: "9000"
          - name: MINIO_ALIAS
            value: minio
        command:
          - /bin/sh
          - -c
          - |
            mc config host add ${MINIO_ALIAS} http://${MINIO_SERVER_HOST}:${MINIO_SERVER_PORT_NUMBER} ${MINIO_SERVER_ACCESS_KEY} ${MINIO_SERVER_SECRET_KEY}

            for category in `ls`; do
              for f in `ls $category/*` ; do
                mc cp $f ${MINIO_ALIAS}/$category/$(basename $f)
              done
            done
      restartPolicy: Never

You see I have here one initContainer and one container. In both containers, I have the same configuration, i.e. the same env section.

Assume I have yet another Job manifest where I use the very same env section again.

It's a lot of duplicated configuration that I bet I can simplify drastically, but I don't know how to do it. Any hint? Any link to some documentation? After some googling, I was not able to come up with anything useful. Maybe with kustomize, but I'm not sure. Or maybe I'm doing it the wrong way with all those environment variables, but I don't think I have a choice, depending on the service I'm using (here it's minio, but I want to do the same kind of stuff with other services which might not be as flexible as minio).

like image 837
Laurent Michel Avatar asked Sep 19 '25 21:09

Laurent Michel


1 Answers

Based on my knowledge you have those 3 options

  • Kustomize
  • Helm
  • ConfigMap

ConfigMap


You can use either kubectl create configmap or a ConfigMap generator in kustomization.yaml to create a ConfigMap.

The data source corresponds to a key-value pair in the ConfigMap, where

key = the file name or the key you provided on the command line

value = the file contents or the literal value you provided on the command line.

More about how to use it in pod here


Helm


As @Matt mentionted in comments you can use helm

helm lets you template the yaml with values. Also once you get into it there are ways to create and include partial templates – Matt

By the way, helm has it's own created minio chart, you might take a look how it is created there.


Kustomize


It's well described here and here how could you do that in kustomize.

Let me know if you have any more questions.

like image 128
Jakub Avatar answered Sep 22 '25 22:09

Jakub