Official Vagrant Boxes repository:
https://app.vagrantup.com/boxes/search
Vagrantfile
mkdir webappcd webapp
$ vagrant initA `Vagrantfile` has been placed in this directory. You are nowready to `vagrant up` your first virtual environment! Please readthe comments in the Vagrantfile as well as documentation on`vagrantup.com` for more information on using Vagrant.$ lsVagrantfile
$ 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 validateVagrantfile validated successfully.
因为在 VMware Workstation 上启的 CentOS 7 VM 内安装 VirtualBox,安装上没问题,但执行上发现问题,因此接下来指令就不跑在 Linux 了,改用 Windows 实体机示範。
启动 VM (ubuntu/bionic64)
PS D:\webapp> vagrant up
PS D:\webapp> vagrant ssh-configHost 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 sshWelcome 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 statusvagrant global-statusvagrant global-status --prune
关机
vagrant halt
暂停
vagrant suspend default
恢复
vagrant reloadvagrant resume
删除
vagrant destroy
先重头再启一个 VM,连进去做更新,再退出
PS D:\webapp> vagrant upPS D:\webapp> vagrant sshWelcome 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 -yvagrant@ubuntu-bionic:~$ exitlogoutConnection 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 sshvagrant@ubuntu-bionic:~$ sudo apt-get install apache2 -yvagrant@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 sshvagrant@ubuntu-bionic:~$ sudo systemctl status apache2Unit 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 keyTimed out while waiting for the machine to boot. This means thatVagrant was unable to communicate with the guest machine withinthe configured ("config.vm.boot_timeout" value) time period.If you look above, you should be able to see the error(s) thatVagrant had when attempting to connect to the machine. These errorsare usually good hints as to what may be wrong.If you're using a custom box, make sure that networking is properlyworking and you're able to connect to the machine. It is a commonproblem 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 increasethe 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.