LoginSignup
22
27

More than 3 years have passed since last update.

vagrant + VirtualBox + CentOS 7.7環境構築 (Windows 10)

Posted at

Windows 10 PCでCent OS7環境の導入するにあたり、vagrantを使ってみましたので導入方法をまとめます。

VirtualBoxインストール

最新版をインストールしました。インストーラはこちらから入手し、実行します。

vagrantインストール

v2.2.7(64bi)をインストール。インストーラはこちらから入手し、実行します。
インストール後、OSの再起動が必要です。

再起動後、コマンドプロンプトを起動し、バージョンを確認します。

>vagrant -v
Vagrant 2.2.7

コマンドプロンプトでpluginをインストールしておきます。
必要に応じてChefをインストールしてくれるオムニバスプラグインと、ホスト・ゲスト間の共有フォルダ構成のためVirtualboxGuestAdditionツールを導入します。(ないとエラーが出る)
複製用にvagrant-hostmanagerもインストールします。

> vagrant plugin install vagrant-omnibus
> vagrant plugin install vagrant-vbguest
> vagrant plugin install vagrant-hostmanager

#インストールされているpluiginの表示
>vagrant plugin list
vagrant-omnibus (1.5.0, global)
vagrant-vbguest (0.23.0, global)

CentOS7 boxの追加

vagrantはboxと呼ばれるイメージファイルと構成ファイルを使って管理します。CentOS 7.7のboxを vagrantcloud.comからダウンロードし、virtualbox用として追加します。これはコマンドプロンプトでいけます。

不安なぐらい処理時間が長いので早くも休憩です。

> vagrant box add bento/centos-7.7 --provider virtualbox
==> box: Loading metadata for box 'bento/centos-7.7'
    box: URL: https://vagrantcloud.com/bento/centos-7.7
==> box: Adding box 'bento/centos-7.7' (v202002.04.0) for provider: virtualbox
    box: Downloading: https://vagrantcloud.com/bento/boxes/centos-7.7/versions/202002.04.0/providers/virtualbox.box
    box: Download redirected to host: vagrantcloud-files-production.s3.amazonaws.com
    box: Progress: 100% (Rate: 457k/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'bento/centos-7.7' (v202002.04.0) for 'virtualbox'!

>

vagrant管理用フォルダの作成と初期化

コマンドプロンプトでvagrant管理用フォルダを作成し、vagrant initコマンドで初期化します。
Vagrantfileが作成され、仮想マシンが起動するとログファイルや管理情報が保存されます。10MB程度です。

C:\>mkdir centos

C:\>cd centos

C:\centos>vagrant init bento/centos-7.7
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.

C:\centos>

Vagrantfileの修正

ローカル接続用のhostonlyネットワークアダプタと、インターネット接続用のNATネットワークアダプタの二つがVirtualBox仮想マシン自動作成されます。Vagrantfileファイルを開き、tera term接続で使うローカル接続用のIPアドレスを修正します。

  # config.vm.network "private_network", ip: "192.168.33.10"
  config.vm.network "private_network", ip: "192.168.33.100" ★この行を追加。アドレスは任意

仮想マシンを起動

vagrant upコマンドで仮想マシンを起動します。

>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'bento/centos-7.7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'bento/centos-7.7' version '202002.04.0' is up to date...
==> default: Setting the name of the VM: centos_default_1586011454115_8146
Vagrant is currently configured to create VirtualBox synced folders with
the `SharedFoldersEnableSymlinksCreate` option enabled. If the Vagrant
guest is not trusted, you may want to disable this option. For more
information on this option, please refer to the VirtualBox manual:
:
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => C:/centos


C:\centos>

"Machine booted and ready!"が表示されていれば起動に成功しています。

仮想マシンにログイン

Tera Termでssh接続します。ローカル接続用のIPアドレス(例では192.168.33.100)で接続します。
ユーザはデフォルトで以下が設定されています。

 username: root
 passwd : vagrant


 username: vagrant #一般ユーザ
 passwd : vagrant

少ない手順であっさり環境が立ち上がるので、これは便利。
以降、rootユーザで操作します。環境設定は例として実行したものをまとめますが、お好みで良いかと思います。

環境設定 - ホスト名

ログインすると、ホスト名がlocalhostとなっていますのでテンションの上がる名前に変更します。

# hostnamectl set-hostname <ホスト名>

例)
# hostnamectl set-hostname venus

環境設定 - タイムゾーン

デフォルトでタイムゾーンがUTCとなっているので、Asia/Tokyoにします。RTC(PCのシステム時間)の参照も無効とします。

