Home > Software engineering >  Create multiple containers with templating
Create multiple containers with templating

Time:05-12

I have a running k8s deployment, with one container.

I want to deploy 10 more containers, with a few differences in the deployment manifest (i.e command launched, container name, ...).

Rather than create 10 more .yml files with the whole deployment, I would prefer use templating. What can I do to achieve this ?

---
apiVersion: v1
kind: CronJob
metadata:
  name: myname
  labels:
    app.kubernetes.io/name: myname
spec:
  schedule: "*/10 * * * *"
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            app.kubernetes.io/name: myname
        spec:
          serviceAccountName: myname
          containers:
            - name: myname
              image: 'mynameimage'
              imagePullPolicy: IfNotPresent
              command: ["/my/command/to/launch"]
          restartPolicy: OnFailure

CodePudding user response:

Kustomize seems to be the go-to tool for templating, composition, multi-environment overriding, etc, in kubernetes configs. And it's built directly into kubectl now as well.

Specifically, I think you can achieve what you want by using the bases and overlays feature. Setup a base which contains the common structure and overlays which contain specific overrides.

CodePudding user response:

Either you can use Helm or Kustomize. Both are templating tools and help you to achieve your goal

CodePudding user response:

You can either specify a set of containers to be created you can do that like this:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: container1
        image: your-image
      - name: container2
        image: your-image
      - name: container3
        image: your-image

and you can repeat that container definition as many times as you want.

The other way around is to use a templating engine like helm/kustomize as mentioned above.

  • Related