vagrant搭建centos
什么是vagrant
Vagrant 是一個簡單易用的部署工具,用英文說應該是 Orchestration Tool 。它能幫助開發人員迅速的構建一個開發環境,幫助測試人員構建測試環境,Vagrant 基於 Ruby 開發,使用開源 VirtualBox 作為虛擬化支持,可以輕松的跨平台部署。
如何使用
1、構建本地的目錄
/Users/yj/vagrant/centos7
2、官方下載對應的鏡像文件,官方下載地址
MacBook-Pro-3:centos7 yj$ wget https://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: https://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 root@127.0.0.1
上面啟動的時候已經告訴我們地址和端口了
賬號: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 root@192.168.44.113
vagrant中的網絡
私有網絡
private_network
私有網絡,對應於virtualbox
的host-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
的橋接模式,這種模式下,虛擬機的網絡和宿主機的物理網卡是平等的,它們在同一個網絡內,虛擬機可以訪問外網,外界網絡(特指能訪問物理網卡的設備)也能訪問虛擬機
vagrant
為virtualbox
配置的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配置虛擬機網絡】https://www.junmajinlong.com/virtual/vagrant/vagrant_network/