Kubernetes ConfigMap无法覆盖Nginx default.conf文件问题求助
问题描述
尝试通过Kubernetes ConfigMap替换Nginx默认的default.conf文件,操作后容器出现Back-off restarting failed container错误。但如果将挂载目录改为新建的nginx.d就能正常生效,怀疑Nginx不允许覆盖现有文件或目录,想知道可行的解决方法,或是Deployment配置是否存在错误。
相关配置文件
Deployment配置
apiVersion: apps/v1 kind: Deployment metadata: name: skadate-deployment labels: tier: backend spec: replicas: 1 selector: matchLabels: app: skadate tier: backend template: metadata: labels: app: skadate tier: backend spec: volumes: - name: skadate-software emptyDir: {} - name: nginx-config configMap: name: skadate-test-nginx-config items: - key: default.conf path: default.conf containers: - name: php image: psiloscop/php-hello-world:latest ports: - containerPort: 81 name: http protocol: TCP volumeMounts: - name: skadate-software mountPath: /var/www/html lifecycle: postStart: exec: command: [ "/bin/sh", "-c", "cp -r /app/. /var/www/html" ] - name: nginx image: nginx:stable ports: - containerPort: 80 volumeMounts: - name: skadate-software mountPath: /var/www/html - name: nginx-config mountPath: /etc/nginx/conf.d/default.conf subPath: default.conf
ConfigMap配置
apiVersion: v1 kind: ConfigMap metadata: name: skadate-test-nginx-config labels: tier: backend data: default.conf : | server { listen 80; location / { root /var/www/html index index.html index.htm; try_files $uri $uri/ /index.html; } }
问题分析
从Pod日志可以看到,Nginx启动失败的核心原因是配置文件语法错误:root /var/www/html这一行末尾缺少了必需的分号;,导致Nginx无法解析配置,进而触发容器重启。
你之前误以为是Nginx禁止覆盖现有文件,实际上是因为挂载到新建目录时,Nginx不会加载原有错误的配置,所以能正常启动;而挂载替换default.conf时,错误的配置直接导致Nginx启动失败。另外,你的Deployment中使用subPath挂载单个文件的方式是完全正确的,Kubernetes支持这种方式覆盖容器内已有的文件。
解决方法
修正ConfigMap的配置语法
在root /var/www/html行末尾添加分号,修正后的ConfigMap如下:apiVersion: v1 kind: ConfigMap metadata: name: skadate-test-nginx-config labels: tier: backend data: default.conf : | server { listen 80; location / { root /var/www/html; index index.html index.htm; try_files $uri $uri/ /index.html; } }更新配置并重启Deployment
执行以下命令应用修正后的配置:kubectl apply -f skadate-test-nginx-config.yaml kubectl rollout restart deployment skadate-deployment
内容的提问来源于stack exchange,提问作者Sergey Filipovich
相关产品推荐
相关产品推荐

