According to the fact that Nginx doesn't support variables in the config file, the file should be adjusted before it's applied to the Nginx container.
Kustomize
vars allowed only in particular places and ConfigMap.data
is not one of them at this moment.
The error tells you that PHP_FPM
variable is defined in kustomization.yaml
but never used for replacement. Link1 Link2
Nevertheless, the problem could be solved by adjusting nginx.conf
content using the Init Container
and then mount it to the Main container
.
Example:
kustomization.yaml:
resources:
- cm-init-pod.yaml
- svc.yaml
patches:
- patch.yaml
configMapGenerator:
- name: mymap
files:
- nginx.conf
vars:
- name: PHP_FPM
objref:
kind: Service
name: app-service
apiVersion: v1
fieldref:
fieldpath: metadata.name
cm-init-pod.yaml:
apiVersion: v1
kind: Pod
metadata:
name: cm-vol-pod
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
nginx.conf:
blalb-a
bla-bla
backend ##PHP_FPM##
bl-abla
bla-bla
svc.yaml
apiVersion: v1
kind: Service
metadata:
name: app-service
spec:
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx
patch.yaml
apiVersion: v1
kind: Pod
metadata:
name: cm-vol-pod
spec:
containers:
- name: nginx
image: nginx
command: [ "bash", "-c", "nginx -g \"daemon off;\"" ]
volumeMounts:
- name: ed
# this is not a real nginx.conf so I've put it next to the original one
mountPath: /etc/nginx/nginx.conf1
subPath: nginx.conf
initContainers:
- name: init
image: nginx
command: [ "/bin/sh", "-c", "cp /mnt/cm/nginx.conf /mnt/ed/nginx.conf && sed -i 's/##PHP_FPM##/$(PHP_FPM)/' /mnt/ed/nginx.conf" ]
volumeMounts:
- name: cm
mountPath: /mnt/cm
- name: ed
mountPath: /mnt/ed
volumes:
- name: cm
configMap:
name: mymap
- name: ed
emptyDir: {}
Results:
$ kustomize build | kubectl apply -f -
# or kubectl apply -k ./
configmap/mymap-k2hbfmf776 created
service/app-service created
pod/cm-vol-pod created
$ kubectl exec -it cm-vol-pod -- bash
root@cm-vol-pod:/# cat /etc/nginx/nginx.conf1
blalb-a
bla-bla
backend app-service
bl-abla
bla-bla
Let's clean up everything
$ kustomize build | kubectl delete -f -
# or
$ kubectl delete -k ./
configmap "mymap-k2hbfmf776" deleted
service "app-service" deleted
pod "cm-vol-pod" deleted