Vagrant詳細教程

哈哈哈hh發表於2022-05-09

映象下載、域名解析、時間同步請點選  阿里雲開源映象站

一、安裝virtualBox

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

file

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

file

在安裝完主程式後,直接雙擊擴充套件包檔案即可安裝擴充套件包。

二、安裝Vagrant

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

file

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

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

file

三、下載虛擬機器映象

使用 Vagrant 建立虛機時,需要指定一個映象,也就是  box。開始這個 box 不存在,所以 Vagrant 會先從網上下載,然後快取在本地目錄中。

Vagrant 有一個 ,裡面列出了都有哪些映象可以用,並且提供了操作文件。

file

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

下面我給出最常用的兩個 Linux 作業系統映象的下載地址:

CentOS

CentOS 的映象下載網站是: 

在其中選擇自己想要下載的版本,列表中有一個  vagrant 目錄,裡面是專門為 vagrant 構建的映象。選擇其中的  .box 字尾的檔案下載即可。這裡可以使用下載工具,以較快的速度下載下來。

Ubuntu

Ubuntu 的映象下載網站是:  http://cloud-images.ubuntu.com/

同樣先選擇想要的版本,然後選擇針對 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

file

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

3、檢視虛擬機器的狀態

指令: vagrant status

file

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

4、連結虛擬機器

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

root 使用者沒有預設密碼,也不能直接登入。需要 root 許可權的命令可以通過在命令前新增  sudo 來執行,也可以執行  sudo -i 直接切換到  root 使用者。

這時候開啟 VirtualBox 程式,可以看到自動建立的虛機:

file

我們也可以在 VirtualBox 的終端上登入系統,預設的登入使用者名稱和密碼都是  vagrant,但是個人覺得不是很方便。

更推薦大家使用 vagrant ssh

file

5、停止虛擬機器:

指令: vagrant halt

file

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
  # 
  # Every Vagrant development environment requires a box. You can search for
  # boxes at 
  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"

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

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

file

還有個地方需要注意,預設的 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 會在啟動虛機時自動建立。所以,如果你想要利用已有的網路,請檢視現有主機網路配置:

file

file

最好這個網路也不要啟用 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.

本文轉自:https://www.cnblogs.com/Se7eN-HOU/archive/2022/04/21/16171456.html


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/70003733/viewspace-2892985/,如需轉載,請註明出處,否則將追究法律責任。

相關文章