Fork me on GitHub

Vagrant之Vagrant入门

Vagrant入门

Vagrant是什么?

官网地址
https://www.vagrantup.com/intro/index.html
Vagrant是一款虚拟机管理工具,可以快速创建、删除虚拟机。Vagrant让我们可以通过代码的方式快速地、可重复地创建针对不同虚拟环境的虚拟机,包括Virtualbox、AWS、Docker等。它使得我们可以一次性地、自动创建多个环境相同的虚拟机,对于软件开发和测试尤其有用。本文我们将以Virtualbox为例,看看Vagrant的基本使用。

安装要求

安装VirtualBox

下载地址:https://www.virtualbox.org/wiki/Linux_Downloads

1
2
3
4
5
6
7
8
9
10
11
12
[root@linux-node8 ~]# wget https://download.virtualbox.org/virtualbox/5.2.6/VirtualBox-5.2-5.2.6_120293_el7-1.x86_64.rpm
[root@linux-node8 ~]# yum install -y SDL SDL-devel
[root@linux-node8 ~]# yum install -y libvpx libvpx-devel libvpx-utils
[root@linux-node8~]# yum install -y *libXmu*
[root@linux-node8 ~]# yum install -y libXcursor libXcursor-devel libXinerama libXinerama-devel fontconfig fontconfig-devel libpng libpng-devel
[root@linux-node8 ~]# rpm -ivh VirtualBox-5.2-5.2.6_120293_el7-1.x86_64.rpm
warning: VirtualBox-5.2-5.2.6_120293_el7-1.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 98ab5139: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:VirtualBox-5.2-5.2.6_120293_el7-1################################# [100%]

Creating group 'vboxusers'. VM users must be member of that group!

安装Vagrant

1
2
3
4
5
[root@linux-node8 ~]# wget https://releases.hashicorp.com/vagrant/2.0.2/vagrant_2.0.2_x86_64.rpm?_ga=2.145148086.346091913.1517627640-765358210.1517627640
[root@linux-node8 ~]# rpm -ivh vagrant_2.0.2_x86_64.rpm
Preparing... ################################# [100%]
Updating / installing...
1:vagrant-1:2.0.2-1 ################################# [100%]

安装增强工具

需求:与虚拟机共享任何文件夹

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
```

# 应用
## 使用vagrant创建虚拟机
开启CPU虚拟化功能
``` bash
[root@linux-node8 ~]# vagrant init hashicorp/precise64 # 创建Vagrantfile
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.
[root@linux-node8 ~]# grep -vE "#|^$" Vagrantfile
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
end
[root@linux-node8 ~]# vagrant up # 开启虚拟机
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'hashicorp/precise64' is up to date...
==> 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: Warning: Connection reset. Retrying...
default: Warning: Connection reset. 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: 4.2.0
default: VirtualBox Version: 5.2
==> default: Mounting shared folders...
default: /vagrant => /root
[root@linux-node8 ~]# vagrant ssh # 登录虚拟机
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic x86_64)

* Documentation: https://help.ubuntu.com/
New release '14.04.5 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

Welcome to your Vagrant-built virtual machine.
Last login: Fri Sep 14 06:23:18 2012 from 10.0.2.2
vagrant@precise64:~$ hostname
precise64
vagrant@precise64:~$

设置端口转发

在默认情况下,Vagrant所创建的Virtualbox虚拟机使用的是NAT网络类型,即外界是不能直接访问你的虚拟机的,就连Host机器也访问不了。此时,如果你在虚拟机中启动的一个Tomcat来部署网站的测试环境,而又想外界能够访问的话,你需要使用端口转发:

1
2
3
Vagrant.configure("2") do |config|
config.vm.network "forwarded_port", guest: 8080, host: 8888
end

以上代码将Host机的8888端口转发到了虚拟机的8080端口,这样你便可以通过在Host机上访问http://localhost:8888来访问虚拟机的Tomcat了。对于Virtualbox来说,只有NAT类型的网络类型支持端口转发,这也是为什么Vagrant创建的Virtualbox虚拟机默认都有一个支持NAT的虚拟网卡,原因就是要能够支持Vagrant级别的端口转发。另外,Vagrant在第一次尝试连接虚拟机时使用的也是NAT。

网络配置

定制虚拟机配置

参考资料

http://www.cnblogs.com/davenkin/p/vagrant-virtualbox.html

======================================================
希望各位朋友支持一下

本文作者:dongsheng
本文地址https://mds1455975151.github.io/archives/1fd805f3.html
版权声明:转载请注明出处!

坚持技术分享,您的支持将鼓励我继续创作!