windows 下使用vargant 搭建虛擬機服務

使用vagrant
下載
vagrant[https://www.vagrantup.com/downloads.html]
下載管理工具VirtualBox[https://www.virtualbox.org/wiki/Downloads]
分別安裝這兩個
安裝好後,可能還須要你安裝一些組件,按照提示進行安裝便可
vagrant plugin repair
vagrant plugin expunge --reinstallhtml

在本地找一個新建一個文件夾,沒有路徑要求[C:\APP\HashiCorp\vm1]
在文件夾目錄下cmd
第一次的第一個虛擬機須要執行鏡像下載
什麼版本,能夠本身找
ubuntu
vagrant box add https://mirrors.huaweicloud.com/ubuntu-cloud-images/bionic/20200107/bionic-server-cloudimg-amd64-vagrant.box --name ubuntuvm4/bionic64
或者加載本地已經下載好的
vagrant box add C:\APPS\OnlyOne\mega\開發工具\HADOOP\cloud\ubuntu_cloud\bionic-server-cloudimg-amd64-vagrant.box --name ubuntu/bionic64
centos
vagrant box add https://mirrors.nju.edu.cn/centos-cloud/centos/7/vagrant/x86_64/images/CentOS-7-x86_64-Vagrant-2004_01.VirtualBox.box --name centos7/bionic64
源連接可能會失效,若是失效,本身再找,通常就是華爲源,清華源,阿里源
華爲源,https://mirrors.huaweicloud.com/
清華源,https://mirrors.tuna.tsinghua.edu.cn/
南京大學源,https://mirrors.nju.edu.cn/
阿里源,http://mirrors.aliyun.com/
找不到就在裏面瘋狂找ios

初始化,ubuntu/bionic64爲你上方設置的title

vagrant init ubuntu/bionic64
這時會在對應文件夾下生成一個Vagrantfile的文件。shell

默認不配置,是單個虛擬機
配置多機的設置apache

servers = {
    :hadoop1 => '192.168.2.220',
    :hadoop2 => '192.168.2.221',
    :hadoop3 => '192.168.2.222',
    :hadoop4 => '192.168.2.223'
} 

第一種批量循環建立,分配的資源同樣
Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/bionic64"
  config.vm.box_check_update = false

  servers.each do |server_name, server_ip|
    config.vm.define server_name do |server_config|
	  server_config.vm.hostname = "#{server_name.to_s}"
	  server_config.vm.network :private_network, ip: server_ip
	  server_config.vm.provider "virtualbox" do |vb|
	    vb.name = server_name.to_s
		  vb.memory = "1024"
		  vb.cpus = 1
	    end
    end
  end
end

第二種 爲每一個主機單獨做配置json

Vagrant.configure("2") do |config|
  config.vm.define "vagrant1" do |vb|
      config.vm.provider "virtualbox" do |v|
      v.memory = 512
      v.cpus = 1
    end
  vb.vm.host_name = "vagrant1"
  vb.vm.network :public_network, ip: "10.0.2.16"
  vb.vm.box = "centos72"
  end

  config.vm.define "vagrant2" do |vb|
    config.vm.provider "virtualbox" do |v|
      v.memory = 512
      v.cpus = 1
    end
  vb.vm.host_name = "vagrant2"
  vb.vm.network :public_network, ip: "10.0.2.16"
  vb.vm.box = "centos72"
  end

  config.vm.define "vagrant3" do |vb|
    config.vm.provider "virtualbox" do |v|
      v.memory = 512
      v.cpus = 1
    end
  vb.vm.host_name = "vagrant3"
  vb.vm.network :public_network, ip: "10.0.2.16"
  vb.vm.box = "centos72"
  end
end

虛擬機默認磁盤10G,若是須要修改磁盤空間
先執行安裝磁盤插件 vagrant plugin install vagrant-disksize
而後配置你想要分配的空間ubuntu

config.vm.box = "ubuntu/bionic64"
  config.vm.box_check_update = false
  config.disksize.size = '50GB'

執行啓動
vagrant up hadoop4 啓動,根據主機名啓動 不跟主機名全啓動
vagrant halt hadoop4 關機,根據主機名關機 不跟主機名全關vim

若是配置完,啓動報錯 CPU虛擬化沒開啓,須要進到bios
VisualBox報VT-x is disabled in the BIOS. (VERR_VMX_MSR_VMXON_DISABLED)
thinkpad重啓F1進入BIOS,選擇:
Sercurity => Virtualization=>Enable
若是報錯
error: Could not find Host Interface Networking driver! Please reinstall
這是由於,你以前確定卸載過虛擬機的虛擬網卡,因此這裏找不到,那麼須要重裝虛擬網卡,以下
能夠在安裝virtualbox目錄../Oracle VM VirtualBox中的 drivers\ network\ netadp6目錄下有三個文件centos

VBoxNetAdp6.cat
VBoxNetAdp6.inf
VBoxNetAdp6.sys

這就是virtualbox虛擬網卡的驅動,右擊VBoxNetAdp6.inf,點安裝便可,安裝完成後
從新 vagrant up 就可成功了。ruby

默認用戶:vagrant
密鑰路徑在 C:\APP\HashiCorp\vm1.vagrant\machines\hadoop1\virtualbox\下private_key文件
全部的配置都在:
C:\APP\HashiCorp\vm1\Vagrantfile 文件上
使用 vagrant 帳戶和密鑰文件登陸
建立root密碼app

vagrant@hadoop2:~$ sudo su
	root@hadoop2:/home/vagrant# passwd root
	Enter new UNIX password: 
	Retype new UNIX password: 
	passwd: password updated successfully
	root@hadoop2:/home/vagrant#

修改
vim /etc/ssh/sshd_config
將如下參數改成yes 容許

PermitRootLogin yes
PasswordAuthentication yes

重啓sshd
systemctl restart sshd
這樣才能夠直接使用root進行登錄

Vagrantfile 文件完整配置以下

單個虛擬機實例

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
servers = {
    :hadoop13 => '192.168.2.225'
}
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "centos7/bionic64"
  config.vm.box_check_update = false
  config.disksize.size = '50GB'
  servers.each do |server_name, server_ip|
    config.vm.define server_name do |server_config|
	  server_config.vm.hostname = "#{server_name.to_s}"
	  server_config.vm.network :private_network, ip: server_ip
	  server_config.vm.provider "virtualbox" do |vb|
	    vb.name = server_name.to_s
		  vb.memory = "4096"
		  vb.cpus = 2
	    end
    end
  end
  # if errorMsg is "Unknown configuration section 'disksize'."  that 
  # you must install then "vagrant plugin install vagrant-disksize" plugin
  
  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL
end

多個虛擬機

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.

servers = {
    :hadoop1 => '192.168.2.220',
    :hadoop2 => '192.168.2.221',
    :hadoop3 => '192.168.2.222',
    :hadoop4 => '192.168.2.223'
}

Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "ubuntu/bionic64"
  config.vm.box_check_update = false
  # config.disksize.size = '50GB'
  # if you don't specify disk space, the default disk space is 10G
  servers.each do |server_name, server_ip|
    config.vm.define server_name do |server_config|
	  server_config.vm.hostname = "#{server_name.to_s}"
	  server_config.vm.network :private_network, ip: server_ip
	  server_config.vm.provider "virtualbox" do |vb|
	    vb.name = server_name.to_s
		  vb.memory = "1024"
		  vb.cpus = 1
	    end
    end
  end

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL
end

接下來,就能夠經過xshell進行登陸操做了。

相關文章
相關標籤/搜索