それマグで!

知識はカップより、マグでゆっくり頂きます。 takuya_1stのブログ

習慣に早くから配慮した者は、 おそらく人生の実りも大きい。

OpenVZ@Ubuntu作る

注意:OpenVZを構成するときはOpenVZを正式採用したDebian lennyをオススメします。


OpenVZ@Ubuntu作る

OpenVZとは

Linuxを仮想化する。XenでいうところのParavitualに相当するのかな。
どっちかというとchroot jailに近い感じ。
商用VPS環境に使われている。

起動中に変更できるのがうれしい。
  • CPU割当
  • メモリ
  • HDDクォータ

を変更できる。

仮想ディスクなんて面倒なこと無し

結構速い快適。

商用の実績がある。

ある程度枯れた技術なので結構楽ちん。

VPSっぽくつかえる

XenVirtualBoxと同じでネットワーク設定みすっても復旧できる。安心。
その一方でXenVirtualBoxよりお手軽に快適にサーバ追加が出来る。

UbuntuにOpenVZを入れる。

Ubuntuの場合はOpenVZカーネルが標準に用意されていない。
そこでDebianのAPT Sourceを利用する。*1
DebianのAPTソースのURLを/etc/apt/sources.listに追加する。

/etc/apt/souces.list

/etc/apt/souces.listに以下を追加

deb http://download.openvz.org/debian etch main

debianの公開鍵をAPTに追加

wget -q http://download.openvz.org/debian/archive.key -O- | apt-key add -

*2
APTを更新して、VZカーネルを探す。

ML115AMD64Ubuntuを入れてたので、amd64版カーネルが見える。
takuya@ML115:~$sudo aptitude update
takuya@ML115:~$sudo aptitude search vz
p   linux-headers-2.6.18-openvz-12-1-amd64                          - Header files related to Linux kernel, specifically,
p   linux-headers-2.6.18-openvz-12-1etch1-amd64                     - Header files related to Linux kernel, specifically,
p   linux-headers-2.6.18-openvz-13-1etch4-amd64                     - Header files related to Linux kernel, specifically,
p   linux-headers-2.6.18-openvz-13-1etch5-amd64                     - Header files related to Linux kernel, specifically,
p   linux-headers-2.6.18-openvz-13-39.1d1-amd64                     - Header files related to Linux kernel, specifically,
p   linux-headers-2.6.18-openvz-13-51.3d1-amd64                     - Header files related to Linux kernel, specifically,
p   linux-headers-2.6.18-openvz-18-51.3d2-amd64                     - Header files related to Linux kernel, specifically,
p   linux-headers-2.6.18-openvz-18-53.5d1-amd64                     - Header files related to Linux kernel, specifically,
p   linux-headers-2.6.18-openvz-18-53.5d3-amd64                     - Header files related to Linux kernel, specifically,
p   linux-headers-2.6.18-openvz-amd64                               - Header files related to Linux kernel, specifically,
p   linux-headers-2.6.24-openvz-24-004.1d1-amd64                    - Header files related to Linux kernel, specifically,
p   linux-headers-2.6.24-openvz-24-004.1d3-amd64                    - Header files related to Linux kernel, specifically,
p   linux-image-2.6.18-openvz-12-1-amd64                            - Linux kernel binary image for version 2.6.18-openvz-12-1-amd64
p   linux-image-2.6.18-openvz-12-1etch1-amd64                       - Linux kernel binary image for version 2.6.18-openvz-12-1etch1-amd64
p   linux-image-2.6.18-openvz-13-1etch4-amd64                       - Linux kernel binary image for version 2.6.18-openvz-13-1etch4-amd64
p   linux-image-2.6.18-openvz-13-1etch5-amd64                       - Linux kernel binary image for version 2.6.18-openvz-13-1etch5-amd64
p   linux-image-2.6.18-openvz-13-39.1d1-amd64                       - Linux kernel binary image for version 2.6.18-openvz-13-39.1d1-amd64
p   linux-image-2.6.18-openvz-13-51.3d1-amd64                       - Linux kernel binary image for version 2.6.18-openvz-13-51.3d1-amd64
p   linux-image-2.6.18-openvz-18-51.3d2-amd64                       - Linux kernel binary image for version 2.6.18-openvz-18-51.3d2-amd64
p   linux-image-2.6.18-openvz-18-53.5d1-amd64                       - Linux kernel binary image for version 2.6.18-openvz-18-53.5d1-amd64
p   linux-image-2.6.18-openvz-18-53.5d3-amd64                       - Linux kernel binary image for version 2.6.18-openvz-18-53.5d3-amd64
p   linux-image-2.6.18-openvz-amd64                                 - Linux kernel binary image for version 2.6.18-openvz-amd64
p   linux-image-2.6.24-openvz-24-004.1d1-amd64                      - Linux kernel binary image for version 2.6.24-openvz-24-004.1d1-amd64
p   linux-image-2.6.24-openvz-24-004.1d3-amd64                      - Linux kernel binary image for version 2.6.24-openvz-24-004.1d3-amd64
p   vzctl                                                           - サーバ仮想化ソリューション - 制御用ツール
p   vzquota                                                         - server virtualization solution - quota tools

OpenVZに対応したカーネルを入れる

