最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

K8S中的mountPath和subPath使用詳解

 更新時(shí)間:2024年12月20日 08:31:38   作者:Blue?summer  
這篇文章主要介紹了K8S中的mountPath和subPath使用的相關(guān)資料,通過(guò)示例展示了如何使用mountPath和subPath來(lái)掛載ConfigMap到Pod中,并解決了服務(wù)無(wú)法啟動(dòng)的問(wèn)題,需要的朋友可以參考下

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)

    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)镽unning
    2025-07-07
  • Kubernetes?權(quán)限管理認(rèn)證鑒權(quán)詳解

    Kubernetes?權(quán)限管理認(rèn)證鑒權(quán)詳解

    這篇文章主要為大家介紹了Kubernetes?權(quán)限管理認(rèn)證鑒權(quán)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • K8S安裝及配置教程

    K8S安裝及配置教程

    這篇文章主要介紹了K8S安裝及配置教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2025-03-03
  • kubeadm安裝k8s高可用集群實(shí)踐

    kubeadm安裝k8s高可用集群實(shí)踐

    文章介紹了如何規(guī)劃和部署一個(gè)Kubernetes高可用集群,包括環(huán)境準(zhǔn)備、節(jié)點(diǎn)配置、Docker安裝、Kubernetes組件安裝和配置、高可用組件部署以及私有倉(cāng)庫(kù)Harbor的安裝
    2026-03-03
  • kubernetes認(rèn)證鑒權(quán)內(nèi)容淺析

    kubernetes認(rèn)證鑒權(quán)內(nèi)容淺析

    這篇文章主要為大家介紹了kubernetes認(rèn)證鑒權(quán)內(nèi)容淺析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • k8s無(wú)法刪除pv,pvc的問(wèn)題及解決

    k8s無(wú)法刪除pv,pvc的問(wèn)題及解決

    在Kubernetes中刪除PVC時(shí),若未指定名稱或使用錯(cuò)誤參數(shù),會(huì)報(bào)錯(cuò)"no name was specified",解決方法為:使用`kubectl delete pvc <pvc-name>`明確指定PVC名稱,或通過(guò)`kubectl delete pvc --all`刪除所有PVC,同時(shí)確保正確指定命名空間(-n)
    2025-09-09
  • Nacos-K8s部署全過(guò)程

    Nacos-K8s部署全過(guò)程

    Nacos-K8s項(xiàng)目提供了多種在Kubernetes環(huán)境中部署Nacos的方案,包括快速啟動(dòng)、NFS持久化、Ceph持久化、Helm和Operator部署,每種方式都有其適用的場(chǎng)景和特點(diǎn),如快速啟動(dòng)適用于測(cè)試,NFS和Ceph持久化支持?jǐn)?shù)據(jù)持久化,Helm和Operator提供更高級(jí)的運(yùn)維功能
    2026-02-02
  • k8s目錄和文件掛載到宿主機(jī)的方式

    k8s目錄和文件掛載到宿主機(jī)的方式

    Docker是一種流行的容器化技術(shù),它允許開(kāi)發(fā)人員在不同的環(huán)境中構(gòu)建、打包和運(yùn)行應(yīng)用程序,下面這篇文章主要給大家介紹了關(guān)于k8s目錄和文件掛載到宿主機(jī)的相關(guān)資料,需要的朋友可以參考下
    2024-01-01
  • kubernetes?k8s入門定義一個(gè)Pod

    kubernetes?k8s入門定義一個(gè)Pod

    這篇文章主要為大家介紹了k8s入門定義一個(gè)Pod以及破底的定義內(nèi)容詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多計(jì)步,早日升職加薪
    2022-03-03
  • Kubernetes調(diào)度機(jī)制與策略實(shí)驗(yàn)詳解

    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

最新評(píng)論

弥勒县| 左权县| 渑池县| 巢湖市| 松江区| 罗山县| 吉木萨尔县| 永登县| 武平县| 屯昌县| 民和| 基隆市| 庄河市| 栾川县| 九龙城区| 涪陵区| 湟源县| 北票市| 武宁县| 四子王旗| 莱阳市| 迁西县| 萨迦县| 吉首市| 建昌县| 普安县| 徐水县| 江都市| 汉阴县| 西乌珠穆沁旗| 绥化市| 黄龙县| 皋兰县| 蓝田县| 贡觉县| 军事| 洛宁县| 肥东县| 邳州市| 繁峙县| 天津市|