gongmingqm10

Life is a journey, not a destination

Vagrant 搭建本地虚拟机

| Comments

多数情况下,为了模拟服务器环境,我们经常使用虚拟机,通过虚拟机,我们可以轻松模拟出真实的服务器环境,从而对我们代码的部署等进行模拟,当然也可以当作自己练手的好工具。把Linux虚拟机当作是真实的服务器,然后通过内网访问虚拟机上的服务器资源。
虚拟机除了大家熟知的界面化的VirtualBox外,其实还有一个神器叫做Vagrant,Vagrant是依赖于VirtualBox的,但是其好处是提供命令行式的操作,我们直接通过命令行控制虚拟机的运行状态,并可以通过ssh命令直接连接到虚拟机中,命令行操作一切,还是相当方面的。

安装过程见官方文档:https://www.vagrantup.com/downloads.html

1. vagrant init

安装完成后,Vagrant就可以直接在我们的命令行中使用。首先我们在选定的文件夹中执行vagrant init,这时我们可以看到此文件夹下自动生成了一个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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  # All Vagrant configuration is done here. The most common configuration
  # options are documented and commented below. For a complete reference,
  # please see the online documentation at vagrantup.com.

  # Every Vagrant virtual environment requires a box to build off of.
  config.vm.box = "base"

  # The url from where the 'config.vm.box' box will be fetched if it
  # doesn't already exist on the user's system.
  # config.vm.box_url = "http://domain.com/path/to/above.box"

  # 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.33.10"

  # 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|
  #   # Don't boot with headless mode
  #   vb.gui = true
  #
  #   # Use VBoxManage to customize the VM. For example to change memory:
  #   vb.customize ["modifyvm", :id, "--memory", "1024"]
  # end
  #
  # View the documentation for the provider you're using for more
  # information on available options.

  # Enable provisioning with Puppet stand alone.  Puppet manifests
  # are contained in a directory path relative to this Vagrantfile.
  # You will need to create the manifests directory and a manifest in
  # the file base.pp in the manifests_path directory.
  #
  # An example Puppet manifest to provision the message of the day:
  #
  # # group { "puppet":
  # #   ensure => "present",
  # # }
  # #
  # # File { owner => 0, group => 0, mode => 0644 }
  # #
  # # file { '/etc/motd':
  # #   content => "Welcome to your Vagrant-built virtual machine!
  # #               Managed by Puppet.\n"
  # # }
  #
  # config.vm.provision :puppet do |puppet|
  #   puppet.manifests_path = "manifests"
  #   puppet.manifest_file  = "init.pp"
  # end

  # Enable provisioning with chef solo, specifying a cookbooks path, roles
  # path, and data_bags path (all relative to this Vagrantfile), and adding
  # some recipes and/or roles.
  #
  # config.vm.provision :chef_solo do |chef|
  #   chef.cookbooks_path = "../my-recipes/cookbooks"
  #   chef.roles_path = "../my-recipes/roles"
  #   chef.data_bags_path = "../my-recipes/data_bags"
  #   chef.add_recipe "mysql"
  #   chef.add_role "web"
  #
  #   # You may also specify custom JSON attributes:
  #   chef.json = { :mysql_password => "foo" }
  # end

  # Enable provisioning with chef server, specifying the chef server URL,
  # and the path to the validation key (relative to this Vagrantfile).
  #
  # The Opscode Platform uses HTTPS. Substitute your organization for
  # ORGNAME in the URL and validation key.
  #
  # If you have your own Chef Server, use the appropriate URL, which may be
  # HTTP instead of HTTPS depending on your configuration. Also change the
  # validation key to validation.pem.
  #
  # config.vm.provision :chef_client do |chef|
  #   chef.chef_server_url = "https://api.opscode.com/organizations/ORGNAME"
  #   chef.validation_key_path = "ORGNAME-validator.pem"
  # end
  #
  # If you're using the Opscode platform, your validator client is
  # ORGNAME-validator, replacing ORGNAME with your organization name.
  #
  # If you have your own Chef Server, the default validation client name is
  # chef-validator, unless you changed the configuration.
  #
  #   chef.validation_client_name = "ORGNAME-validator"
end

上面几乎有所有的配置说明,我们可以根据自己需要进行命令的配置,我根据自己的需要进行了如下配置:

1
2
3
4
5
6
7
8
9
10

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "base"
  config.vm.box = 'centos-6.4'
  config.vm.box_url = 'https://github.com/2creatives/vagrant-centos/releases/download/v6.5.1/centos65-x86_64-20131205.box'
  config.vm.network :private_network, ip:'192.168.3.10'
  config.vm.network :forwarded_port, guest: 27017, host: 8888
end

根据配置名字可以知道各项的具体作用,包括此系统的下载地址,系统的类型及版本号,虚拟机的内网IP,虚拟机的端口转发等。注意这里的host端口默认为27017,但是这个端口被我电脑中的mongodb占用,所以我修改成另外一个闲置的端口。
配置完成后,我们的工作基本完成。

2. vagrant up

vagrant up用于启动虚拟机。文件配置完成之后在此文件夹中运行vagrant up,将加载此文件夹中的Vagrantfile,首次加载时需要下载虚拟机系统,所以耗时会略长。下载完成之后你会看到成功的提示。

3. ssh connect virtual machine

虚拟机成功启动后,我们只需要在terminal中通过ssh就可以连接到虚拟机设备上。然后就想在远程服务器中来操作就可以了。vagrant启动的地址是我们配置文件中的192.168.3.10,其默认的用户名为root,初始密码为 vagrant。所以我们使用如下命令登陆:ssh root@192.168.3.10,提示密码时输入vagrant,最后我们可以成功登陆。

登陆完成后,如果我们想停下虚拟机,可以使用vagrant destroy关闭虚拟机。

Comments