搜索
您的当前位置:首页正文

Vagrant-虚拟机管理

来源:二三娱乐

1. 安装Vagrant与VirtualBox

Vagrant

VirtualBox

2. 添加box

// 查看box列表
C:\Users\DELL>vagrant box list
There are no installed boxes! Use `vagrant box add` to add some.

// 添加box
C:\Users\DELL>vagrant box add puphpet/centos65-x64
==> box: Successfully added box 'puphpet/centos65-x64' (v20161102) for 'virtualbox'!

3. 初始化,启动,连接

// 在桌面新建guosk文件夹来初始化Vagrant
C:\Users\DELL\Desktop\guosk>vagrant box list
puphpet/centos65-x64 (virtualbox, 20161102)

// 初始化
C:\Users\DELL\Desktop\guosk>vagrant init puphpet/centos65-x64

// 启动Vagrant
C:\Users\DELL\Desktop\guosk>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'puphpet/centos65-x64'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'puphpet/centos65-x64' is up to date...
==> default: Setting the name of the VM: guosk_default_1479728274627_41041
==> 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: Warning: Remote connection disconnect. Retrying...
    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: 5.0.26
    default: VirtualBox Version: 5.1
==> default: Mounting shared folders...
    default: /vagrant => C:/Users/DELL/Desktop/guosk
Paste_Image.png
login as: vagrant
vagrant@127.0.0.1's password:
Last login: Mon Nov 21 11:47:02 2016 from 10.0.2.2
[vagrant@localhost ~]$ pwd
/home/vagrant

4. 启动,停止,重启,销毁

// 启动
vagrant up

// 停止
vagrant halt

// 使用ssh连接
vagrant ssh

// 安装Apach服务器
sudo yum install httpd

// 启动服务器
[vagrant@localhost ~]$ sudo service httpd start
exit

// 暂停虚拟机
vagrant suspend

// 恢复虚拟机
vagrant resume

// 重启
vagrant reload

// 销毁
vagrant destroy

yum

5. 共享的目录

默认情况下,vagrant会共享我们的项目目录,在项目的虚拟机里面,会有一个跟我们的项目的目录是同步的。这样我们可以在本地的电脑上去编辑项目的文件,然后在虚拟机上运行它们。

// 虚拟机内的'/vagrant'目录和本地的项目目录同步
 default: /vagrant => C:/Users/DELL/Desktop/guosk

6. 配置共享目录

如果有额外的目录要跟虚拟机同步的话,可以通过修改Vagrantfile这个文件,去添加这些额外的共享目录。

// Vagrantfile
config.vm.synced_folder "../data", "/vagrant_data",
    create: true, owner: "root", group: "root"
    // creat:如果本地data文件夹不存在,就会创建
    // owner:目录的拥有者
    // group:所属群组

// 重启Vagrant
vagrant reload
 default: /vagrant => C:/Users/DELL/Desktop/guosk
 default: /vagrant_data => C:/Users/DELL/Desktop/data

// vagrant目录的拥有者和群组是vagrant
drwxrwxrwx.  1 vagrant vagrant     0 Nov 21 12:28 vagrant
// vagrant_data目录的所有者和所属群组是root
drwxrwxrwx.  1 root    root        0 Nov 21 12:41 vagrant_data

7. 网络配置

Vagrant提供了三种方法:私有网络、公有网络、端口转发

8. 私有网络

// Vagrantfile
 "private_network", ip: "192.168.33.10"
// 开启私有网络并重启
// 然后本机和虚拟机之间就可以通信了

9. 公有网络

如果想让同一局域网内的其他设备也可以访问虚拟机的话,需要去配置公有网络。

// Vagrantfile
 "public_network"
// 设置公有网络并重启
vagrant ssh
// ssh登录
ifconfig
// 查询ip
inet addr:192.168.1.185  Bcast:192.168.1.255  Mask:255.255.255.0
// 虚拟机共有网络IP

10. 搭建Web服务器

在虚拟机上搭建一个Web服务器来测试配置好的网络。

// 安装Apach服务器
sudo yum install httpd

// 安装Vim
sudo yum install vim

$ sudo vim /etc/httpd/conf/httpd.conf
// 编辑Apach的配置文件
NameVirtualHost *:80

<VirtualHost *:80>
#    ServerAdmin 
    DocumentRoot 
#    ServerName 
#    ErrorLog 
#    CustomLog  common
</VirtualHost>
// 

// 启动httpd
$ sudo service httpd start

// 查看虚拟机共有IP
$ ifconfig
192.168.1.185


为/vagrant跟我们电脑上的项目的目录是同步的,所以,我们可以本地开发项目,然后通过在虚拟机上配置好的环境去运行项目。

11. 打包分发

// 打包之前先删除一个文件
sudo rm -rf /etc/udev/rules.d/70-persistent-net.rules
// 执行打包
vagrant package

打包完成后会在当前目录生成一个package.box文件,将这个文件传给其他用户,其他用户只要添加这个box并用其初始化自己的开发目录就能得到一个一模一样的开发环境了。

Top