那一年踩過的NFS

莉森羊
莉森羊
Aug 22, 2017 · 4 min read

需求描述

啟動nfs server,讓client端的docker container能夠共享server端的目錄夾

實作時間

2016.10

作法說明

一段原先想的可能複雜的做法,在遇到困難後,發現轉個彎的替代方案仍能達成需求的方式。

✗ 原先做法

希望能讓兩台host中的nfs container能互相溝通,但實作結果:在client端會無法與本機host成功掛載,client端其他containers無法共享到nfs server的資料夾。

這段過程滿勞力傷神的,因為莫名執著於要百分百用docker實作NFS的server跟client,dockerfile都寫好了,忙著處理防火牆等等,一關突破一關結果最後卻還是卡關。那時腦中浮現的是這畫面……

(學鴨叫)

但其實,能解決問題的都算是好解法,放下執著後,用下面的替代方案簡直秒解啊。

✓ 替代做法(且比較簡單)

改作兩台host間的nfs,完成後再用將docker container以-v的方式與client端的本機資料夾掛載。

實作腳本與驗證

1. server端

# 安裝nfs
yum install nfs-utils
mkdir /var/nfssharechmod -R 777 /var/nfsshare/echo "/var/nfsshare *(rw,sync,no_root_squash,no_all_squash)" >> /etc/exports# 啟動nfs serversystemctl start nfs-server.servicesystemctl enable nfs-server.service# 開通防火牆firewall-cmd --permanent --add-service=nfsfirewall-cmd --reload# 在 NFS Server 上,發佈分享目錄exportfs -r# 在 NFS Server 上,啟動 SELiux 放行規定setsebool -P nfs_export_all_rw onsetsebool -P nfs_export_all_ro on# 在 NFS Server 上,查詢分享狀態showmount -e

client端

# client端也要安裝 nfs-utilsyum install nfs-utils# 永久關閉SELinuxsed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux# 重新開機使設定生效reboot# 檢查 SELinux 的狀態getenforce# 在 client 端,掛載 NFS Server 所分享目錄mkdir /mnt/nfssharemount -t nfs 192.168.10.132:/var/nfsshare /mnt/nfsshare# 下次重新開機後自動掛載echo "192.168.10.132:/var/nfsshare /mnt/nfsshare nfs defaults 0 0" >> /etc/fstab

驗證

# client端cd /mnt/nfsshare/touch 1.txt# 到server端查看是否有新增檔案1.txtcd cd /var/nfsshare/ls# 在client端 run docker container(以mongo為例)docker run --name some-mongo -v /mnt/nfsshare:/share -d mongo# 進入shelldocker exec -ti some-mongo bash>>cd /share/>>ls>>touch 2.txt# 再回去server端查看是否有新增檔案2.txtcd cd /var/nfsshare/ls

)
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade