Vagrant詳細教程


一、安裝virtualBox

  進入 VirtualBox 的主頁,即可進入下載頁面.

  

  VirtualBox 是一個跨平台的虛擬化工具,支持多個操作系統,根據自己的情況選擇對應的版本下載即可。

 

   

 

  在安裝完主程序后,直接雙擊擴展包文件即可安裝擴展包。

 

 二、安裝Vagrant

 

  在 Vagant 網站下載最新的版本,根據自己的操作系統選擇對應的版本下載即可。

  

 

 

 

  注意,Vagrant 是沒有圖形界面的,所以安裝完成后也沒有桌面快捷方式。具體使用方法,接下來會詳細說明。

  Vagrant 的安裝程序會自動把安裝路徑加入到 PATH 環境變量,所以,這時候可以通過命令行執行 vagrant version 檢查是否安裝成功:

  

 

三、下載虛擬機鏡像

 

  使用 Vagrant 創建虛機時,需要指定一個鏡像,也就是 box。開始這個 box 不存在,所以 Vagrant 會先從網上下載,然后緩存在本地目錄中。

  Vagrant 有一個鏡像網站,里面列出了都有哪些鏡像可以用,並且提供了操作文檔。

  

 

  但是這里默認下載往往會比較慢,所以下面我會介紹如何在其它地方下載到基礎鏡像,然后按照自己的需要重置。如果網速較好,下載順利的朋友可以選擇性地跳過部分內容。

  下面我給出最常用的兩個 Linux 操作系統鏡像的下載地址:

  CentOS

    CentOS 的鏡像下載網站是: 

    在其中選擇自己想要下載的版本,列表中有一個 vagrant 目錄,里面是專門為 vagrant 構建的鏡像。選擇其中的 .box 后綴的文件下載即可。這里可以使用下載工具,以較快的速度下載下來。

  Ubuntu

    Ubuntu 的鏡像下載網站是: 

    同樣先選擇想要的版本,然后選擇針對 vagrant 的 .box 文件即可。

    如果這里官網的速度較慢,還可以從 清華大學的鏡像站 下載。

   

四、添加box

 

  接下來我們需要將下載后的 .box 文件添加到 vagrant 中。

  Vagrant 沒有 GUI,只能從命令行訪問,先啟動一個命令行,然后執行:

  指令1:vagrant box list  查詢vagrant 已經管理的 Box 有哪些

houlei@houleideMacBook-Pro ubuntu % vagrant box list          
There are no installed boxes! Use `vagrant box add` to add some.

  指令2:vagrant box add 將 box 添加到vagrant 中, 命令后面跟着的是box文件路徑,並且通過 --name ubuntu 為這個 box 指定一個名字。

houlei@houleideMacBook-Pro ubuntu % vagrant box add /Users/houlei/Desktop/vagrant/box/xenial-server-cloudimg-amd64-vagrant.box --name ubuntu 
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'ubuntu' (v0) for provider: 
    box: Unpacking necessary files from: file:///Users/houlei/Desktop/vagrant/box/xenial-server-cloudimg-amd64-vagrant.box
==> box: Successfully added box 'ubuntu' (v0) for 'virtualbox'!   # 安裝成功
houlei@houleideMacBook-Pro ubuntu % vagrant box list                                                                                        
ubuntu (virtualbox, 0)     # 剛安裝成功的box,在安裝的時候,我去的名字叫ubuntu
houlei@houleideMacBook-Pro ubuntu % 

   指令3:vagrant box remove NAME 根據名字刪除指定的box

 

五、Vagrant基本操作

 

  1、新建虛擬機

    我們在創建虛擬機的時候,會生產一些文件,所以我們為每個虛擬機最好都創建一個獨立的文件。然后進入文件中

/Users/houlei/Desktop/vagrant/ubuntu
houlei@houleideMacBook-Pro ubuntu % 

    我在桌面上創建了一個vagrant文件夾,在里面有創建了ubuntu文件夾,專門用來存放創建的而這個虛擬機的東西

    新建虛擬機指令:vagrant init [boxname]  加上boxname 表示使用哪個box 創建虛擬機

houlei@houleideMacBook-Pro ubuntu % vagrant init ubuntu
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.
houlei@houleideMacBook-Pro ubuntu % 

    創建成功后,會在文件夾中多一個“Vagrantfile”的文件。

  2、啟動虛擬機

    注意: 在當前這個小例子中,上面所有的 vagrant 命令都需要在 Vagrantfile 所在的目錄下執行。

    啟動虛擬機的指令:vagrant up

    

    只要是沒有報錯,就說明啟動成功了

  3、查看虛擬機的狀態 

    指令:vagrant status

    

 

 

     如果是running 就說明我們的虛擬機,啟動成功了。

  4、鏈接虛擬機

    如果啟動沒問題,接下來執行 vagrant ssh 就能以 vagrant 用戶直接登入虛機中。

    root 用戶沒有默認密碼,也不能直接登錄。需要 root 權限的命令可以通過在命令前添加 sudo 來執行,也可以執行 sudo -i 直接切換到 root 用戶。

    這時候打開 VirtualBox 程序,可以看到自動創建的虛機:

    

 

    我們也可以在 VirtualBox 的終端上登錄系統,默認的登錄用戶名和密碼都是 vagrant,但是個人覺得不是很方便。

    更推薦大家使用 vagrant ssh

    

 

 

 

  5、停止虛擬機:

    指令:vagrant halt

    

 

 

 

  6、 暫停虛擬機

    指令:vagrant suspend

  7、恢復虛擬機

    指令:vagrant resume

    注意: 不管虛機是關閉還是暫停狀態,甚至是 error 狀態,都可以執行 vagrant up 來讓虛機恢復運行。

  8、刪除虛擬機

    指令:vagrant destroy

 

 六、Vagrantfile源文件

    

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "ubuntu"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL
end

  這是一個 Ruby 語法的文件,因為 Vagrant 就是用 Ruby 編寫的。如果編輯器沒有語法高亮可以手動設置文件類型為 Ruby。

  這個缺省文件內容幾乎都是注釋,提示有哪些配置項可以修改,我們不需要去學 Ruby 編程也可以照葫蘆畫瓢的完成基本的配置。

  刨除注釋,這個文件的實際生效內容只有3行

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu"
end

  這里的 config.vm.box 對應的就是虛機的鏡像,也就是 box 文件,這是唯一必填的配置項。

  特別提醒,Vagrantfile 文件名是固定的寫法,大小寫也要完全一樣,修改了就不認識了

 

 七、自定義配置Vagrantfile

 

  下面我將針對這份默認的 Vagrantfile 內容,逐個講解其中的配置含義和如何根據實際情況修改。

  1、配置端口轉發

    端口轉發(Port forward)又叫端口映射,就是把虛機的某個端口,映射到宿主機的端口上。這樣就能在宿主機上訪問到虛擬機中的服務。

  例如啟動虛機時,默認的 22 (guest) => 2222 (host) (adapter 1) 就是把虛機的 SSH 服務端口(22)映射到宿主機的 2222 端口,這樣直接在宿主機通過 ssh 客戶端訪問 127.0.0.1:2222 端口就等價於訪問虛擬機的 22 端口。

  下面這兩段配置就是教我們如何配置額外的端口轉發規則,例如把 Web 服務也映射出來:

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  實際上設置端口轉發這個功能並不實用,一個很明顯的問題就是如果啟動多個虛機,很容易就出現宿主機上端口沖突的問題。即使沒有端口沖突,使用起來也不方便,我個人不推薦使用的,可以把這部分配置直接刪掉。直接使用下面的私有網絡。

  這個功能是虛擬機軟件提供的,可以在虛機的網卡設置中展開高級選項,找到相關的配置:

  

 

  還有個地方需要注意,默認的 SSH 端口映射在這里沒法直接修改。比如像我這樣,2222 端口出現莫名問題,如果想要把 22 端口轉發到其它端口如 22222,直接添加下面這樣的配置是沒用的:

   config.vm.network "forwarded_port", guest: 22, host: 22222  它會在原來的基礎上新加一個端口轉發規則,而不是替代原來的,必須要先強制關閉掉默認的那條規則:
  config.vm.network "forwarded_port", guest: 22, host: 2222, id: "ssh", disabled: "true"
  config.vm.network "forwarded_port", guest: 22, host: 22222

  2、配置私有網絡

    下面這段配置用來配置私有網絡,實際上對應的是 VirtualBox 的主機網絡,也就是 HostOnly 網絡。

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"

  取消注釋最下面一行,就可以為虛機設置指定的私有網絡地址:

config.vm.network "private_network", ip: "192.168.33.10"

  如果這個網段的主機網絡在 VirtualBox 中不存在,Vagrant 會在啟動虛機時自動創建。所以,如果你想要利用已有的網絡,請查看現有主機網絡配置:

  

  

 

  最好這個網絡也不要啟用 DHCP,完全由自己來分配地址,這樣更加清楚。

  

config.vm.network "private_network", ip: "192.168.56.10"

 

  修改完成后,執行 vagrant reload 命令重建虛機,就能看到多出來的網卡了。

  私有網絡實際也可以直接使用 DHCP,但是並不推薦:

  

config.vm.network "private_network", type: "dhcp"

  

  3、配置同步文件夾

houlei@houleideMacBook-Pro ubuntu % vagrant reload    
==> default: Attempting graceful shutdown of VM...
==> 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: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> 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: Warning: Connection reset. Retrying...
==> 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: 5.1.38
    default: VirtualBox Version: 6.1
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => /Users/houlei/Desktop/vagrant/ubuntu # /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.

 

    

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM