在我的kustomization.yaml
我有:
configMapGenerator:
- name: nginx-config
files:
- nginx.conf
vars:
- name: PHP_FPM
objref:
kind: Service
name: app-service
apiVersion: v1
fieldref:
fieldpath: metadata.name
然后nginx.conf
我有:
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;
}
当我运行kustomize
var 时,它没有被替换:
❯ 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;
我怎样才能将我的服务名称输入nginx.conf
?
答案1
根据 Nginx不支持变量在配置文件中,该文件在应用到Nginx容器之前应该进行调整。
Kustomize
瓦尔斯仅允许特定的地方而ConfigMap.data
目前还不是其中之一。
错误告诉您PHP_FPM
变量已定义kustomization.yaml
但从未用于替换。链接1 链接2
nginx.conf
尽管如此,可以通过使用 调整内容Init Container
然后将其安装到 来解决该问题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
服务端
apiVersion: v1
kind: Service
metadata:
name: app-service
spec:
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx
补丁文件
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