準(zhǔn)備NFS服務(wù)192.168.1.244
$ yum -y install nfs-utils rpcbind
$ systemctl start nfs-server rpcbind
$ systemctl enable nfs-server rpcbind
$ mkdir -p /data/k8s
$ cd /data/k8s
$ echo 11111111 > index.html
$ vim /etc/exports
/data/k8s *(rw,async,no_root_squash)
$ systemctl restart nfs-server
$ exportfs -arv
客戶端測(cè)試,所有k8s節(jié)點(diǎn)都要安裝nfs客戶端
$ yum -y install nfs-utils rpcbind
$ systemctl start nfs rpcbind
$ systemctl enable nfs rpcbind
$ showmount -e 192.168.1.244
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比永吉網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式永吉網(wǎng)站制作公司更省心,省錢(qián),快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋永吉地區(qū)。費(fèi)用合理售后完善,10余年實(shí)體公司更值得信賴。
創(chuàng)建pod直接掛載nfs服務(wù)器
apiVersion: v1
kind: Pod
metadata:
labels:
run: nginx
name: podxx
spec:
volumes:
- name: nfs
nfs:
server: 192.168.1.244
path: /data/k8s
containers:
- image: nginx
name: nginx
volumeMounts:
- mountPath: /usr/share/nginx/html
name: nfs
$ kubectl exec podxx -it bash
PV 的全稱是:PersistentVolume(持久化卷),是對(duì)底層的共享存儲(chǔ)的一種抽象,PV 由管理員進(jìn)行創(chuàng)建和配置,它和具體的底層的共享存儲(chǔ)技術(shù)的實(shí)現(xiàn)方式有關(guān),比如 Ceph、GlusterFS、NFS 等,都是通過(guò)插件機(jī)制完成與共享存儲(chǔ)的對(duì)接。
PVC 的全稱是:PersistentVolumeClaim(持久化卷聲明),PVC 是用戶存儲(chǔ)的一種聲明,PVC 和 Pod 比較類似,Pod 消耗的是節(jié)點(diǎn),PVC 消耗的是 PV 資源,Pod 可以請(qǐng)求 CPU 和內(nèi)存,而 PVC 可以請(qǐng)求特定的存儲(chǔ)空間和訪問(wèn)模式。對(duì)于真正使用存儲(chǔ)的用戶不需要關(guān)心底層的存儲(chǔ)實(shí)現(xiàn)細(xì)節(jié),只需要直接使用 PVC 即可。
Deployment/pod---->pvc---->pv---->共享存儲(chǔ)
AccessModes(訪問(wèn)模式)
ReadWriteOnce(RWO):讀寫(xiě)權(quán)限,但是只能被單個(gè)節(jié)點(diǎn)掛載
ReadOnlyMany(ROX):只讀權(quán)限,可以被多個(gè)節(jié)點(diǎn)掛載
ReadWriteMany(RWX):讀寫(xiě)權(quán)限,可以被多個(gè)節(jié)點(diǎn)掛載
persistentVolumeReclaimPolicy(回收策略)
Retain(保留)- 保留數(shù)據(jù),需要管理員手工清理數(shù)據(jù)
Recycle(回收)- 清除 PV 中的數(shù)據(jù),效果相當(dāng)于執(zhí)行 rm -rf /thevoluem/*
Delete(刪除)- 與 PV 相連的后端存儲(chǔ)完成 volume 的刪除操作,當(dāng)然這常見(jiàn)于云服務(wù)商的存儲(chǔ)服務(wù),比如 ASW EBS。
目前只有 NFS 和 HostPath 兩種類型支持回收策略。當(dāng)然一般來(lái)說(shuō)還是設(shè)置為 Retain 這種策略保險(xiǎn)一點(diǎn)。
一個(gè) PV 的生命周期中,可能會(huì)處于4中不同的階段
Available(可用):表示可用狀態(tài),還未被任何 PVC 綁定
Bound(已綁定):表示已經(jīng)被 PVC 綁定
Released(已釋放):PVC 被刪除,但是資源還未被集群重新聲明
Failed(失?。?表示該 PV 的自動(dòng)回收失敗
手動(dòng)管理pv和pvc
創(chuàng)建順序:后端存儲(chǔ)---pv---pvc---pod
刪除順序:pod---pvc---pv
1、創(chuàng)建pv
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv2
labels:
app: nfs
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
nfs:
path: /data/k8s
server: 192.168.1.244
2、創(chuàng)建pvc
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: pvc2-nfs
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
selector:
matchLabels:
app: nfs
上述pvc會(huì)自動(dòng)和具有訪問(wèn)模式是ReadWriteOnce、storage大于等于1Gi、標(biāo)簽是app: nfs的pv進(jìn)行綁定
3創(chuàng)建pod使用pvc
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nfs-pvc
spec:
replicas: 3
template:
metadata:
labels:
app: nfs-pvc
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
subPath: nginx1 #需要手動(dòng)指定nfs服務(wù)器中的子目錄,該目錄會(huì)自動(dòng)創(chuàng)建
mountPath: /usr/share/nginx/html
volumes:
- name: www
persistentVolumeClaim:
claimName: pvc2-nfs #第2步中創(chuàng)建的pvc
結(jié)果是:共享存儲(chǔ)中的/data/k8s/nginx1會(huì)被掛載到上述pod中的/usr/share/nginx/html目錄
使用StorageClass管理pv和pvc
通常情況下,只有pv是動(dòng)態(tài)生成的(使用StatefulSet的pvc模板除外),其他的還是需要手動(dòng)創(chuàng)建
動(dòng)態(tài)生成pv需要StorageClass和nfs-client-provisioner的共同作用
1、創(chuàng)建nfs-client-provisioner
provisione直接使用nfs服務(wù)器
$ docker pull quay.io/external_storage/nfs-client-provisioner:latest
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
name: nfs-client-provisioner
spec:
replicas: 1
strategy:
type: Recreate
template:
metadata:
labels:
app: nfs-client-provisioner
spec:
serviceAccountName: nfs-client-provisioner
containers:
- name: nfs-client-provisioner
image: quay.io/external_storage/nfs-client-provisioner:latest
volumeMounts:
- name: nfs-client-root
mountPath: /persistentvolumes
env:
- name: PROVISIONER_NAME
value: fuseim.pri/ifs
- name: NFS_SERVER
value: 192.168.1.244
- name: NFS_PATH
value: /data/k8s
volumes:
- name: nfs-client-root
nfs:
server: 192.168.1.244
path: /data/k8s
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: nfs-client-provisioner
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: nfs-client-provisioner-runner
rules:
- apiGroups: [""]
resources: ["persistentvolumes"]
verbs: ["get", "list", "watch", "create", "delete"]
- apiGroups: [""]
resources: ["persistentvolumeclaims"]
verbs: ["get", "list", "watch", "update"]
- apiGroups: ["storage.k8s.io"]
resources: ["storageclasses"]
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources: ["events"]
verbs: ["list", "watch", "create", "update", "patch"]
- apiGroups: [""]
resources: ["endpoints"]
verbs: ["create", "delete", "get", "list", "watch", "patch", "update"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: run-nfs-client-provisioner
subjects:
- kind: ServiceAccount
name: nfs-client-provisioner
namespace: default
roleRef:
kind: ClusterRole
name: nfs-client-provisioner-runner
apiGroup: rbac.authorization.k8s.io
2、創(chuàng)建StorageClass
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: course-nfs-storage
provisioner: fuseim.pri/ifs
3、創(chuàng)建pvc并動(dòng)態(tài)生成pv
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: test-pvc
annotations:
volume.beta.kubernetes.io/storage-class: "course-nfs-storage"
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 500Mi
$ kubectl get pv #pv會(huì)自動(dòng)生成,并和pvc綁定
4、使用上述pvc創(chuàng)建pod
和手動(dòng)管理pv和pvc不同的是,使用StorageClass管理pv和pvc在創(chuàng)建pod時(shí)不用手動(dòng)指定子目錄,會(huì)在存儲(chǔ)服務(wù)器根目錄/data/k8s內(nèi)自動(dòng)生成一個(gè)隨機(jī)子目錄
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nfs-pvc
spec:
replicas: 3
template:
metadata:
labels:
app: nfs-pvc
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumes:
- name: www
persistentVolumeClaim:
claimName: test-pvc
經(jīng)過(guò)測(cè)試,不管是手動(dòng)創(chuàng)建一個(gè)pod,還是用deployment創(chuàng)建多個(gè)pod,都只能生成一個(gè)隨機(jī)目錄,也就是一對(duì)pvc和pv對(duì)應(yīng)一個(gè)持久存儲(chǔ)目錄
5、創(chuàng)建StatefulSet時(shí),直接在其pvc模板中使用StorageClas自動(dòng)生成pv和pvc
$ kubectl explain StatefulSet.spec
volumeClaimTemplates <[]Object>
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: nfs-web
spec:
serviceName: "nginx"
replicas: 2
template:
metadata:
labels:
app: nfs-web
spec:
terminationGracePeriodSeconds: 10
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: www
annotations:
volume.beta.kubernetes.io/storage-class: course-nfs-storage
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 1Gi
上述腳本會(huì)生成兩個(gè)pvc,兩個(gè)pv,兩個(gè)pod,在共享存儲(chǔ)根目錄/data/k8s內(nèi)會(huì)自動(dòng)生成兩個(gè)隨機(jī)子目錄,因?yàn)楦北緮?shù)是2
和deployment不同,有狀態(tài)的應(yīng)用必須每個(gè)pod有一個(gè)持久存儲(chǔ)文件夾,不能多個(gè)pod共享一個(gè)