下載 VirtualBox 和Vagrant ,並安裝
爲什麼我們要選擇Vagrant呢?因爲它有跨平臺、可移動、自動化部署無需人工參與等優點。
-
VirtualBox 安裝
VirtualBox 是一款開源的虛擬機軟件,和VMWare是同類型的軟件,用於在當前的電腦上構建一臺虛擬機,在這臺虛擬機上可以安裝系統和軟件,與真實的電腦一般無二。
官方網站下載:https://www.virtualbox.org/wiki/Downloads
下載適合你平臺的 VirtualBox 最新版本,直接根據嚮導進行操作即可完成安裝。 -
Vagrant 安裝
Vagrant用於創建和部署虛擬化開發環境。它使用Oracle的開源VirtualBox虛擬化系統.。
官方網站下載:https://www.vagrantup.com/downloads.html
,直接根據嚮導進行操作即可完成安裝,這個安裝完畢後會進行重啓電腦,在重啓電腦後可以打開cmd命令行窗口vagrant -v
進行驗證看是否已經可以使用了。
注意:下載的時候,virtualbox和vagrant的版本要搭配,建議都下載最新版的。還有就是要根據自己的操作系統版本進行選擇32位或者64位下載。在windows系統中,可能還需要配置環境變量以及一定要開啓VT-x/AMD-V硬件加速。
vagrant環境安裝
下載box,本地安裝
- 在線查找需要的box,官方網址:
https://app.vagrantup.com/boxes/search
- 下載box鏡像,將其添加box到本地vagrant環境
vagrant box add {title}{url}
vagrant box add centos7 http://cloud.centos.org/centos/7/vagrant/x86_64/images/CentOS-7-x86_64-Vagrant-1902_01.VirtualBox.box --insecure
註解
{title}可以自行設置,我這裏使用的是 centos7
{url}是下載到本地box路徑。也可以先把box先下載到本地在進行添加(這時就是本地路徑了)
我的路徑是:http://cloud.centos.org/centos/7/vagrant/x86_64/images/CentOS-7-x86_64-Vagrant-1902_01.VirtualBox.box
–insecure:不驗證SSL證書
在線安裝(安裝比較慢)
在空文件夾初始化虛擬機(這時會生成Vagrantfile文件)
vagrant init centos/7
vagrant 命令
描述 | 命令 | 描述 | 命令 |
---|---|---|---|
在初始化完的文件夾內啓動虛擬機 | vagrant up | 查找虛擬機的運行狀態 | vagrant status |
ssh登錄啓動的虛擬機 | vagrant ssh | 掛起啓動的虛擬機 | vagrant suspend |
喚醒虛擬機 | vagrant resume | 重啓 虛擬機 | vagrant reload |
關閉虛擬機 | vagrant halt | 刪除當前虛擬機 | vagrant destroy |
在終端裏對開發環境進行打包 | vagrant package |
box管理命令
描述 | 命令 | 描述 | 命令 |
---|---|---|---|
查看本地box列表 | vagrant box list | 添加box到列表 | vagrant box add name url |
從box列表移除 | vagrant box remove name | 輸出用於ssh連接的一些信息 | vagrant ssh-config |
舉例
- 初始化
$ vagrant init centos/7
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.
- 啓動(在線拉取box比較慢)
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' version '1902.01' is up to date...
==> default: A newer version of the box 'centos/7' for provider 'virtualbox' is
==> default: available! You currently have version '1902.01'. The latest is version
==> default: '2004.01'. Run `vagrant box update` to update.
==> default: Setting the name of the VM: dome1_default_1589966685326_12869
==> 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: 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+VirtualBox/dome/dome1/ => /vagrant
- 查看服務器信息
$ vagrant ssh-config
Host default
HostName 127.0.0.1
User vagrant
Port 2222
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile D:/vagrant+VirtualBox/dome/dome1/.vagrant/machines/default/virtualbox/private_key
IdentitiesOnly yes
LogLevel FATAL
- 連接成功
$ vagrant ssh
[vagrant@bogon ~]$