1.1准备工作:
下载安装 VirtualBox :https://www.virtualbox.org/ 下载安装 Vagrant : http://www.vagrantup.com/ 下载安装git bash : https://git-scm.com/download/ 下载需要使用的 box : 官方提供的范例:http://files.vagrantup.com/precise32.box 还可以在 http://www.vagrantbox.es/ 这里下载更多不同系统甚至是已经配置好环境直接可以用的 box,虽然可以直接在Vagrant直接使用网址,由Vagrant自动下载安装,但是考虑到网络情况,还是建议自行先下载好。
1.2 系统环境
window上安装git bash: 通过git bash 窗口来执行命令 box镜像文件 : centos-6.6-x86_64.box Vagrant: 1.8 VirtualBox: 5.0
1.3 安装部署
1.3.1 挂载镜像的方式:
1.通过网络的形式下载以.box文件结尾的镜像文件. vagrant 命令支持.它会自动按照你给的地址下载
2.本地提前下载好box镜像文件,这样省去下载的过程,也可以直接加入vagarnt中使用
1.3.2 命令介绍
$ vagrant --help
Usage: vagrant [options] <command> [<args>]
-v, --version Print the version and exit.
-h, --help Print this help.
Common commands:
box 管理box的安装,添加,移除 connect 连接一个vagrant远程环境 destroy 停止并摧毁一个vagrant虚拟机 global-status 导出用户的状态环境变量 halt 关机 help 帮助 init 初始化并自动创建一个Vagrantfile 文件 login log in to HashiCorp‘s Atlas package 打包一个vagrant虚拟机成一个box镜像 plugin 管理插件的安装卸载 port 列出端口的映射关系 powershell 通过powershell连接 provision vagrant虚拟机的信息 provisions the vagrant machine push deploys code in this environment to a configured destination rdp 通过window rdp 连接 reload 重启vagrant机器,重新加载Vagrantfile配置文件
resume 恢复一个暂停的虚拟机 share share your Vagrant environment with anyone in the world
snapshot 快照 ssh 通过ssh连接 ssh-config 导出虚拟机ssh配置 status 查看状态 suspend 挂靠暂停一个虚拟机 up 启动虚拟机 version 打印vagrant版本
$ vagrant box --help
Usage: vagrant box <subcommand> [<args>]
Available subcommands:
add list outdated remove repackage update
1.3.3 本地下载好box镜像 vagrant add 导入镜像
自定义的名字centos6.6 随意写
bds@H030500289 MINGW64 /e $ vagrant box list centos6.6 (virtualbox, 0) bds@H030500289 MINGW64 /e $ mkdir vagrantlinux #新建目录,把他作为安装虚拟目录 bds@H030500289 MINGW64 /e $ cd vagrantlinux/ $ vagrant init centos6.6 #执行完这个命令后,就会在当前目录说呢过成Vagrantfile文件 A `Vagrantfile` has been placed in this directory. You are nowready to `vagrant up` your first virtual environment! Please readthe comments in the Vagrantfile as well as documentation on`vagrantup.com` for more information on using Vagrant.
1.3.4 Vagrantfile文件
相当于虚拟机的配置文件,定义ip地址,分配多少内存,共享文件夹,执行命令等. 因此这个配置很重要,一些修改主要更改这个配置文件,暂时我只会这些,想要更多,请参照官方文档
# -*- mode: ruby -*- # vi: set ft=ruby : Vagrant.configure(2) do |config| config.vm.box = "centos6.6" config.vm.box_check_update = false # config.vm.network "public_network" # config.vm.synced_folder "../data", "/vagrant_data" config.vm.define :nginx do |nginx_config| nginx_config.vm.network "private_network", ip: "192.168.10.100" nginx_config.vm.provider "virtualbox" do |vb| nginx_config.vm.network "forwarded_port", guest: 80, host: 8080 vb.gui = false vb.memory = "256" vb.cpus = 2 vb.name = "my_vm01" end end config.vm.define :redis do |redis_config| redis_config.vm.network "private_network", ip: "192.168.10.101" redis_config.vm.provider "virtualbox" do |vb| vb.gui = false vb.cpus = 2 vb.name = "my_vm02" vb.customize ["modifyvm", :id, "--cpuexecutioncap", "50"] vb.customize ["modifyvm", :id, "--memory", "256"] end end config.vm.provision "shell", inline: <<-SHELL echo "192.168.50.10 my-vm01">>/etc/hosts echo "192.168.50.11 my-vm01">>/etc/hosts SHELL end
1.3.5 启动 (只需要一个命令,俩个虚拟机正在后台,嗖嗖的建立,,,so easy)
$ vagrant up Bringing machine ‘nginx‘ up with ‘virtualbox‘ provider... Bringing machine ‘redis‘ up with ‘virtualbox‘ provider... ==> nginx: Importing base box ‘centos6.6‘... ==> nginx: Matching MAC address for NAT networking... ==> nginx: Setting the name of the VM: my_vm01 ==> nginx: Clearing any previously set forwarded ports... ==> nginx: Clearing any previously set network interfaces... ==> nginx: Preparing network interfaces based on configuration... nginx: Adapter 1: nat nginx: Adapter 2: hostonly ==> nginx: Forwarding ports... nginx: 80 (guest) => 8080 (host) (adapter 1) nginx: 22 (guest) => 2222 (host) (adapter 1) ==> nginx: Running ‘pre-boot‘ VM customizations... ==> nginx: Booting VM... ==> nginx: Waiting for machine to boot. This may take a few minutes... nginx: SSH address: 127.0.0.1:2222 nginx: SSH username: vagrant nginx: SSH auth method: private key nginx: nginx: Vagrant insecure key detected. Vagrant will automatically replace nginx: this with a newly generated keypair for better security. nginx: nginx: Inserting generated public key within guest... nginx: Removing insecure key from the guest if it‘s present... nginx: Key inserted! Disconnecting and reconnecting using new SSH key... ==> nginx: Machine booted and ready! ==> nginx: Checking for guest additions in VM... nginx: The guest additions on this VM do not match the installed version of nginx: VirtualBox! In most cases this is fine, but in rare cases it can nginx: prevent things such as shared folders from working properly. If you see nginx: shared folder errors, please make sure the guest additions within the nginx: virtual machine match the version of VirtualBox you have installed on nginx: your host and reload your VM. nginx: nginx: Guest Additions Version: 4.3.28 nginx: VirtualBox Version: 5.0 ==> nginx: Configuring and enabling network interfaces... ==> nginx: Mounting shared folders... nginx: /vagrant => E:/vagrantlinux ==> nginx: Running provisioner: shell... nginx: Running: inline script ==> redis: Importing base box ‘centos6.6‘... ==> redis: Matching MAC address for NAT networking... ==> redis: Setting the name of the VM: my_vm02 ==> redis: Clearing any previously set forwarded ports... ==> redis: Fixed port collision for 22 => 2222. Now on port 2200. ==> redis: Clearing any previously set network interfaces... ==> redis: Preparing network interfaces based on configuration... redis: Adapter 1: nat redis: Adapter 2: hostonly ==> redis: Forwarding ports... redis: 22 (guest) => 2200 (host) (adapter 1) ==> redis: Running ‘pre-boot‘ VM customizations... ==> redis: Booting VM... ==> redis: Waiting for machine to boot. This may take a few minutes... redis: SSH address: 127.0.0.1:2200 redis: SSH username: vagrant redis: SSH auth method: private key redis: Warning: Remote connection disconnect. Retrying... redis: redis: Vagrant insecure key detected. Vagrant will automatically replace redis: this with a newly generated keypair for better security. redis: redis: Inserting generated public key within guest... redis: Removing insecure key from the guest if it‘s present... redis: Key inserted! Disconnecting and reconnecting using new SSH key... ==> redis: Machine booted and ready! ==> redis: Checking for guest additions in VM... redis: The guest additions on this VM do not match the installed version of redis: VirtualBox! In most cases this is fine, but in rare cases it can redis: prevent things such as shared folders from working properly. If you see redis: shared folder errors, please make sure the guest additions within the redis: virtual machine match the version of VirtualBox you have installed on redis: your host and reload your VM. redis: redis: Guest Additions Version: 4.3.28 redis: VirtualBox Version: 5.0 ==> redis: Configuring and enabling network interfaces... ==> redis: Mounting shared folders... redis: /vagrant => E:/vagrantlinux ==> redis: Running provisioner: shell... redis: Running: inline script
1.4 查看状态
$ vagrant status Current machine states: nginx running (virtualbox) redis running (virtualbox)
1.5 ssh连接
用户名: vagrant 密码: vagrant bds@H030500289 MINGW64 /e/vagrantlinux $ vagrant ssh nginx Last login: Sat May 30 12:27:44 2015 from 10.0.2.2Welcome to your Vagrant-built virtual machine. [vagrant@localhost ~]$ su - rootPasswort: [root@localhost ~]#
1.6 各个命令的查看
1.7 package打包虚拟机
大小查看
开了俩个虚拟机 占用资源也不是很大 ,还不错 .....
官方站点:http://docs.vagrantup.com/v2/
本文出自 “胡三刀” 博客,请务必保留此出处http://bdstravel.blog.51cto.com/8870307/1758829
原文地址:http://bdstravel.blog.51cto.com/8870307/1758829