Fedora CoreOSをisoイメージファイルから立ち上げdocker-composeをインストールしてVagrantのBoxファイルを作成

VagrantVirtualBoxUnixDockerインストール
Fedora CoreOSをisoイメージファイルから立ち上げdocker-composeをインストールしてVagrantのBoxファイルを作成
Memo
この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

Fedora CoreOSをisoイメージファイルから立ち上げdocker-composeをインストールしてVagrantのBoxファイルを作成する手順をメモしました。

Fedora CoreOSを利用する背景

商用としてではなく個人用ツールを利用するためだけの環境を検討しており
条件として費用を限りなく抑えてDockerコンテナを複数立ち上げあれる環境を検討してました。

その際にFedora CoreOSは動かすことに特化したOSということで 費用面や運用的に可能かどうかを確かめたかったため使ってみることにしました。

Fedora CoreOSとは

Fedora CoreOSとはcoreOS社のContainer Linux(旧CoreOS)の後継となるOSです。
Core OSをRed Hat社が買収しProject Atomicで開発していたFedora Atomic Host Centos Atomic Host とコンテナに特化したエディションとして統合し、Fedora CoreOS として提供されています。

Fedora CoreOSの特徴

  • コンテナ上でアプリケーションを動かすための必要最小限のソフトウェアのみ搭載のため非常に軽量
  • 初回起動時に「Ignition」という設定ツールが実行され、自動的に各種設定を行える さまざまなパブリッククラウドやプラットフォームで動作(Alibaba,AWS,Azure,GCP、ベアメタル、QEMU、OpenStack、VMwareなど)

やること

  • 1. Fedora CoreOSをVirtualBoxで起動させる
  • 2. ホストOSでignitionの設定を作成
  • 3. ignitionの設定をVM側で取得し初期設定
  • 4. VagrantBoxの作成

環境

  • Mac OS Big SUR 11.3.1
  • Vagrant 2.2.16
  • VirtualBox 6.1.22 r144080

ファイル操作で利用するUnixコマンド

基本的なディレクトリ作成やファイル操作はUnixコマンドを利用します。
Unixコマンドについて詳しくはこちらの記事を参考にしてください。

1. Fedora CoreOSをVirtualBoxで起動させる

導入OSバージョン

今回導入したのは下記バージョンになります。

fedora-coreos-34.20210518.3.0 stable

Fedora CoreOS ISOイメージのダウンロード

下記ページの Bare Metal ISO 34.20210518.3.0 stable をダウンロードします。

Download Fedora CoreOS

Fedora CoreOS ISOイメージのダウンロード

Fedora CoreOS リリースストリーム

Fedora CoreOS 3つのリリースストリーム

Stable:

Stableは安定バージョンになります。

Testing:

リリース前の段階でテストするためのバージョンです。

Next:

カーネル、Systemd、その他コンポーネントの最新バージョンをテストに利用することができます。

VirutalBoxの設定

VirtualBoxを起動と新規を選択

VirtualBoxを起動するとWebcome画面が表示されるので
上部右側の新規を選択します。

VirtualBoxを起動と新規を選択

VirtualBoxの名前とOSの設定

VirtualBoxの名前とOSの設定画面が立ち上がるので下記の内容で入力します。

名前:

vagrant-coreos
任意の名前を追加します。
後ほどVagrantBoxを作成するタイミングで使用します。

マシンフォルダ:

デフォルトの設定そのままの場所を利用します。

タイプ:

Linux

バージョン:

Fedora(64-bit)

VirtualBoxの名前とOSの設定

メモリーサイズの設定

用途に応じてメモリーサイズを設定します。
今回は4GBに設定しました。

メモリーサイズの設定

ハードディスクの設定

仮想ハードディスクを選択するを選択します。

ハードディスクの設定

ハードディスクのファイルタイプの設定

VDI(VirtualBox Disk Image)を選択します。

ハードディスクのファイルタイプの設定

物理ハードディスクにあるストレージ

可変サイズを選択します。

物理ハードディスクにあるストレージ

ファイルの場所とサイズ

ファイルの場所はデフォルトの場所を利用します。
ハードディスクのサイズについては8GBだとOS立ち上げ時に警告が出たため
10GBへ変更しました。

ファイルの場所とサイズ

設定画面を選択

VM(仮想イメージ)が作成できたらメニュー上部の設定を選択します。

設定画面を選択

システムの設定

上部のタブのシステムを選択します。
起動順序:にあるフロッピーの選択を外します。

システムの設定

ストレージの設定

上部のタブのストレージを選択します。
右側のディスクのマークを選択し先ほどダウンロードしたISOイメージをセットします。

ストレージの設定

ネットワーク

上部のタブのネットワークを選択します。
高度を押して表示エリアを開きポートフォワーディングを選択します。

ネットワーク

右の追加アイコンをクリックし下記の内容を入力します。

名前:

SSH

プロトコル:

TCP

ホスト IP:

127.0.0.1

ホストポート:

2222

ゲストポート:

22

SSH用のポートフォワーディングの設定

