无法修复在 kubernetes 中安装 nginx 配置文件时的错误

Cannot fix error in mounting nginx config file in kubernetes

我正在尝试使用 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/;
  }
}

由于您指出 nginx 配置位于 ./nginxProxy 目录

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

因此,k8s 正在尝试在目录 /home/artemda4/kubernetes-engine-samples/muniverse/home/artemda4/kubernetes-engine-samples/muniverse/nginxProxy:

中查找 nginx 配置 default.conf 文件

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

这是 k8s

的当前(即 ./)目录

我建议将 nginx 配置存储在默认位置,例如:/etc/nginx/conf.d

因此,您的配置将如下所示:

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:

在 Kubernetes 中,通常您将这样的配置数据存储在 ConfigMap. It looks like Kompose won't automatically create a ConfigMap for you 中,因此您需要手动创建它。

我建议 converting the Docker Compose YAML file to Kubernetes YAML files

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无论如何都不会生成)。