徐州建站网站模板,唐山玉田网站建设,设计素材网站哪个好用,郑州专门做网站的公司有哪些文章目录概述软件安装安装VirtualBox安装Vagrant配置环境用Vagrant创建一个VMVagrantfile文件配置常用命令概述
Vagrant是一个创建虚拟机的技术#xff0c;是用来创建和管理虚拟机的工具#xff0c;本身自己并不能创建管理虚拟机。创建和管理虚拟机必须依赖于其他的虚拟化技…
文章目录概述软件安装安装VirtualBox安装Vagrant配置环境用Vagrant创建一个VMVagrantfile文件配置常用命令概述
Vagrant是一个创建虚拟机的技术是用来创建和管理虚拟机的工具本身自己并不能创建管理虚拟机。创建和管理虚拟机必须依赖于其他的虚拟化技术的产品例如Virtualbox、AWS、Docker等。可以更方便的创建虚拟机对虚拟机进行管理。
软件安装
安装VirtualBox
VirtualBox是一款开源虚拟机软件。使用者可以在 VirtualBox 上安装并且执行 Solaris、Windows、DOS、Linux、OS/2 Warp、BSD 等系统作为客户端操作系统。 VirtualBox下载链接
安装Vagrant
Vagrant 是一款虚拟机管理工具Vagrant 支持管理多种虚拟化管理工具的虚拟机。可以帮助用户快速启动和配置 VirtualBoxVMware Workstation 等虚拟化工具的虚拟机实例。 Vagrant下载 找到自己对应的系统版本下载安装即可 配置环境
如果你的系统盘的磁盘空间不够大最好在安装完成 Vagrant 后更改Vagrant 的虚拟机镜像存放目录和更改VirtualBox的虚拟机存放路径
配置Vagrant 找到电脑的环境变量设置将默认的vagrant镜像文件路径改为其他盘默认的文件夹为C:\Users\xxx\.vagrant.d.在用户变量或系统变量下选择新建。
用户变量只在本用户生效系统变量在整个系统中生效全部用户 这里我指定的是D:.vagrant.d 配置VirtualBox 启动 VirtualBox 后通过菜单 管理 - 全局设定或者按下快捷键 Ctrl g在全局设定对话框中修改默认虚拟电脑位置指定一个容量较大的磁盘。
用Vagrant创建一个VM
使用 Vagrant 创建虚机时需要指定一个镜像也就是 box。开始这个 box 不存在所以 Vagrant 会先从网上下载然后缓存在本地目录中。 也可以用其他操作系统 在 Vagrant Cloud上有各种虚拟机镜像可以下载。这里以Ubuntu Server 20.04作为演示其他镜像也就是box操作原理相同
启动 PowerShell寻找合适的位置新建文件夹用来存放box的配置文件Vagrantfile 初始Vagrant配置文件 执行vagrant init初始化配置生成Vagrantfile文件
vagrant init peru/ubuntu-20.04-server-amd64其中的peru/ubuntu-20.04-server-amd64就是我们要使用的box名字init指令之后会从网上拉取镜像如果下载过慢可以使用国内镜像站点清华开源软件镜像站 使用以下指令vagrant box add [box名] 文件路径|url|中央仓库镜像
Vagrant初始化时默认创建的虚拟机的网卡使用 NAT 方式。要想能够获得一个本地局域网的动态 IP 地址在初始化box的文件夹中执行
notepad .\Vagrantfile文件内容如下
# -- mode ruby --
# vi set ftruby # All Vagrant configuration is done below. The 2 in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please dont change it unless you know what
# youre 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 peru/ubuntu-20.04-server-amd64# 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: -SHELL# apt-get update# apt-get install -y apache2# SHELL
end将上图所标注的代码注释取消
启动 Ubuntu Sever 20.04 VM在存放box对于的配置文件目录下执行
vagrant up下图表示启动成功. 账户密码默认为vagrant
Vagrantfile文件配置
config.vm.box该名称为box系统镜像名 config.vm.hostname配置虚拟机主机名 config.vm.network这是配置虚拟机网络由于比较复杂我们其后单独讨论 config.vm.synced_folder除了默认的目录绑定外还可以手动指定绑定 config.ssh.username默认的用户是vagrant从官方下载的box往往使用的是这个用户名。如果是自定制的box所使用的用户名可能会有所不同通过这个配置设定所用的用户名。 config.vm.provision我们可以通过这个配置在虚拟机第一次启动的时候进行一些安装配置
常用命令
查看Vagrant版本
vagrant --version查看虚机的状态
vagrant status指定目录下通过ssh连接虚拟机
vagrant ssh关闭虚拟机 直接在 VirtualBox 上关闭虚机或者直接在虚机内部执行 poweroff 命令也都是可以的。
vagrant halt暂停虚拟机
vagrant suspend恢复虚拟机
vagrant resume重载虚拟机 执行下面的命令会重启虚机并且重新加载 Vagrantfile 中的配置信息
vagrant reload删除虚拟机
vagrant destroy查看vagrant下所有box列表box名)
vagrant box list删除相应的box
vagrant box remove box名初始化box的操作会生成vagrant的配置文件Vagrantfile
vagrant init box名通过ssh登录本地环境所在虚拟机前提是已经开启
vagrant ssh [box名]输出用于 ssh 连接的一些信息
vagrant ssh-config