以上でVM(仮想イメージ)の設定は完了です。

VM(仮想イメージ)を起動

起動ををクリックします。

VM(仮想イメージ)を起動

スタートをクリックします。

VM(仮想イメージ)の起動スタート

しばらくするとVM(仮想イメージ)が起動します。

VM(仮想イメージ)の起動

仮想ドライブからディスクを削除

一度VirtualBoxのVMの設定画面へ戻り右側のアイコンから仮想ドライブからディスクを削除を選択します。

ストレージの設定

以上でFedora CoreOSをVirtualBoxで起動させるまでが完了です。

2. ホストOSでignitionの設定を作成

まずホストOSでignitionファイルを作成します。

Ignitionとは?

Ignitionとは基本的にシステムの初回起動時にのみ実行されるもので
ストレージ関連の各種設定やユーザーアカウント関連の設定
サービスのインストール、各種ファイルのダウンロードといった処理を行います。

Ignitionファイルを作成

IgnitionはJSON形式の設定ファイルを使用しますが
直接記述するととても読みにくいためyaml形式で作成し
トランスパイルでJSONへ変換して利用します。

ignition.ymlファイルの作成

プロジェクトディレクトリを作成

任意のプロジェクトディレクトリを作成します。

terminal
$ mkdir ~/vagrant-coreos

ignition.ymlを作成します。
vagrantユーザーの作成とdocker-composeのインストールを実施します。

terminal
$ vi ~/vagrant-coreos/ignition.yml

下記内容を追記します。

ignition.ymlの内容

ignition.yml
variant: fcos version: 1.3.0 passwd: users: - name: core groups: - wheel - docker ssh_authorized_keys: - ssh-rsa XXX.... - name: vagrant groups: - wheel - docker password_hash: $2y$12$Wbwk3ZWM2SXiy6kFnIAwCe9GHdWatq2WSDjQEkEGMpgANlvNwsRnu systemd: units: - name: docker.service enabled: true - name: install-docker-compose.service enabled: true contents: | [Unit] Description=Install docker-compose on first boot DefaultDependencies=no Conflicts=shutdown.target After=network-online.target Before=shutdown.target ConditionFirstBoot=yes [Service] Type=oneshot RemainAfterExit=no ExecStart=/usr/bin/rpm-ostree install -r docker-compose [Install] WantedBy=network-online.target

コードの解説

passwd:

ここにユーザーの設定を記載します。
ユーザーはcorevagrantユーザーを作成しています。

users core:

    - name: core
      groups:
        - wheel
        - docker
      ssh_authorized_keys:
        - ssh-rsa XXX....
groups:

dockerを起動するためwheeldockerグループを設定しています。

ssh_authorized_keys:

sshでログインするための公開鍵を記述します。

users vagrant:

    - name: vagrant
      groups:
        - wheel
        - docker
      password_hash: $2y$12$Wbwk3ZWM2SXiy6kFnIAwCe9GHdWatq2WSDjQEkEGMpgANlvNwsRnu
password_hash:

パスワードの設定になります。
vagrantをハッシュ化して指定しています。

systemd:

docker-composeを起動するためのスクリプトの設定を記載します。
そもそもIgnitionでパッケージは入れられないためsystemd-firstbootの仕組みを利用して初回起動時にのみ実行する形で設定します。

設定については下記サイトを参考にさせていただきました。
Fedora CoreOSのIgnitionでOSと一緒にdocker-composeのインストールもやる

    - name: docker.service
      enabled: true
    - name: install-docker-compose.service
      enabled: true
      contents: |
        [Unit]
        Description=Install docker-compose on first boot
        DefaultDependencies=no
        Conflicts=shutdown.target
        After=network-online.target
        Before=shutdown.target
        ConditionFirstBoot=yes

        [Service]
        Type=oneshot
        RemainAfterExit=no
        ExecStart=/usr/bin/rpm-ostree install -r docker-compose

        [Install]
        WantedBy=network-online.target

ignition.ignファイルの作成

先ほど作成したignition.ymlをトランスパイルしてignition.ignというJSONファイルを作成します。
トランスパイルにはDockerもしくはPodmanのインストールが必要です。

Dockerのインストール

Dockerのインストールは下記を参照ください。

Docker

terminal
$ docker run -i --rm quay.io/coreos/fcct:release --pretty --strict < ignition.yml > ignition.ign

Podman

terminal
$ podman run -i --rm quay.io/coreos/fcct:release --pretty --strict < ignition.yml > ignition.ign

以上でホストOSでignitionの設定を作成までが完了です。

3. ignitionの設定をVM側で取得し初期設定

http経由でVMからアクセスしてファイルを取得します。

ホストOSでhttpサーバーを立ち上げ

ignition.ignのディレクトリへ移動

先ほど作成したignition.ignが存在するディレクトリに移動します。

terminal|ホストOS
$ cd ~/vagrant-coreos $ ls ignition.ign ignition.yml

httpサーバーの起動

作成したIgnitionファイルを仮想マシンに渡すためにFedora CoreOSではhttpを利用することを推奨しているようです。。

