K8S中的mountPath和subPath使用詳解
1 mountPath
mountPath是容器內(nèi)部文件系統(tǒng)的掛載點(diǎn),它定義了容器內(nèi)部將外部存儲(chǔ)卷(如 PersistentVolume、ConfigMap、Secret 等)掛載到哪個(gè)路徑下。通過 mountPath,容器可以訪問這些掛載的數(shù)據(jù)或配置。
2 subPath
subPath 是 mountPath 下的子路徑,它允許容器將掛載的數(shù)據(jù)卷中的特定文件或目錄掛載到容器中的指定路徑下。這樣可以實(shí)現(xiàn)更加精細(xì)的文件系統(tǒng)級(jí)別的訪問控制。
3 mountPath使用場景
比如我需要?jiǎng)?chuàng)建一個(gè)nginx deployment,需要將自定義的nginx.conf配置文件獨(dú)立出來,作為一個(gè)configmap來掛載到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,可以看到如下文件,這就是通過configmap掛載到容器中。
kubectl exec -it nginx-deployment-5b4699b7dd-fh4qc -- /bin/sh # cd /etc/nginx/conf.d # ls my-nginx.conf
4 subPath使用場景
如果我想直接通過configmap定義/etc/nginx/nginx.conf,這時(shí)候如果還是只使用mountPath,就會(huì)有問題了。
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ù)無法起來,會(huì)報(bào)錯(cuò),因?yàn)榇藭r(shí)容器中的/etc/nginx/目錄會(huì)被我們掛載的configmap給覆蓋,所以原先/etc/nginx/目錄下的文件都無法被pod訪問,也就報(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í)原來/etc/nginx/目錄下的文件都不會(huì)受影響,但是依舊會(huì)報(bào)錯(cuò),連容器都無法創(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)的文件,沒法作為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,與最開始我們?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
類似的場景,比如需要在/etc/nginx/conf.d/為不同的host定義不同的配置,我們就可以創(chuàng)建多個(gè)configmap,配合使用subPath來掛載到同一個(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)文章
再分享6個(gè)可無限激活阿里云盤邀請(qǐng)碼,不信你不能激活阿里云盤
這篇文章主要分享6個(gè)可無限激活的阿里云盤邀請(qǐng)碼,不信你不能激活阿里云盤,需要的朋友可以參考下2020-11-11
RFO SIG之openEuler AWS AMI 制作詳解
這篇文章主要為大家介紹了RFO SIG之openEuler AWS AMI 制作詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
Google?Kubernetes?Engine?集群實(shí)戰(zhàn)詳解
這篇文章主要為大家介紹了Google?Kubernetes?Engine?集群實(shí)戰(zhàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
K8s實(shí)戰(zhàn)教程之容器和?Pods資源分配問題
這篇文章主要介紹了K8s實(shí)戰(zhàn)教程之容器和?Pods資源分配,本篇文章通過配置集群中運(yùn)行的容器的?CPU?請(qǐng)求和限制,你可以有效利用集群上可用的?CPU?資源,通過將?Pod?CPU?請(qǐng)求保持在較低水平,可以使?Pod?更有機(jī)會(huì)被調(diào)度,需要的朋友可以參考下2022-07-07
Kubernetes中使用臨時(shí)容器進(jìn)行故障排查的方法
在使用Kubernetes時(shí),用戶常常會(huì)遇到一些錯(cuò)誤和迷惑,下面這篇文章主要給大家介紹了關(guān)于Kubernetes中使用臨時(shí)容器進(jìn)行故障排查的相關(guān)資料,需要的朋友可以參考下2022-03-03
Rancher通過界面管理K8s平臺(tái)的圖文步驟詳解
這篇文章主要為大家介紹了Rancher通過界面管理K8s平臺(tái)通過詳細(xì)的圖文進(jìn)行步驟講解,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03