# timedatectl set-timezone Asia/Tokyo
# timedatectl set-local-rtc 0

※確認
# timedatectl
      Local time: Sun 2020-04-05 12:13:46 JST
  Universal time: Sun 2020-04-05 03:13:46 UTC
        RTC time: Sun 2020-04-05 03:13:46
       Time zone: Asia/Tokyo (JST, +0900) ★タイムゾーン
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no ★RTC時間の利用を無効
      DST active: n/a

パッケージグループのインストールとアップデート

このインストール方法では最小限のcoreパッケージしかインストールされません。
検証目的に応じて、以下のグループパッケージの導入を行います。

ここでは時間はかかるけど、以下をインストール。

# yum -y group install infrastructure-server-environment
# yum -y group install developer-workstation-environment

他にも以下のようなパッケージグループがあり、カッコ内のグループ名を指定してインストールできる。

Installed Environment Groups: ★インストールしたグループ
   インフラストラクチャーサーバー (infrastructure-server-environment)
   開発およびクリエイティブワークステーション (developer-workstation-environment)
Available Environment Groups: ★インストールできるグループ
   最小限のインストール (minimal)
   コンピュートノード (compute-node-environment)
   ファイルとプリントサーバー (file-print-server-environment)
   Cinnamon デスクトップ (cinnamon-desktop-environment)
   MATE デスクトップ (mate-desktop-environment)
   ベーシック Web サーバー (web-server-environment)
   仮想化ホスト (virtualization-host-environment)
   サーバー (GUI 使用) (graphical-server-environment)
   GNOME Desktop (gnome-desktop-environment)
   KDE Plasma Workspaces (kde-desktop-environment)

他、初期インストールパッケージを最新化する。

# yum -y update

DKMSのインストール

VirtualBoxでLinuxを運用する際の推奨として、DKMS (Dynamic Kernel Module Support) のインストールしたらいいらしいので、インストール。

拡張リリースパッケージEPELをインストールし、そのリポジトリを使って導入します。

# yum install epel-release

# yum install dkms

これにより仮想マシンのカーネルが更新されたため、vagrant up実行時にホスト側の認識するカーネルとの差分を検出しエラーが出力されます。今回は共有フォルダがマウントに失敗しました。

コマンドプロンプトに戻り、vagrantのVirtualboxGuestAdditionツールを実行します。仮想マシン側が更新されたので、自動的にカーネルモジュールがアップデートされます。

>vagrant vbguest

そして、毎度EPELリポジトリには用がないので無効化します。EPEL(Extra Packages for Enterprise Linux)は、追加パッケージ群です。
/etc/yum.repos.d/epel.repoファイルを修正しておきます。

/etc/yum.repos.d/epel.repo
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
#baseurl=http://download.fedoraproject.org/pub/epel/7/$basearch
metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch&infra=$infra&content=$contentdir
failovermethod=priority
#enabled=1 ★コメントアウト
enabled=0  ★追加
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

EPELリポジトリで提供されているパッケージの導入は、以下のようにyumコマンドに--enablerepo=epelをつけて実行します。EPELで提供されているhtopコマンドを例に実行してみます。

[root@venus ~]# yum search htop ★オプションなしでは、表示されない
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: ftp.tsukuba.wide.ad.jp
 * extras: ftp.tsukuba.wide.ad.jp
 * updates: ftp.tsukuba.wide.ad.jp
Warning: No matches found for: htop
No matches found
[root@venus ~]#

[root@venus ~]# yum --enablerepo=epel search htop ★オプションをつける
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: ftp.tsukuba.wide.ad.jp
 * epel: nrt.edge.kernel.org
 * extras: ftp.tsukuba.wide.ad.jp
 * updates: ftp.tsukuba.wide.ad.jp
===================================================================================== N/S matched: htop =====================================================================================
htop.x86_64 : Interactive process viewer

  Name and summary matches only, use "search all" for everything.


環境設定 - 日本語導入

一応日本語化しておきます。下記パッケージのインストールと、glibc-commonは再インストールが必要でした。

インストールが必要:
 ibus-kkc
 vlgothic-*

再インストールが必要
 glibc-common

[root@venus ~]# locale -a
C
en_US.utf8
POSIX
[root@venus ~]# ★日本語ロケールがない状態

[root@venus ~]# yum -y install ibus-kkc vlgothic-*
[root@venus ~]# yum -y reinstall glibc-common

[root@venus ~]# locale -a | grep ja_JP ★日本語が出てきた
ja_JP
ja_JP.eucjp
ja_JP.ujis
ja_JP.utf8
[root@venus ~]#

