1. 程式人生 > >LXC內啟動NFS服務(NFS server on LXC)

LXC內啟動NFS服務(NFS server on LXC)

本文記錄在LXC內實現NFS server的過程,google了不少資料+動手實踐總結下配置步驟。

實驗環境

host: localhost lxcbr0 10.0.3.1
lxc:
server:mgmt 10.0.3.100
client: test01 10.0.3.101

操作步驟

整個配置過程按照機器型別分為三部分,分別是host端、lxc_server端和lxc_client端。主要部分在host端。

Host端

1.共享核心版本模組
即,/lib/modules目錄,通過在host端直接在lxc外面共享給lxc
(LXC是否有其他配置可以實現這方面的功能還不太清楚,暫留)

cat /etc/fstab
/lib/modules    /var/lib/lxc/mgmt/rootfs/lib/modules    none    bind,ro 0 0
/lib/modules    /var/lib/lxc/test01/rootfs/lib/modules  none    bind,ro 0 0

2.安裝nfs-kernel服務
須在host端安裝nfs-kernel並載入nfs核心模組

apt-get install nfs-kernel-server
modprobe nfs
root@localhost:~# modinfo nfs
filename:       /lib/modules/3.19
.0-25-generic/kernel/fs/nfs/nfs.ko ……

3.修改LXC配置檔案

vim /var/lib/lxc/mgmt/config
#新增
# When using LXC with apparmor, uncomment the next line to run unconfined:
lxc.aa_profile = unconfined

4.use apparmor
如步驟3註釋部分所述,修改apparmor檔案

root@localhost:~# vim /etc/apparmor.d/lxc/lxc-default

# Do not load this file.  Rather, load /etc/apparmor.d/lxc-containers, which
# will source all profiles under /etc/apparmor.d/lxc profile lxc-container-default flags=(attach_disconnected,mediate_deleted) { #include <abstractions/lxc/container-base> # the container may never be allowed to mount devpts. If it does, it # will remount the host's devpts. We could allow it to do it with # the newinstance option (but, right now, we don't). deny mount fstype=devpts, allow mount fstype=rpc_pipefs, allow mount fstype=nfs, }

新增最後兩行allow內容

root@localhost:~# vim /etc/apparmor.d/lxc/lxc-default-with-mounting
##新增這一行
mount fstype=nfs,
apt-get install vzctl

安裝完成後openvz自動啟動

至此,Host端配置完成。

lxc_server端

1.安裝nfs-utils

yum install rpcbind
yum install nfs-utils
modinfo nfs

2.配置nfs服務

[root@mgmt ~]# vim /etc/sysconfig/nfs

MOUNTD_NFS_V3="yes"
RPCNFSDARGS="-N 4"
NFSD_MODULE="noload"

3.啟動服務

[root@mgmt ~]# /etc/init.d/nfs start
Starting NFS services:                                     [  OK  ]
Starting NFS mountd:                                       [  OK  ]
Starting NFS daemon:                                       [  OK  ]
Starting RPC idmapd:                                       [  OK  ]

檢視狀態

[[email protected] ~]# /etc/init.d/nfs status
rpc.svcgssd is stopped
rpc.mountd (pid 1516) is running...
nfsd dead but subsys locked

雖然提示nfs dead,但是不影響使用。

下面是client端

lxc_client端

1.安裝utils工具

yum install nfs-utils
[root@test01 ~]# showmount -e mgmt
Export list for mgmt:
/home *

掛載之後檢視

[root@test01 ~]# mount -a
[root@test01 ~]# df
Filesystem                                             1K-blocks    Used Available Use% Mounted on
/dev/disk/by-uuid/36634757-bb73-43f2-8b90-97eea7aa57a8  29387860 4102148  23769836  15% /
/dev/disk/by-uuid/36634757-bb73-43f2-8b90-97eea7aa57a8  29387860 4102148  23769836  15% /lib/modules
tmpfs                                                    2015336       0   2015336   0% /dev/shm
mgmt:/home                                              29388288 4102144  23770112  15% /home

完成。

Trouble shooting

lxc_server 首次開機後,lxc_client端可能無法掛載,提示access denied,重啟lxc_server的nfs即可。