一般步驟:搭建nfs>創建pv>創建pvc>創建pod
搭建nfs
#master節點安裝nfs [root@k8s-master nginx]# yum -y install nfs-utils #創建nfs目錄 [root@k8s-master nginx]# mkdir -p /nfs/data/ #修改權限 [root@k8s-master nginx]# chmod -R 777 /nfs/data #編輯export文件,這個文件就是nfs默認的配置文件,要共享多個文件就都在這里注冊一下,並創建相應目錄並給與權限,也可以將*改為指定的IP地址,多個ip就空格隔開 [root@k8s-master nginx]# vim /etc/exports /nfs/data *(rw,no_root_squash,sync) #配置生效 [root@k8s-master nginx]# exportfs -r #查看生效 [root@k8s-master nginx]# exportfs /nfs/data <world> #啟動rpcbind、nfs服務 [root@k8s-master nginx]# systemctl restart rpcbind && systemctl enable rpcbind [root@k8s-master nginx]# systemctl restart nfs && systemctl enable nfs Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service. #查看 RPC 服務的注冊狀況 [root@k8s-master nginx]# rpcinfo -p localhost program vers proto port service 100000 4 tcp 111 portmapper 100000 3 tcp 111 portmapper 100000 2 tcp 111 portmapper 100000 4 udp 111 portmapper #showmount測試 [root@k8s-master nginx]# showmount -e 192.168.0.66 Export list for 192.168.0.66:
#掛載對應的nfs目錄到本地, 掛載前要檢查此目錄是否存在
[root@k8s-master nginx]# mount -t nfs 10.10.14.52:/nfs/data /nfs/data
#取消掛載(對應的本地路徑)
[root@k8s-master nginx]# umount /nfs/data
/etc/exports文件內容格式: <輸出目錄> [客戶端1 選項(訪問權限,用戶映射,其他)] [客戶端2 選項(訪問權限,用戶映射,其他)] a. 輸出目錄: 輸出目錄是指NFS系統中需要共享給客戶機使用的目錄; b. 客戶端: 客戶端是指網絡中可以訪問這個NFS輸出目錄的計算機 客戶端常用的指定方式 指定ip地址的主機:192.168.0.200 指定子網中的所有主機:192.168.0.0/24 192.168.0.0/255.255.255.0 指定域名的主機:david.bsmart.cn 指定域中的所有主機:*.bsmart.cn 所有主機:* c. 選項: 選項用來設置輸出目錄的訪問權限、用戶映射等。NFS主要有3類選項: 訪問權限 設置輸出目錄只讀:ro 設置輸出目錄讀寫:rw 用戶映射 all_squash:將遠程訪問的所有普通用戶及所屬組都映射為匿名用戶或用戶組(nfsnobody); no_all_squash:與all_squash取反(默認設置); root_squash:將root用戶及所屬組都映射為匿名用戶或用戶組(默認設置); no_root_squash:與rootsquash取反; anonuid=xxx:將遠程訪問的所有用戶都映射為匿名用戶,並指定該用戶為本地用戶(UID=xxx); anongid=xxx:將遠程訪問的所有用戶組都映射為匿名用戶組賬戶,並指定該匿名用戶組賬戶為本地用戶組賬戶(GID=xxx); 其它選項 secure:限制客戶端只能從小於1024的tcp/ip端口連接nfs服務器(默認設置); insecure:允許客戶端從大於1024的tcp/ip端口連接服務器; sync:將數據同步寫入內存緩沖區與磁盤中,效率低,但可以保證數據的一致性; async:將數據先保存在內存緩沖區中,必要時才寫入磁盤; wdelay:檢查是否有相關的寫操作,如果有則將這些寫操作一起執行,這樣可以提高效率(默認設置); no_wdelay:若有寫操作則立即執行,應與sync配合使用; subtree:若輸出目錄是一個子目錄,則nfs服務器將檢查其父目錄的權限(默認設置); no_subtree:即使輸出目錄是一個子目錄,nfs服務器也不檢查其父目錄的權限,這樣可以提高效率;
固定nfs服務端口以便設置防火牆 vi /etc/sysconfig/nfs # uncomment the following options RQUOTAD_PORT=875 LOCKD_TCPPORT=32803 LOCKD_UDPPORT=32769 MOUNTD_PORT=892 STATD_PORT=662 # then stop nfs, stop prcbind, start rpcbind, start nfs # use "lsof -nPi" to check the listening ports
iptables中需要開放的端口 -A INPUT -m state --state NEW -m tcp -p tcp --dport 111 -j ACCEPT -A INPUT -m state --state NEW -m udp -p udp --dport 111 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 662 -j ACCEPT -A INPUT -m state --state NEW -m udp -p udp --dport 662 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 875 -j ACCEPT -A INPUT -m state --state NEW -m udp -p udp --dport 875 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 892 -j ACCEPT -A INPUT -m state --state NEW -m udp -p udp --dport 892 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 2049 -j ACCEPT -A INPUT -m state --state NEW -m udp -p udp --dport 2049 -j ACCEPT
創建pvc
apiVersion: v1 kind: PersistentVolume metadata: name: nfs-pv namespace: default labels: pv: nfs-pv spec: capacity: storage: 100Mi accessModes: - ReadWriteMany persistentVolumeReclaimPolicy: Retain # storageClassName: nfs 可以不指定,但要指定前必須先創建,否則pvc會報錯 nfs: server: 192.168.0.66 path: "/nfs/data" #NFS目錄,需要該目錄在NFS上存在
[root@k8s-master nfs]# kubectl apply -f pv.yaml persistentvolume/nfs-pv created [root@k8s-master nfs]# kubectl get pv NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE nfs-pv 100Mi RWX Retain Available
創建pvc
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: nfs-pvc namespace: default spec: accessModes: - ReadWriteMany resources: requests: storage: 50Mi #容量 selector: matchLabels: pv: nfs-pv #關聯pv 的label,key/value要一致,可以不指定,pvc會自動找到匹配的pv,否則會一直處於pending狀態直到有合適的pv
創建pod
vim nginx.yaml #我們用nginx鏡像進行驗證,將html目錄映射到nfs目錄中 #deploy apiVersion: apps/v1 kind: Deployment metadata: name: nfs-nginx namespace: default spec: selector: matchLabels: app: nfs-nginx replicas: 2 template: metadata: labels: app: nfs-nginx spec: containers: - name: nginx-web image: nginx:latest ports: - containerPort: 80 volumeMounts: - mountPath: /usr/share/nginx/html name: html volumes: - name: html persistentVolumeClaim: claimName: nfs-pvc --- #service apiVersion: v1 kind: Service metadata: name: nfs-nginx namespace: default spec: type: NodePort ports: - port: 80 protocol: TCP targetPort: 80 nodePort: 31681 selector: app: nfs-nginx
[root@k8s-master nfs]# kubectl apply -f nginx.yaml [root@k8s-master nfs]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nfs-nginx-7695b95db6-l74zx 1/1 Running 0 12s 10.244.2.93 k8s-node1 <none> <none> nfs-nginx-7695b95db6-qcqp8 1/1 Running 0 12s 10.244.1.22 k8s-node2 <none> <none>
驗證
各節點安裝並啟用nfs yum install nfs-utils systemctl start nfs & systemctl enable nfs systemctl start rpcbind & systemctl enable rpcbind
我們在/nfs/data/nginx目錄創建了一個1.html文件 <html> <body>Test01</body> </html> 在容器1的/usr/share/nginx/html目錄創建文件2.html <html> <body>Test02</body> </html> 在容器2的/usr/share/nginx/html目錄創建文件3.html <html> <body>Test03</body> </html> 分別瀏覽器訪問路由正常 此外我們進入容器查看,目錄中文件是共享的: root@nfs-nginx-7695b95db6-l74zx:/usr/share/nginx/html# ls 1.html 2.html 3.html pod銷毀重建 kubectl delete -f nginx.yaml kubectl apply -f nginx.yaml 再次訪問1.html/2.html/3.html,依舊可以訪問到,說明文件未丟失。 root@nfs-nginx-7695b95db6-78wml:/usr/share/nginx/html# ls 1.html 2.html 3.html #新創建的容器,依舊可以看到這些文件
使用kuboard方式:先創建nfs服務,步驟同上
再在pod里配置:
方法一:適合於配置了pvc的方式
前面的名字logs隨便自定義,下面的存儲卷聲明選自己綁定的pvc
pod配置里添加掛載點,第一行是容器內路徑,自定義,第二行是權限,第三行選你上圖中自定義的名字,后面的子路徑可以自定義。
方法二:沒有配置pvc(更簡便)
第一行第二個選NFS,下面第一行填你nfs服務端的ip,第二行填你nfs服務端的掛載路徑
同樣在pod配置的掛載點里添加容器內掛載路徑,自定義的掛載卷名字以及子路徑