[root@venus ~]# localectl set-locale LANG=ja_JP.utf8 ★ロケールを日本語UTC-8に設定

[root@venus ~]# date
2020年  4月  5日 日曜日 12:44:33 JST ★日本語になった。

仮想マシンの停止

仮想マシンの停止は、vagrant haltコマンドで停止できる。

C:\centos>vagrant halt
==> default: Attempting graceful shutdown of VM...

C:\centos>

パッケージ化

vagrantの本領発揮はおそらくここからです。
作った環境をboxというパッケージにすることで、他者へ配布して共有したり、同じ環境を構築することが出来ます。
vagrant packageコマンドを使います。

vagrant package [vm名] --output [名前.box]

vm名は以下のコマンドで確認できます。vm作成時に指定しなかったので、今回はdefaultという名称でした。

C:\centos>vagrant status
Current machine states:

default                   poweroff (virtualbox)

パッケージ化を実行します。

C:\centos>vagrant package default --output centos7_master.box
==> default: Clearing any previously set forwarded ports...
==> default: Exporting VM...
==> default: Compressing package to: C:/centos/centos7_master.box

C:\centos>

容量は約3GBになりました。

C:\centos>dir *box
:
2020/04/05  13:58     2,921,456,226 centos7_master.box
               1 個のファイル       2,921,456,226 バイト

開発環境のデプロイテスト

パッケージ化したboxを登録します。

C:\centos>vagrant box add jupiter centos7_master.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'jupiter' (v0) for provider:
    box: Unpacking necessary files from: file://C:/centos/centos7_master.box
    box:
==> box: Successfully added box 'jupiter' (v0) for 'virtualbox'!

C:\centos>

別のフォルダを作成しvagrant initコマンドで初期化する。

C:\centos>mkdir ..\centos-test

C:\centos>cd ..\centos-test

C:\centos-test>vagrant init jupiter
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.

C:\centos-test>

ホスト名、IPアドレスを変えたいので、Vagrantfileファイルを以下の通り追加。同じでよければ特に修正は不要。

  config.hostmanager.enabled = true ★hostmanagerプラグインを使用する
  config.hostmanager.manage_host = true ★hostmanagerプラグインを使用する(PC側のhostsに名前解決追加)
  config.vm.define 'jupiter' do |node| ★ホスト名変更
   node.vm.hostname = 'jupiter' ★ホスト名変更
  config.vm.network "private_network", ip: "192.168.33.200" ★IPアドレス変更
  end

vagrant upコマンドで仮想マシンを起動し、vagrant statusコマンドで確認します。

C:\centos-test>vagrant up
Bringing machine 'jupiter' up with 'virtualbox' provider...
==> jupiter: Importing base box 'jupiter'...
==> jupiter: Matching MAC address for NAT networking...
==> jupiter: Setting the name of the VM: centos-test_jupiter_1586069112257_66846
==> jupiter: Clearing any previously set network interfaces...
==> jupiter: Preparing network interfaces based on configuration...
    jupiter: Adapter 1: nat
    jupiter: Adapter 2: hostonly
==> jupiter: Forwarding ports...
    jupiter: 22 (guest) => 2222 (host) (adapter 1)
==> jupiter: Booting VM...
==> jupiter: Waiting for machine to boot. This may take a few minutes...
    jupiter: SSH address: 127.0.0.1:2222
    jupiter: SSH username: vagrant
    jupiter: SSH auth method: private key
==> jupiter: Machine booted and ready!
[jupiter] GuestAdditions 6.1.4 running --- OK.
==> jupiter: Checking for guest additions in VM...
==> jupiter: Setting hostname...
==> jupiter: Configuring and enabling network interfaces...
==> jupiter: Mounting shared folders...
    jupiter: /vagrant => C:/centos-test
==> jupiter: [vagrant-hostmanager:guests] Updating hosts file on active guest virtual machines...
==> jupiter: [vagrant-hostmanager:host] Updating hosts file on your workstation (password may be required)...

C:\centos-test>
C:\centos-test>vagrant status
Current machine states:

jupiter                   running (virtualbox) ★起動している

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.

C:\centos-test>

ログインして確認すると、設定が引き継がれています。

[root@jupiter ~]# rpm -qa | wc -l
1655 ★RPMパッケージ数
[root@jupiter ~]#
[root@jupiter ~]# date
2020年  4月  5日 日曜日 15:47:51 JST ★日本語表示もOK
[root@jupiter ~]#

システムバックアップにもなるので、パッケージ化しておくと後々便利かもしれません。

22
27
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
27