快速使用vagrant创建多Centos7.6环境

vagrant是什么?

创建虚拟机最方便的方式,windows,Linux,Mac都支持的很好。

备注:采用docker-compose安装成功,用k8s安装有点问题

环境准备(windows环境,其他环境类似)

(1) Oracle VM VirtualBox 下载和安装

https://www.virtualbox.org/

配置PATH环境变量 增加 C:\Program Files\Oracle\VirtualBox

$ VBoxManage.exe --version #### 环境确认
6.0.14r133895             

(2) vagrant下载和安装

https://www.vagrantup.com/

配置PATH环境变量 增加 C:\HashiCorp\Vagrant\bin

$ vagrant.exe --version    #### 环境确认
Vagrant 2.2.6              

下载vbox镜像

比如我们需要使用多个centos7.6实例测试k8s集群安装

命令行自己下载运行vbox

首先下载后,再运行:

如果觉得慢,可以使用下面的连接直接下载

一次运行多个centos7.6实例

(1) 创建工作目录

mkdir E:/work

cd work

手动添加box

vagrant box add cent761 vagrant-centos-7.6.box
vagrant box add cent762 vagrant-centos-7.6.box
vagrant box add cent763 vagrant-centos-7.6.box
vagrant box add cent764 vagrant-centos-7.6.box
vagrant box add cent765 vagrant-centos-7.6.box
vagrant box add cent766 vagrant-centos-7.6.box
$ vagrant box add cent761 vagrant-centos-7.6.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'cent761' (v0) for provider:
    box: Unpacking necessary files from: file://E:/work/centos76/vagrant-centos-7.6.box
    box:
==> box: Successfully added box 'cent761' (v0) for 'virtualbox'!

(2) 创建启动文件

touch Vagrantfile

编辑文件 Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.define :cent761 do |cent761|
    cent761.vm.provider "virtualbox" do |v|
          v.customize ["modifyvm", :id, "--name", "cent761", "--memory", "1024"]
    end
    cent761.vm.box = "cent761"
    cent761.vm.hostname = "cent761"
    cent761.vm.network :private_network, ip: "192.168.2.110"
  end
    
  config.vm.define :cent762 do |cent762|
    cent762.vm.provider "virtualbox" do |v|
          v.customize ["modifyvm", :id, "--name", "cent762", "--memory", "1024"]
    end
    cent762.vm.box = "cent762"
    cent762.vm.hostname = "cent762"
    cent762.vm.network :private_network, ip: "192.168.2.120"
  end
      
  config.vm.define :cent763 do |cent763|
    cent763.vm.provider "virtualbox" do |v|
          v.customize ["modifyvm", :id, "--name", "cent763", "--memory", "1024"]
    end
    cent763.vm.box = "cent763"
    cent763.vm.hostname = "cent763"
    cent763.vm.network :private_network, ip: "192.168.2.130"
  end
end

(3) 启动这3个实例

vagrant up

想启动更多,自己加些配置,然后执行vagrant reload即可

状态查看、登录、销毁

(1) 查看注册的box

$ vagrant box list
cent761 (virtualbox, 0)
cent762 (virtualbox, 0)
cent763 (virtualbox, 0)
cent764 (virtualbox, 0)
cent765 (virtualbox, 0)
cent766 (virtualbox, 0)

(2) 状态查看

$ vagrant status  
Current machine states:

cent761                   running (virtualbox)
cent762                   running (virtualbox)
cent763                   running (virtualbox)

(4) SSH登录

$ vagrant ssh cent761

(5) 销毁实例

$ vagrant destroy cent761

(6) 删除虚拟机文件

$ vagrant box remove cent761

常用命令

vagrant init 初始化虚拟机

vagrant up 启动虚拟机

vagrant halt 关闭虚拟机

vagrant reload 重启虚拟机

vagrant ssh 登录虚拟机

vagrant status 查看虚拟机运行状态

vagrant global-status 查看虚拟机状态

vagrant destroy 销毁虚拟机

vagrant box list 查看本地Box列表

vagrant box add 添加Box

vagrant box remove 删除Box

vagrant package 打包虚拟机成Box

vagrant status

更多命令请参考官方网站

https://www.vagrantup.com/docs/cli/