创建vagrant 镜像_Vagrant + VirtualBox 快速创建虚拟机

本文介绍了如何利用Vagrant和VirtualBox快速创建虚拟机。首先,需下载并安装VirtualBox,接着安装Vagrant并验证版本。通过`vagrant init`初始化目录,指定如`centos/7`的box,生成`Vagrantfile`。随后,运行`vagrant up`启动虚拟机,并用`vagrant ssh`登录。遇到`vagrant up`下载慢的问题,可以从第三方网站下载box,使用`vagrant box add`添加。此外,还提到了如何以root用户登录虚拟机。
摘要由CSDN通过智能技术生成

Vagrant + VirtualBox  快速创建虚拟机

本篇向大家介绍一个 工具 Vagrant ,它可以配合VirtualBox 实现快速创建虚拟机

概述

Vagrant是一个基于Ruby的工具,用于创建和部署虚拟化开发环境。它 使用Oracle的开源VirtualBox虚拟化系统,使用 Chef创建自动化虚拟环境。我们可以使用它来干如下这些事:

  • 建立和删除虚拟机

  • 配置虚拟机运行参数

  • 管理虚拟机运行状态

  • 自动配置和安装开发环境

  • 打包和分发虚拟机运行环境

Vagrant的运行,需要依赖某项具体的虚拟化技术,最常见的有VirtualBox以及VMWare两款,早期,Vagrant只支持VirtualBox,后来才加入了VMWare的支持。

为什么我们要选择Vagrant呢?因为它有跨平台可移动自动化部署无需人工参与等优点。

因为我们只需要把Vagrantfile 文件 放到不同的机器上 可以很方便的运行

aa4ed38ba0f9473fc8aa4dfde559b28a.png

在Vagrant体系中,有个box(箱子)的概念,这优点类似于docker体系中的image(镜像)。基于同一个box,不同的人可以运行得到相同的内容。这个我们下文再详细说。

1.下载 VirtualBox 和安装

因为我们知道vagrant依赖virtualbox,所以我们需要在安装vagrant之前先安装virtualbox,

下载地址如下

目前我下载的是 最新版本 VirtualBox 6.1

https://www.virtualbox.org/wiki/Download_Old_Builds

双击即可安装

85956e290c7f1176a5045683f55f121a.png

安装过程很简单 就是傻瓜式的点击下一步即可

2.下载Vagrant

Vagrant 很小 只有30M左右 ,安装后就可使用命令行 输入 Vagrant version 查看是否安装成功

下载地址如下

https://www.vagrantup.com/downloads.html

2.1 验证 vagrant version

输入 vagrant version

a83228a05fb63b08e937c6e2b443d3c9.png

2.2 通过vagrant 初始化目录

输入vagrant init centos/7  , centos/7 在vagrant 中代表 box 和docker的 images 很像

会在当前目录下面生成一个 Vagrantfile文件 是不是很像 Dockerfile 。。

b8f5ca2582d58b8ccb9b78f5fb7372e4.png

默认生成的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 就可以启动这个虚拟机了

f4fc9977b50b554304ddce40ba39f92c.png

此时VirtualBox 就会有新的虚拟机可以看到

fedf7d182d016390769fe1583d656941.png

2.4 vagrant ssh  进入虚拟机

通过输入命令 vagrant ssh 进入虚拟机 ,以 vagrant 作为用户

可以看到直接进入了centos7 ,是不是很方便

104adc2e7944889fb561e41995a039d2.png

3. 问题总结

3.1 vagrant up 特别慢

因为当我们本地不存在 centos/7 box 的时候,那么会去vagrant hub 上面下载,然后这个下载是很慢很慢的 。。。

解决方法

访问如下地址 http://www.vagrantbox.es/ ,然后通过下载工具 下载,如迅雷

提供了很多版本,如centos 、ubuntu 、debian 等等。。复制下载地址 进行下载

b8fd1203d22ee829d575a8e5f938c937.png

下载成功后 通过下面 这个命令

 #加入box  把下载的 box 地址加入进去 $ vagrant box add {title} {url}

6c646c6a29434cab69479fc311b89bb5.png

当把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

84abbd268920ede1d0ed5035bebf7f1b.png

a1d8e1f139c3b76c8ddd78ccd964daa8.png

修改 Vagrantfile 文件

config.ssh.username = "root"config.ssh.password = "密码"

3f4a0b10014fda5f4f422143e77af561.png

提示输入root密码,输入完之后,就可以直接登录了。

输入 vagrant ssh

085d537e6636a11cb29cba4989f20a90.png

总结

本篇主要讲解 Vagrant 的基本使用 以及如何搭配VirtualBox 通过命令行快速创建虚拟机 对虚拟机进行操作,

其实Vagrant 的功能还有很多,可以通过配置Vagrantfile 文件可以让启动的虚拟机自动的 安装一些软件 如Docker 等等,像Vagrantfile 最后面给出来案例 :

config.vm.provision "shell", inline: <

apt-get update

apt-get install -y apache2

后期只要把配置好的Vagrantfile 拿到其他地方 依然可以直接执行,因为它是跨平台的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值