Server Fault Asked by mpen on December 29, 2020
In my kustomization.yaml
I have:
configMapGenerator:
- name: nginx-config
files:
- nginx.conf
vars:
- name: PHP_FPM
objref:
kind: Service
name: app-service
apiVersion: v1
fieldref:
fieldpath: metadata.name
And then in nginx.conf
I have:
location = /index.php {
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass $(PHP_FPM):9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /app/index.php;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
When I run kustomize
the var isn’t replaced:
❯ kustomize build kubernetes/overlays/staging | grep fastcgi_pass
2020/06/07 18:25:05 well-defined vars that were never replaced: PHP_FPM
fastcgi_pass $(PHP_FPM):9000;
How can I get my service name into nginx.conf
?
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
.
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: {}
$ 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
$ kustomize build | kubectl delete -f -
# or
$ kubectl delete -k ./
configmap "mymap-k2hbfmf776" deleted
service "app-service" deleted
pod "cm-vol-pod" deleted
Answered by VASャ on December 29, 2020
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP