k8s挂载目录_K8S中挂载目录引发的血案!
在kubernetes中部署前端項目(使用nginx作為服務器)的時候,遇到了一個報錯,報錯信息如下
2019/11/19 02:16:31 [emerg] 1#1: open() "/etc/nginx/mime.types" failed (2: No such file or directory) in /etc/nginx/nginx.conf:14
nginx: [emerg] open() "/etc/nginx/mime.types" failed (2: No such file or directory) in /etc/nginx/nginx.conf:14
提示的意思是沒有找到mine.types文件,也就是說容器內(nèi)/etc/nginx下不存在這個文件。但是這個文件不是nginx提供的嗎?
我趕忙看了下我是如何部署這個容器的。
dockerfile:
FROM nginx:1.17.3
COPY dist /usr/share/nginx/html
deployment.yml:
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: smcp-web
namespace: nginx-test
spec:
selector:
matchLabels:
app: smcp-web
replicas: 1
template:
metadata:
labels:
app: smcp-web
spec:
containers:
- name: smcp-web
image: docker-registry.xxx.com/fe/fe-nginx:no-nginx-conf
ports:
- containerPort: 80
volumeMounts:
- name: nginx-conf
mountPath: /etc/nginx
volumes:
- name: nginx-conf
configMap:
name: nginx-conf
items:
- key: nginx.conf
path: nginx.conf
這樣就會將在configMap中聲明的nginx.conf對應內(nèi)容掛載到/etc/nginx(mountPath)/nginx.conf文件中。
kubectl create configmap nginx-conf --from-file=nginx.conf=./path/to/nginx.conf -n nginx-test
一想到這里我大概知道了原因,是因為我們掛載的是整個/etc/nginx目錄,但是我們這個目錄里面只加了nginx.conf,而容器中/etc/nginx目錄下面還有很多其他的文件,
我們直接掛載了整個目錄進去,其他的文件會隨著目錄的掛載而消失,自然讀取配置就出了問題。
現(xiàn)在問題已經(jīng)知道了,如何解決呢?
要是可以只掛載這個文件就好了,查閱了下文檔,發(fā)現(xiàn)subPath完全可以滿足我們的需求。
subPath的目的是為了在單一Pod中多次使用同一個volume而設計的
所以將spec部分修改如下:
spec:
containers:
- name: smcp-web
image: docker-registry.xxx.com/fe/fe-nginx:no-nginx-conf
ports:
- containerPort: 80
volumeMounts:
- name: nginx-conf
mountPath: /etc/nginx/nginx.conf
subPath: nginx.conf
volumes:
- name: nginx-conf
configMap:
name: nginx-conf
這樣我們就做到了將nginx.conf文件掛載到/etc/nginx目錄下,同時不影響原有目錄中的內(nèi)容。
總結
以上是生活随笔為你收集整理的k8s挂载目录_K8S中挂载目录引发的血案!的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 遗传算法python实现_Python遗
- 下一篇: andpods授权码订单号分享_微信OA