Vagrant 是一个基于 Ruby 的工具,用于创建和部署虚拟化开发环境。它使用 Oracle 的开源 VirtualBox 虚拟化系统,使用 Chef 创建自动化虚拟环境(百度百科)。可以利用 Vagrant 搭建小小的集群环境,用于学习例如 Ansible 、Ceph 等工具。
去官网地址下载对应的安装包
Download Vagrant
# wget https://releases.hashicorp.com/vagrant/2.2.0/vagrant_2.2.0_x86_64.rpm
# rpm -ivh vagrant_2.2.0_x86_64.rpm
# vagrant --version
Vagrant 2.2.0
进入官网找到相应文件Download VirtualBox for Linux Hosts,
通过添加 yum 源来下载,将下载页面翻至最低,copy 下载地址。
# cd /etc/yum.repos.d
# wget https://download.virtualbox.org/virtualbox/rpm/el/virtualbox.repo
# yum clean all
# yum makecache
# yum install VirtualBox
可以从 Vagrantbox镜像站 下载对应的镜像,并将其保存在 ~/vagrant/vagrant_vbox
新建目录下
# mkdir -p ~/vagrant/vagrant_vbox
# cd ~/vagrant/vagrant_vbox
# wget https://github.com/sepetrov/trusty64/releases/download/v0.0.5/trusty64.box
# ll
total 728732
-rw-r--r-- 1 root root 746217084 May 24 2017 trusty64.box
利用刚刚下载的 ubuntu14.04 镜像, 从本地添加 vbox
# mkdir -p ~/vagrant/vagrant_get_start
# cd ~/vagrant/vagrant_get_start
# vagrant box add ubuntu14.04 ../vagrant_vbox/trusty64.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'ubuntu14.04' (v0) for provider:
box: Unpacking necessary files from: file:///root/vagrant/vagrant_vbox/trusty64.box
==> box: Successfully added box 'ubuntu14.04' (v0) for 'virtualbox'!
其中,Ubuntu14.04
为自定义的 vbox 镜像名,vagrant_vbox/trusty64.box
为 vbox 下载镜像的本地路径
查看已添加的 box 列表
# vagrant box list
ubuntu14.04 (virtualbox, 0)
# cd vagrant_get_start/
# vagrant init ubuntu14.04
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.
# ll
total 4
-rw-r--r-- 1 root root 3014 Nov 2 11:27 Vagrantfile
vagrant_get_strat
目录为所要存储项目工程的目录,使用vagrant init
初始化项目工程, ubuntu14.04
为 刚刚添加的 box
(可以通过 vagrant box list
查看), 初始化之后,可以看到本地生成的 Vagrantfile
文件,这个就为我们启动创建虚拟机的 Vagrant 配置文件。
查看 Vagrantfile 文件的内容可以看到一些默认配置,将其下面几个配置前面的 #
去掉
Vagrant.configure("2") do |config|
config.vm.box = " ubuntu14.04"
config.vm.network "forwarded_port", guest: 80, host: 8080
config.vm.network "private_network", ip: "192.168.33.10"
config.vm.network "public_network", ip: "192.168.12.253"
config.vm.synced_folder "../data", "/vagrant_data"
Vagrant 有三种网络配置:
把宿主计算机的端口映射到虚拟机的某一个端口上,访问宿主计算机端口时,请求实际是被转发到虚拟机上指定端口的。例如上面的配置,将访问宿主计算机 8080 端口的请求都转发到虚拟机的 80 端口上进行处理 。访问localhost:8080,对应访问虚拟机的80端口。
优缺点:
只有主机可以访问虚拟机,如果多个虚拟机设定在同一个网段也可以互相访问,当然虚拟机是可以访问外部网络的。
优缺点:
虚拟机享受实体机器一样的待遇,一样的网络配置,vagrant1.3 版本之后也可以设定静态IP。
优缺点:
设置文件同步
config.vm.synced_folder
可以用于设置文件同步。如配置文件中的 ../data
是本地目录, /vagrant_data
是虚拟机文件目录。两个目录设置为同步,这样就可以直接本地编辑文件,使用虚拟机配置的软件环境了。
Node: ../data
目录应该存在于您的本地目录
# vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box ' ubuntu14.04'...
==> default: Matching MAC address for NAT networking...
如果出现了以下错误信息
VirtualBox is complaining that the kernel module is not loaded. Please
run `VBoxManage --version` or open the VirtualBox GUI to see the error
message which should contain instructions on how to fix this error.
按照提示信息 VBoxManage --version
进行调 bug
启动过程中,如果本地有安装如 Docker ,会出现提示虚拟机需要桥接的网卡
比如查看我的主机存在的网卡有
[root@aliyun ~]# ifconfig
docker0: flags=4099 mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:00:bd:ec:a3 txqueuelen 0 (Ethernet)
RX packets 24802 bytes 18113667 (17.2 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 26494 bytes 50591091 (48.2 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth0: flags=4163 mtu 1500
inet 172.16.27.74 netmask 255.255.192.0 broadcast 172.16.63.255
ether 00:16:3e:10:26:32 txqueuelen 1000 (Ethernet)
RX packets 5144119 bytes 1475329498 (1.3 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4707872 bytes 533983668 (509.2 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 100545 bytes 21940856 (20.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 100545 bytes 21940856 (20.9 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
vboxnet0: flags=4099 mtu 1500
inet 192.168.33.1 netmask 255.255.255.0 broadcast 192.168.33.255
ether 0a:00:27:00:00:00 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
docker0
是我本机 docker 服务启动后的网卡。我们使用本机自带的网卡 e0
。
# vagrant ssh
ssh_exchange_identification: read: Connection reset by peer
执行该命令之后,Vagrant 会生成 ssh 配置
hostname: localhost
port: 2222
Connection Type: SSH
User Name: vagrant
Password: vagrant
然后,便可以使用 ssh 工具,登入该虚拟机
注意!!!! 如果出现以下错误
Stderr: VBoxManage: error: VT-x is not available (VERR_VMX_NO_VMX)
VBoxManage: error: Details: code NS_ERROR_FAILURE (0x80004005), component ConsoleWrap, interface IConsole
说明物理机未开启虚拟化,请到 Bios 上设置虚拟化。进入 Bios 界面将 Intel Virtual Technology
由 Disabled
改为 Enabled
。
关于VT-x
: 英特尔的硬件辅助虚拟化技术(Vanderpool Technology,简称VT技术)是一种设计更简单、实施更高效和可靠的办法,是世界上首个X86平台的硬件辅助虚拟化处理方案。