环境篇:Virtualbox+Vagrant安装Centos7

1,723 阅读7分钟

Virtualbox+Vagrant安装Centos7

「这是我参与11月更文挑战的第7天,活动详情查看:2021最后一次更文挑战

1. 安装Vagrant

Vagrant下载

vagrant -v 查看版本

2. 安装virtualbox

virtualbox下载

注:注意和Vagrant有版本兼容问题

本人版本

Vagrantvirtualbox
vagrant_2.2.19_x86_64VirtualBox-6.1.30-148432-Win

注:

4. 添加本地centos7镜像

  • 由于直接用vagrant安装下载速度慢卡死问题,先下载centos镜像,再安装就不用命令行下载安装超级慢

centos7镜像下载

  • 添加下载好的centos7镜像到本地box

vagrant box add centos/7 E:\VirtualBox\myConf\CentOS-7-x86_64-Vagrant-2004_01.VirtualBox.box

  • 查询是否已添加到box的镜像

vagrant box list

请使用cmd命令窗口运行

如果使用git bash 工具运行添加时报错: D:/vm/Vagrant/embedded/gems/2.2.19/gems/childprocess-4.1.0/lib/childprocess/windows/process_builder.rb:44:in `encode!': "\xE5" to UTF-8 in conversion from ASCII-8BIT to UTF-8 to UTF-16LE (Encoding::UndefinedConversionError)

找到报错信息的process_builder.rb 44行,编码位置换成一下:newstr.encode!('UTF-16LE', invalid: :replace, undef: :replace, replace: '?')

  def to_wide_string(str)
        newstr = str + "\0".encode(str.encoding)
#         newstr.encode!('UTF-16LE')
        newstr.encode!('UTF-16LE', invalid: :replace, undef: :replace, replace: '?')
      end

5. 创建虚拟机环境

  1. 新建一个空文件夹 image.png

  2. 初始化一个Vagrantfile文件

在此目录下cmd命令生成的VagrantFile虚拟机配置文件,vagrant根据VagrantFile配置文件生成对应的虚拟机

vagrant init

  1. 修改Vagrantfile文件

3.1 配置一:一个文件创建一台虚拟机 修改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.
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.
  #拉取镜像centos/7
  config.vm.box = "centos/7"

  # 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"
  
  #采用桥接网络,共享主机网络
  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
  #
   #虚拟机名字ljw-centos7,内存,核数
	config.vm.provider "virtualbox" do |vb|
	vb.memory = "4096"
	vb.name= "ljw-centos7"
	vb.cpus= 2
	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

3.2 配置二:一个文件创建3台虚拟机配置

修改VagrantFile文件内容如下

Vagrant.configure("2") do |config|
   (1..3).each do |i|
        config.vm.define "kafka#{i}" do |node|
            # 设置虚拟机的Box,使用centos/7这个镜像,上面已经下载到本地并加入到Vagrant中了
            node.vm.box = "centos/7"

            # 设置虚拟机的主机名
            node.vm.hostname="kafka#{i}"
            #采用桥接网络,共享主机网络
            #node.vm.network "public_network
            # 设置虚拟机的IP 从101开始,不建议从1开始
            node.vm.network "private_network", ip: "192.168.56.#{100+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 = "kafka#{i}"
                # 设置虚拟机的内存大小
                v.memory = 3072
                # 设置虚拟机的CPU个数
                v.cpus = 3
            end
        end
   end
end

注意:我这里ip从101开始,从1开始有各种关于Virtualbox仅主机网络配置的问题

6. 创建并启动虚拟机

先打开运行VirtualBox界面,再执行命令

vagrant up

7. 连接配置用密码登录,不用vagrant登录

  1. 进入到相应的服务器

vagrant ssh kafka1

  1. 进入root用户

sudo -i

  1. 修改sshd_config文件,使root用户可以使用密码登录

vi /etc/ssh/sshd_config

PasswordAuthentication yes
#root用户登入
PermitRootLogin yes
  1. 设置root用户的新密码

passwd

要连续输入两次密码,有警告说密码较短或不安全,不用理。

  1. 重启密码服务

systemctl restart sshd

  1. 查看网卡情况

注:下面截图是ip从1开始的。发现1的不能主机远程ssh连接。重新换成101开始,没有问题。我建议从101开始。

ip addr

image.png

image.png

image.png

分析:

  • 三台虚拟机eth0网卡都是10.0.2.15,使用了网络地址转换(NAT)模式,如果虚拟机默认使用该模式,虚拟机群是不能互通的。
  • 三台虚拟机eth1是对的,是我们用Vagrant配置,使用了仅主机模式
  1. 查看默认网卡是什么

ip route show

image.png

image.png

image.png

分析:

  • 三台虚拟机都是使用eth0网卡1,因为他们ip相同,三台虚拟机不能互相使用eth0网络ping通,只能通过主机模式的eth1互相访问

8. 修改Virtualbox网络配置

需求:

分析

  • 搭建NAT模式使虚拟机互通-使用网卡1
  • 搭建仅主机模式,使主机能远程登录虚拟机-使用网卡2
  1. 创建全局网络 先把虚拟机关机。

管理-》全局设定-》网络

点击添加一个网络。默认就行

image.png

  1. 网卡1设置NAT网络模式

每个虚拟机网卡1选择NAT网络连接方法,界面名称选择刚才创建的全局网卡名称,这里默认是界面名称:NatNetwork,再点击MAC地址刷新按钮

image.png

  1. 网卡2设置仅主机模式 为每个虚拟机网卡2选择仅主机网络连接方法,再点击MAC地址刷新按钮

image.png

  1. 仅主机网络配置

管理-》主机网络管理器

由于我Virtualbox配置文件是从101开始的,下面是我的配置

全局配置 image.png

image.png

每台虚拟机选择 image.png

9. 重启虚拟机测试

  1. 查看三台虚拟机eth0的ip是否不一致,不一样才正确,NAT网络
  2. 查看默认网卡是否eth0,eth0正确
  3. 查看eth1的ip是否不一致,不一样才正确,仅主机网络
  4. ping www.baidu.com
  5. 互ping虚拟机eth0网络: ping 10.0.2.15

关闭防火墙

  • systemctl stop firewalld
  • systemctl disable firewalld

注意:

  • vagrant创建的虚拟机初始账号密码都是vagrant
  • vagrant up命令无法生成私钥文件的vagrant ssh命令不能自动登录,需要到GUI界面手动输入账号密码
  • vagrant+centos7 安装报错
  • window生成秘钥:ssh-keygen -t rsa
    • 私钥默认路径:.vagrant\machines\default\virtualbox\private_key
  • window配置了config文件没有配置私钥路径的
    • 路径:C:\Users\pc.ssh\config

image.png

vagrant常用命令

  • vagrant init : 初始化
  • vagrant up: 启动虚拟机
  • vagrant halt: 关闭虚拟机
  • vagrant reload: 重启虚拟机
  • vagrant ssh: SSH 至虚拟机
  • vagrant status : 查看虚拟机运行状态
  • vagrant destroy : 销毁当前虚拟机