iT邦幫忙

0

Vagrant Boxes

Official Vagrant Boxes repository:
https://app.vagrantup.com/boxes/search


Vagrantfile

mkdir webapp
cd webapp
$ vagrant init
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.
$ ls
Vagrantfile
$ vim Vagrantfile

將所有的註解刪除後,留下最簡單的 vagrant file:

Vagrant.configure("2") do |config|
  config.vm.box = "base"
end

更換一下 vagrant box:

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/bionic64"
end

驗證一下 vagrant 語法有沒有寫錯

$ vagrant validate
Vagrantfile validated successfully.

因為在 VMware Workstation 上啟的 CentOS 7 VM 內安裝 VirtualBox,安裝上沒問題,但執行上發現問題,因此接下來指令就不跑在 Linux 了,改用 Windows 實體機示範。

啟動 VM (ubuntu/bionic64)

PS D:\webapp> vagrant up
PS D:\webapp> vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile D:/webapp/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL

連進 VM

PS D:\webapp> vagrant ssh
Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 4.15.0-70-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Fri Nov 22 02:44:37 UTC 2019

  System load:  0.0               Processes:             95
  Usage of /:   12.1% of 9.63GB   Users logged in:       0
  Memory usage: 16%               IP address for enp0s3: 10.0.2.15
  Swap usage:   0%


0 packages can be updated.
0 updates are security updates.


vagrant@ubuntu-bionic:~$

看狀態

vagrant status
vagrant global-status
vagrant global-status --prune

關機

vagrant halt

暫停

vagrant suspend default

恢復

vagrant reload
vagrant resume

刪除

vagrant destroy

先重頭再啟一個 VM,連進去做更新,再退出

PS D:\webapp> vagrant up
PS D:\webapp> vagrant ssh
Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 4.15.0-70-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Sat Nov 23 03:06:42 UTC 2019

  System load:  0.1               Processes:             104
  Usage of /:   12.1% of 9.63GB   Users logged in:       0
  Memory usage: 15%               IP address for enp0s3: 10.0.2.15
  Swap usage:   0%


0 packages can be updated.
0 updates are security updates.


vagrant@ubuntu-bionic:~$ sudo apt-get update & apt-get upgrade -y
vagrant@ubuntu-bionic:~$ exit
logout
Connection to 127.0.0.1 closed.

快照,名稱為 updated,並列出快照清單

PS D:\webapp> vagrant snapshot save default updated
==> default: Snapshotting the machine as 'updated'...
==> default: Snapshot saved! You can restore the snapshot at any time by
==> default: using `vagrant snapshot restore`. You can delete it using
==> default: `vagrant snapshot delete`.
PS D:\webapp> vagrant snapshot list
==> default:
updated

再連進去,做一點更動(安裝 apache2),完成後退出

PS D:\webapp> vagrant ssh
vagrant@ubuntu-bionic:~$ sudo apt-get install apache2 -y
vagrant@ubuntu-bionic:~$ exit

還原快照 updated

PS D:\webapp> vagrant snapshot restore default updated
==> default: Forcing shutdown of VM...
==> default: Restoring the snapshot 'updated'...
==> default: Checking if box 'ubuntu/bionic64' version '20191121.0.0' is up to date...
==> default: Resuming suspended VM...
==> 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: Machine booted and ready!
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.

連進去驗證是否回到 apache2 未安裝狀態

PS D:\webapp> vagrant ssh
vagrant@ubuntu-bionic:~$ sudo systemctl status apache2
Unit apache2.service could not be found.
vagrant@ubuntu-bionic:~$ exit

刪除快照,建議刪除之前先行關機

PS D:\webapp> vagrant halt
==> default: Attempting graceful shutdown of VM...
PS D:\webapp> vagrant snapshot delete updated
==> default: Deleting the snapshot 'updated'...
==> default: Snapshot deleted!

複製 webapp 資料夾為 webapp2,切換到 webapp2
用 push 方式產生快照,此方法快照名稱自動產生、無法指定

PS D:\webapp2> vagrant snapshot push
==> default: Snapshotting the machine as 'push_1574481235_1492'...
==> default: Snapshot saved! You can restore the snapshot at any time by
==> default: using `vagrant snapshot restore`. You can delete it using
==> default: `vagrant snapshot delete`.
PS D:\webapp2> vagrant snapshot list
==> default:
push_1574481235_1492

用 pop 方式還原快照,此時快照亦會同時刪除

PS D:\webapp2> vagrant snapshot pop
==> default: Forcing shutdown of VM...
==> default: Restoring the snapshot 'push_1574481235_1492'...
==> default: Deleting the snapshot 'push_1574481235_1492'...
==> default: Snapshot deleted!
==> default: Checking if box 'ubuntu/bionic64' version '20191121.0.0' is up to date...
==> default: Resuming suspended VM...
==> 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
Timed out while waiting for the machine to boot. This means that
Vagrant was unable to communicate with the guest machine within
the configured ("config.vm.boot_timeout" value) time period.

If you look above, you should be able to see the error(s) that
Vagrant had when attempting to connect to the machine. These errors
are usually good hints as to what may be wrong.

If you're using a custom box, make sure that networking is properly
working and you're able to connect to the machine. It is a common
problem that networking isn't setup properly in these boxes.
Verify that authentication configurations are also setup properly,
as well.

If the box appears to be booting properly, you may want to increase
the timeout ("config.vm.boot_timeout") value.
PS D:\webapp2> vagrant snapshot list
==> default: No snapshots have been taken yet!
    default: You can take a snapshot using `vagrant snapshot save`. Note that
    default: not all providers support this yet. Once a snapshot is taken, you
    default: can list them using this command, and use commands such as
    default: `vagrant snapshot restore` to go back to a certain snapshot.

尚未有邦友留言

立即登入留言