K8s 資料掛載 - NFS

NFS (Network File System) 是讓 Unix Like 的作業系統, 能用 mount 的方式來存取遠端伺服器分享出來的目錄, 讓操作起來像操作本機目錄一樣。
 

NFS 怎麼運作

透過 TCP/IP 網路層,Client 會去存取掛在 Server 的檔案,在 Client 的角度看來,檔案就像在自己的本地端一樣
 

為什麼要用 NFS

  • 客戶可以像訪問本地文件一樣訪問遠端主機上的文件。
  • 使用共享應用程序可以降低存放成本,並且無需本地硬碟空間。
  • 數據中心化可以減少系統管理開銷。
    • 降低 Migration 成本
 

Ubuntu 官方操作教學

 
 

NFS server 架設

  1. 安裝並開啟 nfs-kernel-server
sudo apt install nfs-kernel-server sudo systemctl start nfs-kernel-server.service
  1. 設定 /etc/exports 檔案,設定可以共用給 client 的資料夾
sudo vim /etc/exports
/jpetstore *(rw,async,no_subtree_check,no_root_squash)
  • sync / async options control whether changes are gauranteed to be committed to stable storage before replying to requests. async thus gives a performance benefit but risks data loss or corruption. Even though sync is the default, it’s worth setting since exportfs will issue a warning if it’s left unspecified.
    • 也就是說選擇 sync 會比較慢、async 會比較快(不用等 commit),到時候做實驗可能可以調整這個變因
  • With subtree checking enabled, the NFS server checks to ensure that the requested file or directory is within the exported file system tree, and if not, it denies access.
  • no_subtree_checkdisables a security verification that subdirectories a client attempts to mount for an exported filesystem are ones they’re permitted to do so.
    • 也就是說如果關掉的話效能會比較快,但是可能有資安疑慮,因為不會對 access 的目錄做檢查
  • * 必須改成 client 的 ip
  • 其他
    • notion image
 
  1. 創建掛載點的資料夾
sudo mkdir /jpetstore
  1. apply 設定
sudo exportfs -a

NFS Client 架設

  1. 安裝並開啟 nfs-common
sudo apt install nfs-common
  1. 創建資料夾並開始掛載!!
sudo mkdir /mnt/nfs-jpetstore sudo mount {example.hostname.com}:/jpetstore /mnt/nfs-jpetstore
  1. 更改 K8s pvc 設定:hostpathnfs
apiVersion: v1 kind: PersistentVolume metadata: name: mysql-pv-volume namespace: jpetstore-dev labels: type: local spec: storageClassName: manual capacity: storage: 10Gi accessModes: - ReadWriteOnce nfs: server: 140.119.19.25 path: /jpetstore --- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: mysql-pv-claim namespace: jpetstore-dev spec: storageClassName: manual accessModes: - ReadWriteOnce resources: requests: storage: 5Gi
 
 
 
💡
在控管掛載資料夾權限中,還有很多複雜的情況 像是多個不同層級的 client 如果都要共用同一個 NFS Server 的資料夾,該如何控制讀寫的權限 這部分就不在我想探討的問題了,比較偏向另一個實務管理主題