Reputation: 420
I'm bit new to kubernetes, I know we can create multiple deployments using same template. I have already gone through this. But my requirement is slight different. I have 30 deployment files wherein only two parameters that's deployment name
and python script1.py
keeps on updating for all deployments. Below are sample deployment files
deployment1.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: deploy1 <-- Will be updated every time for all deployments
name: deploy1 <-- Will be updated every time for all deployments
spec:
replicas: 3
selector:
matchLabels:
app: deploy1
strategy:
rollingUpdate:
maxSurge: 2
maxUnavailable: 0
type: RollingUpdate
template:
metadata:
labels:
app: deploy1
spec:
containers:
- name: web
image: nginx
command: ["/bin/sh"]
args:
- -c
- >-
python script1.py <-- Will be updated every time for all deployments
deployment2.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: deploy2 <-- Will be updated every time for all deployments
name: deploy2 <-- Will be updated every time for all deployments
spec:
replicas: 3
selector:
matchLabels:
app: deploy2
strategy:
rollingUpdate:
maxSurge: 2
maxUnavailable: 0
type: RollingUpdate
template:
metadata:
labels:
app: deploy2
spec:
containers:
- name: web
image: nginx
command: ["/bin/sh"]
args:
- -c
- >-
python script2.py <-- Will be updated every time for all deployments
I want to know how can I convert this into single template so that multiple deployments can be deployed into the cluster. Eventually I want to integrate this into cloud build as a part of my CI/CD.
Any help would be appreciated here.
Update 1 :
@Moritz Schmitz v. Hülst I have updated my code to include below files in my helm chart.
here is my values.yaml
deployments:
- image: nginx
- name: deploy1
script: script1.py
- name: deploy2
script: script2.py
template/deployment.yaml
{{- range .Values.deployments }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: {{ .name }}
name: {{ .name }}
spec:
replicas: 3
selector:
matchLabels:
app: {{ .name }}
strategy:
rollingUpdate:
maxSurge: 2
maxUnavailable: 0
type: RollingUpdate
template:
metadata:
labels:
app: {{ .name }}
spec:
containers:
- name: web
image: {{ .image }}
ports:
- containerPort: 80
{{- end }}
template/service.yaml
{{- range .Values.deployments }}
apiVersion: v1
kind: Service
metadata:
name: {{ .name }}
spec:
type: ClusterIP
ports:
- port: 80
targetPort: 80
selector:
app: {{ .name }}
{{- end }}
I get below error while running helm install demo-nginx demo-hello/
,
Error: INSTALLATION FAILED: unable to build kubernetes objects from release manifest: error validating "": error validating data: [unknown object type "nil" in Deployment.metadata.labels.app, unknown object type "nil" in Deployment.spec.selector.matchLabels.app, unknown object type "nil" in Deployment.spec.template.metadata.labels.app]
Upvotes: 0
Views: 996
Reputation: 3652
deployment.yaml:
{{- range .Values.deployments }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: {{ .name }}
name: {{ .name }}
spec:
replicas: 3
selector:
matchLabels:
app: {{ .name }}
strategy:
rollingUpdate:
maxSurge: 2
maxUnavailable: 0
type: RollingUpdate
template:
metadata:
labels:
app: {{ .name }}
spec:
containers:
- name: web
image: nginx
command: ["/bin/sh"]
args:
- -c
- >-
python {{ .script }}
{{- end }}
values.yaml:
deployments:
- name: deploy1
script: script1.py
- name: deploy2
script: script2.py
Upvotes: 1