VagrantでCentOS7の仮想環境を構築

環境構築

こんにちは!ゆきまさです!!(@yyykms

今回はタイトルの通りCentOS7で仮想環境を構築したので、その過程を記録していきます。

ただし仮想サーバーを作っただけで中身はまっさらなので、必要なソフトウェアは今後入れていきたいと思います!

今回は以下の記事を元に進めました。

https://qiita.com/sudachi808/items/3614fd90f9025973de4b

またVagrantコマンドの一覧など参考になった記事も載せておきます。

環境

  • macOS Mojave 10.14.6
  • VirtualBox(ver6.0.14)
  • Vagrant(ver2.2.6)
  • CentOS Linux release 7.7.1908 (Core)

VirtualBoxをインストール

仮想環境を作るためのソフトをインストールする。

今回は「VirtualBox」をインストール >> https://www.virtualbox.org/

注意
「機能拡張がブロックされました」と表示されたら「システム環境設定」→「セキュリティとプライバシー」から読み込みを許可しましょう

Vagrantをインストール

Vagrantとは仮想化ソフト(VirtualBox)を使用し、コマンドを入力することで仮想環境を管理したり構築したりすることができるツールです。

インストールはこちらから >> https://www.vagrantup.com/

仮想化ソフトは他にもいくつかあるそうですが「VirtualBox」を使っていれば良いみたいです。

参考サイト >> こちら

インストールしたらバージョンを確認してみる。

$ vagrant --version
Vagrant 2.2.6

インストールされていますね。

CentOS7の環境を作成

必要なソフトをインストールしたので仮想環境を構築していきます。

boxを追加する

boxとはOSのディスクイメージファイルで、これを追加することで仮想環境を作成できる。

https://app.vagrantup.com/centos/boxes/7

上記のリンクの公式を確認すると「centos/7」があるのでこれを取得する。

VagrantCloudにあるboxを追加する場合は名前だけでも良い。

$ vagrant box add centos/7

nameを「centos/7」とし、実行するとproviderは何かと聞かれるので「virtualbox」の「3」を選択する。

$ vagrant box add centos/7
 ==> box: Loading metadata for box 'https://app.vagrantup.com/centos/boxes/7'
 This box can work with multiple providers! The providers that it
 can work with are listed below. Please review the list and choose
 the provider you will be working with.
 
 1) hyperv
 2) libvirt
 3) virtualbox
 4) vmware_desktop
 
 Enter your choice: 3
 ==> box: Adding box 'centos/7' (v1902.01) for provider: virtualbox
     box: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/1902.01/providers/virtualbox.box
     box: Download redirected to host: cloud.centos.org
 ==> box: Successfully added box 'centos/7' (v1905.1) for 'virtualbox'!


Successfulluyとなったので、boxがちゃんと追加されたか確認する。

 $ vagrant box list
 centos/7 (virtualbox, 1905.1) 

これで追加されました!

名前をつけてインストールすることもできる。

$ vagrant box add {name} {url}

nameはわかりやすいのに指定し、urlは公式からリンクをコピペする。

これで同じく追加できました。

初期設定

「Vagrantfile」という設定ファイルを作成します。

まず「CentOS7」というディレクトリを作って、そこに環境設定をしていきたいと思います。

$ mkdir CentOS7
$ cd CentOS7

これでディレクトリが作成され移動したので、ここに初期設定をしていきます。

$ vagrant init centos/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. 

上記のコマンドを実行すると「Vagrantfile」という設定ファイルが作成されます。

Finderで確認するとファイルができていました。

FinderのCentOS7フォルダに「Vagrantfile」が入っている
注意
「init」時にboxを指定してあげないとエラーが出るので「centos/7」のように指定してあげましょう。

このファイルは仮想マシンを起動する際のconfigやprovisionが記述してあったり、IPアドレスなどの各種設定が記載されています。

また、このあと使う「vagrant up」などを使用していけます。

「Vagrantfile」の詳しい設定などは以下の記事が参考になると思います!

