Cannot fix error in mounting nginx config file in kubernetes

9/23/2019

I am trying to use nginx reverse-proxy and use simple default.conf. Here is my docker-compose file:

version: '3'
services:
  authorizationmicroservice:
    image: gcr.io/root-booking-245613/authorizationmicroservice:v1
    container_name: authorizationmicroservice
    restart: always
    labels:
      - "kompose.service.type=LoadBalancer"
    ports:
      - "3002:${PORT:-3002}"
    networks:
      - backend
  musicmicroservice:
    image: gcr.io/root-booking-245613/musicmicroservice:v1
    container_name: musicmicroservice
    restart: always
    ports:
      - "3001:${PORT:-3001}"
    networks:
      - backend
    labels:
      - "kompose.service.type=LoadBalancer"
  nginx:
    image: nginx:latest
    networks:
      - backend
    ports:
      - "8080:${PORT:-8080}"
    volumes:
      - ./nginxProxy:/etc/nginx/conf.d
    depends_on:
      - authorizationmicroservice
      - musicmicroservice
    labels:
      - "kompose.service.type=LoadBalancer"
networks:
    backend:

Everything works fine if I do docker-compose up, but when I try to deploy it with kubernetes, I' ve got the following error log in nginx pod:

Warning  Failed     
34m
                   kubelet, gke-hello-cluster-default-pool-8c57f061-7hd8 

Error: failed to start container "nginx": Error response from daemon:
 OCI runtime create failed: container_linux.go:345: starting container process caused "process_linux.go:424: container init caused \"rootfs_linux.go:58: mounting \\\"/home/artemda4/kubernetes-engine-samples/muniverse/home/artemda4/kubernetes-engine-samples/muniverse/nginxProxy/default.conf\\\" to rootfs \\\"/var/lib/docker/overlay2/751acda10027acdcca21d16df3be48197170c04dd3520cd7fa8aeb083b5b6bc1/merged\\\" at \\\"/var/lib/docker/overlay2/751acda10027acdcca21d16df3be48197170c04dd3520cd7fa8aeb083b5b6bc1/merged/etc/nginx/conf.d/default.conf\\\" caused \\\"not a directory\\\"\"": unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type

How should i mount volumes in Kubernetes? This is my nginx config,it is placed in nginxProxy

server {
  listen 8080;
  client_max_body_size 50m;
  fastcgi_send_timeout 600;
  fastcgi_read_timeout 600;
  proxy_connect_timeout 600;
  proxy_send_timeout 600;
  proxy_read_timeout 600;
  send_timeout 600;
  root /srv/www/static;
  location /api/authorization {
    proxy_pass http://authorizationmicroservice:3002;
  }
  location /api/music {
    proxy_pass http://musicmicroservice:3001;
  }
  location /api/playlist {
    proxy_pass http://musicmicroservice:3001;
  }
  #this where socket io will be handling the request
  location /socket.io {
    proxy_pass http://musicmicroservice:3001/socket.io/;
  }
}
-- Артем Дачевский
docker-compose
kubernetes
nginx-reverse-proxy

2 Answers

9/23/2019

Since you've pointed that nginx config located in ./nginxProxy directory

 volumes:
      - ./nginxProxy:/etc/nginx/conf.d

So, k8s is trying to find nginx config default.conf file in the directory /home/artemda4/kubernetes-engine-samples/muniverse/home/artemda4/kubernetes-engine-samples/muniverse/nginxProxy:

mounting \\\"/home/artemda4/kubernetes-engine-samples/muniverse/home/artemda4/kubernetes-engine-samples/muniverse/nginxProxy/default.conf\\\" to rootfs

It's a current (i.e. ./) directory for k8s

I'd suggest to store nginx config in it's default place, e.g.: /etc/nginx/conf.d

So, your config will look like:

version: '3'
services:
  authorizationmicroservice:
    image: gcr.io/root-booking-245613/authorizationmicroservice:v1
    container_name: authorizationmicroservice
    restart: always
    labels:
      - "kompose.service.type=LoadBalancer"
    ports:
      - "3002:${PORT:-3002}"
    networks:
      - backend
  musicmicroservice:
    image: gcr.io/root-booking-245613/musicmicroservice:v1
    container_name: musicmicroservice
    restart: always
    ports:
      - "3001:${PORT:-3001}"
    networks:
      - backend
    labels:
      - "kompose.service.type=LoadBalancer"
  nginx:
    image: nginx:latest
    networks:
      - backend
    ports:
      - "8080:${PORT:-8080}"
    volumes:
      - /etc/nginx/conf.d:/etc/nginx/conf.d
    depends_on:
      - authorizationmicroservice
      - musicmicroservice
    labels:
      - "kompose.service.type=LoadBalancer"
networks:
    backend:
-- Yasen
Source: StackOverflow

9/23/2019

In Kubernetes, typically you store configuration data like this in a ConfigMap. It looks like Kompose won't automatically create a ConfigMap for you so you need to create it by hand.

I'd suggest converting the Docker Compose YAML file to Kubernetes YAML files

kompose convert

The easiest thing to do here is to create a ConfigMap from your existing config file. (For longer-term use, I'd package it into a YAML file next to the other files Kompose created.)

kubectl create configmap nginx --from-file=./nginxProxy/

In the generated nginx-deployment.yaml file you are looking for two things, one to make the ConfigMap available in the pod and one to mount the resulting storage into the container.

# Inside the deployment template
spec:
  volumes:
    - name: nginx-config
      configMap:
        name: nginx # matches `kubectl create` command
  containers:
    - name: nginx
      # other standard container settings
      volumeMounts:
        - name: nginx-config # matches `volumes:` above
          mountPath: /etc/nginx/conf.d

Then when you run kubectl apply -f . to install these objects into the cluster, it will read the configuration from the in-cluster ConfigMap, and not depend on any host-specific paths (which Kompose isn't generating anyways).

-- David Maze
Source: StackOverflow