windows下安装配置vagrant

探索发现

作者:biner · 时间:2016-07-15 · 点击数:5890 · 评论:3

工具及环境下载地址说明
宿主机win10也可以选择其他系统
VirtualBoxwww.virtualbox.org各大系统版本都有
vagrantwww.vagrantup.com各大系统版本都有
boxwww.vagrantbox.es,mirrors.hypo.cn,atlas.hashicorp.com镜像
puttyputty.exewindows下链接Linux的工具

VirtualBox,vagrant 下载并安装,putty绿色软件,box为系统镜像

这里我用迅雷会员下载的是这个镜像centos7,3分钟就下载好了

开始设置

工作区我放在D://workspace
下载好的virtualbox.box重命名为centos7.box放在D://workspace/centos7.box

初始化

启动cmd命令行工具,切换到workspace->初始化->加载镜像

d:
cd workspace
vagrant init
vagrant box add centos7 centos7.box

输出示例

D:>cd workspace
D:workspace>vagrant init
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
D:workspace>vagrant box add centos7 centos7.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos7' (v0) for provider:
    box: Unpacking necessary files from: file://D:/workspace/centos7.box
    box: Progress: 100% (Rate: 33.8M/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'centos7' (v0) for 'virtualbox'!
修改Vagrantfile文件

此文件有两份
1.修改D://workspace/Vagrantfile
在Vagrant.configure(“2”) do |config|下面添加

config.vm.box = "centos7"
config.vm.define :centos7 do |haproxy_config|
        haproxy_config.vm.network :private_network, ip: "10.8.8.8"
        haproxy_config.vm.network :forwarded_port, guest: 80, host: 8080
        config.vm.provider :virtualbox do |vb|
            vb.name = "centos7"
            #Display the VirtualBox GUI when booting the machine
            vb.gui = false
            #Customize the amount of memory on the VM:
            vb.memory = "1024"
            vb.cpus = 2
            #vb.customize ["modifyvm", :id, "--memory", "256"]
        end
end

同时开启4台虚拟机

app_servers = {
  :http => '192.168.58.20',
  :php => '192.168.58.21'
}

Vagrant.configure("2") do |config|
  config.vm.box = "centos6.3"

  config.vm.define :haproxy do |haproxy_config|
    haproxy_config.vm.network :private_network, ip: "192.168.58.10"
    haproxy_config.vm.network :forwarded_port, guest: 80, host: 8080
    config.vm.provider :virtualbox do |vb|
      vb.name = "haproxy"
      vb.customize ["modifyvm", :id, "--memory", "256"]
    end
  end

  app_servers.each do |app_server_name, app_server_ip|
    config.vm.define app_server_name do |app_config|
      app_config.vm.hostname = "#{app_server_name.to_s}.vagrant.internal"
      app_config.vm.network :private_network, ip: app_server_ip
       # app_config.vm.synced_folder "../app", "/opt/app"
      app_config.vm.provider "virtualbox" do |vb|
        vb.name = app_server_name.to_s
        vb.customize ["modifyvm", :id, "--memory", "256"]
      end
    end
  end

  config.vm.define :redis do |redis_config|
    redis_config.vm.hostname = "redis.vagrant.internal"
    redis_config.vm.network :private_network, ip: "192.168.58.30"
    redis_config.vm.provider "virtualbox" do |vb|
      vb.name = "redis"
      vb.customize ["modifyvm", :id, "--cpuexecutioncap", "50"]
      vb.customize ["modifyvm", :id, "--memory", "256"]
    end
  end
end

注意config.vm.box和vb.name要一致,ip可以修改,可以使用该IP访问虚拟主机,IP可以是其他网段

2.修改文件 C://Users/{您的用户名}/.vagrant.d/boxes/centos7/virtualbox/Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.base_mac = "525400bd971e"
  #config.vm.synced_folder ".", "/home/vagrant/sync", type: "rsync"
end

注意第三行注释掉,不然启动会提示找不到 rsync

启动虚拟机

D:workspace>vagrant up centos7
Bringing machine 'centos7' up with 'virtualbox' provider...
==> centos7: Clearing any previously set network interfaces...
==> centos7: Preparing network interfaces based on configuration...
    centos7: Adapter 1: nat
    centos7: Adapter 2: hostonly
    centos7: Adapter 3: hostonly
==> centos7: Forwarding ports...
    centos7: 80 (guest) => 8080 (host) (adapter 1)
    centos7: 22 (guest) => 2222 (host) (adapter 1)
==> centos7: Running 'pre-boot' VM customizations...
==> centos7: Booting VM...
==> centos7: Waiting for machine to boot. This may take a few minutes...
    centos7: SSH address: 127.0.0.1:2222
    centos7: SSH username: vagrant
    centos7: SSH auth method: private key
    centos7: Warning: Remote connection disconnect. Retrying...
    centos7: Warning: Remote connection disconnect. Retrying...
    centos7: Warning: Remote connection disconnect. Retrying...
    centos7:
    centos7: Vagrant insecure key detected. Vagrant will automatically replace
    centos7: this with a newly generated keypair for better security.
    centos7:
    centos7: Inserting generated public key within guest...
    centos7: Removing insecure key from the guest if it's present...
    centos7: Key inserted! Disconnecting and reconnecting using new SSH key...
==> centos7: Machine booted and ready!
==> centos7: Checking for guest additions in VM...
    centos7: No guest additions were detected on the base box for this VM! Guest
    centos7: additions are required for forwarded ports, shared folders, host only
    centos7: networking, and more. If SSH fails on this machine, please install
    centos7: the guest additions and repackage the box to continue.
    centos7:
    centos7: This is not an error message; everything may continue to work properly,
    centos7: in which case you may ignore this message.
==> centos7: Configuring and enabling network interfaces...
==> centos7: Mounting shared folders...
    centos7: /vagrant => D:/web
Failed to mount folders in Linux guest. This is usually because
the "vboxsf" file system is not available. Please verify that
the guest additions are properly installed in the guest and
can work properly. The command attempted was:

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

The error output from the last command was:

mount: unknown filesystem type 'vboxsf'

提示挂载错误,共享目录失败,但是虚拟机已经成功启动了,这个错误是因为没有安装virtualbox增强功能导致的
可以按照这个教程来安装
我是直接使用sftp把解压的C://Program Files/Oracle/VirtualBox/VBoxGuestAdditions.iso文件上传到虚拟机中,然后在ssh中执行

[root@localhost vagrant]# sudo sh -x ./VBoxLinuxAdditions.run
Verifying archive integrity... All good.
Uncompressing VirtualBox 5.0.22 Guest Additions for Linux............
VirtualBox Guest Additions installer
Copying additional installer modules ...
Installing additional modules ...
Removing existing VirtualBox non-DKMS kernel modules[  OK  ]
Building the VirtualBox Guest Additions kernel modules
Building the main Guest Additions module[  OK  ]
Building the shared folder support module[  OK  ]
Building the graphics driver module[  OK  ]
Doing non-kernel setup of the Guest Additions[  OK  ]
Starting the VirtualBox Guest Additions Installing the Window System drivers
Could not find the X.Org or XFree86 Window System, skipping.
[  OK  ]

增强包安装成功,如果还是出现此错误,请在命令行下执行 

yum install gcc kernel kernel-devel

重启vagrant

D:workspace>vagrant reload
==> centos7: Attempting graceful shutdown of VM...
==> centos7: Clearing any previously set forwarded ports...
==> centos7: Clearing any previously set network interfaces...
==> centos7: Preparing network interfaces based on configuration...
    centos7: Adapter 1: nat
    centos7: Adapter 2: hostonly
==> centos7: Forwarding ports...
    centos7: 80 (guest) => 8080 (host) (adapter 1)
    centos7: 22 (guest) => 2222 (host) (adapter 1)
==> centos7: Running 'pre-boot' VM customizations...
==> centos7: Booting VM...
==> centos7: Waiting for machine to boot. This may take a few minutes...
    centos7: SSH address: 127.0.0.1:2222
    centos7: SSH username: vagrant
    centos7: SSH auth method: private key
    centos7: Warning: Remote connection disconnect. Retrying...
==> centos7: Machine booted and ready!
==> centos7: Checking for guest additions in VM...
==> centos7: Configuring and enabling network interfaces...
==> centos7: Mounting shared folders...
    centos7: /vagrant => D:/workspace
    centos7: /data/wwwroot => D:/web

安装lnmp环境

使用此一键包,也可以全部自己安装

其他

虚拟机打包

当在启动Vagrant后,对于虚拟机有进行过安装环境相关的配置,如果并不希望写在Vagrant的启动shell里面每次都重新安装配置一遍,可以将当前配置好的虚拟机打包成box
关闭当前机器

vagrant package -hUsage: vagrant package [options] [name]Options:
--base NAME                  virtualbox程序里面的虚拟机的名称,不是box的名字也不是Vagrantfile里面的虚拟机名称.默认是打包当前目录下面的虚拟机。
--output NAME                要打包成的box名称,不会自动添加.box后缀,要手动加.默认值package.box        --include FILE...            打包时包含的文件名,你可以把.box文件理解为一个压缩包
--vagrantfile FILE           打包时包含的Vagrantfile文件,原理和上面类似
-h, --help                       Print this help
c:workspace>vagrant package --base centos7 --output centos7_new.box
1.拷贝centos7_new.box到其他机器上
2.在其他机器上执行vagrant box add centos7 centos7_new.box
3.在其他机器上执行vagrant init centos7
如果已经初始化环境请vagrant destroy再删除Vagrantfile再执行上述命令

解决Warning: Authentication failure. Retrying...的问题

sudo wget https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub -O ~/.ssh/authorized_keys
sudo rm /etc/udev/rules.d/70-persistent-net.rules

vagrant up (启动虚拟机)
vagrant halt (关闭虚拟机——对应就是关机)
vagrant suspend (暂停虚拟机——只是暂停,虚拟机内存等信息将以状态文件的方式保存在本地,可以执行恢复操作后继续使用)
vagrant resume (恢复虚拟机 —— 与前面的暂停相对应)
vagrant destroy (删除虚拟机,删除后在当前虚拟机所做进行的除开Vagrantfile中的配置都不会保留)
vagrant ssh (ssh到虚拟机)
vagrant reload  重启虚拟机(重新载入配置文件)
vagrant  -h ( 更多命令查看帮助)
登录虚拟机

使用putty连接虚拟机
登录的帐号密码均为 vagrant ,登录之后如果需要 su root ,密码也是 vagrant
如果root密码不正确,请修改初始密码

sudo passwd root
Vagrant下共享目录静态文件(js/jpg/png等)“缓存”问题

nginx,nginx.conf

sendfile off;

Apache,httpd.conf

EnableSendfile off

MAC下安装请参考
Vagrant简介和安装配置

标签:
评论列表
1/1
  • chongyi   
    2016-10-19
    Homestead 是个好东西啊~~~
    顶 (0) 回复
    2016-10-19
    是啊,你开始用mac了?
    回复
    chongyi   
    2016-11-29
    整理邮件才发现你回复了。。。埋没在一堆 github 的邮件中。 我上半年上的 mbp
    回复
1/1