一、NFS 介紹
NFS 即網絡文件系統(Network File-System),能夠經過網絡讓不一樣機器、不一樣系統之間能夠實現文件共享。經過 NFS,能夠訪問遠程共享目錄,就像訪問本地磁盤同樣。NFS 只是一種文件系統,自己並無傳輸功能,是基於 RPC(遠程過程調用)協議實現的,採用 C/S 架構。html
二、安裝 NFS 軟件包apache
#1安裝nfs服務端 sudo apt install nfs-kernel-server -y #2. 建立目錄 sudo mkdir -p /data/k8s/ #3. 使任何客戶端都可訪問 sudo chown nobody:nogroup /data/k8s/ #sudo chmod 755 /data/k8s/ sudo chmod 777 /data/k8s/ #4. 配置/etc/exports文件, 使任何ip都可訪問(加入如下語句) vi /etc/exports /data/k8s/ *(rw,sync,no_subtree_check) #5. 檢查nfs服務的目錄 sudo exportfs -ra (從新加載配置) sudo showmount -e (查看共享的目錄和容許訪問的ip段) #6. 重啓nfs服務使以上配置生效 sudo systemctl restart nfs-kernel-server #sudo /etc/init.d/nfs-kernel-server restart #查看nfs服務的狀態是否爲active狀態:active(exited)或active(runing) systemctl status nfs-kernel-server #7. 測試nfs服務是否成功啓動 #安裝nfs 客戶端 sudo apt-get install nfs-common #建立掛載目錄 sudo mkdir /data/k8s/ #7.4 在主機上的Linux中測試是否正常 sudo mount -t nfs -o nolock -o tcp 192.168.100.11:/data/k8s/ /data/k8s/(掛載成功,說明nfs服務正常) #錯誤 mount.nfs: access denied by server while mounting
3建立pv(master上)vim mypv.yaml //內容以下vim
apiVersion: v1 kind: PersistentVolume metadata: name: pv001 spec: capacity: storage: 100M accessModes: - ReadWriteMany nfs: path: /data/k8s/ server: 192.168.100.11
kubectl create -f mypv.yamlapi
kubectl get pvbash
狀態爲Available,這是由於它尚未綁定到任何的pvc上面,當定義完pvc後,就能夠自動綁定了。網絡
4 建立pvc(master上)vim mypvc.yaml //內容以下架構
kind: PersistentVolumeClaim apiVersion: v1 metadata: name: myclaim spec: accessModes: - ReadWriteMany resources: requests: storage: 8M
kubectl create -f mypvc.yamlcurl
kubectl get pvctcp
能夠看到,pvc狀態爲Bound,它綁定了pv001ide
5 定義pod vim pvpod.yaml //內容以下
apiVersion: v1 kind: Pod metadata: name: httpd-pvpod spec: containers: - image: httpd name: httpd-withpvc-pod imagePullPolicy: Always volumeMounts: - mountPath: "/usr/local/apache2/htdocs/" name: httpd-volume volumes: - name: httpd-volume persistentVolumeClaim: claimName: myclaim
kubectl create -f pvpod.yaml
kubectl describe pod httpd-pvpod //查看Volumes那部分裏的ClaimName
6 驗證
#1到NFS的共享目錄下建立一個文件 cd /data/k8s/ echo "Test file" > 1.html #2進入到httpd-pod裏 #kubectl exec -it httpd-pvpod bash kubectl exec -it httpd-pvpod /bin/sh cat /usr/local/apache2/htdocs/1.html #3刪除httpd-pvpod kubectl delete pod httpd-pvpod cat /data/k8s/1.html #4重建httpd-pod kubectl create -f pvpod.yaml #5curl訪問 kubectl get pod httpd-pvpod -o wide //查看其對應的IP