OpenVZを使ってみる

はじめに

共用サーバのような物を作ろうと思って,chrootSElinuxの組み合わせをどうすればいいのか考えていたらOpenVZというものを教えてもらいました.VMWare上で環境を動かすところまでうまくいったので,そのまとめです.

ホスト環境

windowsXPVMWare Serverを入れて,その上にCentOS 5.4 x86_64をインストールしました.サーバの名前はpotate,ネットワークはNAT構成で,512MBのメモリと8GBの仮想ディスクを設定しました.インストールに使ったのはCentOS-5.4-x86_64-netinstall.isoで,textベースのインストーラを使用し,ナビゲーションは英語で,キーボード設定はjp106,IPv4dhcpのみ使って,http://ftp.riken.jp/Linux/centos/5.4/os/x86_64から必要なデータをダウンロードしました.Package Selectionでは何も選択していません.SELinuxはdiabledにしておきました.

OpenVZのインストール

基本的にはQuick installationを見ながらの作業になりますが,x86_64のツールにはバグがあるようなので,途中からInstall OpenVZ on a x86_64を見る必要があります.

Quick installationを見ての作業

まずはパッケージを最新の状態にしてからOpenVZ用のカーネルをインストールします.

# yum update
# cd /etc/yum.repos.d
# wget http://download.openvz.org/openvz.repo
# rpm --import  http://download.openvz.org/RPM-GPG-Key-OpenVZ
# yum install ovzkernel.x86_64
...
Installed:
  ovzkernel.x86_64 0:2.6.18-164.10.1.el5.028stab067.4    

次にkernel周りの設定を変更します.まずは/boot/grub/grub.conf

--- grub.conf.org       2010-02-09 18:09:13.000000000 +0900
+++ grub.conf   2010-02-09 18:09:29.000000000 +0900
@@ -11,7 +11,7 @@
 timeout=5
 splashimage=(hd0,0)/grub/splash.xpm.gz
 hiddenmenu
-title CentOS (2.6.18-164.10.1.el5.028stab067.4)
+title OpenVZ (2.6.18-164.10.1.el5.028stab067.4)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-164.10.1.el5.028stab067.4 ro root=/dev/VolGroup00/LogVol00
        initrd /initrd-2.6.18-164.10.1.el5.028stab067.4.img

それから/etc/sysct.confを変更します.

--- sysctl.conf.org     2010-02-09 18:26:50.000000000 +0900
+++ sysctl.conf 2010-02-09 18:29:56.000000000 +0900
@@ -4,7 +4,10 @@
 # sysctl.conf(5) for more details.
 
 # Controls IP packet forwarding
-net.ipv4.ip_forward = 0
+net.ipv4.ip_forward = 1
+
+# Controls proxy arp
+net.ipv4.default.proxy_arp = 0
 
 # Controls source route verification
 net.ipv4.conf.default.rp_filter = 1
@@ -12,8 +15,12 @@
 # Do not accept source routing
 net.ipv4.conf.default.accept_source_route = 0
 
+# We do not want all our interfaces to send redirects
+net.ipv4.conf.default.send_redirects = 1
+net.ipv4.conf.all.send_redirects = 0
+
 # Controls the System Request debugging functionality of the kernel
-kernel.sysrq = 0
+kernel.sysrq = 1
 
 # Controls whether core dumps will append the PID to the core filename
 # Useful for debugging multi-threaded applications

これで準備できたのでpotateをリブートします.Quick installationを見るのはここまでです.

Install OpenVZ on a x86 64 system Centos-Fedora を見ての作業

といっても,パッチをダウンロードして実行するだけです.

# cd /tmp
# wget http://linux.carreira.com.pt/ovzutils/setx86_64-0.5.tar.gz
# tar xvfz setx86_64-0.5.tar.gz
# sh setx86_64
---------------------------------------------
 Openvz to x86_64 platform (Ver. 0.5)
