阅读 138

环境篇 一一 Virtualbox+Vagrant安装Centos7

1. 安装Vagrant

Vagrant下载

vagrant -v 查看版本

2.  安装virtualbox

virtualbox下载

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

本人版本

Vagrantvirtualbox
Vagrant 2.2.186.1.22

注:

  • Hyper-V启用导致VirtualBox, VMware无法使用说明

  • virtualbox需要电脑关闭虚拟化支持

  • 想用Docker for Windows来做开发环境, 虚拟机必须用Hyper-v, VirtualBox和Docker for Windows不能同时使用

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

5. 创建虚拟机环境

  1. 新建一个空文件夹

image.png

  1. 初始化一个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             node.vm.network "private_network", ip: "192.168.56.#{10+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 复制代码

6. 创建并启动虚拟机

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

vagrant up

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

  1. 进入到相应的服务器vagrant ssh kafka1  

  2. 进入root用户sudo -i

  3. 修改sshd_config文件,使root用户可以使用密码登录 vi /etc/ssh/sshd_config

PasswordAuthentication yes #root用户登入 PermitRootLogin yes 复制代码

  1. 使用 passwd 设置新密码为root

  2. 使用 systemctl restart sshd 重启密码服务

  3. 使用ip addip route show 查看eth1网卡的ip即可使用远程工具登录root用户。root/root和vagrant/vagrant

注意:

  • 账号密码都是vagrant

  • vagrant up命令无法生成私钥文件的vagrant ssh命令不能自动登录,需要到GUI界面手动输入账号密码

    • 私钥默认路径:.vagrant\machines\default\virtualbox\private_key

  • window配置了config文件没有配置私钥路径的

    • 路径:C:\Users\pc.ssh\config

image.png

注:

  • vagrant+centos7 安装报错

  • window生成秘钥:ssh-keygen -t rsa

vagrant常用命令

  • vagrant init : 初始化

  • vagrant up: 启动虚拟机

  • vagrant halt: 关闭虚拟机

  • vagrant reload: 重启虚拟机

  • vagrant ssh: SSH 至虚拟机

  • vagrant status : 查看虚拟机运行状态

  • vagrant destroy :  销毁当前虚拟机


作者:小伙子vae
链接:https://juejin.cn/post/7027705321858531335


文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