欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

K8S中的mountPath和subPath使用詳解

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

1 mountPath

mountPath是容器內(nèi)部文件系統(tǒng)的掛載點(diǎn),它定義了容器內(nèi)部將外部存儲卷(如 PersistentVolume、ConfigMap、Secret 等)掛載到哪個路徑下。通過 mountPath,容器可以訪問這些掛載的數(shù)據(jù)或配置。

2 subPath

subPath 是 mountPath 下的子路徑,它允許容器將掛載的數(shù)據(jù)卷中的特定文件或目錄掛載到容器中的指定路徑下。這樣可以實(shí)現(xiàn)更加精細(xì)的文件系統(tǒng)級別的訪問控制。

3 mountPath使用場景

比如我需要創(chuàng)建一個nginx deployment,需要將自定義的nginx.conf配置文件獨(dú)立出來,作為一個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,這時候如果還是只使用mountPath,就會有問題了。

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ù)無法起來,會報(bào)錯,因?yàn)榇藭r容器中的/etc/nginx/目錄會被我們掛載的configmap給覆蓋,所以原先/etc/nginx/目錄下的文件都無法被pod訪問,也就報(bào)錯了。

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

此時原來/etc/nginx/目錄下的文件都不會受影響,但是依舊會報(bào)錯,連容器都無法創(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容器中/etc/nginx/nginx.conf這個路徑已經(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

這樣就不會報(bào)錯,但是這樣的效果是,容器中會創(chuàng)建一個目錄/etc/nginx/mynginx.conf/,這個目錄下有個文件nginx.conf,與最開始我們在/etc/nginx/conf.d/定義my-nginx.conf一樣,但這并不是我們所要的。

kubectl exec -it nginx-deployment-6bf5f55df8-f452d -- /bin/sh
# cd /etc/nginx/mynginx.conf/
# ls
nginx.conf

這個時候,我們就需要使用subPath了,將volumeMount做如下修改,

      - name: nginx
        image: nginx
        volumeMounts:
        - name: nginx-config-volume
          mountPath: /etc/nginx/nginx.conf
          subPath: nginx.conf

這時服務(wù)就按照我們預(yù)期啟動了,容器內(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)建多個configmap,配合使用subPath來掛載到同一個目錄下,

      - 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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:

相關(guān)文章

  • kubernetes k8s常用問題排查方法

    kubernetes k8s常用問題排查方法

    新手學(xué)習(xí)K8s最大的難度感覺是在起步動手實(shí)踐的時候,Pod沒有正常啟動起來,或者運(yùn)行了一段時間Pod自己崩潰了。是什么問題導(dǎo)致了它沒運(yùn)行起來,或是什么因素導(dǎo)致了它的崩潰,本文來學(xué)習(xí)總結(jié)幾個使用 K8s時常見的錯誤現(xiàn)象以及排查這些現(xiàn)象背后問題的方法
    2022-06-06
  • podman容器工具的具體使用

    podman容器工具的具體使用

    本文主要介紹了podman容器工具的具體使用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • 再分享6個可無限激活阿里云盤邀請碼,不信你不能激活阿里云盤

    再分享6個可無限激活阿里云盤邀請碼,不信你不能激活阿里云盤

    這篇文章主要分享6個可無限激活的阿里云盤邀請碼,不信你不能激活阿里云盤,需要的朋友可以參考下
    2020-11-11
  • RFO SIG之openEuler AWS AMI 制作詳解

    RFO SIG之openEuler AWS AMI 制作詳解

    這篇文章主要為大家介紹了RFO SIG之openEuler AWS AMI 制作詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • 如何在Centos中搭建 K8s 1.23 集群

    如何在Centos中搭建 K8s 1.23 集群

    文章詳細(xì)介紹了在CentOS上搭建Kubernetes 1.23集群的步驟,包括準(zhǔn)備環(huán)境、安裝Kubernetes軟件包、上傳離線鏡像、初始化集群、添加節(jié)點(diǎn)、安裝網(wǎng)絡(luò)插件以及測試驗(yàn)證,感興趣的朋友一起看看吧
    2025-03-03
  • Google?Kubernetes?Engine?集群實(shí)戰(zhàn)詳解

    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資源分配問題

    這篇文章主要介紹了K8s實(shí)戰(zhàn)教程之容器和?Pods資源分配,本篇文章通過配置集群中運(yùn)行的容器的?CPU?請求和限制,你可以有效利用集群上可用的?CPU?資源,通過將?Pod?CPU?請求保持在較低水平,可以使?Pod?更有機(jī)會被調(diào)度,需要的朋友可以參考下
    2022-07-07
  • Kubernetes中使用臨時容器進(jìn)行故障排查的方法

    Kubernetes中使用臨時容器進(jìn)行故障排查的方法

    在使用Kubernetes時,用戶常常會遇到一些錯誤和迷惑,下面這篇文章主要給大家介紹了關(guān)于Kubernetes中使用臨時容器進(jìn)行故障排查的相關(guān)資料,需要的朋友可以參考下
    2022-03-03
  • kubenetes集群版本升級方式

    kubenetes集群版本升級方式

    本文詳細(xì)介紹了使用kubeadm和二進(jìn)制方式搭建及升級Kubernetes集群的方法,介紹了版本控制、升級步驟、備份ETCD數(shù)據(jù)、升級各節(jié)點(diǎn)組件等關(guān)鍵操作,并提供了操作示例和注意事項(xiàng),幫助理解和實(shí)施Kubernetes集群的搭建和升級過程
    2024-09-09
  • Rancher通過界面管理K8s平臺的圖文步驟詳解

    Rancher通過界面管理K8s平臺的圖文步驟詳解

    這篇文章主要為大家介紹了Rancher通過界面管理K8s平臺通過詳細(xì)的圖文進(jìn)行步驟講解,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-03-03

最新評論