您好,登錄后才能下訂單哦!
本篇文章為大家展示了k8s如何使用emptyDir,hostPath,nfs,pv,pvc做存儲,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
存儲卷三種方式:emptyDir,gitRepo,hostPath
emptyDir:一個pod創建兩個容器,一個pod提供請求服務,另一個pod提供文件存儲,pod刪除,存儲卷就刪除。
gitRepo:使用docker鏡像提供存儲
hostPath:宿主機路徑,pod刪除,存儲卷還在(在多個node節點要創建路徑)
nfs:使用共享存儲(多個pod要在共享存儲中創建多個目錄)
幫助:
[root@k8s1 ~]# kubectl explain pods.spec.volumes.persistentVolumeClaim --pvc幫助
[root@k8s1 ~]# kubectl explain pods.spec.volumes --查看幫助
[root@k8s1 ~]# kubectl explain pv --pv幫助
1.使用emptyDir做存儲(兩個pod,一個做存儲,一個提供服務)
[root@k8s1 ~]# vim 11.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-demo --定義一個pod
namespace: default
labels:
app: myapp
tier: frontend
spec:
containers:
- name: myapp --定義一個容器
image: ikubernetes/myapp:v1
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html --myapp容器html卷掛載到/usr/share/nginx/html(是nginx默認路徑)
- name: busybox
image: busybox:latest
imagePullPolicy: IfNotPresent
volumeMounts:
- name: html --busybox容器將html卷掛載到/data
mountPath: /data/
command: ["/bin/sh","-c","while true;do echo $(date) >> /data/index.html;sleep 2;done"]
volumes: --定義一個html卷
- name: html
emptyDir: {}
[root@k8s1 ~]# kubectl apply -f 11.yaml
pod/pod-demo created
[root@k8s1 ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod-demo 2/2 Running 0 103s 10.244.1.13 k8s2 <none> <none>
[root@k8s1 ~]# kubectl exec -it pod-demo -c busybox -- /bin/sh
/ # cat /data/index.html
Fri Feb 22 09:39:53 UTC 2019
Fri Feb 22 09:39:55 UTC 2019
Fri Feb 22 09:39:57 UTC 2019
Fri Feb 22 09:39:59 UTC 2019
[root@k8s1 ~]# curl http://10.244.1.13
Fri Feb 22 09:39:53 UTC 2019
Fri Feb 22 09:39:55 UTC 2019
Fri Feb 22 09:39:57 UTC 2019
Fri Feb 22 09:39:59 UTC 2019
Fri Feb 22 09:40:01 UTC 2019
Fri Feb 22 09:40:03 UTC 2019
Fri Feb 22 09:40:05 UTC 2019
[root@k8s1 ~]#
2.使用hostPath做存儲(如果node節點宕機,pod訪問宕機node的數據就不存在了)
node1節點:
[root@k8s2 ~]# mkdir -p /data/pod
[root@k8s2 ~]# cat /data/pod/index.html --為了區分node節點,將文件內容寫不一樣
node1
[root@k8s2 ~]#
node2節點:
[root@k8s3 ~]# mkdir -p /data/pod
[root@k8s3 ~]# cat /data/pod/index.html
node2
[root@k8s3 ~]#
master節點:
[root@k8s1 ~]# vim 12.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-vol-hostpath
namespace: default
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
volumeMounts:
- name: html --使用html卷存儲
mountPath: /usr/share/nginx/html --nginx網頁根目錄
volumes:
- name: html
hostPath:
path: /data/pod/ --html卷的路徑(對應的node節點新建目錄,pod在哪個node上就要新建)
type: DirectoryOrCreate
[root@k8s1 ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod-demo 2/2 Running 0 64m 10.244.1.13 k8s2 <none> <none>
pod-vol-hostpath 1/1 Running 0 4s 10.244.2.22 k8s3 <none> <none>
[root@k8s1 ~]# curl http://10.244.2.22 --pod在node2節點上,所以訪問的是node2的網頁,如果在node1就是node1的內容
node2
[root@k8s1 ~]#
3.使用nfs共享存儲
nfs存儲:
[root@liutie1 ~]# mkdir /data/v6
[root@liutie1 ~]# vim /etc/exports
/data/v6 172.16.8.0/24(rw,no_root_squash)
[root@liutie1 ~]# systemctl restart nfs
[root@liutie1 ~]# exportfs -arv
exporting 172.16.8.0/24:/data/v6
[root@liutie1 ~]# showmount -e
Export list for liutie1:
/data/v6 172.16.8.0/24
[root@liutie1 ~]#
k8s節點:
[root@k8s1 ~]# mkdir /data/v6 --創建共享目錄
[root@k8s1 ~]# mount.nfs 172.16.8.108:/data/v6 /data/v6 --測試手動掛載
[root@k8s1 ~]# umount /data/v6
[root@k8s1 ~]# vim nfs.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-vol-nfs
namespace: default
spec:
containers:
- name: pod-nfs
image: ikubernetes/myapp:v1
volumeMounts:
- name: html1
mountPath: /usr/share/nginx/html
volumes:
- name: html1
nfs:
path: /data/v6
server: 172.16.8.108
[root@k8s1 ~]# kubectl apply -f nfs.yaml
pod/pod-vol-nfs created
[root@k8s1 ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod-vol-nfs 1/1 Running 0 2m21s 10.244.1.78 k8s2 <none> <none>
[root@k8s1 ~]#
在nfs存儲創建文件
[root@liutie1 ~]# cd /data/v6/
[root@liutie1 v6]# cat index.html
nfs store
[root@liutie1 v6]#
在k8s節點打開網頁
[root@k8s1 ~]# curl 10.244.1.78 --pod的ip地址
nfs store
[root@k8s1 ~]#
4.使用nfs共享存儲(固定大小)
nfs服務器:
[root@liutie1 ~]# mkdir /data/v{1,2,3,4,5} --在存儲上新建目錄
[root@liutie1 ~]# yum install nfs* -y --安裝nfs
[root@liutie1 ~]# vim /etc/exports --共享目錄
/data/v1 172.16.8.0/24(rw,no_root_squash)
/data/v2 172.16.8.0/24(rw,no_root_squash)
/data/v3 172.16.8.0/24(rw,no_root_squash)
/data/v4 172.16.8.0/24(rw,no_root_squash)
/data/v5 172.16.8.0/24(rw,no_root_squash)
[root@liutie1 ~]# exportfs -arv
exporting 172.16.8.0/24:/data/v5
exporting 172.16.8.0/24:/data/v4
exporting 172.16.8.0/24:/data/v3
exporting 172.16.8.0/24:/data/v2
exporting 172.16.8.0/24:/data/v1
[root@liutie1 ~]# showmount -e
Export list for liutie1:
/data/v5 172.16.8.0/24
/data/v4 172.16.8.0/24
/data/v3 172.16.8.0/24
/data/v2 172.16.8.0/24
/data/v1 172.16.8.0/24
[root@liutie1 ~]#
node各節點:
[root@k8s2 ~]# yum install nfs-common nfs-utils -y --所有node節點必須安裝nfs-utils軟件包,否則會出錯
master節點:
[root@k8s1 ~]# yum install -y nfs-utils
[root@k8s1 ~]# kubectl explain PersistentVolume --幫助信息
[root@k8s1 ~]# vim pv.yaml --將遠程的nfs目錄轉換成pv
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv001
labels:
name: pv001
spec:
nfs:
path: /data/v1
server: 172.16.8.108
accessModes: ["ReadWriteMany","ReadWriteOnce"]
capacity:
storage: 5Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv002
labels:
name: pv002
spec:
nfs:
path: /data/v2
server: 172.16.8.108
accessModes: ["ReadWriteMany","ReadWriteOnce"]
capacity:
storage: 15Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv003
labels:
name: pv003
spec:
nfs:
path: /data/v3
server: 172.16.8.108
accessModes: ["ReadWriteMany","ReadWriteOnce"]
capacity:
storage: 1Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv004
labels:
name: pv004
spec:
nfs:
path: /data/v4
server: 172.16.8.108
accessModes: ["ReadWriteMany","ReadWriteOnce"]
capacity:
storage: 20Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv005
labels:
name: pv005
spec:
nfs:
path: /data/v5
server: 172.16.8.108
accessModes: ["ReadWriteMany","ReadWriteOnce"]
capacity:
storage: 13Gi
[root@k8s1 ~]# kubectl apply -f pv.yaml --生成pv
persistentvolume/pv001 created
persistentvolume/pv002 created
persistentvolume/pv003 created
persistentvolume/pv004 created
persistentvolume/pv005 created
[root@k8s1 ~]# kubectl get pv --查看pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv001 5Gi RWO,RWX Retain Available 2m40s
pv002 15Gi RWO,RWX Retain Available 2m40s
pv003 1Gi RWO,RWX Retain Available 2m40s
pv004 20Gi RWO,RWX Retain Available 2m40s
pv005 13Gi RWO,RWX Retain Available 2m40s
[root@k8s1 ~]# vim pvc.yaml --創建pvc,pvc的大小為6G
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mypvc
namespace: default --定義一個mypvc名字的pvc
spec:
accessModes: ["ReadWriteMany"]
resources:
requests:
storage: 6Gi
---
apiVersion: v1
kind: Pod --定義一個pod,pod使用pvc
metadata:
name: pod-vol-pvc
namespace: default
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
volumeMounts:
- name: html --使用mypvc存儲
mountPath: /usr/share/nginx/html
volumes:
- name: html
persistentVolumeClaim:
claimName: mypvc --引用上面的mypvc
[root@k8s1 ~]# kubectl apply -f pvc.yaml
persistentvolumeclaim/mypvc created
pod/pod-vol-pvc created
[root@k8s1 ~]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv001 5Gi RWO,RWX Retain Available 8m31s
pv002 15Gi RWO,RWX Retain Available 8m31s
pv003 1Gi RWO,RWX Retain Available 8m31s
pv004 20Gi RWO,RWX Retain Available 8m31s
pv005 13Gi RWO,RWX Retain Bound default/mypvc 8m31s --Bound表示使用
[root@k8s1 ~]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
mypvc Bound pv005 13Gi RWO,RWX 2m31s --使用了pv005的mypvc存儲卷
[root@k8s1 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
pod-demo 2/2 Running 0 141m
pod-vol-hostpath 1/1 Running 0 77m
pod-vol-pvc 1/1 Running 0 4s
[root@k8s1 ~]# kubectl describe pods pod-vol-pvc --查看詳細信息
上述內容就是k8s如何使用emptyDir,hostPath,nfs,pv,pvc做存儲,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。