您好,登錄后才能下訂單哦!
準備NFS服務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
客戶端測試,所有k8s節點都要安裝nfs客戶端
$ yum -y install nfs-utils rpcbind
$ systemctl start nfs rpcbind
$ systemctl enable nfs rpcbind
$ showmount -e 192.168.1.244
創建pod直接掛載nfs服務器
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(持久化卷),是對底層的共享存儲的一種抽象,PV 由管理員進行創建和配置,它和具體的底層的共享存儲技術的實現方式有關,比如 Ceph、GlusterFS、NFS 等,都是通過插件機制完成與共享存儲的對接。
PVC 的全稱是:PersistentVolumeClaim(持久化卷聲明),PVC 是用戶存儲的一種聲明,PVC 和 Pod 比較類似,Pod 消耗的是節點,PVC 消耗的是 PV 資源,Pod 可以請求 CPU 和內存,而 PVC 可以請求特定的存儲空間和訪問模式。對于真正使用存儲的用戶不需要關心底層的存儲實現細節,只需要直接使用 PVC 即可。
Deployment/pod---->pvc---->pv---->共享存儲
AccessModes(訪問模式)
ReadWriteOnce(RWO):讀寫權限,但是只能被單個節點掛載
ReadOnlyMany(ROX):只讀權限,可以被多個節點掛載
ReadWriteMany(RWX):讀寫權限,可以被多個節點掛載
persistentVolumeReclaimPolicy(回收策略)
Retain(保留)- 保留數據,需要管理員手工清理數據
Recycle(回收)- 清除 PV 中的數據,效果相當于執行 rm -rf /thevoluem/*
Delete(刪除)- 與 PV 相連的后端存儲完成 volume 的刪除操作,當然這常見于云服務商的存儲服務,比如 ASW EBS。
目前只有 NFS 和 HostPath 兩種類型支持回收策略。當然一般來說還是設置為 Retain 這種策略保險一點。
一個 PV 的生命周期中,可能會處于4中不同的階段
Available(可用):表示可用狀態,還未被任何 PVC 綁定
Bound(已綁定):表示已經被 PVC 綁定
Released(已釋放):PVC 被刪除,但是資源還未被集群重新聲明
Failed(失敗): 表示該 PV 的自動回收失敗
手動管理pv和pvc
創建順序:后端存儲---pv---pvc---pod
刪除順序:pod---pvc---pv
1、創建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、創建pvc
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: pvc2-nfs
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
selector:
matchLabels:
app: nfs
上述pvc會自動和具有訪問模式是ReadWriteOnce、storage大于等于1Gi、標簽是app: nfs的pv進行綁定
3創建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 #需要手動指定nfs服務器中的子目錄,該目錄會自動創建
mountPath: /usr/share/nginx/html
volumes:
- name: www
persistentVolumeClaim:
claimName: pvc2-nfs #第2步中創建的pvc
結果是:共享存儲中的/data/k8s/nginx1會被掛載到上述pod中的/usr/share/nginx/html目錄
使用StorageClass管理pv和pvc
通常情況下,只有pv是動態生成的(使用StatefulSet的pvc模板除外),其他的還是需要手動創建
動態生成pv需要StorageClass和nfs-client-provisioner的共同作用
1、創建nfs-client-provisioner
provisione直接使用nfs服務器
$ 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、創建StorageClass
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: course-nfs-storage
provisioner: fuseim.pri/ifs
3、創建pvc并動態生成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會自動生成,并和pvc綁定
4、使用上述pvc創建pod
和手動管理pv和pvc不同的是,使用StorageClass管理pv和pvc在創建pod時不用手動指定子目錄,會在存儲服務器根目錄/data/k8s內自動生成一個隨機子目錄
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
經過測試,不管是手動創建一個pod,還是用deployment創建多個pod,都只能生成一個隨機目錄,也就是一對pvc和pv對應一個持久存儲目錄
5、創建StatefulSet時,直接在其pvc模板中使用StorageClas自動生成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
上述腳本會生成兩個pvc,兩個pv,兩個pod,在共享存儲根目錄/data/k8s內會自動生成兩個隨機子目錄,因為副本數是2
和deployment不同,有狀態的應用必須每個pod有一個持久存儲文件夾,不能多個pod共享一個
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。