---
title: Vagrant 參考
tags: Vagrant, VM, 簡報
description: 本簡報的內容是 cwang 在做新生訓練時,所有做的相關操作。
langs: zh
slideOptions:
theme: moon
transition: slide
viewDistance: 5
allottedMinutes: 10
help: true
preloadIframes: true
showNotes: true
# spotlight:
# enabled: true
---
# Vagrant 參考
slide: https://hackmd.io/@cwang/SkyKdDHeF
---
這是個讓開發環境變簡單的玩意。

---
## 學習誘因
----
1. 如果在虛擬主機上設定錯誤的話,可能造成原本寫對的作業評測又過不了了。
- 這時如果有方法能明確定義每個打開的虛擬機都是固定的設定,是否會比較好呢?
2. 如果虛擬主機因為沒注意到而做了某些操作,可能導致原本寫對的作業評測又要重弄。
- 這時如果有方法可以讓每一題都可以快速且自動化,是否就能重現環境以及減少手動重建得時間呢?
----
3. 單⼀虛擬機的簡單界⾯,學習曲線⼩。
4. 其實對系統管理員,就某種層面來說自動化可算是顯學了(?)
---
## 學成效益
----
1. 讓大家更好地表示自己的開發環境及規格。
3. 更方便與優雅的使用虛擬主機,以減少多餘的操作。
3. 減少啟動虛擬主機前的操作,像是手動下載 iso 與一步步安裝的過程。
4. 初步地實現 Infrastructure as code(IaC),輕量容易傳遞分享,並使其可程式化、版控化與自動化。
---
## 簡短介紹
----
[Vagrant](https://www.vagrantup.com) 是一款由 [HashiCorp](https://www.hashicorp.com) 基於 [Ruby](https://www.ruby-lang.org) 且用於構建及配置虛擬開發環境的軟體,主要以 cli 的方式運行。
主要使用 [Oracle](https://www.oracle.com) 的開源 [VirtualBox](https://www.virtualbox.org) 虛擬化系統,與 [Ansible](https://www.ansible.com), [Chef](https://www.chef.io), [Salt](https://saltproject.io), [Puppet](https://puppet.com) 等環境配置管理軟體搭配使用,可以實行快速虛擬開發環境的構建。
---
## 如何安裝
----
1. 安裝 [Vagrant](https://www.vagrantup.com/downloads)。
2. 安裝 [VirtualBox](https://www.virtualbox.org/)。
---
## 如何使用
----
1. 先到 [Vagrant Cloud](https://app.vagrantup.com/boxes/search) 找一個想要用的 Image。
2. 假設我想要使用這個 [generic/freebsd13](https://app.vagrantup.com/generic/boxes/freebsd13),只需要使用以下指令即可在當前目錄獲得 [Vagrantfile 模板](https://www.vagrantup.com/docs/vagrantfile)。
```bash=
vagrant init generic/freebsd13
```
3. 然後再使用以下指令,一個裝有 13 版的 [FreeBSD](https://www.freebsd.org) 的虛擬主機就起起來給你用了。
```bash=+
vagrant up
```
---
## 其餘常用指令
----
### ssh 到虛擬機
```bash=+
vagrant ssh
```
### 重載
```bash=+
vagrant reload
```
----
### 關機
```bash=+
vagrant halt
```
### 刪除
```bash=+
vagrant destroy
```
[Vagrant Command-Line Interface](https://www.vagrantup.com/docs/cli)
---
## 定義虛擬機規格
----
編輯 `Vagrantfile`
```ruby=
Vagrant.configure("2") do |config|
config.vm.box = "generic/freebsd13"
config.vm.provider :virtualbox do |v|
v.gui = false
v.customize ["modifyvm", :id, "--cpus", 1]
v.customize ["modifyvm", :id, "--memory", 1024]
end
end
```
[Vagrant VirtualBox Configuration](https://www.vagrantup.com/docs/providers/virtualbox/configuration)
[VBoxManage](https://www.virtualbox.org/manual/ch08.html)
---
## 設定虛擬機網路
----
### 設定私有靜態 IP
編輯 `Vagrantfile`
```ruby=
Vagrant.configure("2") do |config|
config.vm.box = "generic/freebsd13"
config.vm.network "private_network", ip: "192.168.50.4"
end
```
----
### Forwarded Ports
編輯 `Vagrantfile`
```ruby=
Vagrant.configure("2") do |config|
config.vm.box = "generic/freebsd13"
config.vm.network "forwarded_port", guest: 80, host: 8080
end
```
[Vagrant Networking](https://www.vagrantup.com/docs/networking)
---
## 添加初始化指令
----
編輯 `Vagrantfile`
```ruby=
Vagrant.configure("2") do |config|
config.vm.box = "generic/freebsd13"
config.vm.provision "shell", inline: <<-SHELL
pkg install -y git vim
ln -s /usr/share/zoneinfo/Asia/Taipei /etc/localtime
SHELL
end
```
[Vagrant Shell Provisioner](https://www.vagrantup.com/docs/provisioning/shell)
---
## 同步資料夾
----
編輯 `Vagrantfile`
```ruby=
Vagrant.configure("2") do |config|
config.vm.box = "generic/freebsd13"
config.vm.synced_folder ".", "/vagrant"
end
```
[Vagrant Synced Folders](https://www.vagrantup.com/docs/synced-folders)
---
## 添加額外的硬碟
----
編輯 `Vagrantfile`
```ruby=
Vagrant.configure("2") do |config|
config.vm.box = "generic/freebsd13"
config.vm.disk :disk, name: "a1", size: "1GB"
config.vm.disk :disk, name: "a2", size: "1GB"
end
```
[Vagrant Disks Usage](https://www.vagrantup.com/docs/disks/usage)
----
### vagrant up 前輸入指令
#### powershell
```=
set VAGRANT_EXPERIMENTAL 1
```
#### cmd
```=
set VAGRANT_EXPERIMENTAL=1
```
#### sh
```bash=
export VAGRANT_EXPERIMENTAL=1
```
---
## 一次建立多台 VM
----
編輯 `Vagrantfile`
```ruby=
Vagrant.configure("2") do |config|
config.vm.box = "generic/freebsd13"
config.vm.box_version = "3.3.4"
config.vm.define "nfs" do |nfs|
nfs.vm.hostname = "nfs.1.nctu.cs"
nfs.vm.network :private_network, ip: "10.113.0.1"
end
config.vm.define "www" do |www|
www.vm.hostname = "www.1.nctu.cs"
www.vm.network :private_network, ip: "10.113.0.2"
end
end
```
[Vagrant Multi-Machine](https://www.vagrantup.com/docs/multi-machine)
----
### 只對 nfs 操作
```bash=
vagrant up nfs
```
### 同時對 nfs 與 www 操作
```bash=
vagrant up
```
---
## 使用 Vagrant 插件
以 vagrant-hosts 為例
目的為成功同步所有 VM 的 hosts
----
### 為所有 VM 設定 hosts
安裝 vagrant-hosts 插件
```bash=
vagrant plugin install vagrant-hosts
```
編輯 `Vagrantfile`
```ruby=
Vagrant.configure("2") do |config|
config.vm.box = "generic/freebsd13"
config.vm.provision :hosts do |provisioner|
provisioner.autoconfigure = true
provisioner.sync_hosts = true
end
end
```
[vagrant-hosts](https://github.com/oscar-stack/vagrant-hosts)
---
## 將上述 Vagrantfile 組合
----
```ruby=
Vagrant.configure("2") do |config|
config.vm.box = "generic/freebsd13"
config.vm.box_version = "3.3.4"
config.vm.provision :hosts do |provisioner|
provisioner.autoconfigure = true
provisioner.sync_hosts = true
end
config.vm.provider :virtualbox do |v|
v.customize ["modifyvm", :id, "--cpus", 1]
v.customize ["modifyvm", :id, "--memory", 1024]
end
config.vm.provision "shell", inline: <<-SHELL
pkg install -y git vim
ln -s /usr/share/zoneinfo/Asia/Taipei /etc/localtime
SHELL
```
----
```ruby=+
config.vm.define "nfs" do |nfs|
nfs.vm.hostname = "nfs.1.nctu.cs"
nfs.vm.network :private_network, ip: "10.113.0.1"
nfs.vm.network :forwarded_port, guest: 21, host: 21, id: 'ftp'
nfs.vm.disk :disk, name: "a1", size: "1GB"
nfs.vm.disk :disk, name: "a2", size: "1GB"
nfs.vm.synced_folder ".", "/mnt"
end
```
----
```ruby=+
config.vm.define "www" do |www|
www.vm.hostname = "www.1.nctu.cs"
www.vm.network :private_network, ip: "10.113.0.2"
www.vm.network :forwarded_port, guest: 443, host: 8443
www.vm.provider :virtualbox do |v|
v.customize ["modifyvm", :id, "--cpus", 2]
v.customize ["modifyvm", :id, "--memory", 2048]
end
end
end
```
---
## 補充
----
- 可以選擇不使用 [VirtualBox](https://www.virtualbox.org),而改使用 [VMware Workstation](https://www.vmware.com/products/workstation-player/workstation-player-evaluation.html) 或是 [Parallels Desktop](https://www.parallels.com/)。那你得去看 [Vagrant VMware Provider](https://www.vagrantup.com/docs/providers/vmware) 或是 [Vagrant Parallels Provider](https://parallels.github.io/vagrant-parallels)。
- 若欲使用的系統沒有提供給當前使用之虛擬化平台,則可以在 [Vagrant Cloud generic](https://app.vagrantup.com/generic) 中尋找。另外,我們能發現他的專案 [lavabit/robox](https://github.com/lavabit/robox) 是使用 [Packer](https://www.packer.io) 來自動化 Build 多種平台的 Image。
----
- 如果是在 PRDO 環境下,則可以根據情境將 [Vagrant](https://www.vagrantup.com) 所做的事情分別拆給 [Terraform](https://www.terraform.io) 和 [Ansible](https://www.ansible.com) 以及 [Packer](https://www.packer.io) 去做,以達到更精細的控制。
---
## Thank you! 