k8s中subpath掛載單個文件報錯處理


1.報錯環境

  此環境是由於k8s中掛載單個配置文件導致,需要掛載的目標配置文件為/etc/nginx/conf.d/default.conf。

  以下演示僅分為2步,首先創建configmap,然后在deployment的配置文件中引用掛載這個configamap到pod中容器為nginx的默認文件上。

  首先創建一個configmap

[root@public test]#  kubectl create configmap nginx-php-config --from-file=./nginx.conf -n devcon
#該Nginx配置文件只是一個默認的nginx配置文件
[root@public test]# cat nginx.conf 
server{
    listen 80;
    charset      utf-8;
    proxy_connect_timeout           5;
    proxy_read_timeout             10;
    proxy_send_timeout             15;
    fastcgi_connect_timeout         5;
    fastcgi_read_timeout           10;
    fastcgi_send_timeout           15;
    fastcgi_buffers            2 256k;
    fastcgi_buffer_size          128k;
    fastcgi_busy_buffers_size    256k;
    fastcgi_temp_file_write_size 256k;

    location / {
        rewrite  ^/(.*)$  /index.php?s=/$1  last;
    }

    location ~ [^/]\.php(/|$) {
        root /usr/share/nginx/html/public;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

  查看deployment.yaml文件,pod里面有2個容器,nignx和php。主要是掛載到nginx的默認配置文件。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: gcc-21ldj-backend
  namespace: dev
spec:
  replicas: 1
  selector:
    matchLabels:
      app: gcc-21ldj-backend
  template:
    metadata:
      labels:
        app: gcc-21ldj-backend
    spec:
      containers:
      - image: denadocker.tencentcloudcr.com/h5-web/gcc-21ldj-backend:develop-7
        lifecycle:
          postStart:
            exec:
              command:
              - /bin/sh
              - -c
              - cp -r /app/. /usr/share/nginx/html
        name: php-fpm
        ports:
        - containerPort: 9000
        resources:
          limits:
            cpu: 500m
            memory: 500Mi
          requests:
            cpu: 100m
            memory: 100Mi
        volumeMounts:
        - mountPath: /usr/share/nginx/html
          name: nginx-www
      - image: nginx:1.19.10-alpine
        name: nginx
        ports:
        - containerPort: 80
        resources:
          limits:
            cpu: 500m
            memory: 500Mi
          requests:
            cpu: 100m
            memory: 100Mi
        volumeMounts:
        - mountPath: /usr/share/nginx/html
          name: nginx-www
        - mountPath: /etc/nginx/conf.d/default.conf
          name: nginx-php-config
         #這里就是掛載單獨配置文件的配置
          subPath: default.conf
      imagePullSecrets:
      - name: tcr-h5web-dev
      volumes:
      - emptyDir: {}
        name: nginx-www
      - configMap:
          name: nginx-php-config
        name: nginx-php-config

  在volumeMouonts.mountPath.subPath中指定掛載的配置文件,(官方指定掛載單個文件固定方式)。一切都沒有問題,但是在部署的時候,nginx的一直無法啟動,提示如下錯誤。

  報錯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,並且pod只有一個容器是啟動狀態。

 

2.報錯處理

  經過各種問題排查,使用過其它鏡像,k8s版本等等其它方式,都沒有解決。最后仔細看了一下掛載的文件路徑/etc/nginx/conf.d/default.conf,最后核對發現,創建configmap.yaml時,文件名是nginx.conf,會不會是這個問題?將nginx.conf改名為default.conf之后果然解決。

  首先看一下使用文件名nginx.conf創建configmap后的k8s輸出。

[root@public test]#  kubectl create configmap nginx-php-config --from-file=./nginx.conf -n devcon
[root@public test]# kubectl get configmap nginx-php-config -n dev -o yaml
apiVersion: v1
data:
  #配置文件名為nginx.conf
  nginx.conf: |+
    server{
        listen 80;
        charset      utf-8;
        proxy_connect_timeout           5;
        proxy_read_timeout             10;
        proxy_send_timeout             15;
        fastcgi_connect_timeout         5;
        fastcgi_read_timeout           10;
        fastcgi_send_timeout           15;
        fastcgi_buffers            2 256k;
        fastcgi_buffer_size          128k;
       ………
#這里只展示一些重要配置

  可以看到,配置文件名是nginx.conf.

  現在將配置文件改名然后在創建configmap

[root@public test]# kubectl delete configmap nginx-php-config -n dev
[root@public test]#kubectl create configmap nginx-php-config --from-file=./default.conf -n devcon
[root@public test]# kubectl get configmap nginx-php-config -n dev -o yaml
apiVersion: v1
data:
  #配置文件名改為default.conf
  default.conf: |+
    server{
        listen 80;
        charset      utf-8;
        proxy_connect_timeout           5;
        proxy_read_timeout             10;
        proxy_send_timeout             15;
        fastcgi_connect_timeout         5;
        fastcgi_read_timeout           10;
        fastcgi_send_timeout           15;
        fastcgi_buffers            2 256k;
        fastcgi_buffer_size          128k;
       ………
#這里只展示一些重要配置

  因為是subpath,不會更新configmap(后面會說明),所以需要重新deployment發布后查看pod是否正常。

 

3.總結

  1.若需要掛載單個文件,configmap的文件名需要與目標掛載名保持一致

  2.容器以subPath卷掛載方式使用ConfigMap時,將無法接收ConfigMap的更新。需要重新發布。

  3.不建議在線上環境使用單獨掛載文件方式


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM