本地開發環境搭建利器--vagrant

今天分享一篇簡短的實踐文章,明天就是五一假期了,祝大家玩得開心。

解決的問題

筆者需要部署一個 etcd 集羣,並非使用 goreman 工具部署的僞集羣。爲了與真實環境對等,需要用到三臺機器,然而筆者手上只有一臺主機,Centos 系統,配置爲 8G 雙核。由此想到在單機 Centos 再啓動虛擬機的想法。

環境準備

vagrant 的使用需要結合 VirtualBox,我們我們準備這兩個軟件的安裝源,以及用到的 centos7 鏡像:

#virtualbox
https://www.virtualbox.org/wiki/Linux_Downloads

#vagrant
https://www.vagrantup.com/downloads.html

#centos7.box
http://cloud.centos.org/centos/7/vagrant/x86_64/images/

筆者下載的是最新的版本,官網的下載速度實在慢(有需要可以聯繫筆者獲取下載好的安裝包),文件如下:

$ 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                                     

安裝

$ yum  -y  localinstall VirtualBox-6.1-6.1.6_137129_el7-1.x86_64.rpm vagrant_2.2.7_x86_64.rpm

如上的命令,即安裝好 vagrant 和 VirtualBox。

初始化

vagrant 的常用命令如下,用於管理boxes的命令,比如添加、刪除等等。此命令的功能主要通過以下子命令完成:

#添加box文件,就是我們剛剛下載的鏡像文件,添加後可以到處使用。
vagrant  box add  CentOS-7-x86_64-Vagrant-2002_01.VirtualBox.box --name centos7
#創建配置文件
vagrant init centos7

初始化的步驟,會生成一個 Vagrantfile,即配置文件。

$ vagrant init centos7

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.

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/。筆者使用的配置如下:

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|
     # Display the VirtualBox GUI when booting the machine
     vb.gui = false
     # Customize the amount of memory on the VM:
     vb.memory = "1024"
   end
end

可以看到,配置比較簡單,使用橋接的方式指定了網絡,設置關閉 gui,並限制內存爲 1GB。

啓動

$ vagrant  up

The provider 'virtualbox' that was requested to back the machine
'default' is reporting that it isn't usable on this system. The
reason is shown below:

VirtualBox is complaining that the kernel module is not loaded. Please
run `VBoxManage --version` or open the VirtualBox GUI to see the error
message which should contain instructions on how to fix this error.

參考,virtualbox-is-complaining-that-the-kernel-module-is-not-loaded。原因是 vagrant 依賴 kernel-devel gcc make perl, 啓動 kernel-devel ,最好按照 vagrant 的提示進行安裝和正在使用內核版本一直的庫。

通過uname查看內核版本:

$ uname -a 

Linux aoho 3.10.0-1062.1.2.el7.x86_64 #1 SMP Mon Sep 30 14:19:46 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

因此我們需要安裝相應的包:

kernel-devel-3.10.0-1062.1.2.el7.x86_64.rpm

在官網下載了對應的包,並安裝。啓動 vagrant:

#啓動虛擬機
vagrant up
#進入虛擬機
vagrant ssh

大功告成!

推薦閱讀

面試合集

訂閱最新文章,歡迎關注我的公衆號

微信公衆號

參考

  1. Vagrant的配置文件Vagrantfile詳解-2

  2. 如何處理VirtualBox啓動錯誤消息:The vboxdrv kernel module is not loaded

  3. virtualbox-is-complaining-that-the-kernel-module-is-not-loaded

  4. www.vagrantup.com

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章