Combine vagrant with virtualbox to quickly build a virtual machine:
-
Download the latest version of vagrant : Vagrant by HashiCorp (vagrantup.com)
-
Download the latest version of virtualbox: Oracle VM VirtualBox
-
Then build the virtual machine:
-
Check the network card settings of our virtualbox
-
Then set the storage location of our virtual machine globally
-
Come to the location where we saved the vagrant file
and open the dos command to initialize the file with all our heart
$ vagrant init centos/7
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.
After executing the above command, the Vagrantfile file will be generated in the user's home directory
VagrantFile file:
Vagrant.configure("2") do |config|
#遍历3次。生成3个
(1..3).each do |i|
config.vm.define "k8s-node#{i}" do |node|
# 设置虚拟机的Box。指定本地的box文件
node.vm.box = "centos/7"
# 设置虚拟机的主机名
node.vm.hostname="k8s-node#{i}"
# 设置虚拟机的IP
node.vm.network "private_network", ip: "192.168.56.#{99+i}", netmask: "255.255.255.0"
# 设置主机与虚拟机的共享目录
# node.vm.synced_folder "~/Documents/vagrant/share", "/home/vagrant/share"
# VirtaulBox相关配置
node.vm.provider "virtualbox" do |v|
# 设置虚拟机的名称
v.name = "k8s-node#{i}"
# 设置虚拟机的内存大小
v.memory = 4096
# 设置虚拟机的CPU个数
v.cpus = 4
end
end
end
end
-
then start working
-
Since we are going to download centos/7 remotely, it will be very long to wait for the trial lecture, so we can download the file directly first.
-
Then add centos/7 to vagrant
vagrant box add centos/7 D:\developsoft\CentOS-7-x86_64-Vagrant-1905_01.VirtualBox.box
-
Come to the installation directory (make sure to start virtualbox) Open the dos command box and enter the command
vagrant up
-
To create a virtual machine:
Encountered problem one:
The problem is that virtualization is not turned on:
Solution: Turn on virtualization
Modify access:
Use vagrant's ssh to connect and use it
vagrant ssh 虚拟机名称
Then enter the file to modify:
vi /etc/ssh/sshd_config
Then restart the virtual machine:
service sshd restart