Vagrant
Vagrantは、仮想環境を管理するためのオープンソースのソフトウェアです。仮想マシンを簡単に作成、管理、共有することができます。Vagrantは、さまざまな仮想化プラットフォームに対応しており、VirtualBox、VMware、libvirt/KVM、Hyper-V、Dockerなどがサポートされています。仮想化プラットフォームのことをVagrantではprovider(プロバイダ)と呼んでいます。
この記事ではプロバイダをlibvirt/KVMとしてvagrantで仮想マシンを立ち上げていきます。
動作環境
- Ubuntu 22.04
- vagrant 2.3.4
- libvirt-dev 8.0.0
- vagrant-libvirt 0.11.2
インストール
apt installコマンドでもVagrantをインストールは可能ですがバージョンが少し古いものになります。そこでHashiCorp社が管理しているリポジトリを登録して、そこからVagrantをインストールしてきます。
リポジトリの登録
まず、HashiCorp社のリポジトリの公開鍵を取得してきます。
$ wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
上のコマンドを実行すると/usr/share/keyrings/hashicorp-archive-keyring.gpgというファイルができています。
$ ls /usr/share/keyrings/hashicorp-archive-keyring.gpg
/usr/share/keyrings/hashicorp-archive-keyring.gpg
次に、aptリポジトリの定義を追加します。
$ echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
上のコマンド実行するとaptのリポジトリの定義が追加されます。
$ cat /etc/apt/sources.list.d/hashicorp.list
deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com jammy main
Vagrantのインストール
リポジトリの登録が完了したら次は、Vagrantをインストールします。
$ sudo apt update && sudo apt install vagrant
インストールが完了したらVagrantのバージョンを確認してみましょう。ここではバージョン2.3.4がインストールされています。
$ vagrant --version
Vagrant 2.3.4
libvirt-devのインストール
libvirt-devはvagrantプラグインのvagrant-libvirtを使用する時に必要なライブラリです。
$ sudo apt install libvirt-dev
もし、libvirt-devを未インストールでvagrant-libvirtをインストールしようとすると下のようなエラーが出ます。
$ vagrant plugin install vagrant-libvirt
Installing the 'vagrant-libvirt' plugin. This can take a few minutes...
Building native extensions. This could take a while...
Vagrant failed to install the requested plugin because it depends
on a library which is not currently installed on this system. The
following library is required by the 'vagrant-libvirt' plugin:
libvirt
Please install the library and then run the command again.
vagrant-libvirt
libvirt-devをインストールしたらvagrant-libvirtをインストールしましょう。vagrant plugin installコマンドを実行します。そうすることで、vagrant-libvirtをインストールできます。
$ vagrant plugin install vagrant-libvirt
Installing the 'vagrant-libvirt' plugin. This can take a few minutes...
Fetching formatador-1.1.0.gem
Fetching fog-core-2.3.0.gem
Fetching fog-json-1.2.0.gem
Fetching nokogiri-1.14.2-x86_64-linux.gem
Fetching fog-xml-0.1.4.gem
Fetching ruby-libvirt-0.8.0.gem
Building native extensions. This could take a while...
Fetching fog-libvirt-0.11.0.gem
Fetching xml-simple-1.1.9.gem
Fetching diffy-3.4.2.gem
Fetching vagrant-libvirt-0.11.2.gem
Installed the plugin 'vagrant-libvirt (0.11.2)'!
vagrant plugin listコマンドでインストール済みのプラグインを確認できます。ここでは、vagrant-livbirtのバージョン0.11.2がインストールされています。
$ vagrant plugin list
vagrant-libvirt (0.11.2, global)
Vagrant box
仮想マシンのテンプレートとなるVagrant boxというものがあります。Vagrant boxにはOSやアプリケーションがインストール済みです。ここでは、Vagrant Cloudというサイトにあるgeneric/ubuntu2204というboxを利用します。
vagrant init
それでは、環境の初期化をしましょう。vagrant init <box名>コマンドを実行します。そうすると、Vagrantfileが作成されます。ここでは、box名にはgeneric/ubuntu2204を使用します。
$ vagrant init generic/ubuntu2204
以下のようなVagrantfileが作成されます。
Vagrant.configure("2") do |config|
config.vm.box = "generic/ubuntu2204"
end
次に、これを少し修正します。使用するプロバイダをlibvit。CPUを4個、メモリを4GBの仮想マシンになるように設定しています。
Vagrant.configure("2") do |config|
config.vm.box = "generic/ubuntu2204"
config.vm.provider "libvirt" do |vb|
vb.cpus = "4"
vb.memory = "4096"
end
end
vagrant up
それでは、仮想マシンを作成してみましょう。vagrant upコマンドを実行します。
$ vagrant up
vagrant statusコマンドで起動していることが確認できます。
$ vagrant status
Current machine states:
default running (libvirt)
The Libvirt domain is running. To stop this machine, you can run
`vagrant halt`. To destroy the machine, you can run `vagrant destroy`.
コメント