Vagrant實戰

介紹

平時工作一直在Linux上做開發,Docker用的比較多,最近要用Vagrant搭建Linux開發環境,所以用了一下,整理一些資料。

Vagrant其實是一個可以方便設置使用虛擬機的工具,底層支持VirtualBox和VMWare,我的Windows機器上已經安裝了VirtualBox,我就基於VirtualBox說說怎麼使用吧。

安裝

首先從下面地址下載vagrant安裝包,直接安裝可執行文件就可以了。
https://www.vagrantup.com

虛擬機管理

通常情況下,我們可以通過下面兩個命令來創建並啓動虛擬機,但是由於下載實在太慢,所以不建議這麼玩。

vagrant init centos/7
vagrant up

我們可以先通過 https://app.vagrantup.com/boxes/search 地址,直接下載虛擬機的vbox文件,然後基於這個vbox文件來創建虛擬機。

比如,這裏我使用下面地址來下載CentOS7的vbox文件

https://app.vagrantup.com/centos/boxes/7

文件名爲:CentOS-7-x86_64-Vagrant-1905_01.VirtualBox.box

創建centos7目錄,然後把CentOS-7-x86_64-Vagrant-1905_01.VirtualBox.box文件複製到目錄下。

添加虛擬機

D:\vagrant\centos7> vagrant box add centos7 CentOS-7-x86_64-Vagrant-1905_01.VirtualBox.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos7' (v0) for provider:
    box: Unpacking necessary files from: file://D:/vagrant/centos7/CentOS-7-x86_64-Vagrant-1905_01.VirtualBox.box
    box:
==> box: Successfully added box 'centos7' (v0) for 'virtualbox'!

初始化

D:\vagrant\centos7> vagrant init centos7
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.

經過這一步後,會在當前目錄下產生Vagrantfile文件,這個文件類似於Docker裏的Dockerfile文件。

啓動虛擬機

D:\vagrant\centos7> vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos7'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: centos7_default_1576066403778_3997
==> 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/centos7/ => /vagrant

查看虛擬機

D:\vagrant\centos7>vagrant box list
centos7 (virtualbox, 0)

訪問虛擬機

虛擬機啓動後,可以通過兩種方式登錄到虛擬機裏

第一種方式是通過vagrant ssh命令方式

D:\vagrant\centos7>vagrant ssh
[vagrant@localhost ~]$ cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)

第二種方式是通過ssh 127.0.0.1 2222命令方式。在使用vagrant up啓動虛擬機的過程種,日誌裏會打印下面信息

...
==> 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
...

這是告訴我們vagrant做了一個端口轉發,把容器的22端口映射成當前物理機的2222端口,用戶是vagrant,使用private key認證。其中private key文件默認位置在當前虛擬機目錄下的.vagrant\machines\default\virtualbox\private_key文件。此時我們就可以通過xshell來登錄127.0.0.1的2222端口了。

掛起虛擬機

D:\vagrant\centos7>vagrant suspend
==> default: Saving VM state and suspending execution...

恢復虛擬機

D:\vagrant\centos7>vagrant resume
==> 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.

停止虛擬機

D:\vagrant\centos7>vagrant halt
==> default: Attempting graceful shutdown of VM...

啓動虛擬機

D:\vagrant\centos7>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> 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/centos7/ => /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.

銷燬虛擬機

停止當前運行的虛擬機並銷燬所有創建的資源

D:\vagrant\centos7> vagrant destroy

刪除虛擬機

D:\vagrant> vagrant box remove centos7
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章