分享一篇簡短的實踐文章,明天就是五一假期了,祝大家玩得開心。
解決的問題
筆者需要部署一個 etcd 集群,並非使用 goreman 工具部署的偽集群。為了與真實環境對等,需要用到三臺機器,然而筆者手上只有一臺主機,Centos 系統,配置為 8G 雙核。由此想到在單機 Centos 再啟動虛擬機的想法。
環境準備
vagrant 的使用需要結合 VirtualBox,我們我們準備這兩個軟件的安裝源,以及用到的 centos7 鏡像:
<code> https: https: http:/<code>
筆者下載的是最新的版本,官網的下載速度實在慢(有需要可以聯繫筆者獲取下載好的安裝包),文件如下:
<code>$ ls CentOS-7
-x86_64-Vagrant-2002
_01.VirtualBox.box VirtualBox-6.1
-6.1
.6
_137129_el7-1.
x86_64.rpm vagrant_2.2
.7
_x86_64.rpm /<code>
安裝
<code>$ yum -y localinstall VirtualBox-6.1
-6.1
.6
_137129_el7-1.
x86_64.rpm vagrant_2.2
.7
_x86_64.rpm/<code>
如上的命令,即安裝好 vagrant 和 VirtualBox。
初始化
vagrant 的常用命令如下,用於管理boxes的命令,比如添加、刪除等等。此命令的功能主要通過以下子命令完成:
<code> vagrant boxadd
CentOS-7
-x86_64-Vagrant-2002
_01.VirtualBox.box --name centos7 vagrant init centos7/<code>
初始化的步驟,會生成一個 Vagrantfile,即配置文件。
<code>$ vagrant init centos7 A `Vagrantfile` has been placedin
this
directory. You are now ready to `vagrant up` your firstvirtual
environment! Please read the commentsin
the Vagrantfileas
wellas
documentationon
`vagrantup.com`for
more informationon
using
Vagrant./<code>
Vagrantfile 配置
常用的配置如下所示:
- config.vm.box:該名稱是再使用 vagrant init 中後面跟的名字
- config.vm.hostname:配置虛擬機主機名
- config.vm.network:這是配置虛擬機網絡,由於比較複雜,我們其後單獨討論
- config.vm.synced_folder:除了默認的目錄綁定外,還可以手動指定綁定
- config.ssh.username:默認的用戶是vagrant,從官方下載的box往往使用的是這個用戶名。如果是自定製的box,所使用的用戶名可能會有所不同,通過這個配置設定所用的用戶名。
- config.vm.provision:我們可以通過這個配置在虛擬機第一次啟動的時候進行一些安裝配置
還有更詳細的配置,參見:
https://www.vagrantup.com/docs/vagrantfile/。筆者使用的配置如下:
<code>Vagrant.configure("2"
)do
|config|
config.vm.box ="centos7"
config.vm.network"public_network"
,ip:
"192.168.0.5"
config.vm.provider"virtualbox"
do
|vb|
vb.gui =false
vb.memory ="1024"
end
end
/<code>
可以看到,配置比較簡單,使用橋接的方式指定了網絡,設置關閉 gui,並限制內存為 1GB。
啟動
<code>$ vagrant upThe
provider 'virtualbox' that was requested to back the machine 'default
'is
reporting that it isn't usable on this system.The
reasonis
shown below:VirtualBox
is
complaining that the kernel moduleis
not loaded.Please
run `VBoxManage
--version` oropen
theVirtualBox
GUI
to see the error message which should contain instructions on how to fix this error./<code>
參考,
virtualbox-is-complaining-that-the-kernel-module-is-not-loaded。原因是 vagrant 依賴 kernel-devel gcc make perl, 啟動 kernel-devel ,最好按照 vagrant 的提示進行安裝和正在使用內核版本一直的庫。
通過uname查看內核版本:
<code>$
uname
-a
Linux
aoho
3.10
.0
-1062.1
.2
.el7.x86_64
/<code>
因此我們需要安裝相應的包:
<code>kernel-devel-3
.10
.0-1062
.1
.2
.el7
.x86_64
.rpm
/<code>
在官網下載了對應的包,並安裝。啟動 vagrant:
<code>vagrant
up vagrant ssh/<code>
大功告成!