自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

云原生 | K8s中VolumeMounts.subPath的巧妙用法

云計(jì)算 云原生
subPath其實(shí)就是volumes掛載的子目錄或單個(gè)文件,是不是目錄和單文件,取決于subPath在volumes掛載的目錄下是否存在subPath定義的文件(文件或目錄),如果不存在,則會(huì)volumes對(duì)應(yīng)的目錄下創(chuàng)建一個(gè)subPath目錄。

一、概述

有時(shí),在單個(gè) Pod 中共享卷以供多方使用是很有用的。volumeMounts.subPath 屬性可用于指定所引用的卷內(nèi)的子路徑,而不是其根路徑。

官方文檔:https://kubernetes.io/zh-cn/docs/concepts/storage/volumes/#using-subpath

二、使用場(chǎng)景

  1. 一個(gè)共享卷, 掛載多個(gè)路徑。
  2. ConfigMap或Secret掛載到特定目錄的特定路徑, 而 該目錄下已經(jīng)有其他文件且不希望被覆蓋掉。

三、共享卷中使用, 掛載多個(gè)路徑

作為configmap/secret使用時(shí),subPath代表configmap/secret的子路徑。

【示例1】掛載目錄,hostPath

apiVersion: v1
kind: Pod
metadata:
name: my-lamp-site
spec:
nodeName: local-168-182-110 # 為了測(cè)試方便,指定調(diào)度機(jī)器
containers:
- name: mysql
image: mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: "rootpasswd"
volumeMounts:
- mountPath: /var/lib/mysql #掛載到容器的某個(gè)路徑下
name: site-data #掛載設(shè)備的名字,與volumes[*].name 需要對(duì)應(yīng)
subPath: mysql # volumes path中的子路徑(會(huì)自動(dòng)在volumes path目錄下創(chuàng)建mysql空目錄)
- name: php
image: php:7.0-apache
volumeMounts:
- mountPath: /var/www/html #掛載到容器的某個(gè)路徑下
name: site-data # volumes path中的子路徑(會(huì)自動(dòng)在volumes path目錄下創(chuàng)建site-data【空目錄】)
subPath: html
volumes:
- name: nginx #和上面保持一致 這是本地的文件路徑,上面是容器內(nèi)部的路徑
hostPath:
path: /opt/k8s/subPath/lamp #此路徑需要實(shí)現(xiàn)創(chuàng)建

【示例2】掛載目錄,pvc

# StorageClass
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
---
# pvc
apiVersion: v1
kind: PersistentVolume
metadata:
name: local-lamp-pv
labels:
name: local-lamp-pv
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: local-storage
local:
path: /opt/k8s/subPath/lamp-pvc
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- local-168-182-110

---
# pvc
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: my-lamp-site-data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: local-storage
selector:
matchLabels:
name: local-lamp-pv
---
apiVersion: v1
kind: Pod
metadata:
name: my-lamp-site-pvc
spec:
containers:
- name: mysql
image: mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: "rootpasswd"
volumeMounts:
- mountPath: /var/lib/mysql
name: site-data
subPath: mysql
- name: php
image: php:7.0-apache
volumeMounts:
- mountPath: /var/www/html
name: site-data
subPath: html
volumes:
- name: site-data
persistentVolumeClaim:
claimName: my-lamp-site-data

如果使用PVC模板就不用手動(dòng)創(chuàng)建PVC了,示例如下:

  volumeClaimTemplates:   #可看作pvc的模板
- metadata:
name: nginx-pvc
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: "local-storage" #存儲(chǔ)類名,就是上面nginx-sc.yaml metadata.name
resources:
requests:
storage: 1Gi

【示例3】共享單個(gè)文件那么如果 subPath 不是文件夾,而是一個(gè)文件,又該如何解決呢?同樣的道理,只需要通過(guò) subPath 指定出該文件即可,注意 subPath 要使用相對(duì)目錄。具體如下所示:

apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-test
spec:
replicas: 1
selector:
matchLabels:
app: nginx-pod
template:
metadata:
labels:
app: nginx-pod
spec:
nodeName: local-168-182-110 # 為了測(cè)試方便,指定調(diào)度機(jī)器
containers:
- name: nginx
image: docker.io/library/nginx:latest
volumeMounts:
- mountPath: /etc/nginx/nginx.conf
name: nginx-conf
subPath: nginx-conf
volumes:
- name: nginx-conf #和上面保持一致 這是本地的文件路徑,上面是容器內(nèi)部的路徑
hostPath:
path: /opt/k8s/subPath/nginx #此路徑需要實(shí)現(xiàn)創(chuàng)建

圖片

【結(jié)論】以宿主機(jī)上的文件為準(zhǔn),會(huì)覆蓋pod里原先默認(rèn)的的文件內(nèi)容。

四、ConfigMap 和 Secret 中使用 subPath

作為configmap/secret使用時(shí),subPath代表configmap/secret?的子路徑。如果不使用subPath?會(huì)把容器里原本的文件(volumeMounts.mountPath?對(duì)應(yīng)的目錄)都清空,自會(huì)把ConfigMap 和 Secret 的文件放在volumeMounts.mountPath對(duì)應(yīng)的目錄下。

【示例1】ConfigMap

apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-conf
data:
nginx.conf: |+
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-test2
spec:
replicas: 1
selector:
matchLabels:
app: nginx-pod
template:
metadata:
labels:
app: nginx-pod
spec:
nodeName: local-168-182-110 # 為了測(cè)試方便,指定調(diào)度機(jī)器
containers:
- name: nginx
image: docker.io/library/nginx:latest
volumeMounts:
- name: nginx-cm # 與volumes.name一致
mountPath: /etc/nginx/nginx.conf
subPath: nginx.conf
volumes:
- name: nginx-cm
configMap:
name: nginx-conf # configMap名稱

【示例1】Secret

apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
username: admin
password: MWYyZDFlMmU2N2Rm

---

vim myapp-demo.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-demo
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: mysql
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
volumeMounts:
- name: mysql
mountPath: /tmp/data
subPath: data
volumes:
- name: mysql
secret:
secretName: mysecret

【結(jié)論】會(huì)在/tmp目錄下面存放data文件信息,如果存在則覆蓋。如果不存在,則自動(dòng)創(chuàng)建。

最后對(duì)volumeMounts.subPath來(lái)一個(gè)總結(jié):

  • subPath其實(shí)就是volumes掛載的子目錄或單個(gè)文件,是不是目錄和單文件,取決于subPath在volumes掛載的目錄下是否存在subPath定義的文件(文件或目錄),如果不存在,則會(huì)volumes對(duì)應(yīng)的目錄下創(chuàng)建一個(gè)subPath目錄。
  • 如果ConfigMap 和 Secret 中使用 subPath,如果不指定subPath,則會(huì)把volumeMounts.mountPath對(duì)應(yīng)的目錄下的文件都清掉,然后只存放ConfigMap 或者 Secret 定義的文件。

關(guān)于volumeMounts.subPath的用法就先到這里了,有疑問(wèn)的小伙伴,歡迎給我留言哦,后續(xù)文章更精彩,請(qǐng)小伙伴耐心等待哦~

責(zé)任編輯:武曉燕 來(lái)源: 大數(shù)據(jù)與云原生技術(shù)分享
相關(guān)推薦

2024-09-26 09:50:07

2022-04-07 10:17:18

云原生服務(wù)器優(yōu)化

2023-03-06 07:19:50

2021-04-25 10:26:58

云計(jì)算云原生

2022-07-18 18:48:32

Kubernetes云原生

2023-03-03 07:54:21

2025-01-03 08:08:56

2022-11-08 08:55:31

2023-03-07 07:56:37

Sqoopk8s底層

2022-10-14 07:42:50

LuceneHTTPWeb

2024-06-12 13:21:06

2023-01-04 17:42:22

KubernetesK8s

2021-08-13 07:00:41

云原生k8sspringboot

2021-08-26 07:20:05

云原生K8sSpringboot

2022-11-06 21:31:11

云原生Sentinel集群模式

2023-02-08 07:55:33

K8sHPA服務(wù)器

2023-03-01 07:42:12

HBase編排部署數(shù)據(jù)

2024-06-06 09:19:09

2023-02-24 14:52:20

Redis存儲(chǔ)開(kāi)發(fā)

2023-11-03 08:43:00

云原生TLS 證書(shū)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)