1.1準備工做:html
下載安裝 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 系統環境linux
window上安裝git bash: 經過git bash 窗口來執行命令 box鏡像文件 : centos-6.6-x86_64.box Vagrant: 1.8 VirtualBox: 5.0
1.3 安裝部署nginx
1.3.1 掛載鏡像的方式:
git
1.經過網絡的形式下載以.box文件結尾的鏡像文件. vagrant 命令支持.它會自動按照你給的地址下載
redis
2.本地提早下載好box鏡像文件,這樣省去下載的過程,也能夠直接加入vagarnt中使用shell
1.3.2 命令介紹centos
$ vagrant --help
Usage: vagrant [options] <command> [<args>]
-v, --version Print the version and exit.
-h, --help Print this help.
Common commands:
ruby
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>]
bash
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打包虛擬機
大小查看
開了倆個虛擬機 佔用資源也不是很大 ,還不錯 .....