本篇文章為大家展示了k8s如何使用emptyDir,hostPath,nfs,pv,pvc做存儲(chǔ),內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
成都創(chuàng)新互聯(lián)公司公司2013年成立,先為烏爾禾等服務(wù)建站,烏爾禾等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢(xún)服務(wù)。為烏爾禾企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。
存儲(chǔ)卷三種方式:emptyDir,gitRepo,hostPath
emptyDir:一個(gè)pod創(chuàng)建兩個(gè)容器,一個(gè)pod提供請(qǐng)求服務(wù),另一個(gè)pod提供文件存儲(chǔ),pod刪除,存儲(chǔ)卷就刪除。
gitRepo:使用docker鏡像提供存儲(chǔ)
hostPath:宿主機(jī)路徑,pod刪除,存儲(chǔ)卷還在(在多個(gè)node節(jié)點(diǎn)要?jiǎng)?chuàng)建路徑)
nfs:使用共享存儲(chǔ)(多個(gè)pod要在共享存儲(chǔ)中創(chuàng)建多個(gè)目錄)
幫助:
[root@k8s1 ~]# kubectl explain pods.spec.volumes.persistentVolumeClaim --pvc幫助
[root@k8s1 ~]# kubectl explain pods.spec.volumes --查看幫助
[root@k8s1 ~]# kubectl explain pv --pv幫助
1.使用emptyDir做存儲(chǔ)(兩個(gè)pod,一個(gè)做存儲(chǔ),一個(gè)提供服務(wù))
[root@k8s1 ~]# vim 11.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-demo --定義一個(gè)pod
namespace: default
labels:
app: myapp
tier: frontend
spec:
containers:
- name: myapp --定義一個(gè)容器
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默認(rèn)路徑)
- 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: --定義一個(gè)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
[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做存儲(chǔ)(如果node節(jié)點(diǎn)宕機(jī),pod訪(fǎng)問(wèn)宕機(jī)node的數(shù)據(jù)就不存在了)
node1節(jié)點(diǎn):
[root@k8s2 ~]# mkdir -p /data/pod
[root@k8s2 ~]# cat /data/pod/index.html --為了區(qū)分node節(jié)點(diǎn),將文件內(nèi)容寫(xiě)不一樣
node1
[root@k8s2 ~]#
node2節(jié)點(diǎn):
[root@k8s3 ~]# mkdir -p /data/pod
[root@k8s3 ~]# cat /data/pod/index.html
node2
[root@k8s3 ~]#
master節(jié)點(diǎn):
[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卷存儲(chǔ)
mountPath: /usr/share/nginx/html --nginx網(wǎng)頁(yè)根目錄
volumes:
- name: html
hostPath:
path: /data/pod/ --html卷的路徑(對(duì)應(yīng)的node節(jié)點(diǎn)新建目錄,pod在哪個(gè)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
pod-vol-hostpath 1/1 Running 0 4s 10.244.2.22 k8s3
[root@k8s1 ~]# curl http://10.244.2.22 --pod在node2節(jié)點(diǎn)上,所以訪(fǎng)問(wèn)的是node2的網(wǎng)頁(yè),如果在node1就是node1的內(nèi)容
node2
[root@k8s1 ~]#
3.使用nfs共享存儲(chǔ)
nfs存儲(chǔ):
[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節(jié)點(diǎn):
[root@k8s1 ~]# mkdir /data/v6 --創(chuàng)建共享目錄
[root@k8s1 ~]# mount.nfs 172.16.8.108:/data/v6 /data/v6 --測(cè)試手動(dòng)掛載
[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
[root@k8s1 ~]#
在nfs存儲(chǔ)創(chuàng)建文件
[root@liutie1 ~]# cd /data/v6/
[root@liutie1 v6]# cat index.html
nfs store
[root@liutie1 v6]#
在k8s節(jié)點(diǎn)打開(kāi)網(wǎng)頁(yè)
[root@k8s1 ~]# curl 10.244.1.78 --pod的ip地址
nfs store
[root@k8s1 ~]#
4.使用nfs共享存儲(chǔ)(固定大小)
nfs服務(wù)器:
[root@liutie1 ~]# mkdir /data/v{1,2,3,4,5} --在存儲(chǔ)上新建目錄
[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各節(jié)點(diǎn):
[root@k8s2 ~]# yum install nfs-common nfs-utils -y --所有node節(jié)點(diǎn)必須安裝nfs-utils軟件包,否則會(huì)出錯(cuò)
master節(jié)點(diǎn):
[root@k8s1 ~]# yum install -y nfs-utils
[root@k8s1 ~]# kubectl explain PersistentVolume --幫助信息
[root@k8s1 ~]# vim pv.yaml --將遠(yuǎn)程的nfs目錄轉(zhuǎn)換成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 --創(chuàng)建pvc,pvc的大小為6G
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mypvc
namespace: default --定義一個(gè)mypvc名字的pvc
spec:
accessModes: ["ReadWriteMany"]
resources:
requests:
storage: 6Gi
---
apiVersion: v1
kind: Pod --定義一個(gè)pod,pod使用pvc
metadata:
name: pod-vol-pvc
namespace: default
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
volumeMounts:
- name: html --使用mypvc存儲(chǔ)
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存儲(chǔ)卷
[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 --查看詳細(xì)信息
上述內(nèi)容就是k8s如何使用emptyDir,hostPath,nfs,pv,pvc做存儲(chǔ),你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。