vagrant構建centos虛擬環境

vagrant搭建centos

什麼是vagrant

Vagrant 是一個簡單易用的部署工具,用英文說應該是 Orchestration Tool 。它能幫助開發人員迅速的構建一個開發環境,幫助測試人員構建測試環境,Vagrant 基於 Ruby 開發,使用開源 VirtualBox 作為虛擬化支援,可以輕鬆的跨平台部署。

如何使用

1、構建本地的目錄

 /Users/yj/vagrant/centos7

2、官方下載對應的鏡像文件,官方下載地址

MacBook-Pro-3:centos7 yj$ wget //github.com/CommanderK5/packer-centos-template/releases/download/0.7.2/vagrant-centos-7.2.box

3、導入剛剛下載的鏡像(box文件)

MacBook-Pro-3:centos7 yj$ vagrant box add centos7.2 /Users/yj/vagrant/centos7/vagrant-centos-7.2.box 
==> vagrant: A new version of Vagrant is available: 2.2.15 (installed version: 2.2.14)!
==> vagrant: To upgrade visit: //www.vagrantup.com/downloads.html

==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos7.2' (v0) for provider: 
    box: Unpacking necessary files from: file:///Users/yj/vagrant/centos7/vagrant-centos-7.2.box
==> box: Successfully added box 'centos7.2' (v0) for 'virtualbox'!

4、初始化

MacBook-Pro-3:centos7 yj$ vagrant init

這時候當前目錄會生成一個Vagrantfile文件

5、修改Vagrantfile中的box名稱

config.vm.box = "centos7-1"

6、啟動

MacBook-Pro-3:centos7 yj$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos7-1'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: centos7_default_1619487768038_36727
==> default: Fixed port collision for 22 => 2222. Now on port 2200.
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2200 (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:2200
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Remote connection disconnect. Retrying...
    default: Warning: Connection reset. Retrying...
    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: 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.0.14
    default: VirtualBox Version: 6.1
==> default: Mounting shared folders...
    default: /vagrant => /Users/yj/vagrant/centos7

7、登入

可直接只用vagrant ssh登入

MacBook-Pro-3:centos7 yj$ vagrant ssh
Last failed login: Mon Apr 26 22:52:26 BRT 2021 from 10.0.2.2 on ssh:notty
There were 5 failed login attempts since the last successful login.
Last login: Mon Apr 26 22:50:07 2021 from 10.0.2.2
[vagrant@localhost ~]$ 

也可以使用ssh

$ ssh -p 2200 [email protected]

上面啟動的時候已經告訴我們地址和埠了

帳號:root  
密碼:vagrant

同時構建多台

修改Vagrantfile

修改之前產生的Vagrantfile文件為

Vagrant.configure("2") do |config|
  
  config.vm.define "centos7-1" do |vb|
      config.vm.provider "virtualbox" do |v|
      v.memory = 1024
      v.cpus = 1
    end
  vb.vm.host_name = "centos7-1"
  vb.vm.network "private_network", ip: "192.168.56.111"
  vb.vm.box = "centos7.2"
  end

  config.vm.define "centos7-2" do |vb1|
      config.vm.provider "virtualbox" do |v|
      v.memory = 1024
      v.cpus = 1
    end
  vb1.vm.host_name = "centos7-2"
  vb1.vm.network "private_network", ip: "192.168.56.112"
  vb1.vm.box = "centos7.2"
  end

  config.vm.define "centos7-3" do |vb2|
      config.vm.provider "virtualbox" do |v|
      v.memory = 1024
      v.cpus = 1
    end
  vb2.vm.host_name = "centos7-3"
  vb2.vm.network "private_network", ip: "192.168.56.113"
  vb2.vm.box = "centos7.2"
  end
end

網路使用的是私有網路,私有網路和公有網路區別可以看下文

啟動

MacBook-Pro-3:centos7 yj$ vagrant up

默認的帳號還是root,密碼還是vagrant

這裡設置了靜態的ip,我們就可以通過靜態ip直接訪問虛擬機了

$ ssh [email protected]

vagrant中的網路

私有網路

private_network

私有網路,對應於virtualboxhost-only網路模型,這種模型下,虛擬機之間和宿主機(的虛擬網卡)之間可以互相通訊,但不在該網路內的設備無法訪問虛擬機

如果私有網路的虛機不在一個網路,vagrant為這些private_network網路配置的IP地址並不在同一個網段。vagrant會自動為不同網段創建對應的host-only網路。

所以使用private_network如果沒有外部機器(虛擬機宿主機之外的機器)連接,使用這種方式設置的靜態ip,能夠擺脫主機網路變換的限制。

PS:比如public_network如果更換了wefi連接,之前設置的靜態ip可能就不可用了,因為網段不一樣了。

vb1.vm.network "private_network", ip: "192.168.56.112"

公有網路

public_network

公有網路,對應於virtualbox的橋接模式,這種模式下,虛擬機的網路和宿主機的物理網卡是平等的,它們在同一個網路內,虛擬機可以訪問外網,外界網路(特指能訪問物理網卡的設備)也能訪問虛擬機

vagrantvirtualbox配置的public_network,其本質是將虛擬機加入到了virtualbox的橋接網路內。

vagrant在將虛擬機的網卡加入橋接網路時,默認會互動式地詢問用戶要和哪個宿主機上的網卡進行橋接,一般來說,應該選擇可以上外網的物理設備進行橋接。

由於需要非互動式選擇或者需要先指定要橋接的設備名,而且不同用戶的網路環境不一樣,因此如非必要,一般不在vagrant中為虛擬機配置public_network

公有網路的iP網路要和主機的網段一致。

vb.vm.network "public_network", ip: "192.168.44.111",bridge: "en0: Wi-Fi (AirPort)"

常用的命令

子命令 功能說明
box 管理box鏡像(box是創建虛擬機的模板)
init 初始化項目目錄,將在當前目錄下生成Vagrantfile文件
up 啟動虛擬機,第一次執行將創建並初始化並啟動虛擬機
reload 重啟虛擬機
halt 將虛擬機關機
destroy 刪除虛擬機(包括虛擬機文件)
suspend 暫停(休眠、掛起)虛擬機
resume 恢復已暫停(休眠、掛起)的虛擬機
snapshot 管理虛擬機快照(hyperv中叫檢查點)
status 列出當前目錄(Vagrantfile所在目錄)下安裝的虛擬機列表及它們的狀態
global-status 列出全局已安裝虛擬機列表及它們的狀態
ssh 通過ssh連接虛擬機
ssh-config 輸出ssh連接虛擬機時使用的配置項
port 查看各虛擬機映射的埠列表(hyperv不支援該功能)

參考

【熟練使用vagrant(11):vagrant配置虛擬機網路】//www.junmajinlong.com/virtual/vagrant/vagrant_network/

Tags: