Combine vagrant with virtualbox to quickly build a virtual machine:

Combine vagrant with virtualbox to quickly build a virtual machine:

  1. Download the latest version of vagrant : Vagrant by HashiCorp (vagrantup.com)

  2. Download the latest version of virtualbox: Oracle VM VirtualBox

  3. Then build the virtual machine:

    1. Check the network card settings of our virtualbox[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-O2FRA7is-1670080555807) (k8s deployment: .assets/image-20221203224045589.png)]

    2. Then set the storage location of our virtual machine globallyinsert image description here

Come to the location where we saved the vagrant file
(img-oBhhb0zb-1670080555809)(k8s Department: .assets/image-20221203224506269.png)]
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[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-rCA3CbNR-1670080555809) (k8s deployment: .assets/image-20221203224644567.png)]

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
  1. then start working

  2. 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.

  3. Then add centos/7 to vagrant

    vagrant box add centos/7 D:\developsoft\CentOS-7-x86_64-Vagrant-1905_01.VirtualBox.box
    
  4. Come to the installation directory (make sure to start virtualbox) Open the dos command box and enter the command

    vagrant up
    
  5. To create a virtual machine:[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-kEuF0IMJ-1670080555809) (k8s deployment: .assets/image-20221203225100268.png)]

Encountered problem one:

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-UUAkyb9C-1670080555809) (k8s deployment: .assets/image-20221203225253691.png)]

The problem is that virtualization is not turned on:

Solution: Turn on virtualization

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-cnKhsxZB-1670080555810) (k8s deployment: .assets/image-20221203225402882.png)]

Modify access:

Use vagrant's ssh to connect and use it

vagrant ssh 虚拟机名称

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-XIiTTXPg-1670080555810) (k8s deployment: .assets/image-20221203225646739.png)]

Then enter the file to modify:

vi /etc/ssh/sshd_config

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-T1bnGllr-1670080555810) (k8s deployment: .assets/image-20221203230151450.png)]

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-p4GemLcW-1670080555811) (k8s deployment: .assets/image-20221203230101458.png)]

Then restart the virtual machine:

service sshd restart

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-pn7l43Ii-1670080555811) (k8s deployment: .assets/image-20221203230300167.png)]

Guess you like

Origin blog.csdn.net/qq_63946922/article/details/128168082