參考官方文檔
https://microk8s.io/docs/how-to-nfs
https://github.com/kubernetes-csi/csi-driver-nfs/blob/master/docs/install-csi-driver-v4.4.0.md
1.設置nfs服務器
# 安裝服務
sudo apt-get install nfs-kernel-server
# 創(chuàng)建用于NFS的目錄:
sudo mkdir -p /srv/nfs
sudo chown nobody:nogroup /srv/nfs
sudo chmod 0777 /srv/nfs
# 編輯 /etc/exports 文件。確保所有MicroK8s節(jié)點的IP地址都能夠裝載此共享男摧。例如典阵,要允許 10.0.0.0/24 子網中的所有IP地址:
sudo mv /etc/exports /etc/exports.bak
echo '/srv/nfs 10.0.0.0/24(rw,sync,no_subtree_check)' | sudo tee /etc/exports
# 不確定ip限制可以用
# 最后袱蚓,重新啟動NFS服務器:/srv/nfs *(rw,sync,no_subtree_check)
sudo systemctl restart nfs-kernel-server
2.安裝NFS的CSI驅動程序
git clone git@github.com:kubernetes-csi/csi-driver-nfs.git
cd csi-driver-nfs/deploy/v4.1.0
kubectl apply -f rbac-csi-nfs.yaml -f csi-nfs-driverinfo.yaml -f csi-nfs-controller.yaml -f csi-nfs-node.yaml
## 檢查節(jié)點是否正常
kubectl -n kube-system get pod -o wide -l app=csi-nfs-controller
kubectl -n kube-system get pod -o wide -l app=csi-nfs-node
# 解決鏡像拉取問題
microk8s ctr image pull registry.aliyuncs.com/google_containers/csi-node-driver-registrar:v2.5.1
microk8s ctr image tag registry.aliyuncs.com/google_containers/csi-node-driver-registrar:v2.5.1 registry.k8s.io/sig-storage/csi-node-driver-registrar:v2.5.1
microk8s ctr image pull registry.aliyuncs.com/google_containers/livenessprobe:v2.7.0
microk8s ctr image tag registry.aliyuncs.com/google_containers/livenessprobe:v2.7.0 registry.k8s.io/sig-storage/livenessprobe:v2.7.0
microk8s ctr image pull lank8s.cn/sig-storage/nfsplugin:v4.1.0
microk8s ctr image tag lank8s.cn/sig-storage/nfsplugin:v4.1.0 registry.k8s.io/sig-storage/nfsplugin:v4.1.0
microk8s ctr image pull lank8s.cn/sig-storage/csi-provisioner:v3.2.0
microk8s ctr image tag lank8s.cn/sig-storage/csi-provisioner:v3.2.0 registry.k8s.io/sig-storage/csi-provisioner:v3.2.0
microk8s ctr image pull lank8s.cn/sig-storage/csi-snapshotter:v6.2.2
microk8s ctr image tag lank8s.cn/sig-storage/csi-snapshotter:v6.2.2 registry.k8s.io/sig-storage/csi-snapshotter:v6.2.2
# 成功后顯示如下
root@ubuntu02:/home/k8s/csi-driver-nfs/deploy/v4.4.0# kubectl -n kube-system get pod -o wide -l app=csi-nfs-controller
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
csi-nfs-controller-6f5f9cb8f-fw7pw 4/4 Running 0 3m54s 192.168.1.9 ubuntu04 <none> <none>
root@ubuntu02:/home/k8s/csi-driver-nfs/deploy/v4.4.0# kubectl -n kube-system get pod -o wide -l app=csi-nfs-node
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
csi-nfs-node-7s5zx 3/3 Running 0 4m2s 192.168.1.7 ubuntu02 <none> <none>
csi-nfs-node-w4kpp 3/3 Running 0 4m2s 192.168.1.9 ubuntu04 <none> <none>
csi-nfs-node-z65sn 3/3 Running 0 4m2s 192.168.1.8 ubuntu03 <none> <none>
root@ubuntu02:/home/k8s/csi-driver-nfs/deploy/v4.4.0# microk8s kubectl get csidrivers
NAME ATTACHREQUIRED PODINFOONMOUNT STORAGECAPACITY TOKENREQUESTS REQUIRESREPUBLISH MODES AGE
nfs.csi.k8s.io false false false <unset> false Persistent 5m29s
3.為NFS創(chuàng)建StorageClass
# 我們需要創(chuàng)建一個使用 nfs.csi.k8s.io CSI驅動程序的Kubernetes存儲類矾兜。假設您已經配置了NFS共享 /srv/nfs 划栓,并且NFS服務器的地址是 10.0.0.42 庸推,請創(chuàng)建以下文件:
# sc-nfs.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: nfs-csi
provisioner: nfs.csi.k8s.io
parameters:
server: 10.1.31.0
share: /srv/nfs
reclaimPolicy: Delete
volumeBindingMode: Immediate
mountOptions:
- hard
- nfsvers=4.1
# 注意:上面YAML的最后一行表示NFS的特定版本。這應該與正在使用的NFS服務器的版本相匹配-如果您使用的是現(xiàn)有服務星爪,請檢查它使用的版本并進行相應調整。
# 然后在MicroK 8 s集群上應用:
microk8s kubectl apply -f sc-nfs.yaml
4.創(chuàng)建新PVC
# pvc-nfs.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pv-claim
spec:
storageClassName: nfs-csi
accessModes: [ReadWriteOnce]
resources:
requests:
storage: 5Gi
# 然后創(chuàng)建PVC:
microk8s kubectl apply -f pvc-nfs.yaml
# 驗證是否成功
root@ubuntu02:/home/k8s/nfs# microk8s kubectl describe pvc pv-claim
Name: pv-claim
Namespace: default
StorageClass: nfs-csi
Status: Bound
Volume: pvc-204c120b-123d-4343-a116-166f751ba616
Labels: <none>
Annotations: pv.kubernetes.io/bind-completed: yes
pv.kubernetes.io/bound-by-controller: yes
volume.beta.kubernetes.io/storage-provisioner: nfs.csi.k8s.io
volume.kubernetes.io/storage-provisioner: nfs.csi.k8s.io
Finalizers: [kubernetes.io/pvc-protection]
Capacity: 5Gi
Access Modes: RWO
VolumeMode: Filesystem
Used By: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Provisioning 2m31s nfs.csi.k8s.io_ubuntu04_b5d71960-e4c6-47f8-a498-4a1d4e90a399 External provisioner is provisioning volume for claim "default/pv-claim"
Normal ExternalProvisioning 2m31s (x2 over 2m31s) persistentvolume-controller waiting for a volume to be created, either by external provisioner "nfs.csi.k8s.io" or manually created by system administrator
Normal ProvisioningSucceeded 2m30s nfs.csi.k8s.io_ubuntu04_b5d71960-e4c6-47f8-a498-4a1d4e90a399 Successfully provisioned volume pvc-204c120b-123d-4343-a116-166f751ba616