skip to Main Content

I’m trying to create a Nginx server using Kubernetes and the official docker image. Unfortunately, when I’m trying to mount a custom config file, nothing happen. My container works just fine but with it’s default configuration file. Moreover the another mount on /etc (the lets encrypt folder) doesn’t work too. Nevertheless the certbot mount works just fine…
(If I check inside the container /etc/nginx/nginx.conf it’s not the file I’m trying to mount, and /etc/letsencrypt doesn’t exist)
I link my deployment file just below..
If someone has an idea and want to help it would be delightful !

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 1 
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.19.6
        ports:
        - containerPort: 80
        - containerPort: 443 
        volumeMounts:
        - name: nginx-config
          mountPath: /etc/nginx/nginx.conf
          subPath: nginx.conf
          readOnly: true
        volumeMounts:
        - name: letsencrypt
          mountPath: /etc/letsencrypt
          readOnly: true
        volumeMounts:
        - name: certbot
          mountPath: /var/www/certbot
      volumes:
        - name: nginx-config
          nfs: 
            server: 192.168.2.9
            path: /volume1/nginx/nginx.conf     
        - name: letsencrypt
          nfs: 
            server: 192.168.2.9
            path: /volume1/nginx/letsencrypt
        - name: certbot
          nfs: 
            server: 192.168.2.9
            path: /volume1/nginx/certbot

Edit :
To solve this problem I had to put all my volume mount inside a single volumeMount section and to remove the reference to file in the volume section, like this :

    volumeMounts:
    - name: nginx-config
      mountPath: /etc/nginx/nginx.conf
      subPath: nginx.conf
      readOnly: true
    - name: letsencrypt
      mountPath: /etc/letsencrypt
      readOnly: true

    - name: certbot
      mountPath: /var/www/certbot
  volumes:
    - name: nginx-config
      nfs: 
        server: 192.168.2.9
        path: /volume1/nginx/    
    - name: letsencrypt
      nfs: 
        server: 192.168.2.9
        path: /volume1/nginx/letsencrypt
    - name: certbot
      nfs: 
        server: 192.168.2.9
        path: /volume1/nginx/certbot

2

Answers


  1. This is not working because you are incorrectly referencing your Volumes. There is no need to reference the file name at the Volumes mount stage:

          volumes:
            - name: nginx-config
              nfs: 
                server: 192.168.2.9
                path: /volume1/nginx/nginx.conf  
    

    Once I applied your config my pod was in CreateContainerConfigError status with
    failed to prepare subPath for volumeMount "nginx-config" of container "nginx" error.

    The correct yaml should in this case look like this:

          volumes:
            - name: nginx-config
              nfs: 
                server: 192.168.2.9
                path: /volume1/nginx/  
    

    I have added a line #This is my custom config just to know that my custom file was load and there are the results of that:

    ➜ keti nginx-deployment-66c5547c7c-nsfzv  -- cat  /etc/nginx/nginx.conf
    
    #This is my custom config
    
    user  nginx;
    worker_processes  1;
    
    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/nginx.pid;
    
    
    events {
        worker_connections  1024;
    }
    
    .....
    
    Login or Signup to reply.
  2. I’m writing this answer as community wiki for better visibility of OP solution which was placed as an edit to question instead of answer.

    The solution for the problem was combination of OP solution to putt all the volumes mount inside singe volumeMount with my answer to remove the reference to a file in the volume section:

        volumeMounts:
        - name: nginx-config
          mountPath: /etc/nginx/nginx.conf
          subPath: nginx.conf
          readOnly: true
        - name: letsencrypt
          mountPath: /etc/letsencrypt
          readOnly: true
    
        - name: certbot
          mountPath: /var/www/certbot
      volumes:
        - name: nginx-config
          nfs: 
            server: 192.168.2.9
            path: /volume1/nginx/    
        - name: letsencrypt
          nfs: 
            server: 192.168.2.9
            path: /volume1/nginx/letsencrypt
        - name: certbot
          nfs: 
            server: 192.168.2.9
            path: /volume1/nginx/certbot
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search