I am trying to make an nginx deployment and during the container creation, I want to create multiply symbolic links. But for some reason, it doesn't work and the container crashes.
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: tcc
component: nginx
name: tcc-nginx-deployment
namespace: dev2
spec:
replicas: 1
selector:
matchLabels:
app: tcc
component: nginx
template:
metadata:
labels:
app: tcc
component: nginx
spec:
containers:
- image: nginx
name: nginx
command:
- /bin/sh
- -c
- |
ln -s /shared/apps/ /var/www
rm -r /etc/nginx/conf.d
ln -s /shared/nginx-config/ /etc/nginx/conf.d
ports:
- containerPort: 80
protocol: TCP
volumeMounts:
- mountPath: /shared
name: efs-pvc
volumes:
- name: efs-pvc
persistentVolumeClaim:
claimName: tcc-efs-storage-claim
CodePudding user response:
That's because you are asking container just to create symbolic links. So once those
symbolic links are created container is getting exited.To avoid that add
"nginx" "-g" "daemon off;"
as following:
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: tcc
component: nginx
name: tcc-nginx-deployment
spec:
replicas: 1
selector:
matchLabels:
app: tcc
component: nginx
template:
metadata:
labels:
app: tcc
component: nginx
spec:
containers:
- image: nginx
name: nginx
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- |
ln -s /shared/apps/ /var/www
rm -r /etc/nginx/conf.d
ln -s /shared/nginx-config/ /etc/nginx/conf.d
"nginx" "-g" "daemon off;" ; # after creating above symbolic links it will start nginx daemon
ports:
- containerPort: 80
protocol: TCP
volumeMounts:
- mountPath: /shared
name: efs-pvc
volumes:
- name: efs-pvc
persistentVolumeClaim:
claimName: tcc-efs-storage-claim
CodePudding user response:
The container is not running, because after the command
block is executed, container is exiting, which is expected behaviour.
Instead of playing with symbolic links in command
in yaml template (which is not the best practice solution), why just don't use solution builtin Kubernetes and do not use command
block at all?
You should use subPath
which is designed to share directories from one volume for multiple, different directories on the single pod:
Sometimes, it is useful to share one volume for multiple uses in a single pod. The
volumeMounts.subPath
property specifies a sub-path inside the referenced volume instead of its root.
In your case, the deployment yaml should look like this:
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: tcc
component: nginx
name: tcc-nginx-deployment
namespace: dev2
spec:
replicas: 1
selector:
matchLabels:
app: tcc
component: nginx
template:
metadata:
labels:
app: tcc
component: nginx
spec:
containers:
- image: nginx
name: nginx
ports:
- containerPort: 80
protocol: TCP
volumeMounts:
- mountPath: /shared
name: efs-pvc
- mountPath: /etc/nginx/conf.d
name: efs-pvc
subPath: nginx-config
- mountPath: /var/www
name: efs-pvc
subPath: apps
volumes:
- name: efs-pvc
persistentVolumeClaim:
claimName: tcc-efs-storage-claim
Also if you want to mount only config files for NGINX, you may use ConfigMap instead of volume - check this answer for more information.