LXC 可以讓不同 Container 之間都可以共同存取,用來假裝 NFS 目錄,作一些情境演練。例如,PostgreSQL 的 HA 設置中,需要共用目錄存放 WAL 備份。
當然,直接讓某個 Container 設置 NFS 讓其他 Container 存取也是完全 OK 的~
這邊示範,把外部的 /media/share/ 目錄,掛載到兩個 container 裡面的 /mount/share/ 目錄下。主要是設定 Container 外部的 Config 檔案就可以了
[user@lxc-lab ~]$ sudo mkdir -p /media/share/ [user@lxc-lab ~]$ sudo chmod 777 /media/share/ [user@lxc-lab ~]$ sudo mkdir -p /var/lib/lxc/container1/rootfs/mount/share/ /var/lib/lxc/container2/rootfs/mount/share/ [user@lxc-lab ~]$ [user@lxc-lab ~]$ sudo tee -a /var/lib/lxc/container1/config << EOF # Mounting external Directory in R/W mode lxc.mount.entry = /media/share/ mount/share/ none bind 0.0 EOF [user@lxc-lab ~]$ sudo tee -a /var/lib/lxc/container2/config << EOF # Mounting external Directory in R/W mode lxc.mount.entry = /media/share/ mount/share/ none bind 0.0 EOF [user@lxc-lab ~]$
然後啟動兩個 Container,進去 container1 產生一個空檔案,再到 container2 寫看看,再回 container1 確認一下
[user@lxc-lab ~]$ sudo ./startenv.sh container1 container2 Starting container environment container1 Starting container environment container2 [user@lxc-lab ~]$ sudo ./envlogin.sh container1 Connected to tty 0 Typeto exit the console, to enter Ctrl+a itself CentOS Linux 7 (Core) Kernel 3.10.0-957.1.3.el7.x86_64 on an x86_64 container1 login: root Password: Last login: Sun Jan 27 13:55:45 on lxc/console [root@container1 ~]# [root@container1 ~]# touch /mount/share/container1file [root@container1 ~]# exit logout CentOS Linux 7 (Core) Kernel 3.10.0-957.1.3.el7.x86_64 on an x86_64 container1 login: [user@lxc-lab ~]$ [user@lxc-lab ~]$
[brandon_hsu@lxc-lab ~]$ sudo ./envlogin.sh container2 Connected to tty 0 Typeto exit the console, to enter Ctrl+a itself CentOS Linux 7 (Core) Kernel 3.10.0-957.1.3.el7.x86_64 on an x86_64 container2 login: root Password: [root@container2 ~]# ls /mount/share/ container1file [root@container2 ~]# [root@container2 ~]# echo 'Write by container2' >> /mount/share/container1file [root@container2 ~]# exit logout CentOS Linux 7 (Core) Kernel 3.10.0-957.1.3.el7.x86_64 on an x86_64 container2 login: [brandon_hsu@lxc-lab ~]$ [brandon_hsu@lxc-lab ~]$
[brandon_hsu@lxc-lab ~]$ sudo ./envlogin.sh container1 Connected to tty 0 Typeto exit the console, to enter Ctrl+a itself CentOS Linux 7 (Core) Kernel 3.10.0-957.1.3.el7.x86_64 on an x86_64 container1 login: root Password: Last login: Sun Jan 27 13:56:07 on lxc/console [root@container1 ~]# cat /mount/share/container1file Write by container2 [root@container1 ~]#
這樣就是掛載成功了,兩個 Container 可以同時讀寫這個目錄的內容了
沒有留言:
張貼留言