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?
With a templating tool like Helm
where you would template the exact specification of your workload and then iterate over it with different values (see the example)
Use the Kubernetes official documentation on work queue topics:
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:
jobs:
- name: job1
command: ['"perl", "-Mbignum=bpi", "-wle", "print bpi(100)"']
- name: job2
command: ['"perl", "-Mbignum=bpi", "-wle", "print bpi(200)"']
{{- 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.
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: