2016年11月9日星期三

Vagrant_001:Mac 下安装与使用 Vagrant

环境:MAC OS X 10.12.1 + Vagrant 1.8.6

Vagrant 可以用来在单台物理机器编程管理多个虚拟机。其支持原生VirtualBox,并同时提供了对 VMware Fusion、Amazon EC2 虚拟机集群的插件支持。
Vagrant 提供了极易使用、基于Ruby的内部DSL,允许用户使用它们的配置参数定义一个或多个虚拟机。
对于自动部署,Vagrant 支持多种机制:可以使用 puppet、chef 或者在 Vagrant 配置文件中定义的所有虚拟机上自动安装软件程序和配置的 shell 脚本等。
因此,Vagrant 可以在运行着多台虚机的系统上定义复杂的虚拟框架,非常酷。

1. 下载并安装 VirtualBox
下载地址:https://www.virtualbox.org/wiki/Downloads

2. 下载并安装 Vagrant
下载地址:https://www.vagrantup.com/downloads.html
$ vagrant -v
Vagrant 1.8.6

3. 下载 Vagrant 官方提供的 OS 镜像
下载地址:http://www.vagrantbox.es/
选择 CentOS 7.0 x64 (Minimal, VirtualBox Guest Additions 4.3.28, Puppet 3.8.1 - see here for more infos)。

4. 创建开发目录

5. 添加下载好的 OS 镜像到 Vagrant
$ cd ~/Vagrant/projects/test
$ vagrant box add centos-7.0 ~/Vagrant/boxes/centos-7.0-x86_64.box

6. 初始化开发环境
$ vagrant init centos7
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.

7. 启动开发环境
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos-7.0'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: test_default_1478242742435_87282
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 4.3.28
    default: VirtualBox Version: 5.0
==> default: Mounting shared folders...
    default: /vagrant => /Users/maping/Vagrant/projects/test

8. 登录到虚机
$ vagrant ssh

9. 修改 Vagrantfile,去掉下面这行前面的注释
config.vm.network "private_network", ip: "192.168.33.10"

$ vagrant reload // 重启虚机
$ ping 192.168.33.10

10. 在虚机中安装 http 服务
[vagrant@localhost ~]$ sudo yum install httpd // 安装 http 服务
[vagrant@localhost ~]$ sudo systemctl start httpd.service // 启动 http 服务
[vagrant@localhost ~]$ sudo systemctl enable httpd // 设置 http 服务状态
[vagrant@localhost ~]$ sudo systemctl list-unit-files | grep enabled // 查看 http 服务的状态
[vagrant@localhost ~]$ sudo firewall-cmd --stat // 检测防火墙状态
[vagrant@localhost ~]$ sudo firewall-cmd --permanent --zone=public --add-service=http // 永久性开放 http 端口
[vagrant@localhost ~]$ sudo iptables -L -n | grep 21 // 查看80端口是否开放
[vagrant@localhost ~]$ sudo systemctl restart firewalld // 重启防火墙
[vagrant@localhost ~]$ exit
$ vagrant reload // 重启虚机
访问 http://192.168.33.10/

11. 修改 Vagrantfile,设置共享文件夹
(1) config.vm.synced_folder "../data", "/vagrant_data"
第1个目录是你本地的文件夹,第2个是挂在到虚拟机上的文件夹。
(2) 给共享文件夹设置权限

12. 打包分发
$ vagrant package

13. 其它 vagrant 常用命令
$ vagrant halt  // 关闭虚拟机
$ vagrant status  // 查看虚拟机运行状态
$ vagrant destroy  // 销毁当前虚拟机

参考文献:
1. http://www.jianshu.com/p/7747c31012f8 Mac OSX+VirtualBox+Vagrant+CentOS初体验
2. http://blog.csdn.net/ebw123/article/details/44138725 mac 下 安装vagrant

没有评论: