您的位置:首页 > 其它

虚拟机管理工具vagrant:安装入门

2016-10-17 11:26 253 查看
What is “Vagrant”?

Vagrant is able to define and control multiple guest machines per Vagrantfile. This is known as a "multi-machine" environment.

首先介绍一下Vagrant:Vagrant通过一种Vagrant
file文件定义和配置虚拟机,并且提供许多简单的命令管理和运行虚拟机。

Vagrant需要配合virtualbox使用,通过Vagrant创建的虚拟机在virtualbox管理界面中可见。

启动Vagrant虚拟机不需要通过virtualbox管理界面,直接在命令行就能用简单的命令操作。

下面我们开始介绍在Win7环境下搭建一个Vagrant环境!

1.安装virtualbox

下载:https://www.virtualbox.org/

安装步骤省略

2.安装Vagrant

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

安装步骤省略

3.用Vagrant创建一个虚拟机

1)初始化

<span style="font-size:12px;">vagrant init hashicorp/precise64</span>


这段命令会在当前文件夹内创建一个Vagrantfile文件,该文件就是虚拟机的配置文件,包括设定虚拟机采用的镜像、开放端口、名称等等。除此之外,

还会创建一个名为.vagrant的隐藏文件夹,该文件夹包含了已经创建了的虚拟机的配置信息。整个初始化的过程类似git
init,可以自行体会。

2)运行

vagrant up
这段命令会根据Vagrantfile文件启动一个虚拟机,初次运行会耗费较长时间,请耐心等待

3)登录虚拟机

vagrant ssh

这段命令会直接以vagrant用户登录刚才启动的虚拟机(密码同用户名),不需要输入密码就能直接登录。

如果你需要root权限,请在
执行每条命令前加sudo

4)操作虚拟机

登录虚拟机后就可以像操作普通linux一样操作

5)关闭虚拟机

首先退出系统:

logout


接着关闭虚拟机

vagrant halt
至此,使用vagrant创建虚拟机的操作结束

4.自定义虚拟机

在一般情况下,虚拟机都有不同的作用,因此需要自行配置虚拟机。

Vagrant创建的虚拟机并不是一尘不变的,可以随时通过修改Vagrantfile来自定义虚拟机。

下面解读一下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://atlas.hashicorp.com/search. config.vm.box = "hashicorp/precise64" //虚拟机基于的镜像文件,每一个虚拟机都需要一个基础镜像(box),
// hashicorp/precise64是官方的ubuntu12镜像
config.vm.hostname="master" //设置虚拟机名,默认同box名,这个推荐设置,不然启动多个虚拟机都是重名的

# 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 //启动虚拟机检查box更新,默认不更新。

# 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.
config.vm.network "forwarded_port", guest: 4506, host: 4506 //配置虚拟机的端口映射,对于某些服务,需要配置此项

# Create a private network, which allows host-only access to the machine
# using a specific IP.
config.vm.network "private_network", ip: "192.168.1.1" //配置虚拟机的内网IP,使虚拟机可以在局域网内互联,推荐配置

# 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" //配置公网IP,默认是桥接模式

# 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 //每次启动虚拟机是否打开VirtualBox图形界面,默认不打开
#
#   # 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.

# Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
# such as FTP and Heroku are also available. See the documentation at
# https://docs.vagrantup.com/v2/push/atlas.html for more information.
# config.push.define "atlas" do |push|
#   push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
# end

# Enable provisioning with a shell script. Additional provisioners such as
# Puppet, Chef, Ansible, Salt, and Docker 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

5.总结

以上就是虚拟机管理工具vagrant:安装入门教程,由于本人水平有限,难免有所疏漏,请读者批评指正。

读者应该亲自动手实践,任何指导文档只有经过自己动手才能有所收获!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  vagrant