Multipass で Ubuntu 仮想環境を構築する
Ubuntu 開発でお馴染みの Canonical 社が開発している仮想環境構築管理ソフトの Multipass を使ってみました。
Multipass
数ステップの簡単なコマンドで、ローカルに Ubuntu VM を構築することができます。
以下のような特徴があります。
- クロスプラットフォーム (Linux/Mac/Windows) 対応
- Apple silicon (M1/M2) でも使える
- ハイパーバイザー型 VM (macOS では HyperKit (Intel) / QEMU (Apple silicon))
- cloud-init 対応
- CLI なインタフェース
環境
今回 Multipass を使ってみた環境は以下の通りです。
Homebrew はインストール済みの前提です。
インストール
% brew install --cask multipass
最新版 Ubuntu LTS を使ってみる
インスタンス起動
multipass launch
コマンドで VM インスタンスを起動します。
--name
オプションでインスタンス名を設定します。
% multipass launch --name ubuntu-latest Launched: ubuntu-latest
multipass list
コマンドでインスタンスの状態を確認します。
% multipass list Name State IPv4 Image ubuntu-latest Running 192.168.XXX.XXX Ubuntu 22.04 LTS
インスタンスへのシェル接続
multipass shell
コマンドで起動したインスタンスのシェルへ接続します。
% multipass shell ubuntu-latest Welcome to Ubuntu 22.04.2 LTS (GNU/Linux 5.15.0-78-generic aarch64) (省略) $ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 22.04.2 LTS Release: 22.04 Codename: jammy $ uname -a Linux ubuntu-latest 5.15.0-78-generic #85-Ubuntu SMP Fri Jul 7 15:29:30 UTC 2023 aarch64 aarch64 aarch64 GNU/Linux $ exit logout
インスタンス停止/起動
multipass stop
コマンドでインスタンスを停止します。
% multipass stop ubuntu-latest % multipass list Name State IPv4 Image ubuntu-latest Stopped -- Ubuntu 22.04 LTS
multipass start
コマンドでインスタンスを起動します。
% multipass start ubuntu-latest % multipass list Name State IPv4 Image ubuntu-latest Running 192.168.XXX.XXX Ubuntu 22.04 LTS
インスタンス削除
インスタンスの削除は、multipass delete
-> multipass purge
コマンドで行います。
multipass delete
コマンドでインスタンスを削除可能な状況 (Deleted) にして、multipass purge
コマンドで完全削除します。
Deleted な状態であれば、multipass recover
コマンドで復旧することが可能です。
% multipass stop ubuntu-latest % multipass list Name State IPv4 Image ubuntu-latest Stopped -- Ubuntu 22.04 LTS % multipass delete ubuntu-latest #State が 「Deleted」 (削除可能状態)になる % multipass list Name State IPv4 Image ubuntu-latest Deleted -- Not Available % multipass recover ubuntu-latest #State が 「Deleted」 から 「Stopped」 に戻る(削除可能状態から復旧) % multipass list Name State IPv4 Image ubuntu-latest Stopped -- Ubuntu 22.04 LTS % multipass delete ubuntu-latest % multipass purge #完全削除 % multipass list No instances found.
cloud-init の使用
multipass launch
時に --cloud-init
オプションで cloud-init で初期化することが可能です。
以下では、Timezone と Lacale の設定、sl パッケージをインストールする cloud-init 設定です。
cloud-init.yaml
#cloud-config repo_update: true repo_upgrade: all timezone: Asia/Tokyo locale: ja_JP.UTF-8 packages: - sl
上記の YAML ファイルを指定して、インスタンスを起動します。
% multipass launch --name ubuntu-latest --cloud-init /path/to/cloud-init.yaml % multipass shell ubuntu-latest $ timedatectl (省略) Time zone: Asia/Tokyo (JST, +0900) $ localectl System Locale: LANG=ja_JP.UTF-8 (省略) $ which sl /usr/games/sl
最新版 LTS 以外のインスタンス起動
multipass find
コマンドで使用できるイメージが表示されます。
multipass launch
時にイメージ名を指定すれば、最新版 LTS 以外の Ubuntu を起動することもできます。
% multipass find Image Aliases Version Description 20.04 focal 20230731 Ubuntu 20.04 LTS 22.04 jammy,lts 20230729 Ubuntu 22.04 LTS 23.04 lunar 20230729 Ubuntu 23.04 Blueprint Aliases Version Description anbox-cloud-appliance latest Anbox Cloud Appliance charm-dev latest A development and testing environment for charmers docker 0.4 A Docker environment with Portainer and related tools jellyfin latest Jellyfin is a Free Software Media System that puts you in control of managing and streaming your media. minikube latest minikube is local Kubernetes ros-noetic 0.1 A development and testing environment for ROS Noetic. ros2-humble 0.1 A development and testing environment for ROS 2 Humble.
Ubuntu 23.04 を起動します。
multipass exec
は、インスタンスでコマンドを実行するコマンドです。
% multipass launch 23.04 --name ubuntu2304 Launched: ubuntu2304 % multipass list Name State IPv4 Image ubuntu2304 Running 192.168.XXX.XXX Ubuntu 23.04 % multipass exec ubuntu2304 -- lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 23.04 Release: 23.04 Codename: lunar
Ubuntu 23.04 が起動しました。
感想
手軽に Ubuntu 環境が準備できるのはいいなぁと思いました。
cloud-init も使えるので、ある程度のプロビジョニングなら簡単にできるし、クラウド環境で cloud-init を使う際のテスト環境にもいいなと思いました。
Vagrant で仮想マシンを構築する(6) 仮想マシンの設定
Vagrant で起動した仮想マシンのホスト名や cpu 数、メモリサイズなどの設定方法です。
変更頻度が高めの項目に絞っています。
プロバイダは VirtualBox です。
vm 名
VirtualBox の GUI ツールで表示される vm 名を設定します。
Vagrant.configure(2) do |config| config.vm.provider "virtualbox" do |vb| vb.name = "vm-host01" end end
cpu 数
仮想マシンの cpu 数(コア数)を設定します。
Vagrant.configure(2) do |config| config.vm.provider "virtualbox" do |vb| vb.cpus = 2 end end
メモリサイズ
仮想マシンのメモリサイズを設定します。
Vagrant.configure(2) do |config| config.vm.provider "virtualbox" do |vb| vb.memory = 1024 end end
その他、設定
VBoxManage modifyvm の項目は設定可能です。
cpu の使用率制限設定は、以下の感じです。
Vagrant.configure(2) do |config| config.vm.provider "virtualbox" do |vb| vb.customize ["modifyvm", :id, "--cpuexecutioncap", "50"] end end
config.vm - Vagrantfile - Vagrant Documentation
Configuration - VirtualBox Provider - Vagrant Documentation
Vagrant で仮想マシンを構築する(5) 複数仮想マシンの起動
明けましておめでとうございます。
今年もよろしくお願いします。
引き続き、Vagrant です。
Vagrant では、一つの Vagrantfile で複数の仮想マシンを起動することができます。
仮想マシン同士の通信は、プライベートネットワークで行います。*1
複数仮想マシンの起動
config.vm.define で複数マシンを起動します。
使用する box や共有フォルダ、プロビジョニングは、仮想マシン毎に設定可能です。*2
Vagrant.configure(2) do |config| config.vm.box = "ubuntu/trusty64" config.vm.define "host01" do |config| config.vm.network "private_network", ip: "192.168.56.10" config.vm.synced_folder "./data/host01", "/home/vagrant/data" end config.vm.define "host02" do |config| config.vm.network "private_network", ip: "192.168.56.11" config.vm.synced_folder "./data/host02", "/home/vagrant/data" end end
複数仮想マシンの制御
上記の設定で vagrant up すると、2 つの仮想マシンが起動します。
$ vagrant status 2015-01-01 15:30:50 Current machine states: host01 running (virtualbox) host02 running (virtualbox) .....
仮想マシンを制御する、reload、halt、destroy などのコマンドは、定義名(host01, host02) を指定することで仮想マシン毎に実行できます。
定義名を指定しない場合は、全ての仮想マシンに対して実行されます。
(host01 への ssh ログイン) $ vagrant ssh host01 (host01 再起動) $ vagrant reload host01 (host01 停止) $ vagrant halt host01 (host01 削除) $ vagrant destroy host01
Vagrant で仮想マシンを構築する(4) 仮想マシンのネットワーク
仮想マシンで使用できるネットワークは、以下の3つがあります。
- フォワードされたネットワーク
- プライベートネットワーク(ホストのみのネットワーク)
- パブリックネットワーク(ブリッジネットワーク)
フォワードされたネットワーク
ホストの特定ポートへのアクセスを仮想マシンへ転送するネットワーク構成です。
以下のように転送したいポートを Vagrantfile へ設定します。
Vagrant.configure(2) do |config| config.vm.network "forwarded_port", guest: 80, host: 8888 end
デフォルトのプロトコルは tcp ですが、udp も転送できます。
Vagrant.configure(2) do |config| config.vm.network "forwarded_port", guest: 80, host: 8888, protocol: 'udp' end
プライベートネットワーク(ホストのみのネットワーク)
ホストと仮想マシンだけ、また仮想マシン同士を接続するプライベートなネットワーク構成です。
仮想マシンへ割り当てるスタティック ip アドレスを設定します。
Vagrant.configure(2) do |config| config.vm.network "private_network", ip: "192.168.56.10" end
VirtualBox のホストオンリーネットワークアダプタ vboxnet も設定してくれます。
パブリックネットワーク(ブリッジネットワーク)
仮想マシンがホストと同じネットワークに接続される構成です。
ブリッジに使用するホスト側のネットワークインタフェースを設定します。
Vagrant.configure(2) do |config| config.vm.network "public_network", bridge: 'en0: Wi-Fi (AirPort)' end
まとめ
Vagrant のネットワーク設定は、複数の構成を組み合わせることができるので、柔軟なネットワーク構成を簡単に作成することができます。
Networking - Vagrant Documentation
Vagrant で仮想マシンを構築する(3) 仮想マシンへのプロビジョニング(chef solo)
Vagrant プロビジョニングの chef 編です。
shell script でプロビジョニングした nginx を chef solo で行います。
chef zero とかありますが、chef solo でいきます。
仮想マシン起動
$ mkdir -p vagrant_example $ cd vagrant_example $ vagrant init ubuntu/trusty64 $ vagrant up
vagrant-omnibus のインストールと設定
仮想マシン起動時に chef をインストールしてくれるプラグイン vagrant-omnibus をインストールします。
$ vagrant plugin install vagrant-omnibus $ vagrant plugin list vagrant-omnibus (1.4.1) …..
自動で最新バージョンをインストールするように設定します。
$ vi Vagrantfile Vagrant.configure(2) do |config| config.omnibus.chef_version = :latest end
仮想マシンを再起動します。
$ vagrant reload –provision
プロビジョニング用 chef recipe の作成
@local host $ mkdir -p cookbooks/nginx/recipes $ vi cookbooks/nginx/recipes/default.rb execute 'aptitude update' do command 'aptitude update' end package 'nginx' do action :install end execute 'rm -rf /usr/share/nginx/html' link '/usr/share/nginx/html' do to '/home/vagrant/data' end service 'nginx' do supports :restart => true, :reload => true action [ :enable, :start] end
Vagrantfile 設定
$ vi Vagrantfile Vagrant.configure(2) do |config| config.vm.box = "ubuntu/trusty64" config.vm.network "forwarded_port", guest: 80, host: 8888 config.vm.synced_folder "./data", "/home/vagrant/data" config.omnibus.chef_version = :latest config.vm.provision "chef_solo" do |chef| chef.cookbooks_path = "./cookbooks/" chef.add_recipe "nginx" end end
仮想マシンを再起動します。
$ vagrant reload –provision
仮想マシンに nginx がインストールされることを確認します。
vagrant provision - Command-Line Interface - Vagrant Documentation
Chef Solo - Provisioning - Vagrant Documentation