0

I would like to find the best practice of creating several pods with different env values.

Let's say that my system should ping several websites every pod will ping a different website, the only difference is the URL, I would like to write one deployment file for all the different pods and one file with the list of URLs and that k8s will create a pod for each URL in the list.

Is it possible?

Tomer
  • 335
  • 4
  • 15
  • 1
    Yes, it is possible, by putting multiple complete Deployment specs in the same file. For the example you give, though, do these need to be separate processes, or can you achieve the same thing with a single Deployment worker and a job queue (like RabbitMQ) that pushes out the URLs? – David Maze Jun 28 '21 at 10:46
  • Need to agree with David Maze. Also I'd reckon you could use some templating tool like (Helm) and create a template (`Job` for example) that would be reused to different URL's. – Dawid Kruk Jun 28 '21 at 15:15
  • "do these need to be separate processes" Yes I want them to be separate processes that will not have any dependency on each other (The ping example was only an example) – Tomer Jun 29 '21 at 10:59

1 Answers1

1

Posting this community wiki answer to give more of a baseline approach with some potential solutions rather than a definitive one.

Feel free to edit and expand.


Addressing the question from the title:

Kubernetes creation of multiple deployment with one deployment file

You can't create a single Deployment that each replica would be different from each other. The Deployment creates sets of identical Pods:

What is a Deployment?

Deployments represent a set of multiple, identical Pods with no unique identities. A Deployment runs multiple replicas of your application and automatically replaces any instances that fail or become unresponsive. In this way, Deployments help ensure that one or more instances of your application are available to serve user requests. Deployments are managed by the Kubernetes Deployment controller.

-- Cloud.google.com: Kubernetes Engine: Docs: Concepts: Deployment: What is a Deployment

Some of the ways that you could achieve the setup that you've described:

  • As pointed by @David Maze:

Yes, it is possible, by putting multiple complete Deployment specs in the same file. For the example you give, though, do these need to be separate processes, or can you achieve the same thing with a single Deployment worker and a job queue (like RabbitMQ) that pushes out the URLs?


Example:

As I said previously, you can use Helm to template the workload and spawn different Jobs that would be configured with a different command.

Side notes!

  • Please do not treat this example as a production ready.
  • This example does not acknowledge persistently storing the data after the Job is finished. You would need to examine available solutions and choose the one that fits your needs the most.

Assuming that basic Helm template is created and it's having modified files:

  • values.yaml
jobs:
  - name: job1
    command: ['"perl",  "-Mbignum=bpi", "-wle", "print bpi(100)"']
  - name: job2
    command: ['"perl",  "-Mbignum=bpi", "-wle", "print bpi(200)"']
  • templates/job.yaml
{{- range $jobs := .Values.jobs }}
apiVersion: batch/v1
kind: Job
metadata:
  name: {{ $jobs.name }}
spec:
  template:
    spec:
      containers:
      - name: {{ $jobs.name }}
        image: perl
        command: {{ $jobs.command}} 
      restartPolicy: Never
  backoffLimit: 4
---
{{- end }}

By above example you will create 2 Jobs that will calculate pi to its 100 or 200 decimal number. You can modify this example to support the workload that you are intending to run.

  • $ kubectl get pods
NAME         READY   STATUS      RESTARTS   AGE
job1-sgr86   0/1     Completed   0          3h36m
job2-4jxh5   0/1     Completed   0          3h36m
  • $ echo "one:"; kubectl logs job1-sgr86; echo "two:"; kubectl logs job2-4jxh5
one:
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068
two:
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303820

Additional resources:

Dawid Kruk
  • 8,982
  • 2
  • 22
  • 45