以下省略!

タイダルウ(ry ほぼ毎日1記事執筆運動実施中。 ※記事に広告(アフィリエイト)リンクを掲載している場合があります。

コンテナ型仮想化技術のLXCを使ってみた

f:id:abyssluke:20150322171410j:plain:w240:leftLinux用のコンテナ型仮想化技術である、LXCを使ってみた。UbuntuUbuntuを動かしたり、UbuntuCentOSを動かしたりできる。解説は15分で分かるLXC(Linux Containers)の仕組みと基本的な使い方 - さくらのナレッジが詳しい*1ので省略して、使ってみた時の記録&メモを。かなり長いので続きを読むから。

LXCをインストール

Ubuntu 14.04 LTSの標準レポジトリに入っている。
Ubuntuのデフォルトカーネルでは問題ないと思うが、lxc-checkconfigを実行しすべてenabledではない場合、動作しないか、制限がかかる場合もあるのでカスタムカーネルなどを使っていたりカーネル設定を変えている場合は注意。

$ sudo apt-get install lxc
$ lxc-checkconfig
Kernel configuration not found at /proc/config.gz; searching...
Kernel configuration found at /boot/config-3.13.0-46-generic
--- Namespaces ---
Namespaces: enabled
Utsname namespace: enabled
Ipc namespace: enabled
Pid namespace: enabled
User namespace: enabled
Network namespace: enabled
Multiple /dev/pts instances: enabled

--- Control groups ---
Cgroup: enabled
Cgroup clone_children flag: enabled
Cgroup device: enabled
Cgroup sched: enabled
Cgroup cpu account: enabled
Cgroup memory controller: enabled
Cgroup cpuset: enabled

--- Misc ---
Veth pair device: enabled
Macvlan: enabled
Vlan: enabled
File capabilities: enabled

Note : Before booting a new kernel, you can check its configuration
usage : CONFIG=/path/to/config /usr/bin/lxc-checkconfig

Ubuntuのコンテナ作成から起動まで

ubuntuというテンプレート(-tオプション)でubuntu01というコンテナ(-nオプション)を作成し、起動してみる。
・ホストOSがUbuntuの場合はホストと同じバージョンのUbuntuがダウンロードされインストールされる(多分)。
・初期ID/PWはubuntu/ubuntu。sudoでroot権限を行使可能。
・lxc-startで-dオプションを付けておくとバックグラウンドでコンテナが動作し、lxc-consoleでコンテナ内のコンソールに入れる(Ctrl+a、qの順に押すと抜ける)。付けない場合はそのままコンテナ内のコンソールに入る(halt(shutdown)でコンテナを終了&抜けるしかない)。

$ sudo lxc-create -t ubuntu -n ubuntu01
(ダウンロードやらアップデート状況やらが出てくる。初回は時間がかかる)
$ sudo lxc-start -n ubuntu01 -d
$ sudo lxc-console -n ubuntu01

Ubuntu 14.04.2 LTS ubuntu01 console

ubuntu01 login: ubuntu
Password: (ubuntu)
Welcome to Ubuntu 14.04.2 LTS (GNU/Linux 3.13.0-46-generic i686)

 * Documentation:  https://help.ubuntu.com/

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

ubuntu@ubuntu01:~$ 

CentOSのコンテナ作成から起動まで

・テンプレート(-tオプション)をcentosにすればいいが、Ubuntuの場合yumが無いと言われコンテナを作成できないので予めyum*2をインストールしておく(関連ライブラリもインストールされる)。
・rootパスワードは自動生成(ログイン時に変更を促される)。chrootを使ってコンテナ内のファイルシステムにルートを切り替えユーザーパスワードを変えることも可能(CentOSだけに限らず、パスワードを忘れた時などにも使える)。

$ sudo apt-get install yum
$ sudo lxc-create -t centos -n centos01
(ダウンロードやらアップデート状況やらが出てくる、中略)
The temporary root password is stored in:

        '/var/lib/lxc/centos01/tmp_root_pass'


The root password is set up as expired and will require it to be changed
at first login, which you should do as soon as possible.  If you lose the
root password or wish to change it without starting the container, you
can change it from the host by running the following command (which will
also reset the expired flag):

        chroot /var/lib/lxc/centos01/rootfs passwd


$ sudo cat /var/lib/lxc/centos01/tmp_root_pass
(初期のrootパスワードが表示される)
$ sudo lxc-start -n centos01 -d
$ sudo lxc-console -n centos01

CentOS release 6.6 (Final)
Kernel 3.13.0-46-generic on an i686

centos01 login: root
Password: (初期rootパスワード)
You are required to change your password immediately (root enforced)
Changing password for root.
(current) UNIX password: (初期rootパスワード)
New password: (任意のパスワード)
Retype new password: (任意のパスワード)
[root@centos01 ~]# 

コンテナを終了する

ホスト(コンテナ外)では
$ sudo lxc-stop -n ubuntu01

コンテナ内では
$ sudo halt

コンテナの一覧、状態を確認する

・-fオプションを付けると動作状態やコンテナに割り振られているプライベートIPアドレスも確認できる。

$ sudo lxc-ls -f
NAME       STATE    IPV4        IPV6  AUTOSTART  
-----------------------------------------------
centos01   RUNNING  10.0.1.112  -     NO         
ubuntu01   RUNNING  10.0.1.43   -     NO  

コンテナを複製する

・コンテナが停止している必要がある。

$ sudo lxc-clone centos01 centos01b
Created container centos01b as copy of centos01

コンテナを消去する

$ sudo lxc-destroy -n centos01

*1:簡単に言えばchrootにリソース隔離機能を加えたすごいやつ

*2:パッケージ管理方式が違うのになんでUbuntuレポジトリyumなどのrpm関連があるんでしょうかね…