「status」で確認すると仮想環境の状態を確認できます。

 $ vagrant status
 Current machine states:

 default                   not created (virtualbox)
 
 The environment has not yet been created. Run `vagrant up` to
 create the environment. If a machine is not created, only the
 default provider will be shown. So if a provider is not listed,
 then the machine is not created for that environment. 

「default」が仮想マシンの名前で、「not created」がまだ作成されていないという状態を表しています。

仮想マシンを起動する

初期設定が終わったので「up」というコマンドで起動してみます。

$ vagrant up
 Bringing machine 'default' up with 'virtualbox' provider...
 ==> default: Importing base box 'centos/7'...
 ==> default: Matching MAC address for NAT networking...
 ==> default: Checking if box 'centos/7' version '1902.01' is up to date...
 ==> default: Setting the name of the VM: CentOS7_default_1553647485601_31203
 ==> 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
     default: 
     default: Vagrant insecure key detected. Vagrant will automatically replace
     default: this with a newly generated keypair for better security.
     default: 
     default: Inserting generated public key within guest...
     default: Removing insecure key from the guest if it's present...
     default: Key inserted! Disconnecting and reconnecting using new SSH key...
 ==> default: Machine booted and ready!
 ==> default: Checking for guest additions in VM...
     default: No guest additions were detected on the base box for this VM! Guest
     default: additions are required for forwarded ports, shared folders, host only
     default: networking, and more. If SSH fails on this machine, please install
     default: the guest additions and repackage the box to continue.
     default: 
     default: This is not an error message; everything may continue to work properly,
     default: in which case you may ignore this message.
 ==> default: Rsyncing folder: /Users/hogehoge/CentOS7/ => /vagrant 

起動できたか「status」で確認すると

$ vagrant status
 Current machine states:
 
 default                   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`. 

先ほどの状態の「not created」 から「running」に変わりました。

「VirtualBox」でも確認してみると、

VirtualBoxで確認している画像

ちゃんと実行中になっています。

仮想マシンにログインする

仮想マシンが起動したので「ssh」でログインしてみます。

 $ vagrant ssh
 [vagrant@localhost ~]$  

これでログインできましたので、バージョンを確認してみます。

なおユーザーはデフォルトではvagranになっていますね。

CentOSのバージョンは、/etc/redhat-releaseファイルの中身を見れば確認できます。

 [vagrant@localhost ~]$ cat /etc/redhat-release
 CentOS Linux release 7.6.1810 (Core)  

「cat」コマンドで実行し確認するとちゃんと見れますね。

仮想マシンを停止する

まずはログアウトします。logoutは「exit」もしくは「control + D」でできます。

 [vagrant@localhost ~]$ exit
 logout
 Connection to 127.0.0.1 closed. 

これでログアウトができました。次は仮想マシンを停止します。

停止は「halt」コマンドでできます。

$ vagrant halt
 ==> default: Attempting graceful shutdown of VM... 

停止したら状態を確認してみましょう!状態の確認は「status」ですね。

$ vagrant status
 Current machine states: 

 default                   poweroff (virtualbox)
 
 The VM is powered off. To restart the VM, simply run `vagrant up`

「running」から「poweroff」に変わりました。

これで仮想マシンの起動→ログイン→ログアウト→停止の基本的な流れができるようになりましたので、あとは必要なソフトを入れて構築していけば良いでしょう。

まとめ

今回はまっさらな仮想マシンを構築するまでを書いてきました。

大まかな流れをまとめると

  • 「VirtualBox」と「Vagrant」をインストールする
  • 「$ vagrant box add centos/7」でboxを追加する
  • 初期設定でVagrantfileを作成する
  • 「$ vagrant up」で仮想マシンを起動
  • 「$ vagrant ssh」でログイン
  • 「$ exit」でログアウト
  • 「$ vagrant halt」で停止

となります。

一応ここまで環境構築ができたのでまとめてみました。

今後は必要なソフトをインストールしていろいろ試していきたいと思います!