這個章節(jié)展示了如何使用PersistentVolume配置pod的存儲乓诽。以下是過程的摘要:
1.集群管理員創(chuàng)建一個由物理存儲支持的PersistentVolume椅挣。集群管理員不將該卷跟任何pod關(guān)聯(lián)把沼。
2.集群用戶創(chuàng)建一個PersistentVolumeClaim蜈出,它自動綁定到一個合適的PersistentVolume详民。
3.創(chuàng)建一個pod使用PersistentVolumeClaim作為存儲延欠。
在node上創(chuàng)建一個index.html文件
打開集群節(jié)點(diǎn)上的shell。如何打開shell取決于如何設(shè)置集群沈跨。例如由捎,如果使用的是Minikube,可以使用minikube ssh打開集群節(jié)點(diǎn)上的shell。
在shell里創(chuàng)建一個/tmp/data文件目錄:
mkdir /tmp/data
在/tmp/data文件目錄里創(chuàng)建一個index.html文件:
echo 'Hello from Kubernetes storage' > /tmp/data/index.html
創(chuàng)建一個PersistentVolume
在本次實(shí)驗饿凛,新建一個主機(jī)路徑 PersistentVolume狞玛。kubernetes支持在單節(jié)點(diǎn)集群上的開發(fā)和測試的主機(jī)路徑软驰。主機(jī)路徑PersistentVolume使用節(jié)點(diǎn)上的文件或目錄模擬網(wǎng)絡(luò)連接存儲。
在生成集群中不能使用主機(jī)路徑为居。相反碌宴,集群管理員可以提供網(wǎng)絡(luò)資源,如Google Compute Engine持久磁盤蒙畴,NFS共享或Amazon彈性塊存儲卷贰镣。集群管理員還可以使用StorageClasses來設(shè)置動態(tài)配置。
以下是主機(jī)路徑PersistentVolume配置文件:
kind: PersistentVolume
apiVersion: v1
metadata:
name: task-pv-volume
labels:
type: local
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/tmp/data"
配置文件指定卷/tmp/data在集群的節(jié)點(diǎn)膳凝。配置也指定了10G大小和ReadWriteOnce訪問模式碑隆,這意味著卷可以通過單個節(jié)點(diǎn)的讀寫來安裝。
創(chuàng)建一個PersistentVolume:
kubectl create -f http://k8s.io/docs/tasks/configure-pod-container/task-pv-volume.yaml
查看PersistentVolume的詳細(xì)信息:
kubectl get pv tesk-pv-volume
輸出展示PersistentVolume的STATUS是Available蹬音。這意味著還沒有被綁定到PersistentVolumeClaim上煤。
創(chuàng)建一個PersistentVolumeClaim
下一步是創(chuàng)建一個PersistentVolumeClaim。pod使用PersistentVolumeClaim請求物理存儲著淆。在本次實(shí)驗劫狠,你創(chuàng)建一個PersistentVolumeClaim,它請求一個至少三個可以為至少一個節(jié)點(diǎn)提供讀寫訪問的gibibytes的卷永部。下面是PersistentVolumeClaim的配置文件:
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: task-pv-claim
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 3Gi
創(chuàng)建一個PersistentVolumeClaim:
kubectl create -f http://k8s.io/docs/tasks/configure-pod-container/task-pv-claim.yaml
創(chuàng)建PersistentVolumeClaim以后独泞,Kubernetes控制面板會查找滿足聲明要求的PersistentVolume。如果控制面板找到合適的PersistentVolume苔埋,它將聲明綁定到卷懦砂。
重新看一下PersistentVolume:
kubectl get pv tesk-pv-volume
現(xiàn)在輸出展示STATUS是Bound。
kubectl get pv task-pv-volume
NAME CAPACITY ACCESSMODES RECLAIMPOLICY STATUS CLAIM REASON AGE
task-pv-volume 10Gi RWO Retain Bound default/task-pv-claim 8m
查看PersistentVolumeClaim:
kubectl get pvc tesk-pv-claim
輸出展示PersistentVolumeClaim綁定到了你的PersistentVolume组橄,task-pv-volume荞膘。
NAME STATUS VOLUME CAPACITY ACCESSMODES AGE
task-pv-claim Bound task-pv-volume 10Gi RWO 5s
創(chuàng)建一個pod
下一步是創(chuàng)建一個pod使用PersistentVolumeClaim作為一個卷。
下面是pod的配置文件:
kind: Pod
apiVersion: v1
metadata:
name: task-pv-pod
spec:
volumes:
- name: task-pv-storage
persistentVolumeClaim:
claimName: task-pv-claim
containers:
- name: task-pv-container
image: nginx
ports:
- containerPort: 80
name: "http-server"
volumeMounts:
- mountPath: "/usr/share/nginx/html"
name: task-pv-storage
注意pod配置文件指定了PersistentVolumeClaim玉工,但是沒有指定PersistentVolume羽资。從pod的角度來看claim是一個卷。
創(chuàng)建一個pod:
kubectl create -f http://k8s.io/docs/tasks/configure-pod-container/task-pv-pod.yaml
驗證容器是否運(yùn)行:
kubectl get pod task-pv-pod
使用shell進(jìn)入到運(yùn)行容器的內(nèi)部:
kubectl exec -it task-pv-pod -- /bin/bash
在shell里面驗證nginx是否應(yīng)用從主機(jī)路徑卷提供的index.html文件:
root@task-pv-pod:/# apt-get update
root@task-pv-pod:/# apt-get install curl
root@task-pv-pod:/# curl localhost
輸出展示了寫入到主機(jī)路徑卷index.html里的內(nèi)容:
Hello from Kubernetes storage
訪問控制
配置存儲組ID(GID)只允許pod使用相同的GID進(jìn)行寫操作瓮栗。不匹配或者丟失GID將引起權(quán)限定義錯誤削罩。為了減少與用戶協(xié)調(diào)的需要,管理員可以使用GID注釋PersistentVolume费奸。然后GID自動加到任何使用PersistentVolume的pod弥激。
使用 pv.beta.kubernetes.io/gid注釋如下面的例子:
kind: PersistentVolume
apiVersion: v1
metadata:
name: pv1
annotations:
pv.beta.kubernetes.io/gid: "1234"
當(dāng)pod消耗具有GID注釋的persistentVolume時,注釋的GID將以與pod的安全的上下文中指定的GID相同的方式應(yīng)用與pod中所有的容器愿阐。每個GID無論是源自PersistentVolume注釋還是pod的規(guī)范微服,都會應(yīng)用與每個container中運(yùn)行的第一個進(jìn)程。
注意:當(dāng)pod消耗PersistentVolume時缨历,與PersistentVolume相關(guān)聯(lián)的GID不存在于pod資源本身以蕴。