ときどきAnsible日記

主にITインフラ基盤の自動化に関する事を書いているブログです

Dockerコンテナの概要

お疲れ様です。伊藤です。

先にインストールについて書いてしまったので順序が逆ですが、Dockerコンテナの概念的なことを記事にさせていただきます。
いまいちコンテナと言っても何を指しているかわからないですよね。。特にインフラに絡んでいない方には。。というわけで私のわかる限りでコンテナについて書いておきます。基本はGoogleで調べればわかることではあるので、「そんなの知ってるよ」という方は読み飛ばしてください。


まず、コンテナって何?というところですが、私の感覚では小さい仮想マシン、という認識です。実はこれはDockerコンテナについてはちょっと語弊を産む表現なのですが、その辺がまたややこしいところです。まぁ、いったんここは呑み込んでいただいて。。。


なぜコンテナは小さくて済むのか?についてですが、これは簡単。ホストOSのカーネルを兼用している為です。ハイパーバイザ型の仮想マシンというのはホストOSの上にゲストOSのカーネルを実装しています。そのために非常に容量やCPUを使うのですが、コンテナはホストOSのカーネルを共有しています。要はただのアプリケーションの扱いなんです。そのためにLinuxマシン上ではLinuxのOSしか動かせません。このあたりがハイパーバイザ型との大きな違いになります(ちなみにDockerは現時点ではLinuxしか対応していません)2017/11/16 訂正 WindowsServerにも対応しております。DockerのWindows対応について - ときどきAnsible日記参照


f:id:pj_doaa:20170808151703p:plain

コンテナはただのアプリケーションですが、ユーザとかメモリ領域とかネットワークとかを他のコンテナに影響させないように切り分けたものになります。疑似的にマシンを作っている、という形ですね(まあ仮想マシンってそういうものですが)


その切り分けに使っているのが名前空間(ネームスペース)と呼ばれる技術になります。ちょっとわかりづらいですよね。。直訳してるから。。。まあ、そういうものだと思ってもらえばいいです。で切り分けられるのは下記です。

  • プロセス管理
  • ネットワーク管理
  • IPCリソースアクセス管理
  • マウントポイント管理
  • カーネル・バージョン管理
  • ユーザ管理

これらの情報がそれぞれのコンテナで別々に使えます。この分離するための技術にカーネルバージョン3.1以上(RHEL7相当)の技術を使っているので、それ以前バージョンでは使えません。(Dockerのバージョンが古ければRHEL6でも対応してましたが、最新バージョンは動きません。docker1.10以前ぐらいまでは動くかも)


コンテナの概要について書いてきましたが、コンテナも大きく二つに分かれます。それがLinuxコンテナとDockerコンテナです。違いは下記の通り。

  • Linuxコンテナ(LXC)
    • 小さい仮想マシンを意識している
    • 仮想マシン意識なので色々乗っている
    • 色々乗ってるが故に管理がめんどくさい
    • ハイパーバイザ型と同じように使える
  • Dockerコンテナ
    • アプリケーションの実行環境を意識している
    • 必要最低限なものしか載っていない
    • 本来であればOS起動時に動くプロセスも動かない
    • すぐに作ってすぐ壊すポリシーみたい

う~ん、、ちょっと偏ってるかもしれませんが私の認識はこんな感じ。結果として今流行っているのはDockerです。すぐに作って(すぐに壊せる)というのが今の需要に合っていると思われます。ただし今後もDockerが使われ続けるかは??。噂ではすでに業界から・・・・という話も。。。

今後の状況を見据えつつ使っていきましょう。
以上です。お疲れ様でした。

Vagrantを使ってみる

お疲れ様です。伊藤です。


自動化について調べていくとVagrant(ベイグラント)によくあたります。要はVirtualBoxを使った仮想マシンの作成→OSインストールがすべてワンクリックで出来る!というものらしいです。よくわかってなかったのでこれを機に使ってみたいと思います。


まずは公式HPからダウンロード。
www.vagrantup.com

で、ダウンロードしたファイルをクリックしてインストール。ここで注意!インストール後強制的にWindowsが再起動されます。うかつに進めると再起動走るので注意してください。(この記事も消えかけた)


インストール後は下記からBoxファイルというOSイメージが入ったファイルをダウンロード
https://atlas.hashicorp.com/search
って、ちょっとこれは怖いので今回はやめておきます。OSイメージとかは正規のものを用意して、自前でBoxファイルを作ります。