takuya@ML115:~$ sudo aptitude install vzctl
takuya@ML115:~$ sudo aptitude install linux-image-2.6.24-openvz-24-004.1d3-amd64

インストール後のAPT検索結果

takuya@ML115:~$ sudo aptitude search vz
(中略)
p   linux-image-2.6.24-openvz-24-004.1d1-amd64   
i   linux-image-2.6.24-openvz-24-004.1d3-amd64   
i   vzctl                                        
i A vzquota                                      

再起動する。

方法1:GRUBメニューからOpenVZカーネルを選択して起動。

起動時カーネル選択画面でOpenVZカーネルを選択して起動すればOK

方法2:GRUB設定を変更してOpenVZカーネルを自動起動にする。

SSH経由などでカーネル選択画面が見えないとき、GRUB画面を見に行くのが面倒なとき
この場合は起動するOpenVZカーネル起動をGRUBに設定する。
(設定ミスると、もちろんサーバーのモニタを見に行くことになる)

/boot/grub/menu.lst の10行目-14行目
 10 # You can specify 'saved' instead of a number. In this case, the default entry
 11 # is the entry saved with the command 'savedefault'.
 12 # WARNING: If you are using dmraid do not use 'savedefault' or your
 13 # array will desync and will not let you boot your system.
 14 default         4


GRUBのメニューリストはTitleから○○(空行)までが一つのカテゴリになっており。
複数個ある場合は、プログラムらしく0から数えます。
5番目にある、OpenVZのカーネルをデフォルトに指定するときは

14 default         4

と書きます。

menu.lstの例

132
133 title           Ubuntu 9.04, kernel 2.6.28-13-server
134 uuid            34f7dc95-2f48-4faf-9bbd-83cf1c1ad5c8
135 kernel          /vmlinuz-2.6.28-13-server root=/dev/mapper/pc160193095185-root ro quiet splash
136 initrd          /initrd.img-2.6.28-13-server
137 quiet
138
139 title           Ubuntu 9.04, kernel 2.6.28-13-server (recovery mode)
140 uuid            34f7dc95-
141 kernel          /vmlinuz-2.6.28-13-server root=/dev/mapper/pc160193095185-root ro  single
142 initrd          /initrd.img-2.6.28-13-server
143
144 title           Ubuntu 9.04, kernel 2.6.28-11-server
145 uuid            34f7dc95-
146 kernel          /vmlinuz-2.6.28-11-server root=/dev/mapper/pc160193095185-root ro quiet splash
147 initrd          /initrd.img-2.6.28-11-server
148 quiet
149
150 title           Ubuntu 9.04, kernel 2.6.28-11-server (recovery mode)
151 uuid            34f7dc95-
152 kernel          /vmlinuz-2.6.28-11-server root=/dev/mapper/pc160193095185-root ro  single
153 initrd          /initrd.img-2.6.28-11-server
154
155 title           Ubuntu 9.04, kernel 2.6.24-openvz-24-004.1d3-amd64
156 uuid            34f7dc95-
157 kernel          /vmlinuz-2.6.24-openvz-24-004.1d3-amd64 root=/dev/mapper/pc160193095185-root ro quiet splash
158 initrd          /initrd.img-2.6.24-openvz-24-004.1d3-amd64
159 quiet
160
161 title           Ubuntu 9.04, kernel 2.6.24-openvz-24-004.1d3-amd64 (recovery mode)
162 uuid            34f7dc95-
163 kernel          /vmlinuz-2.6.24-openvz-24-004.1d3-amd64 root=/dev/mapper/pc160193095185-root ro  single
164 initrd          /initrd.img-2.6.24-openvz-24-004.1d3-amd64
165
166 title           Ubuntu 9.04, memtest86+
167 uuid            34f7dc95-
168 kernel          /memtest86+.bin
169 quiet

これで再起動後OpenVZカーネルで起動します。

設定が問題ないことを確認して再起動

takuya@ML115:~$ uname -a #←再起動前にunameの実行結果を保存しておくと後で比較しやすいかもね。
takuya@ML115:~$ sudo shutdown -r now

OpenVZのカーネルで起動したことを確認します。

takuya@ML115:~$ uname -a
Linux ML115 2.6.24-openvz-24-004.1d3-amd64 #1 SMP Sat Jan 10 16:50:52 MSK 2009 x86_64 GNU/Linux


これでOpenVZの準備は完了です。

仮想サーバが外部と通信できるようにする。

このままでは、ホストOS外とVPSが通信できない。ネットワークを疎通させる。
特にVPSにグローバルIPを割り振れる環境など。

/etc/sysctl.conf
 68 net.ipv4.ip_forward = 1
 69 net.ipv4.conf.default.proxy_arp = 0
 70 net.ipv4.conf.all.rp_filter = 1
 71 kernel.sysrq = 1
 72 net.ipv4.conf.default.send_redirects = 1
 73 net.ipv4.conf.all.send_redirects = 0
sysctlをリロード
sudo sysctl -p

*1:vzctlはあるのに変な感じ、ミスなのかな

*2:openvz公式にdebiandebian-systsがあるのだけれど違いがよくわからなかった。debian.systsって非公式のユーザーレポジトリらしい。