terminal|ホストOS
$ python3 -m http.server Serving HTTP on :: port 8000 (http://[::]:8000/) ...

8000番でhttpサーバーが立ち上がりました。

VMからアクセス

VirtualBoxで立ち上げているVMのからホストOSへアクセスします。

ゲストOSからホストOS(Mac)へのアクセス

特に設定を変えていない場合のゲストOSからホストOS(Mac)は
10.0.2.2でアクセスできます。

curlコマンドで取得

curlコマンドで先ほど起動したhttpサーバーのポートを指定して取得します。

ゲストOS
$ curl -LO http://10.0.2.2:8000/ignition.ign
curlコマンドで取得

coreosの初期設定

coreos-installerコマンドの実行

coreos-installerコマンドで先ほど取得したignition.ignを指定して実行します。

ゲストOS
$ sudo coreos-installer install /dev/sda --ignition-file ignition.ign
coreos-installerコマンドの実行

VMのシャットダウン

起動に成功したらVMのシャットダウンを実施します。

VMを再度起動

もう一度VMを起動します。

SSHで接続確認

ホストOSからsshで接続できるか確認します。

terminal|ホストOS
$ ssh core@127.0.0.1 -p 2222 Fedora CoreOS 34.20210518.3.0 Tracker: https://github.com/coreos/fedora-coreos-tracker Discuss: https://discussion.fedoraproject.org/c/server/coreos/ Last login: Fri Jun 4 02:52:09 2021 from 10.0.2.2

dockerコマンドの確認

docker -vコマンドでドッカーのインストールを確認します。
Fedora CoreOSはデフォルトでインストールされています。

terminal|ゲストOS
$ docker -v Docker version 20.10.6, build 370c289

docker-compose コマンドの確認

docker-compose -vコマンドでドッカーのインストールを確認します。

terminal|ゲストOS
$ docker-compose -v docker-compose version 1.28.6, build unknown

Vagrantユーザーの確認

vagrantユーザーが作成されいるか確認します。
パスワードはvagrantです。

[core@localhost ~]$ su - vagrant
Password: 
Last login: Fri Jun  4 11:18:59 UTC 2021 from 10.0.2.2 on pts/0
[vagrant@localhost ~]$ 

無事接続できたらインストールは成功です。

以上でgnitionの設定をVM側で取得し初期設定までが完了です。

4. VagrantBoxの作成

Vagrantのpackage化

VMをシャットダウン

Vagrantのパッケージを作成するにあたりVMをシャットダウンします。

プロジェクトディレクトリへ移動

プロジェクトディレクトリへ移動します。

terminal|ホストOS
$ cd ~/vagrant-coreos

VMの名前を指定してパッケージ化

vagrant packageコマンドでVM名を指定してパッケージ化します。

terminal|ホストOS
$ vagrant package --base vagrant-coreos ==> vagrant-coreos: Clearing any previously set forwarded ports... ==> vagrant-coreos: Pruning invalid NFS exports. Administrator privileges will be required... ==> vagrant-coreos: Exporting VM... ==> vagrant-coreos: Compressing package to: /Users/XXXX/vagrant-coresos/package.box

VagrantのBox化

作成したパッケージからBoxを作成します。

terminal|ゲストOS
$ vagrant box add --name coreos package.box ==> box: Box file was not detected as metadata. Adding it directly... ==> box: Adding box 'coreos' (v0) for provider: box: Unpacking necessary files from: file:///Users/XXXX/vagrant-coresos/package.box ==> box: Successfully added box 'coreos' (v0) for 'virtualbox'!

VagrantBoxの確認

作成したBoxを確認します。

terminal|ゲストOS
$ vagrant box list coreos (virtualbox, 0)

Vagrantを初期化

作成したVagrantBoxからVagrantを初期化します。

terminal|ゲストOS
$ vagrant init coreos 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.

VMを起動

vagrant upコマンドでVMを起動します。

terminal|ゲストOS
$ vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Importing base box 'centos-coreos'... ==> default: Matching MAC address for NAT networking... ==> default: Setting the name of the VM: sakura-tools_default_1622779040051_78027 ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat default: Adapter 2: hostonly ==> 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 default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... default: Warning: Authentication failure. Retrying... 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 keyを設定し直すことで解決しました。

VMへ接続

vagrant sshコマンドで接続します。

terminal|ゲストOS
$ vagrant ssh Fedora CoreOS 34.20210518.3.0 Tracker: https://github.com/coreos/fedora-coreos-tracker Discuss: https://discussion.fedoraproject.org/c/server/coreos/ Last login: Fri Jun 4 11:19:56 2021

作成したVagrant BoxからVMを作成しアクセスすることができました。

以上でVagrantBoxの作成は完了です。

終わりに

最後までご覧いただきありがとうございます。
この記事ではFedora CoreOSをisoイメージファイルから立ち上げdocker-composeをインストールしてVagrantのBoxファイルを作成について紹介させていただきました。

これからも皆様の開発に役立つ情報を提供していきたいと考えています。
今後ともよろしくお願いいたします。