とりあえずVirtualBoxを起動して、いったんRHEL7.3をインストールした仮想マシンを用意します。それを基にBoxファイルを作成していきましょう。
仮想マシン構築部分は割愛~
実際には↑の作業は20~30分かかるのでこの作業がどれくらいに短縮できるかは楽しみです。


仮想マシンの用意が終わりました。仮想マシン名はrhel7_3です。
では早速Vagrantを動かしていきます。
まずはインストール確認

C:\Users\test>vagrant -v
Vagrant 1.9.7

無事インストールされているようです。

続いてVagrant用のディレクトリを作成

C:\Users\test>mkdir C:\Vagrant & cd C:\Vagrant
C:\Vagrant>
C:\Vagrant>mkdir rhel73 & cd rhel73
C:\Vagrant\rhel73>

Boxファイルがない事を確認

C:\Vagrant\rhel73>vagrant box list
There are no installed boxes! Use `vagrant box add` to add some.

Boxファイル作成

C:\Vagrant\rhel73>vagrant package --base rhel7_3
==> rhel7_3: Exporting VM...
==> rhel7_3: Compressing package to: C:/Vagrant/rhel73/package.box

ちょっと時間がかかりますがこれでBoxファイルが出来ました。
package.boxというファイルが530Mbyteでできています。

これをVagrantに登録

C:\Vagrant\rhel73>vagrant box add --name rhel7.3-x86_64-ja-20170802 package.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'rhel7.3-x86_64-ja-20170802' (v0) for provider:
    box: Unpacking necessary files from: file://C:/Vagrant/rhel7_3/package.box
    box: Progress: 100% (Rate: 890M/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'rhel7.3-x86_64-ja-20170802' (v0) for 'virtualbox'!

上手く行ったっぽい。-nameのあとの名前は適当です。

C:\Vagrant\rhel73>vagrant box list
rhel7.3-x86_64-ja-20170802 (virtualbox, 0)


これを基にVagrantファイルを作成。

C:\Vagrant\rhel73>vagrant init rhel7_3
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

Vagrantfileというファイルが出来ました。早速マシンを作って起動してみましょう。
f:id:pj_doaa:20170802112433p:plain

その前に先ほど作った仮想マシンは削除して。。。。実行!

C:\Vagrant\rhel73>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'rhel7_3' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Box file was not detected as metadata. Adding it directly...
==> default: Adding box 'rhel7_3' (v0) for provider: virtualbox
    default: Downloading: rhel7_3
    default:
An error occurred while downloading the remote file. The error
message, if any, is reproduced below. Please fix this error and try
again.

Couldn't open file /C:/Vagrant/rhel73/rhel7_3

あれえ。。。
なんかファイルが開けないって言われました。どうやら先ほどのpackage.boxを
rhel7_3という名前で読みに行きたい様子。ファイル名を変更して再開!
f:id:pj_doaa:20170802112508p:plain

C:\Vagrant\rhel73>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'rhel7_3' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Box file was not detected as metadata. Adding it directly...
==> default: Adding box 'rhel7_3' (v0) for provider: virtualbox
    default: Unpacking necessary files from: file://C:/Vagrant/rhel73/rhel7_3
    default: Progress: 100% (Rate: 972M/s, Estimated time remaining: --:--:--)
==> default: Successfully added box 'rhel7_3' (v0) for 'virtualbox'!
==> default: Importing base box 'rhel7_3'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: rhel7_3_default_1501639887630_57662
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
Timed out while waiting for the machine to boot. This means that
Vagrant was unable to communicate with the guest machine within
the configured ("config.vm.boot_timeout" value) time period.

If you look above, you should be able to see the error(s) that
Vagrant had when attempting to connect to the machine. These errors
are usually good hints as to what may be wrong.

If you're using a custom box, make sure that networking is properly
working and you're able to connect to the machine. It is a common
problem that networking isn't setup properly in these boxes.
Verify that authentication configurations are also setup properly,
as well.

If the box appears to be booting properly, you may want to increase
the timeout ("config.vm.boot_timeout") value.

...なんかタイムアウトになってる。。
おそらくこれは元のマシンがネットワークの設定とかしてないのでsshの設定を勝手にやろうとして失敗している様子。てかVagrantってSSHの設定やっちゃうのね。。

Virtualboxを開いてみるとrhel7_3_default_1501639887630_57662という名前の仮想マシンが出来て起動しています。
とりあえずこれで自動的にマシンを作ることはできました。
時間もかなり短縮されそうです(おそらくマシンを作るだけなら3分ぐらいで終わっている様子)ただし、色々と設定する項目があるようですね。


とりあえず今回は以上です。お疲れ様でした。