vagrant 虚拟机配置最佳实践

Mac VirtualBox Vagrant 管理虚拟机

这篇文章定位是在理解了 vagrant 相关概念之后,教你如何灵活玩转自己的虚拟机配置

本文为 @favoorr 常用的 Mac VirtualBox Vagrant 管理虚拟机的通用配置向导

我的云梯 VPN 连接是 http://referyt.com/?r=9ea6f6df9967e57c

使用这个连接,你优惠 10 元,我获得 10 元

1.去官方找自己想要的镜像文件信息
https://atlas.hashicorp.com/search

例如想来个 ubuntu 16.04 64位,那么关键字就是 xenial64

找到的地址就是 https://atlas.hashicorp.com/ubuntu/boxes/xenial64

2.在本地文件系统建立对应的存放目录,本地目录按照自己意愿,任意位置

1
2
该目录存放所有使用 vagrant 管理的虚拟机
$ cd ~/Documents/try/vagrant/

3.初始化虚拟机存放目录

1
2
3
4
5
6
7
新建存放 ubuntu 16.04 64 位的文件位置
$ mkdir xenial64 && cd xenial64
使用 vagrant 命令初始化虚拟机
$ vagrant init ubuntu/xenial64
执行完之后,发现 xenial64 目录下多了个配置文件 Vagrantfile

4.修改 Vagrantfile ,配置自己的虚拟机,

其实什么都不修改,按照默认配置,这时候执行 vagrant up 的时候已经可以运行虚拟机了,这个是 hello world 的做法,实际使用中,还是要自己学会修改配置,知道每个配置项会带来的影响
有些配置是没有的,有些是有的默认是注释的

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
配置主机名,进入操作系统执行 hostname 显示的名字,我用的规则是 系统版本-IP
系统是 xenial64,IP 最后一位是 155
config.vm.hostname = "xenial64-155"
vagrant box 的版本,如果不配置,会自动去下载最新版本
我用的是 daily build 的版本,这个看自己喜好,我这个本来就是尝试一些新东西玩的
config.vm.box_version = "20161209.0.0"
配置端口映射,通过访问本机的 8080 端口,访问安装后虚拟机的 8080 端口
config.vm.network "forwarded_port", guest: 8080, host: 8080
配置虚拟机的 ip,默认网卡一是 NAT 方式,不需要配置
这里创建网卡二 Host-Only,对应 VirtualBox 的 vbox0
config.vm.network "private_network", ip: "192.168.56.155"
配置共享文件夹目录,进入系统后,自动挂载 /lesson
config.vm.synced_folder "/Users/User/zl/python/lesson", "/lesson"
VirtualBox 的配置
config.vm.provider "virtualbox" do |vb|
vb.name = "xenial64-155"
# 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"
end

5.在 xenial64 目录下执行命令,初始化并启动虚拟机

这里要注意,如果是第一次启动,会自动下载 vagrant box,最好挂 VPN了,不多解释,
如果没有 VPN,就手工下载包,导入后续会有文章介绍

1
vagrant up --provider virtualbox

6.在 xenial64 目录下执行命令,进入虚拟机,测试网络

1
2
3
4
5
进入虚拟机
vagrant ssh
测试网络,我全程 VPN,虚拟机内部访问 google 也是无压力的
ping www.google.com

参考 Vagrantfile 配置文件

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
大专栏  vagrant 虚拟机配置最佳实践iv class="line">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
64
65
66
67
68
69
70
71
72
73
74
75
# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://atlas.hashicorp.com/search.
config.vm.box = "ubuntu/xenial64"
config.vm.hostname = "xenial64-155"
config.vm.box_version = "20161209.0.0"
# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
# config.vm.box_check_update = false
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# config.vm.network "forwarded_port", guest: 80, host: 8080
# Create a private network, which allows host-only access to the machine
# using a specific IP.
config.vm.network "private_network", ip: "192.168.56.155"
# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
# config.vm.synced_folder "../data", "/vagrant_data"
# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
config.vm.provider "virtualbox" do |vb|
vb.name = "xenial64-155"
# 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"
end
#
# View the documentation for the provider you are using for more
# information on available options.
# Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
# such as FTP and Heroku are also available. See the documentation at
# https://docs.vagrantup.com/v2/push/atlas.html for more information.
# config.push.define "atlas" do |push|
# push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
# end
# Enable provisioning with a shell script. Additional provisioners such as
# Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
# documentation for more information about their specific syntax and use.
# config.vm.provision "shell", inline: <<-SHELL
# apt-get update
# apt-get upgrade
# SHELL
end
原文地址:https://www.cnblogs.com/lijianming180/p/12247723.html