FreeBSD jail 類似 Linux 中的 KVM ,可以在同一個作業系統裡並存多個子系統,與虛擬機器的不同處在於這些子系統是共用同一份核心,因此佔用較少的硬體資源;通常這種子系統稱為容器 (container),除了用於沙箱實驗外,也常出現 service jail 的應用,可以想成是系統層級的 bundle 。
- 目標
- 使用 ezjail 設定與管理 jail
- 使用 pf.conf 設定 NAT 允許從外部存取 jail 內的 nginx
- 在 jail 中架設 nginx
- Note
- 以下步驟需要使用 root 權限,或是另行安裝 sudo 。
- 文中以 host / jail 分別代表安裝 jail 的主系統 與 jail 子系統
- 除了 ezjail 外其實有另一套 qjail ,這兩個之間似乎有點恩怨。
Step 1. 安裝與初始化 (host)
# 取得 ezjail 套件。
pkg install -y ezjail
# 指定從何處取得佈署檔案,不給 -h 參數的話會從 ftp 取得,這邊我是從安裝光碟取得
ezjail-admin install -h file:///media/cdrom/usr/freebsd-dist
Optional: 啟用 ezjail 中的 zfs 支援 (host)
若你的 host 使用 zfs 的話,可以啟用 ezjail 中的相關選項讓 jails 使用 zfs 的 datasets
# 建立 zfs dataset 給 jail 用
zfs create zroot/usr/jails
編輯 /usr/local/etc/ezjail.conf
# ZFS options
# Setting this to YES will start to manage the basejail and newjail in ZFS
ezjail_use_zfs="YES"
# Setting this to YES will manage ALL new jails in their own zfs
ezjail_use_zfs_for_jails="YES"
# The name of the ZFS ezjail should create jails on, it will be mounted at the ezjail_jaildir
ezjail_jailzfs="zroot/usr/jails"
Step 2. 建立 jail (host)
ezjail-admin create www 10.1.1.1
剛建立好的 jail 還需要設定網路環境才能從外部存取。
Step 3. 設定網路 (host)
每一個 jail 都需要一個固定 IP ,這裡採用建立新的 local network (lo1) 的方式,可移植性較佳。
編輯 /etc/rc.conf
# 給所有 jail 使用的介面 (不可與其它介面重複)
cloned_interfaces="lo1"
ifconfig_lo1="inet 10.1.1.1 netmask 255.255.255.0"
# 額外的 jail 使用同一介面的別名 (alias) ,注意遮罩設定為 255.255.255.255
# ifconfig_lo1_alias0="inet 10.1.1.2 netmask 255.255.255.255"
# 啟用 NAT 與 packet filter
pf_enable="YES"
pf_rules="/etc/pf.conf"
# 開機時啟動 jail
ezjail_enable="YES"
新增介面 lo1
% ifconfig lo1 create
% ifconfig lo1 inet 10.1.1.1 netmask 255.255.255.0
編輯 /etc/pf.conf
# 介面,用 ifconfig 查看對外介面 (external interface, ext_if) 的名稱,我的是 em0
ext_if="em0"
int_if="lo1"
jailnet= $int_if:network
# NAT 允許從 jailnet 到 ext_if 的傳輸
nat on $ext_if from $jailnet to any -> ($ext_if)
# port forwarding
# www 是我們支後要建立的 jail 名稱,IP 則是前面指定過的
www="10.1.1.1"
# 允許 HTTP/HTTPS 的傳輸
rdr pass on $ext_if inet proto tcp to port http -> $www port http
rdr pass on $ext_if inet proto tcp to port https -> $www port https
接著就重新啟動吧!
Step 4. 設定 jail 內部環境 (jail)
使用 jls 查看有哪些 jail 跑起來
% jls
JID IP Address Hostname Path
1 10.1.1.1 www /usr/jails/www
進入 jail
% jexec www tcsh
編輯 /etc/resolv.conf
nameserver 8.8.8.8
這時候應該可以從 jail 裡面存取外部網路了,用 host 檢查一下
% host freebsd.org
freebsd.org has address 8.8.178.110
freebsd.org has IPv6 address 2001:1900:2254:206a::50:0
freebsd.org mail is handled by 10 mx1.freebsd.org.
Step 5. 安裝 nginx (jail)
使用 pkg 安裝 nginx
% pkg install -y nginx
編輯 /etc/rc.conf
nginx_enable="YES"
離開 jail 並重新啟動所有 jails
% exit
# this will block if any other session is attached to a jail via jexec
% service ezjail restart
Step 6. 測試(host)
% curl 10.1.1.1 # 也可以找不同網域的機器嘗試存取,記得 IP 須換成 host IP
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
結論
- 目標
- 使用 ezjail 設定與管理 jail
- 使用 pf.conf 設定 NAT 允許從外部存取 jail 內的 nginx
- 在 jail 中架設 nginx
以上目標達成!
為了一個簡單的 web service 使用 jail 看似多餘,但是當額外的應用越來越多,比如架設一些應用性的服務、資料庫等等,維護套件相依性的問題就會越來越複雜,jail 讓我們得以把需要的套件區隔開來,尤其再遇到套件版本問題時,jail 的好處就更加顯著。
Future Work
- Bugzilla in Jail (postgresql/nginx)
- Jail backup/migration
Written with StackEdit.
留言
張貼留言