---------------------------------------------
Your OS seems to be........ CentOS release 5.4 (Final)
rpm, yum and python........ Found!
setx86_64: line 90: [: too many arguments
Python version... 2.4...... OK!
rpm-python for x86_64...... Found!
---------------------------------------------
ALL TESTS PASSED!

Do you want to go on with the installation? [N/y] 

...
 All templates installed!
vzrpm python is fixed for 64 bits...
Applying patches...
patching file /usr/share/vzpkg/cache-os
patching file /usr/share/vzpkg/functions
patching file /usr/bin/vzyum
patching file /usr/bin/vzrpm
patching file /vz/template/centos/5/x86_64/config/install-post

END INSTALL!

End INSTALL!がでれば作業終了です.ここで,コンテナ(OpenVZ用語でのゲストOS)でlocaleを変更するには/vz/template/centos/5/x86_64/config/.rpmmacrosの"%_install_langs C"と言う行を"%_install_langs C:pt_PT:pt_PT.UTF-8:en_US:en_US.UTF-8"のように変更するようですが,lang Cで構わなかったので,何も設定しませんでした.インストールできているかどうかは,x86_64系のテンプレートが存在するかどうかでチェックします.

# vzpkgls | grep x86_64
...
centos-5-x86_64-minimal
centos-5-x86_64-default
centos-4-x86_64-minimal
centos-4-x86_64-default

このテンプレートからキャッシュテンプレートを作ります.自分でも用語がよくわからなくなってきましたが,まぁそこはきにせず.出来上がる際にcron.daily関連のエラーが出ますが,これは無視します.

# vzpkgcache -f centos-5-x86_64-minimal
...
Complete!
chmod: cannot access `/etc/cron.daily/slocate.cron': No such file or directory
chmod: cannot access `/etc/cron.daily/makewhatis.cron': No such file or directory
Packing cache file centos-5-x86_64-minimal.tar.gz ...
Cache file centos-5-x86_64-minimal.tar.gz [120M] created.

これでできた/vz/template/cacheにあるキャッシュテンプレートからコンテナを作ります.

# vzctl create 101 --ostemplate centos-5-x86_64-minimal
Creating container private area (centos-5-x86_64-minimal)
Performing postcreate actions
Container private area was created

コンテナIDとしては101以上の値を使ってください.これでコンテナができたので,起動します.

# vzctl start 101
Starting container ...
Container is mounted
Setting CPU units: 1000
Configure meminfo: 65536
Container start in progress...

起動できたかどうかの確認はvzlistコマンドを使います.

# vzlist -a
      CTID      NPROC STATUS  IP_ADDR         HOSTNAME                        
       101          2 running -               -                               

動いているようなので,コンテナに入ってみます.exitすればコンテナから抜けます.

# vzctl enter 101
entered into CT 101
[root@potate /]# exit
logout

exited from CT 101

コンテナに入らずホストからモジュールをインストールしたりアップデートしたりできます.例えばコンテナにアップデートするには,

# vzyum 101 update
exec /usr/bin/yum -c /vz/template/centos/5/x86_64/config/yum.conf --installroot /vz/root/101 update
Excluding Packages in global exclude list
Finished
Setting up Update Process
No Packages marked for Update

となりますし,dhcpクライアントをインストールするには

# vzyum 101 install dhclient

となります.コンテナ制御のコマンドはContainer creationのページにまとまっています.

ネットワーク設定

コンテナは初期状態でネットワーク設定がまったくありません.ifconfig -aを実行するとこんな感じです.venetはコンテナ用の簡易インタフェースで,簡単に設定できるのですが,あえて使いません.

# vzctl exec 101 ifconfig -a
lo        Link encap:Local Loopback  
          LOOPBACK  MTU:16436  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

venet0    Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  
          BROADCAST POINTOPOINT NOARP  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

いばらの道を求めてeth0をコンテナに作ります.

# vzctl set 101 --netif_add eth0 --save
Configure veth devices: veth101.0 
Saved parameters for CT 101
# ifconfig veth101.0 0

できあがったインタフェースをホストとコンテナの両方で確認してみると次のようになります.

# ifconfig -a
eth0      Link encap:Ethernet  HWaddr 00:0C:29:4F:48:12  
          inet addr:192.168.121.138  Bcast:192.168.121.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe4f:4812/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:884 errors:0 dropped:0 overruns:0 frame:0
          TX packets:451 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:83836 (81.8 KiB)  TX bytes:63347 (61.8 KiB)
          Base address:0x2000 Memory:d8920000-d8940000 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:8 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:560 (560.0 b)  TX bytes:560 (560.0 b)

sit0      Link encap:IPv6-in-IPv4  
          NOARP  MTU:1480  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

venet0    Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  
          UP BROADCAST POINTOPOINT RUNNING NOARP  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

veth101.0 Link encap:Ethernet  HWaddr 00:18:51:54:D2:13  
          inet6 addr: fe80::218:51ff:fe54:d213/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:22 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

# vzctl exec 101 ifconfig -a
eth0      Link encap:Ethernet  HWaddr 00:18:51:53:19:C4  
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

lo        Link encap:Local Loopback  
          LOOPBACK  MTU:16436  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

venet0    Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  
          BROADCAST POINTOPOINT NOARP  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

ホストとコンテナの間で通信ができるようにするため,ホストにブリッジユーティリティをインストールして,vzbr0というブリッジデバイスを作成します.

# yum install bridge-utils
# brctl addbr vzbr0
# brctl show
bridge name     bridge id               STP enabled     interfaces
vzbr0           8000.000000000000       no

vzbr0用にネットワーク設定を行います.

# ifconfig vzbr0 0
# ls /proc/sys/net/ipv4/conf
all  default  eth0  lo  venet0  vzbr0
# cat /proc/sys/net/ipv4/conf/vzbr0/forwarding
1
# cat /proc/sys/net/ipv4/conf/vzbr0/proxy_arp
0
# echo 1 > /proc/sys/net/ipv4/conf/vzbr0/proxy_arp

network-scripts配下のifcfg-eth0を変更して,ifcfg-vzbr0とifcfg-veth101.0を新規作成します.

# cd /etc/sysconfig/network-scripts

# diff -u ifcfg-eth0.org ifcfg-eth0
--- ifcfg-eth0.org      2010-02-10 11:44:20.000000000 +0900
+++ ifcfg-eth0  2010-02-10 11:46:06.000000000 +0900
@@ -1,6 +1,5 @@
 # Intel Corporation 82545EM Gigabit Ethernet Controller (Copper)
 DEVICE=eth0
-BOOTPROTO=dhcp
-HWADDR=00:0C:29:4F:48:12
+BOOTPROTO=none
 ONBOOT=yes
-DHCP_HOSTNAME=potate
+BRIDGE=vzbr0

# cat ifcfg-vzbr0
DEVICE=vzbr0
BOOTPROTO=dhcp
ONBOOT=yes
DHCP_HOSTNAME=potate
TYPE=bridge

# cat ifcfg-veth101.0
DEVICE=veth101.0
ONBOOT=yes
BRIDGE=vzbr0

これでネットワーク周りの設定ができたのでリブートします.リブート後にインタフェースが正常に動作していることを確認します.

# ifconfig -a
eth0      Link encap:Ethernet  HWaddr 00:0C:29:4F:48:12  
          inet6 addr: fe80::20c:29ff:fe4f:4812/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1231 errors:0 dropped:0 overruns:0 frame:0
          TX packets:102 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:699302 (682.9 KiB)  TX bytes:18000 (17.5 KiB)
          Base address:0x2000 Memory:d8920000-d8940000 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:8 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:560 (560.0 b)  TX bytes:560 (560.0 b)

sit0      Link encap:IPv6-in-IPv4  
          NOARP  MTU:1480  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

venet0    Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  
          UP BROADCAST POINTOPOINT RUNNING NOARP  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

veth101.0 Link encap:Ethernet  HWaddr 00:18:51:54:D2:13  
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:7 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

vzbr0     Link encap:Ethernet  HWaddr 00:0C:29:4F:48:12  
          inet addr:192.168.121.138  Bcast:192.168.121.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe4f:4812/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:100 errors:0 dropped:0 overruns:0 frame:0
          TX packets:123 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:7624 (7.4 KiB)  TX bytes:19997 (19.5 KiB)

vzbr0にアドレスが正しく割振られていることがわかります.ちなみにブリッジの状態は以下のようになります.

# brctl show
bridge name     bridge id               STP enabled     interfaces
vzbr0           8000.000c294f4812       no              veth101.0
                                                        eth0

ここまでできたら,後はコンテナ側の作業です./etc/sysconfig/networkと/etc/sysconfig/network-scripts/ifcfg-eth0を新設します.

# vzctl enter 101
entered into CT 101

# cd /etc/sysconfig
# cat network
NETWORKING="yes"
NETWORKING_IPV6=no
HOSTNAME=taro

# cd /etc/sysconfig/network-scripts
# cat ifcfg-eth0
DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes
DHCP_HOSTNAME=taro

これで準備が完了したので,コンテナから抜けてリスタートさせます.

# exit

# vzctl restart 101
Restarting container
Stopping container ...
Container was stopped
Container is unmounted
Starting container ...
Container is mounted
Setting CPU units: 1000
Configure meminfo: 65536
Configure veth devices: veth101.0 
Container start in progress...

# vzlist
      CTID      NPROC STATUS  IP_ADDR         HOSTNAME                        
       101          3 running -               -                               
# vzctl exec 101 ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:18:51:53:19:C4  
          inet addr:192.168.121.139  Bcast:192.168.121.255  Mask:255.255.255.0
          inet6 addr: fe80::218:51ff:fe53:19c4/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:11 errors:0 dropped:0 overruns:0 frame:0
          TX packets:13 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:1251 (1.2 KiB)  TX bytes:1800 (1.7 KiB)

これでコンテナがdhcpを使ってIPアドレスを使えるようになりました