K8S中的mountPath和subPath使用詳解
1 mountPath
mountPath是容器內(nèi)部文件系統(tǒng)的掛載點(diǎn),它定義了容器內(nèi)部將外部存儲(chǔ)卷(如 PersistentVolume、ConfigMap、Secret 等)掛載到哪個(gè)路徑下。通過(guò) mountPath,容器可以訪問(wèn)這些掛載的數(shù)據(jù)或配置。
2 subPath
subPath 是 mountPath 下的子路徑,它允許容器將掛載的數(shù)據(jù)卷中的特定文件或目錄掛載到容器中的指定路徑下。這樣可以實(shí)現(xiàn)更加精細(xì)的文件系統(tǒng)級(jí)別的訪問(wèn)控制。
3 mountPath使用場(chǎng)景
比如我需要?jiǎng)?chuàng)建一個(gè)nginx deployment,需要將自定義的nginx.conf配置文件獨(dú)立出來(lái),作為一個(gè)configmap來(lái)掛載到pod中。
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-config
data:
my-nginx.conf: |
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- name: nginx-config-volume
mountPath: /etc/nginx/conf.d
volumes:
- name: nginx-config-volume
configMap:
name: nginx-config
部署完成后,你可以進(jìn)入pod,可以看到如下文件,這就是通過(guò)configmap掛載到容器中。
kubectl exec -it nginx-deployment-5b4699b7dd-fh4qc -- /bin/sh # cd /etc/nginx/conf.d # ls my-nginx.conf
4 subPath使用場(chǎng)景
如果我想直接通過(guò)configmap定義/etc/nginx/nginx.conf,這時(shí)候如果還是只使用mountPath,就會(huì)有問(wèn)題了。
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-config
data:
nginx.conf: |
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- name: nginx-config-volume
mountPath: /etc/nginx
volumes:
- name: nginx-config-volume
configMap:
name: nginx-config
創(chuàng)建容器后,服務(wù)無(wú)法起來(lái),會(huì)報(bào)錯(cuò),因?yàn)榇藭r(shí)容器中的/etc/nginx/目錄會(huì)被我們掛載的configmap給覆蓋,所以原先/etc/nginx/目錄下的文件都無(wú)法被pod訪問(wèn),也就報(bào)錯(cuò)了。
2024/03/25 06:56:58 [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
那如果我將volumeMount改為如下配置呢,
- name: nginx
image: nginx
volumeMounts:
- name: nginx-config-volume
mountPath: /etc/nginx/nginx.conf
此時(shí)原來(lái)/etc/nginx/目錄下的文件都不會(huì)受影響,但是依舊會(huì)報(bào)錯(cuò),連容器都無(wú)法創(chuàng)建,
Error: failed to create containerd task: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: error mounting "/var/lib/kubelet/pods/6755a0be-2f05-4edb-813b-ece2dcc2e8f1/volumes/kubernetes.io~configmap/nginx-config-volume" to rootfs at "/etc/nginx/nginx.conf": mount /var/lib/kubelet/pods/6755a0be-2f05-4edb-813b-ece2dcc2e8f1/volumes/kubernetes.io~configmap/nginx-config-volume:/etc/nginx/nginx.conf (via /proc/self/fd/6), flags: 0x5001: not a directory: unknown
這是因?yàn)榇藭r(shí)容器中/etc/nginx/nginx.conf這個(gè)路徑已經(jīng)是存在的,鏡像中默認(rèn)的文件,沒(méi)法作為mountpath使用。
當(dāng)然,你可以將nginx.conf換成其他名字,比如mynginx.conf,
- name: nginx
image: nginx
volumeMounts:
- name: nginx-config-volume
mountPath: /etc/nginx/mynginx.conf
這樣就不會(huì)報(bào)錯(cuò),但是這樣的效果是,容器中會(huì)創(chuàng)建一個(gè)目錄/etc/nginx/mynginx.conf/,這個(gè)目錄下有個(gè)文件nginx.conf,與最開(kāi)始我們?cè)?etc/nginx/conf.d/定義my-nginx.conf一樣,但這并不是我們所要的。
kubectl exec -it nginx-deployment-6bf5f55df8-f452d -- /bin/sh # cd /etc/nginx/mynginx.conf/ # ls nginx.conf
這個(gè)時(shí)候,我們就需要使用subPath了,將volumeMount做如下修改,
- name: nginx
image: nginx
volumeMounts:
- name: nginx-config-volume
mountPath: /etc/nginx/nginx.conf
subPath: nginx.conf
這時(shí)服務(wù)就按照我們預(yù)期啟動(dòng)了,容器內(nèi)文件如下,而且nginx.conf的內(nèi)容就是我們configmap中定義的配置,
kubectl exec -it nginx-deployment-bb7d454c6-75bwz -- /bin/sh # cd /etc/nginx/ # ls conf.d fastcgi_params mime.types modules nginx.conf scgi_params uwsgi_params
類似的場(chǎng)景,比如需要在/etc/nginx/conf.d/為不同的host定義不同的配置,我們就可以創(chuàng)建多個(gè)configmap,配合使用subPath來(lái)掛載到同一個(gè)目錄下,
- name: nginx
image: nginx
volumeMounts:
- name: nginx-config-volume
mountPath: /etc/nginx/conf.d/nginx.conf
subPath: nginx.conf
- name: my-nginx-config-volume
mountPath: /etc/nginx/conf.d/my-nginx.conf
subPath: my-nginx.conf
參考文檔:
- https://stackoverflow.com/questions/65399714/what-is-the-difference-between-subpath-and-mountpath-in-kubernetes
總結(jié)
到此這篇關(guān)于K8S中的mountPath和subPath使用詳解的文章就介紹到這了,更多相關(guān)K8S的mountPath和subPath內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
k8s部署問(wèn)題解決方案(節(jié)點(diǎn)狀態(tài)為 NotReady)
kubectl get nodes顯示節(jié)點(diǎn)NotReady,因kube-flannel鏡像拉取失敗,手動(dòng)拉取鏡像并修改名稱版本后,K8s自動(dòng)重試,最終恢復(fù)節(jié)點(diǎn)狀態(tài)為Ready,相關(guān)Pod也變?yōu)镽unning2025-07-07
Kubernetes?權(quán)限管理認(rèn)證鑒權(quán)詳解
這篇文章主要為大家介紹了Kubernetes?權(quán)限管理認(rèn)證鑒權(quán)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
kubernetes認(rèn)證鑒權(quán)內(nèi)容淺析
這篇文章主要為大家介紹了kubernetes認(rèn)證鑒權(quán)內(nèi)容淺析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Kubernetes調(diào)度機(jī)制與策略實(shí)驗(yàn)詳解
本文通過(guò)實(shí)際操作,詳細(xì)介紹了Kubernetes調(diào)度機(jī)制的核心原理和常用調(diào)度策略的配置方法,包括節(jié)點(diǎn)選擇、親和性與反親和性、污點(diǎn)與容忍等,實(shí)驗(yàn)環(huán)境為Kubernetes集群,并通過(guò)一系列步驟驗(yàn)證了這些調(diào)度策略的實(shí)際應(yīng)用2025-12-12

