Vagrant
是一個基於Ruby
的工具,主要用於建立和部署虛擬化開發環境。它以來於Oracle
的開源VirtualBox
虛擬化系統,經過使用 Chef
建立自動化虛擬環境。Vagrant
主要的功能以下:html
Vagrant
依賴於某種虛擬化技術,目前支持常見的 VirtualBox
、 VMWare
等,因此在使用Vagrant
以前咱們須要先安裝VirtualBox
或 VMWare
,否則沒法使用。推薦安裝 VirtualBox
。vagrant
能夠快速,方便,全自動的構建虛擬化環境,這也是咱們選擇它的緣由,而不是讓咱們像之前同樣所有本身來部署。docker
這種,有本身的倉庫,咱們直接能夠經過命令從倉庫中拉取虛擬鏡像來快速構建VirtualBox
下載地址:https://www.virtualbox.org/wiki/Downloads ,下載好後安裝直接下一步操做vagrant
下載地址:https://www.vagrantup.com/downloads.html ,也是直接下一步的操做完成,須要重啓電腦安裝完。注意:docker
- 二者軟件最好都下載最新的,省得出現兼容問題,
- 須要安裝虛擬機,須要先開啓處理器虛擬化技術,_VT-x/AMD-V硬件加速。_
命令 | 做用 |
---|---|
vagrant box add | 添加box的操做 |
vagrant init | 初始化box的操做,會生成vagrant的配置文件Vagrantfile |
vagrant up | 啓動本地環境 |
vagrant ssh | 經過 ssh 登陸本地環境所在虛擬機 |
vagrant halt | 關閉本地環境 |
vagrant suspend | 暫停本地環境 |
vagrant resume | 恢復本地環境 |
vagrant reload | 修改了 Vagrantfile 後,使之生效(至關於先 halt,再 up) |
vagrant destroy | 完全移除本地環境 |
vagrant box list | 顯示當前已經添加的box列表 |
vagrant box remove | 刪除相應的box |
vagrant package | 打包命令,能夠把當前的運行的虛擬機環境進行打包 |
vagrant plugin | 用於安裝卸載插件 |
vagrant status | 獲取當前虛擬機的狀態 |
vagrant global-status | 顯示當前用戶Vagrant的全部環境狀態 |
vagrant
,這個名字隨機,就是存放要新建的虛擬機的配置的目錄,以後在vagrant
目錄中打開 cmd
或Power Shell
窗口,vagrant init centos/7 --box-version 2004.01
PS D:\vagrant> vagrant init centos/7 --box-version 2004.01 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.
up
命令,這個過程會去下載咱們須要的鏡像,是比較漫長的過程,下載完後會直接啓動,vagrant up
命令原本就是啓動命令,這是是由於沒有因此會先去下載,PS D:\vagrant> vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Box 'centos/7' could not be found. Attempting to find and install... default: Box Provider: virtualbox default: Box Version: 2004.01 ==> default: Loading metadata for box 'centos/7' default: URL: https://vagrantcloud.com/centos/7 ==> default: Adding box 'centos/7' (v2004.01) for provider: virtualbox default: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/2004.01/providers/virtualbox.box Download redirected to host: cloud.centos.org Progress: 3% (Rate: 371k/s, Estimated time remaining: 0:18:28)
vagrant box add {name} {url}
的命令進行本地安裝,其中,{name}
是咱們要安裝的名稱, url
是咱們下載到本地的鏡像路徑PS D:\vagrant> vagrant box add centos/7 E:\迅雷下載\CentOS-7-x86_64-Vagrant-1905_01.VirtualBox.box ==> box: Box file was not detected as metadata. Adding it directly... ==> box: Adding box 'centos/7' (v0) for provider: box: Unpacking necessary files from: file:///E:/%D1%B8%C0%D7%CF%C2%D4%D8/CentOS-7-x86_64-Vagrant-1905_01.VirtualBox.box box: ==> box: Successfully added box 'centos/7' (v0) for 'virtualbox'!
vagrant up
來啓動,以下:PS D:\vagrant> vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Checking if box 'centos/7' version '2004.01' is up to date... ==> default: Clearing any previously set forwarded ports... ==> 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: Machine booted and ready! ==> default: Checking for guest additions in VM... default: No guest additions were detected on the base box for this VM! Guest default: additions are required for forwarded ports, shared folders, host only default: networking, and more. If SSH fails on this machine, please install default: the guest additions and repackage the box to continue. default: default: This is not an error message; everything may continue to work properly, default: in which case you may ignore this message. ==> default: Rsyncing folder: /cygdrive/d/vagrant/ => /vagrant ==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision` ==> default: flag to force provisioning. Provisioners marked to run always will still run.
vagrant ssh
開啓SSH,並登錄到 centos7
Windows
電腦的 IP
,其中有個網卡 VirtualBox Host-Only Network
。,這就是虛擬機的網卡,看到其IP
地址段
vagrant
的目錄下的 Vagrantfile
文件中就行配置修改,這是咱們剛纔建立的虛擬機的配置文件 ,配置 config.vm.network "private_network", ip: "192.168.56.10"
,以下所示:
192.168.56.1
,那麼咱們將這臺的配置爲 192.168.56.10
,配置好以後須要重啓虛擬機,經過 vagrant reload
,進行重啓,重啓後咱們能夠驗證其與主機是否能互通
Xshell
或 Secure CRT
進行遠程鏈接呢?vagrant ssh
到虛擬機,以後找到 /etc/ssh/sshd_config
文件修改它,經過 sudo vi sshd_config
,修改裏面的以下兩項內容,修改後直接 wq
保存退出vi
PermitRootLogin yes PasswordAuthentication yes
SSHD
,經過 systemctl restart sshd
,這時候會讓你輸入root
的密碼,root
帳號的密碼默認也是 vagrant
,你能夠選擇直接用 sudo
執行。
xshell
下測試是否能登陸
本文由AnonyStar 發佈,可轉載但需聲明原文出處。
歡迎關注微信公帳號 :雲棲簡碼 獲取更多優質文章
更多文章關注筆者博客 : 雲棲簡碼 i-code.online