正在學(xué)習(xí)Docker Swarm刽漂,接觸到了Vagrant芹枷。Vagrant是一個創(chuàng)建虛擬機的技術(shù),可以把它認(rèn)為是一個Vmware膝擂,它讓我們可以通過代碼的方式快速地虑啤、可重復(fù)地創(chuàng)建針對不同虛擬環(huán)境的虛擬機,包括Virtualbox架馋、AWS狞山、Docker等。它使得我們可以一次性地叉寂、自動創(chuàng)建多個環(huán)境相同的虛擬機萍启,對于軟件開發(fā)和測試尤其有用,以下是在使用過程中常用到的一些基本命令屏鳍。
1. 顯示當(dāng)前版本
# vagrant --version
Vagrant 2.0.2
2. 列出所有Box
#vagrant box list
iamseth/rhel-7.3 (virtualbox, 1.0.0)
3. 添加一個Box
#vagrant box add [options] <name, url, or path>
1)可以從https://app.vagrantup.com/boxes/search下載各種Vagrant映像文件
#vagrant box add ubuntu/trusty64
2) 通過指定的URL添加遠(yuǎn)程box
#vagrant box add https://atlas.hashicorp.com/ubuntu/boxes/trusty64
3) 添加一個本地box
命令格式如下
# vagrant box add {box_name} {file_path}
示例如下:
# vagrant box add CentOS7.1 file:///D:/Work/VagrantBoxes/CentOS-7.1.1503-x86_64-netboot.box
4. 初始化一個新VM
# vagrant init ubuntu/trustry64
此命令會在當(dāng)前目錄創(chuàng)建一個名為Vagrantfile的配置文件勘纯,內(nèi)容大致如下:
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/trusty64"
end
當(dāng)在此目錄啟動Vagrant后,Vagrant會從互聯(lián)網(wǎng)下載“ubuntu/trusty64”這個box到本地钓瞭,并使用它作為VM的映像驳遵。
5. 初始化一個新VM
# vagrant up
如果我們想啟動任意VM,首先進(jìn)入有Vagrantfile配置文件的目錄山涡,然后執(zhí)行上面的命令堤结。控制臺的輸出通常如下:
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu/trusty64'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'ubuntu/trusty64' is up to date...
==> default: Setting the name of the VM: start_default_1518789015107_16928
==> default: Clearing any previously set forwarded ports...
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:
https://www.virtualbox.org/manual/ch04.html#sharedfolders
This option can be disabled globally with an environment variable:
VAGRANT_DISABLE_VBOXSYMLINKCREATE=1
or on a per folder basis within the Vagrantfile:
config.vm.synced_folder '/host/path', '/guest/path', SharedFoldersEnableSymlinksCreate: false
==> 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: The guest additions on this VM do not match the installed version of
default: VirtualBox! In most cases this is fine, but in rare cases it can
default: prevent things such as shared folders from working properly. If you see
default: shared folder errors, please make sure the guest additions within the
default: virtual machine match the version of VirtualBox you have installed on
default: your host and reload your VM.
default:
default: Guest Additions Version: 4.3.36
default: VirtualBox Version: 5.2
==> default: Mounting shared folders...
default: /vagrant => /Users/qianlong/DockerProjects/vagrant/start
6.啟用SSH登陸VM
進(jìn)入Vagrantfile配置文件所在的目錄鸭丛,執(zhí)行以下命令:
# vagrant ssh
如果需要從虛擬機中退出竞穷,直接在虛擬機中的命令行輸入exit
命令即可
7. 查看VM當(dāng)前的狀態(tài)
進(jìn)入Vagrantfile配置文件所在的目錄,執(zhí)行以下命令:
# vagrant status
如果VM正在運行鳞溉,你可以在控制臺中看到如下信息:
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`.
8.關(guān)閉VM
進(jìn)入Vagrantfile配置文件所在的目錄瘾带,執(zhí)行以下命令:
# vagrant halt
9.銷毀VM
命令格式如下:
vagrant destory [name|id]
示例如下:
# vagrant destroy ubuntu/trusty64
如果我們是使用了默認(rèn)的虛擬機名"default",那其實我們可以直接輸入如下命令銷毀VM
# vagrant desotry
運行結(jié)果如下:
default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Forcing shutdown of VM...
==> default: Destroying VM and associated drives...
需要注意的是穿挨,我們要慎重使用該命令月弛,該命令會停止VM的運行肴盏,并銷毀所有在VM中創(chuàng)建的資源