vagrant 学习总结
1 需要 ruby (>=2.2) 版本的,来开发和构造 vagrant.。ruby 和 vagrant对应的版本可在github 上的vagrant.gemspec 上查看。
(1)不要用系统自带的ruby ,使用ruby的版本管理工具来控制ruby 的版本,例如rvm。
(2)vagrant 的配置是基于系统的当前环境,如果vagrant的安装是通过 source,他们是无法使用在通过package环境安装。
2 vagrant + virtualbox 构建集成开发环境
(1) 首先下载最新版本的 vagrant和virtualbox
(2) 配置任何vagrant project项目的第一步是创建 Vagrantfile文件,创建这个文件的目的有两个:
(a)标记项目的root directory。在vagrant中,一些配置选项都与这个 root directory有关。
(b) 描述你项目需要的machine 和 resource 的种类。以及安装的软件和怎样去获取它。
vagrant 有一个内置的命令来初始化:vagrant init
$ mkdir vagrant_getting_started
$ cd vagrant_getting_started
$ vagrant init
这会在当前的目录下创建一个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 = "base"
# 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.
# config.vm.network "forwarded_port", guest: 80, host: 8080
# 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.
# 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
(3) 安装box
下部分代码决定vagrant 使用的box
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
end
也可以加box的版本号
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
config.vm.box_version = "1.1.0"
end
也可以直接指定box的URL地址
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
config.vm.box_url ="http://files.vagrantup.com/precise64.box"
end
(4)up and ssh
$ vagrant up
vagrant up 后你将有一个虚拟机 运行着ubuntu
$ vagrant ssh
连上虚拟机
在虚拟机上
Vagrant shares a directory at /vagrant with the directory on the host containing your Vagrantfile
退出 虚拟机 CTRL+D。
当你不想再操作虚拟机的时候,可以执行 vagrant destroy 回退到你的主机。vagrant 会终止使用任何在虚拟机上使用的资源。
注: vagrant destroy 实际上并不会删除下载的box file 文件。想完全移除box file文件,你可以使用 vagrant box remove 命令
(5) Synced Folders
通过使用同步的文件夹,vagrant 会自动的同步你的电脑上的文件到虚拟机。
默认情况下,vagrant 会将你电脑上的项目目录(注意,这个目录中需要包含Vagrantfile)分享到 虚拟机的 /vagrant 目录。
注意: 当你通过ssh 登陆虚拟机的时候,你所在的目录是 /home/vagrant,而不是同步的/vagrant目录。
$ vagrant up
...
$ vagrant ssh
...
vagrant@precise64:~$ ls /vagrant
Vagrantfile
在虚拟机上看到的Vagrantfile实际上与你电脑上的Vagrantfile文件是一样的。
(6) Provisioning
我们可以通过ssh 登陆到虚拟机安装 webserver,但是这样,每一个人都得这样做。所以,我们通过vagrant 内建的自动化支持,当执行vagrant up后,环境将会被创建号。
Install Apache
创建一个 bootstrap.sh 保存以下内容
#!/usr/bin/env bash
apt-get update
apt-get install -y apache2
if ! [ -L /var/www ]; then
rm -rf /var/www
ln -fs /vagrant /var/www
fi
下一步,配置vagrant 执行 bootstrap.sh文件,编辑 Vagrantfile 文件
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
config.vm.provision :shell, path: "bootstrap.sh"
end
provision 是告知 vagrant 使用 shell provision 设置machine,使用 bootstrap.sh。bootstrap路径是项目的当前路径,即是 Vagrantfile所在的路径。
当所有的都配置好后,可以执行vagrant up 来设置你的虚拟机,如果虚拟机已经在运行,可以执行vagrant reload –provision,将会很快重新启动你的虚拟机,并且跳过初始的导入步骤。
当vagrant 执行完毕,web server 将会启动运行,暂时你不能通过你电脑的浏览器查看。但是你可以确定 provision 已经运行 通过ssh 加载一个文件
$ vagrant ssh
...
vagrant@precise64:~$ wget -qO- 127.0.0.1
默认情况下,apahce的DocumentRoot会指向 /vagrant 文件夹,vagrant默认的同步文件夹。
(7)networking
我们可以通过配置 端口转发,在你的电脑上通过浏览器访问虚拟机上的apahce 项目。
我们通过设置转发的端口,通过我们的电脑访问 apache。编辑 Vagrantfile 如下:
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
config.vm.provision :shell, path: "bootstrap.sh"
config.vm.network :forwarded_port, guest: 80, host: 4567
end
执行vagrant reload 或者vagrant up 命令来 改变我们的状态。
加载 http://127.0.0.1:4567 在 你的浏览器上,你可以看到虚拟机提供的web page
(8) Teardown
Suspending 虚拟机,执行 vagrant suspend 会保存当前虚拟机的状态并且停止虚拟机。当你需要重新工作后,执行 vagrant up 就可以。
vagrant halt
vagrant destroy