Vagrant + VirtualBox 快速创建虚拟机
本篇向大家介绍一个 工具 Vagrant ,它可以配合VirtualBox 实现快速创建虚拟机
概述
Vagrant是一个基于Ruby的工具,用于创建和部署虚拟化开发环境。它 使用Oracle的开源VirtualBox虚拟化系统,使用 Chef创建自动化虚拟环境。我们可以使用它来干如下这些事:
建立和删除虚拟机
配置虚拟机运行参数
管理虚拟机运行状态
自动配置和安装开发环境
打包和分发虚拟机运行环境
Vagrant的运行,需要依赖某项具体的虚拟化技术,最常见的有VirtualBox以及VMWare两款,早期,Vagrant只支持VirtualBox,后来才加入了VMWare的支持。
为什么我们要选择Vagrant呢?因为它有跨平台、可移动、自动化部署无需人工参与等优点。
因为我们只需要把Vagrantfile 文件 放到不同的机器上 可以很方便的运行
在Vagrant体系中,有个box(箱子)的概念,这优点类似于docker体系中的image(镜像)
。基于同一个box,不同的人可以运行得到相同的内容。这个我们下文再详细说。
1.下载 VirtualBox 和安装
因为我们知道vagrant依赖virtualbox,所以我们需要在安装vagrant之前先安装virtualbox,
下载地址如下
目前我下载的是 最新版本 VirtualBox 6.1
https://www.virtualbox.org/wiki/Download_Old_Builds
双击即可安装
安装过程很简单 就是傻瓜式的点击下一步即可
2.下载Vagrant
Vagrant 很小 只有30M左右 ,安装后就可使用命令行 输入 Vagrant version 查看是否安装成功
下载地址如下
https://www.vagrantup.com/downloads.html
2.1 验证 vagrant version
输入 vagrant version
2.2 通过vagrant 初始化目录
输入vagrant init centos/7
, centos/7 在vagrant 中代表 box 和docker的 images 很像
会在当前目录下面生成一个 Vagrantfile文件 是不是很像 Dockerfile 。。
默认生成的Vagrantfile 默认如下, 有个 config.vm.box = "centos/7" 就是会去拉取 centos/7 的 vagrant box
# -*- 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. 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" # 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: < # apt-get update # apt-get install -y apache2 # SHELLend
2.3 vagrant up 启动虚拟机
直接输入命令
vagrant up
就可以启动这个虚拟机了
此时VirtualBox 就会有新的虚拟机可以看到
2.4 vagrant ssh 进入虚拟机
通过输入命令
vagrant ssh
进入虚拟机 ,以 vagrant 作为用户
可以看到直接进入了centos7 ,是不是很方便
3. 问题总结
3.1 vagrant up 特别慢
因为当我们本地不存在 centos/7 box 的时候,那么会去vagrant hub 上面下载,然后这个下载是很慢很慢的 。。。
解决方法
访问如下地址 http://www.vagrantbox.es/ ,然后通过下载工具 下载,如迅雷
提供了很多版本,如centos 、ubuntu 、debian 等等。。复制下载地址 进行下载
下载成功后 通过下面 这个命令
#加入box 把下载的 box 地址加入进去 $ vagrant box add {title} {url}
当把box 添加好后,再去启动我们的虚拟机 就会很快啦
#此时再去 vagrant up$ vagrant up
3.2 vagrant root登录 虚拟机
默认 vagrant ssh 是通过vagrant 用户登录的,很多命令需要 sudo ,下面看看如何切换成 root 登录
sudo -s passwd
vim /etc/ssh/sshd_configPermitRootLogin prohibit-password改为PermitRootLogin yes把 PasswordAuthentication no 改成`PasswordAuthentication yes``重启ssh服务`service sshd restart
修改 /etc/ssh/sshd_config
修改 Vagrantfile 文件
config.ssh.username = "root"config.ssh.password = "密码"
提示输入root密码,输入完之后,就可以直接登录了。
输入 vagrant ssh
总结
本篇主要讲解 Vagrant 的基本使用 以及如何搭配VirtualBox 通过命令行快速创建虚拟机 对虚拟机进行操作,
其实Vagrant 的功能还有很多,可以通过配置Vagrantfile 文件可以让启动的虚拟机自动的 安装一些软件 如Docker 等等,像Vagrantfile 最后面给出来案例 :
config.vm.provision "shell", inline: <
apt-get update
apt-get install -y apache2
后期只要把配置好的Vagrantfile 拿到其他地方 依然可以直接执行,因为它是跨平台的。