vagrant学习笔记 - 基本命令的使用
為什么80%的碼農都做不了架構師?>>> ??
vagrant基本命令,根據操作的目的,可以對基本命令進行分類:
1 操作鏡像
box package2 操作虛擬機
connect destroy halt init powershell provision rdp reload resume share snapshot ssh suspend up3 監控虛擬機
global-status ssh-config port status4 其他
help login plugin push version1 操作鏡像
該命令有兩個,用來管理本地鏡像
1.1 vagrant box
1.1.1 添加鏡像到本地倉庫
$ vagrant box add [box-name] [box鏡像文件或鏡像名]1.1.2 移除本地鏡像
$ vagrant box remove [box-name]box多版本共存的情況 如果box升級過,那么在box list中會出現兩個同名,但版本不同的鏡像。如:
$ vagrant box list |grep coreos coreos-alpha (virtualbox, 745.1.0) coreos-alpha (virtualbox, 928.0.0)使用該鏡像創建虛擬機的時候,默認會使用高版本的box。 如果想使用低版本,需要修改Vagrantfile,指定box-version 在config.vm.box=xxx下一行,如上面的例子中,在“config.vm.box = "coreos-alpha"”后面增加一行配置信息:
config.vm.box_version = "745.1.0"同樣,如果想刪除一個box,如下操作會失敗:
$ vagrant box remove coreos-alpha You requested to remove the box 'coreos-alpha' with provider 'virtualbox'. This box has multiple versions. You must explicitly specify which version you want to remove with the `--box-version` flag or specify the `--all` flag to remove all versions. The available versions for this box are:* 745.1.0* 928.0.0這時有兩個選擇:
- vagrant box remove coreos-alpha --all
#刪除所有同名鏡像
- vagrant box remove coreos-alpha --box-version=745.1.0
刪除指定版本的鏡像
1.1.3 升級鏡像
檢查鏡像是否有升級?
$ cd ~/vm/ubuntu $ vagrant box outdated Checking if box 'ubuntu/trusty64' is up to date... A newer version of the box 'ubuntu/precise64' is available! You currently have version '20160122.0.0'. The latest is version '20160209.0.0'. Run `vagrant box update` to update.中央倉庫有新版更新了,手動更新box。更新的結果并不是替換舊版本,而是在本地倉庫中增加了新版的box鏡像。
$ cd ~/vm/ubuntu $ vagrant box update ==> default: Checking for updates to 'ubuntu/precise64'default: Latest installed version: 20160120.0.0default: Version constraints:default: Provider: virtualbox ==> default: Updating 'ubuntu/precise64' with provider 'virtualbox' from version ==> default: '20160120.0.0' to '20160201.0.0'... ==> default: Loading metadata for box 'https://atlas.hashicorp.com/ubuntu/precise64?access_token=cXR0wCgWXoRpMw.atlasv1.ydBAS4ev1YCWzSK4S1l6iVjssRbO5Q50a8YVnEPqoyYjcQVeaMdEsiQ8rz8tOcSHLuY' ==> default: Adding box 'ubuntu/precise64' (v20160201.0.0) for provider: virtualboxdefault: Downloading: https://atlas.hashicorp.com/ubuntu/boxes/precise64/versions/20160201.0.0/providers/virtualbox.box ==> default: Successfully added box 'ubuntu/precise64' (v20160201.0.0) for 'virtualbox'!$ vagrant box list | grep precise64 ubuntu/precise64 (virtualbox, 20160120.0.0) ubuntu/precise64 (virtualbox, 20160201.0.0)也可以檢查本地倉庫中的所有鏡像是否有升級,使用 --global 開關,這時候不需要進入工作目錄
$ vagrant box outdated --global * 'ubuntu/trusty64' is outdated! Current: 20160122.0.0. Latest: 20160209.0.0 * 'ubuntu/precise64' (v20160201.0.0) is up to date * 'pollyduan/bento_oracle_xe' wasn't added from a catalog, no version information * 'phusion/ubuntu-14.04-amd64' (v2014.04.30) is up to date * 'hashicorp/precise32' (v1.0.0) is up to date * 'hashicorp/boot2docker' (v1.7.8) is up to date * 'debian/jessie64' wasn't added from a catalog, no version information * 'coreos-alpha' is outdated! Current: 928.0.0. Latest: 955.0.0 * 'centos7' wasn't added from a catalog, no version information * 'centos65' wasn't added from a catalog, no version information * 'centos64' wasn't added from a catalog, no version information * 'bento/ubuntu-14.04' (v2.2.3) is up to date * 'bento/opensuse-13.2-x86_64' (v2.2.1) is up to date * 'bento/freebsd-10.2' (v2.2.3) is up to date * 'bento/fedora-22' (v2.2.3) is up to date * 'bento/debian-8.2' (v2.2.3) is up to date * 'bento/centos-7.2' (v2.2.3) is up to date * 'bento/centos-6.7' (v2.2.3) is up to date不進入工作目錄進行升級
$ vagrant box update --box coreos-alpha Checking for updates to 'coreos-alpha' Latest installed version: 928.0.0 Version constraints: > 928.0.0 Provider: virtualbox Updating 'coreos-alpha' with provider 'virtualbox' from version '928.0.0' to '955.0.0'... Loading metadata for box 'http://alpha.release.core-os.net/amd64-usr/current/coreos_production_vagrant.json' Adding box 'coreos-alpha' (v955.0.0) for provider: virtualbox Downloading: http://alpha.release.core-os.net/amd64-usr/955.0.0/coreos_production_vagrant.box Box download is resuming from prior download progress Progress: 1% (Rate: 92912/s, Estimated time remaining: 0:38:09) ...1.2 打包鏡像
創建vm以后,我們自己根據需要安裝軟件,配置環境,都一切就緒了。如何分發給小伙伴使用呢?這里就要涌到package命令了,把鏡像打包分發。
$ ll ~/VirtualBox\ VMs/ |grep ubuntu drwx------ 6 pollyduan staff 204 2 2 11:18 ubuntu_default_1453944793418_7699先看一下我們的vm目錄名,這里有個容易混淆的目錄:
-
Vagrantfile所在的目錄——vagrant的工作目錄
-
虛擬機文件所在的目錄——virtualbox的工作目錄
這兩個目錄名不一定相同,如果在Vagrantfile中指定了vb.name,virtualbox工作目錄就取這個名字;否則,命名為:vagrant工作目錄_隨機字符串。
或者,也可以使用virtual box的管理工具來看vm的名稱。
$ VBoxManage list vms|grep ubuntu "ubuntu_default_1453944793418_7699" {0362edc2-548b-400b-a55d-776b0a24cd8d}package命令要使用的是virtual box工作目錄。
格式:vagrant package --base [virtualbox的工作目錄] --output [保存的文件名,缺省為package.box]
$ vagrant package --base ubuntu_default_1453944793418_7699 --output ubuntu_myproject_test.box ==> ubuntu_default_1453944793418_7699: Clearing any previously set forwarded ports... ==> ubuntu_default_1453944793418_7699: Exporting VM... ==> ubuntu_default_1453944793418_7699: Compressing package to: /Users/pollyduan/vm/tmp/ubuntu_myproject_test.box導出后,可以通過IM、ftp或其他方式分發給小伙伴,那么大家使用的環境就是一致的了。
2 操作虛擬機
2.1 啟動vm
2.1.1 對于單虛擬機
$ vagrant up2.1.2 如果同一個Vagrantfile定義了一個以上的虛擬機,則:
$ vagrant up [vm-name]其他命令類似。如果省略vm-name,則依次啟動所有vm。
2.2 重啟
$ vagrant reload [vm-name]2.3 關機
$ vagrant halt [vm-name]2.4 銷毀虛擬機
$ vagrant destroy [vm-name]2.5 ssh登錄虛擬機
$ vagrant ssh [vm-name]2.6 休眠與喚醒
這一對冤家也無需多說,對于開發環境來說,個人覺得用處不是很大。
$ vagrant suspend ==> default: Saving VM state and suspending execution... $ vagrant status Current machine states:default saved (virtualbox)To resume this VM, simply run `vagrant up`. $ vagrant resume ==> default: Resuming suspended VM... ==> default: Booting VM... …...2.7 快照
vagrant snapshot命令是vm的月光寶盒,如果vm中有任務沒有跑完,需要關閉virtual box,就可以給vm做一個快照,保存vm當前所有的狀態,在virtualbox重新啟動后,再回復快照。
2.7.1 查看當前保存的快照
$ vagrant snapshot list ==> default: No snapshots have been taken yet!2.7.2 創建一個命名快照
$ vagrant snapshot save shot1 ==> default: Snapshotting the machine as 'shot1'... $ vagrant snapshot list shot12.7.3 恢復快照
$ vagrant snapshot restore shot1 ==> default: Forcing shutdown of VM... ==> default: Restoring the snapshot 'shot1'... …...恢復后,快照會一直存在,直到你手動刪除它。
2.7.4 刪除快照
$ vagrant snapshot delete shot1 ==> default: Deleting the snapshot 'shot1'... Progress: 90% Progress: 100% ==> default: Snapshot deleted!這個操作會刪除持久化的數據文件,稍微有點慢,耐心等待。這個內在的原理沒有深入研究,有點不太理解,刪除一個文件理論上應該比保存一個文件更快才對。
2.7.5 盜夢空間
push和pop,每調用一次push命令會自動創建一個命名快照,名為:push+一串隨機數,如:push_1455524411_6632;每調用一次pop,會逐級恢復到最新的快照,并刪除快照。看例子:
$ vagrant snapshot list ==> default: No snapshots have been taken yet! $ vagrant snapshot push ==> default: Snapshotting the machine as 'push_1455525041_2882'... $ vagrant snapshot list push_1455525041_2882 $ vagrant snapshot push ==> default: Snapshotting the machine as 'push_1455525049_7456'... $ vagrant snapshot push ==> default: Snapshotting the machine as 'push_1455525058_6891'... $ vagrant snapshot list push_1455525041_2882 push_1455525049_7456 push_1455525058_6891 $ vagrant snapshot pop ==> default: Forcing shutdown of VM... ==> default: Restoring the snapshot 'push_1455525058_6891'... ==> default: Deleting the snapshot 'push_1455525058_6891'... ==> default: Snapshot deleted! $ vagrant snapshot list push_1455525041_2882 push_1455525049_7456Tips: 文本的日志看起來還不夠形象,在push三個snapshot后在virtual box中是樹形顯示的;每次pop,樹枝會逐級退回,看起來更像穿越的感覺。 Tips: 在pop清空之前,隨時可以通過restore恢復其中一個快照,同樣快照不會刪除;不影響pop的測試。
2.8 遠程連接分享
遠程連接通過share connect兩個命令可以實現通過本機vagrant連接另外一臺host上的虛機。
2.8.1 允許ssh連接
$ vagrant share --ssh ==> default: Detecting network information for machine...default: Local machine address: 127.0.0.1default:default: Note: With the local address (127.0.0.1), Vagrant Share can onlydefault: share any ports you have forwarded. Assign an IP or address to yourdefault: machine to expose all TCP ports. Consult the documentationdefault: for your provider ('virtualbox') for more information.default:default: An HTTP port couldn't be detected! Since SSH is enabled, this isdefault: not an error. If you want to share both SSH and HTTP, please setdefault: an HTTP port with `--http`.default:default: Local HTTP port: disableddefault: Local HTTPS port: disableddefault: SSH Port: 2200default: Port: 2200 ==> default: Generating new SSH key...default: Please enter a password to encrypt the key: [輸入授權密碼]default: Repeat the password to confirm:[再輸一次]default: Inserting generated SSH key into machine... ==> default: Checking authentication and authorization... ==> default: Creating Vagrant Share session...default: Share will be at: vile-ibex-8238 ==> default: Your Vagrant Share is running! Name: vile-ibex-8238 ==> default: ==> default: You're sharing your Vagrant machine in "restricted" mode. This ==> default: means that only the ports listed above will be accessible by ==> default: other users (either via the web URL or using `vagrant connect`). ==> default: ==> default: You're sharing with SSH access. This means that another user ==> default: simply has to run `vagrant connect --ssh vile-ibex-8238` ==> default: to SSH to your Vagrant machine. ==> default: ==> default: Because you encrypted your SSH private key with a password, ==> default: the other user will be prompted for this password when they ==> default: run `vagrant connect --ssh`. Please share this password with them ==> default: in some secure way.Tips: 你可以通過--name指定一個名稱,否則會隨機生成一個共享名,如本例中的vile-ibex-8238
2.8.2 連接遠端ssh虛機
$ vagrant connect --ssh vile-ibex-8238 --static-ip 10.2.136.211 Loading share 'vile-ibex-8238'... The SSH key to connect to this share is encrypted. You will require the password entered when creating to share to decrypt it. Verify you access to this password before continuing.Press enter to continue, or Ctrl-C to exit now.[回車] Password for the private key:[輸入授權密碼] Executing SSH... vagrant@vagrant-ubuntu-trusty-64:~$2.8.3 共享http
vagrant share可以把host主機的http開放到遠端,供任何人訪問,這好像跟vm沒什么關系,但的確它發生了。
$ ~/apache-tomcat-8.0.28/bin/startup.sh $ vagrant share --http 80 ==> default: Detecting network information for machine...default: Local machine address: 127.0.0.1default:default: Note: With the local address (127.0.0.1), Vagrant Share can onlydefault: share any ports you have forwarded. Assign an IP or address to yourdefault: machine to expose all TCP ports. Consult the documentationdefault: for your provider ('virtualbox') for more information.default:default: Local HTTP port: 80default: Local HTTPS port: disableddefault: Port: 2200 ==> default: Checking authentication and authorization... ==> default: Creating Vagrant Share session...default: Share will be at: enchanting-buffalo-1493 ==> default: Your Vagrant Share is running! Name: enchanting-buffalo-1493 ==> default: URL: http://enchanting-buffalo-1493.vagrantshare.com ==> default: ==> default: You're sharing your Vagrant machine in "restricted" mode. This ==> default: means that only the ports listed above will be accessible by ==> default: other users (either via the web URL or using `vagrant connect`).我的host電腦在內網,在外網的任意一臺電腦上,訪問http://enchanting-buffalo-1493.vagrantshare.com,奇跡發生了。
話說這個有什么用呢?別忘記,vagrant有一個端口映射的功能,在后面的Vagrantfile配置里會提到,這樣做的結果,就是可以在互聯網任意一個角落可以訪問到你的虛機的http服務。
2.9 windows相關的操作
powershelgl和rdp是windows vm相關的操作,未做測試,忽略。
2.10 虛機環境部署
provision用于通過Vagrantfile配置文件,對vm進行部署,如安裝軟件,發布應用等,在這里不多說,專門一章來記錄。
2.11 指定vmid操作虛擬機
在3.3.2中,我們可以看到當前工作機中的所有虛機,其中第一列數據為vmid,我們可以無需進入vagrant工作目錄,操作這些虛機。如:
$ vagrant up 63093ce該方式適用于前面提到的up、reload、halt、destroy等命令。
3 監控虛擬機
3.1 查看sshd配置信息
$ vagrant ssh-config Host defaultHostName 127.0.0.1User vagrantPort 2222UserKnownHostsFile /dev/nullStrictHostKeyChecking noPasswordAuthentication noIdentityFile "/Users/pollyduan/vm/ubuntu/.vagrant/machines/default/virtualbox/private_key"IdentitiesOnly yesLogLevel FATAL3.2 查看虛擬機開放的端口
$ vagrant port The forwarded ports for the machine are listed below. Please note that these values may differ from values configured in the Vagrantfile if the provider supports automatic port collision detection and resolution.22 (guest) => 2222 (host)3.3 查看虛擬機狀態
3.3.1 查看當前vm狀態
$ cd ~/vm/ubuntu $ vagrant status Current machine states:default poweroff (virtualbox)The VM is powered off. To restart the VM, simply run `vagrant up`狀態可能是:
not create | 執行vagrant init命令后,從未啟動過 poweroff | 關機 running | 運行中 saved | 休眠3.3.2 查看全部虛機狀態
此命令無需進入vagrant工作目錄。
$ vagrant global-status id name provider state directory -------------------------------------------------------------------------------- be5dee2 mfsmaster virtualbox poweroff /Users/pollyduan/vm/mfs a523de6 mfschunk1 virtualbox poweroff /Users/pollyduan/vm/mfs 8377e0d mfschunk2 virtualbox poweroff /Users/pollyduan/vm/mfs b772b1f metalogger virtualbox poweroff /Users/pollyduan/vm/mfs 8a5f10e mfsclient virtualbox poweroff /Users/pollyduan/vm/mfs 63093ce default virtualbox poweroff /Users/pollyduan/vm/ubuntu你可能會發現,為什么有的是default,有的是有名字的。這就是因為mfsxxxx是在vagrantfile中指定了vb.name,他對應的virtualbox工作目錄也是這個值,而ubuntu這個虛機沒有指定,所以是default,而且其virtualbox工作目錄也是比較長的——ubuntu_default_1453944793418_7699。
global-status統計信息不是實時的,所有不能保證數據是絕對準確的。如果在vagrant up啟動后,我們在virtualbox管理終端關閉vm,global-status是捕獲不到的,它還是會顯示running狀態。 截至1.8.1還是這樣的,應該算是一個bug。處女座可能無法接受這個現實,那么你可以進入vagrant工作目錄,手動再指定一次vagrant halt,狀態就同步了。
4 其他命令
4.1 help
略。
4.2 login
登錄到中央倉庫
4.3 plugin
插件管理,略。
4.4 push
發布鏡像到中央倉庫
###4.5 version 略。
后面的一些命令一般人真是用不到,反正我是用不到。
轉載于:https://my.oschina.net/polly/blog/800071
總結
以上是生活随笔為你收集整理的vagrant学习笔记 - 基本命令的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用ajax预加载图片
- 下一篇: C(++) Websocket实现扫码二