Install Vagrant on Ubuntu 14.04
1. Install VirtualBox (https://www.virtualbox.org/wiki/Downloads)
2. Install Vagrant (https://www.vagrantup.com/downloads.html v1.6.5)
Verify: vagrant -v
3. In the command line:
cd /opt/apps/vagrant
vagrant box add chef/ubuntu-14.04 url ###远端的box地址或者本地的box文件名(box中的镜像文件被放到了:/home/vagrant/.vagrant.d/boxes/)
vagrant init chef/ubuntu-14.04
vagrant up
vagrant ssh
账号密码均为vagrant,若需要su root,密码也是vagrant
/vagrant这个目录是自动映射的,被映射到/opt/apps/vagrant
Vagrantfile配置文件详解
config.vm.box = "base" ##
config.vm.hostname = "go-app"
config.vm.provision :shell, path: "bootstrap.sh"
config.vm.synced_folder "/Users/astaxie/data", "/vagrant_data" ##设置额外的同步目录,第一个参数是主机的目录,第二个参数是虚拟机挂载的目录
config.vm.network :forwarded_port, guest: 80, host: 8080 ##把对host机器上8080端口的访问请求forward到虚拟机的80端口的服务上
config.vm.network :private_network, ip: "11.11.11.11" ##一种是host-only(主机模式),意思是主机和虚拟机之间的网络互访,而不是虚拟机访问internet的技术
vagrant reload
vagrant destroy
常用的命令
$ vagrant package # 把当前的运行的虚拟机环境进行打包
$ vagrant init # 初始化
$ vagrant up # 启动虚拟机
$ vagrant halt # 关闭虚拟机
$ vagrant reload # 重启虚拟机
$ vagrant ssh # SSH 至虚拟机
$ vagrant status # 查看虚拟机运行状态
$ vagrant destroy # 销毁当前虚拟机
$ vagrant ssh-config # 输出用于ssh连接的一些信息
vagrant package --output NAME --vagrantfile FILE
可选参数:
--output NAME : (可选)设置通过NAME来指定输出的文件名
--vagrantfile FILE:(可选)可以将Vagrantfile直接封进box中
注:如果网络模式中使用 private_network 的话,在打包之前需要清除一下private_network的设置,避免不必要的错误:
sudo rm -f /etc/udev/rule.d/70-persistent-net.rules
制作完成之后直接将box文件拿到其他计算机上配置即可使用。
#example of docker
Vagrant.configure("2") do |config|
config.vm.provision "docker",
images: ["ubuntu"]
end
Vagrant.configure("2") do |config|
config.vm.provision "docker" do |d|
d.pull_images "ubuntu"
d.pull_images "vagrant"
end
end
#example of network
Vagrant.configure("2") do |config|
config.vm.network "forwarded_port", guest: 80, host: 8080,
auto_correct: true
end
#auto_correct parameter set to true tells Vagrant to auto correct any collisions.
Vagrant.configure("2") do |config|
config.vm.network "private_network", ip: "192.168.50.4",
auto_config: false
end
8. 注意事项
使用 Apache/Nginx 时会出现诸如图片修改后但页面刷新仍然是旧文件的情况,是由于静态文件缓存造成的。需要对虚拟机里的 Apache/Nginx 配置文件进行修改:
# Apache 配置添加:
EnableSendfile off
# Nginx 配置添加:
sendfile off;
https://docs.vagrantup.com/v2/getting-started/index.html
https://github.com/astaxie/Go-in-Action/blob/master/ebook/zh/01.3.md
https://vagrantcloud.com/discover/featured
http://www.vagrantbox.es/
https://github.com/kraksoft/vagrant-box-ubuntu/releases/download/14.04/ubuntu-14.04-amd64.box
#example of MULTI-MACHINE
config.vm.define :haproxy do |haproxy_config|
haproxy_config.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--name", "haproxy", "--memory", "1024"]
end
haproxy_config.vm.box = "ubuntu-14.04"
haproxy_config.vm.hostname = "haproxy"
haproxy_config.vm.network "private_network",ip: "127.0.2.10"
end
config.vm.define :etcd do |etcd_config|
etcd_config.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--name", "etcd", "--memory", "1024"]
end
etcd_config.vm.box = "ubuntu-14.04"
etcd_config.vm.hostname = "etcd"
etcd_config.vm.network "private_network",ip: "127.0.2.12"
end
config.vm.define :docker do |docker_config|
docker_config.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--name", "docker", "--memory", "1024"]
end
docker_config.vm.box = "ubuntu-14.04"
docker_config.vm.hostname = "docker"
docker_config.vm.network "private_network",ip: "127.0.2.14"
end
http://stackoverflow.com/questions/22717428/vagrant-error-failed-to-mount-folders-in-linux-guest
error message :
mount -t vboxsf -o uid=`id -u vagrant`,gid=`getent group vagrant | cut -d: -f3` vagrant /vagrant
mount -t vboxsf -o uid=`id -u vagrant`,gid=`id -g vagrant` vagrant /vagrant
1. Install VirtualBox (https://www.virtualbox.org/wiki/Downloads)
2. Install Vagrant (https://www.vagrantup.com/downloads.html v1.6.5)
Verify: vagrant -v
3. In the command line:
cd /opt/apps/vagrant
vagrant box add chef/ubuntu-14.04 url ###远端的box地址或者本地的box文件名(box中的镜像文件被放到了:/home/vagrant/.vagrant.d/boxes/)
vagrant init chef/ubuntu-14.04
vagrant up
vagrant ssh
账号密码均为vagrant,若需要su root,密码也是vagrant
/vagrant这个目录是自动映射的,被映射到/opt/apps/vagrant
Vagrantfile配置文件详解
config.vm.box = "base" ##
config.vm.hostname = "go-app"
config.vm.provision :shell, path: "bootstrap.sh"
config.vm.synced_folder "/Users/astaxie/data", "/vagrant_data" ##设置额外的同步目录,第一个参数是主机的目录,第二个参数是虚拟机挂载的目录
config.vm.network :forwarded_port, guest: 80, host: 8080 ##把对host机器上8080端口的访问请求forward到虚拟机的80端口的服务上
config.vm.network :private_network, ip: "11.11.11.11" ##一种是host-only(主机模式),意思是主机和虚拟机之间的网络互访,而不是虚拟机访问internet的技术
vagrant reload
vagrant destroy
常用的命令
$ vagrant package # 把当前的运行的虚拟机环境进行打包
$ vagrant init # 初始化
$ vagrant up # 启动虚拟机
$ vagrant halt # 关闭虚拟机
$ vagrant reload # 重启虚拟机
$ vagrant ssh # SSH 至虚拟机
$ vagrant status # 查看虚拟机运行状态
$ vagrant destroy # 销毁当前虚拟机
$ vagrant ssh-config # 输出用于ssh连接的一些信息
vagrant package --output NAME --vagrantfile FILE
可选参数:
--output NAME : (可选)设置通过NAME来指定输出的文件名
--vagrantfile FILE:(可选)可以将Vagrantfile直接封进box中
注:如果网络模式中使用 private_network 的话,在打包之前需要清除一下private_network的设置,避免不必要的错误:
sudo rm -f /etc/udev/rule.d/70-persistent-net.rules
制作完成之后直接将box文件拿到其他计算机上配置即可使用。
#example of docker
Vagrant.configure("2") do |config|
config.vm.provision "docker",
images: ["ubuntu"]
end
Vagrant.configure("2") do |config|
config.vm.provision "docker" do |d|
d.pull_images "ubuntu"
d.pull_images "vagrant"
end
end
#example of network
Vagrant.configure("2") do |config|
config.vm.network "forwarded_port", guest: 80, host: 8080,
auto_correct: true
end
#auto_correct parameter set to true tells Vagrant to auto correct any collisions.
Vagrant.configure("2") do |config|
config.vm.network "private_network", ip: "192.168.50.4",
auto_config: false
end
8. 注意事项
使用 Apache/Nginx 时会出现诸如图片修改后但页面刷新仍然是旧文件的情况,是由于静态文件缓存造成的。需要对虚拟机里的 Apache/Nginx 配置文件进行修改:
# Apache 配置添加:
EnableSendfile off
# Nginx 配置添加:
sendfile off;
https://docs.vagrantup.com/v2/getting-started/index.html
https://github.com/astaxie/Go-in-Action/blob/master/ebook/zh/01.3.md
https://vagrantcloud.com/discover/featured
http://www.vagrantbox.es/
https://github.com/kraksoft/vagrant-box-ubuntu/releases/download/14.04/ubuntu-14.04-amd64.box
#example of MULTI-MACHINE
config.vm.define :haproxy do |haproxy_config|
haproxy_config.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--name", "haproxy", "--memory", "1024"]
end
haproxy_config.vm.box = "ubuntu-14.04"
haproxy_config.vm.hostname = "haproxy"
haproxy_config.vm.network "private_network",ip: "127.0.2.10"
end
config.vm.define :etcd do |etcd_config|
etcd_config.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--name", "etcd", "--memory", "1024"]
end
etcd_config.vm.box = "ubuntu-14.04"
etcd_config.vm.hostname = "etcd"
etcd_config.vm.network "private_network",ip: "127.0.2.12"
end
config.vm.define :docker do |docker_config|
docker_config.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--name", "docker", "--memory", "1024"]
end
docker_config.vm.box = "ubuntu-14.04"
docker_config.vm.hostname = "docker"
docker_config.vm.network "private_network",ip: "127.0.2.14"
end
http://stackoverflow.com/questions/22717428/vagrant-error-failed-to-mount-folders-in-linux-guest
error message :
mount -t vboxsf -o uid=`id -u vagrant`,gid=`getent group vagrant | cut -d: -f3` vagrant /vagrant
mount -t vboxsf -o uid=`id -u vagrant`,gid=`id -g vagrant` vagrant /vagrant