I'm trying to replace the existing default.conf Nginx
file with my own one using ConfigMap
object but this action leads to "Back-off restarting failed container" error. If I change overwriting directory from conf.d to new nginx.d then the passing my config will be successful. It looks like nginx doesn't allow to rewrite its existing files and directories. Is there any workaround or I made a mistake in my deployment file?
Here are my yaml
files.
Deployment file
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 file
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;
}
}
Here is the Deployment pod logs:
CodePudding user response:
Me too have faced something similar recently. To manage this we can hack the CMD (command) to do the needful.
First create startup script for the nginx process for the nginx container. & this has to be a configmap
for sure.
#!/bin/sh
rm -rf /etc/nginx/conf.d/default.conf
cp /tmp/default.conf /etc/nginx/conf.d/default.conf
nginx -g daemon off;
Then use this script for the nginx container in the deployment file. Sometime like below:
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
command: ['sh','/script/nginx-start']
ports:
- containerPort: 80
volumeMounts:
- name: skadate-software
mountPath: /var/www/html
- name: nginx-config
mountPath: /tmp
subPath: default.conf
- name: nginx-start-script
mountpath: /script
subPath: nginx-start
Here the two important changes are:
- The new default.conf file we are placing in the /tmp path & will be taken care by the custom startup script to copy into the /etc/nginx/conf.d/default.conf path.
- A custom script has been added to replace the default.conf file with new one & then start the nginx process
An extra configmap
for the custom startup script
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-start-script
labels:
app: nginx
data:
nginx-start : |
#!/bin/sh
rm -rf /etc/nginx/conf.d/default.conf
cp /tmp/default.conf /etc/nginx/conf.d/default.conf
nginx -g daemon off;
Hope this would resolve your issue. Please share the results.