patternyamlkubernetesMinor
Kubernetes configuration with yaml anchors
Viewed 0 times
withkubernetesanchorsconfigurationyaml
Problem
I have a
But it seems like
Is it possible to use YAML anchors or is there another preferred approach of how to reuse repeating blocks for k8s configuration?
deployment.yaml file and want to reuse the environment for all my deployments like this:apiVersion: apps/v1
kind: Deployment
metadata:
name: beat
spec:
selector:
matchLabels:
app: beat
template:
metadata:
labels:
app: beat
spec:
containers:
- name: beat
image: myimage
command: ["celery", "-A", "wsgi.celery", "beat"]
env: &default
- name: FIRST_ENV
value: my-value
- name: SECOND_ENV
value: another-value
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: flower
spec:
selector:
matchLabels:
app: flower
template:
metadata:
labels:
app: flower
spec:
containers:
- name: flower
image: myimage
command: ["celery", "flower", "-A", "wsgi.celery"]
env: *defaultenvBut it seems like
kubectl apply -f deployment.yaml won't work with YAML anchors.error: error parsing deployment.yaml: error converting YAML to JSON: yaml: unknown anchor 'defaultenv' referencedIs it possible to use YAML anchors or is there another preferred approach of how to reuse repeating blocks for k8s configuration?
Solution
YAML anchors are supported, but only for the same YAML file. You can't create the anchor (
If you want to share ENVs values across multiple Deployments, you can create a ConfigMap with the ENVs and use the
&) on a deployment file and reference (*) the value on another one.If you want to share ENVs values across multiple Deployments, you can create a ConfigMap with the ENVs and use the
envFrom spec. Example:apiVersion: v1
kind: ConfigMap
metadata:
name: my-configmap
data:
FIRST_ENV: my-value
SECOND_ENV: another-value
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: beat
spec:
selector:
matchLabels:
app: beat
template:
metadata:
labels:
app: beat
spec:
containers:
- name: beat
image: myimage
command: ["celery", "-A", "wsgi.celery", "beat"]
envFrom:
- configMapRef:
name: my-configmap
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: flower
spec:
selector:
matchLabels:
app: flower
template:
metadata:
labels:
app: flower
spec:
containers:
- name: flower
image: myimage
command: ["celery", "flower", "-A", "wsgi.celery"]
envFrom:
- configMapRef:
name: my-configmapCode Snippets
apiVersion: v1
kind: ConfigMap
metadata:
name: my-configmap
data:
FIRST_ENV: my-value
SECOND_ENV: another-value
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: beat
spec:
selector:
matchLabels:
app: beat
template:
metadata:
labels:
app: beat
spec:
containers:
- name: beat
image: myimage
command: ["celery", "-A", "wsgi.celery", "beat"]
envFrom:
- configMapRef:
name: my-configmap
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: flower
spec:
selector:
matchLabels:
app: flower
template:
metadata:
labels:
app: flower
spec:
containers:
- name: flower
image: myimage
command: ["celery", "flower", "-A", "wsgi.celery"]
envFrom:
- configMapRef:
name: my-configmapContext
StackExchange DevOps Q#12837, answer score: 8
Revisions (0)
No revisions yet.