首页 > 解决方案 > 无法修复在 kubernetes 中挂载 nginx 配置文件的错误

问题描述

我正在尝试使用 nginx 反向代理并使用简单的 default.conf。这是我的 docker-compose 文件:

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:

如果我执行 docker-compose up,一切正常,但是当我尝试使用 kubernetes 部署它时,我在 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

我应该如何在 Kubernetes 中挂载卷?这是我的 nginx 配置,它放在 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/;
  }
}

标签: kubernetesdocker-composenginx-reverse-proxy

解决方案


在 Kubernetes 中,通常您将这样的配置数据存储在ConfigMap中。看起来Kompose 不会自动为您创建 ConfigMap,因此您需要手动创建它。

我建议将 Docker Compose YAML 文件转换为 Kubernetes YAML 文件

kompose convert

这里最简单的做法是从现有的配置文件创建一个 ConfigMap。(为了长期使用,我会将它打包到一个 YAML 文件中,与 Kompose 创建的其他文件相邻。)

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

在生成的nginx-deployment.yaml文件中,您正在寻找两件事,一是使 ConfigMap 在 pod 中可用,一是将生成的存储挂载到容器中。

# 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

然后,当您运行kubectl apply -f .将这些对象安装到集群中时,它将从集群内的 ConfigMap 中读取配置,而不依赖于任何特定于主机的路径(Kompose 无论如何都不会生成)。


推荐阅读