Vagrant,VirtualBox,VMware Workstation

1.Vagrant

Vagrant官网https://www.vagrantup.com/
Vagrant下载https://www.vagrantup.com/downloads.html
Vagrant是一款虚拟机管理工具,Vagrant支持管理多种虚拟化管理工具的虚拟机。可以帮助用户快速启动和配置VirtualBox,VMware Workstation等虚拟化工具的虚拟机实例。
Vagrant使用教程https://www.vagrantup.com/intro/getting-started/project_setup.html

Install Vagrant

Vagrant must first be installed on the machine you want to run it on. To make installation easy, Vagrant is distributed as a binary package for all supported platforms and architectures. This page will not cover how to compile Vagrant from source, as that is covered in the README and is only recommended for advanced users.
»
Installing Vagrant

To install Vagrant, first find the appropriate package for your system and download it. Vagrant is packaged as an operating-specific package. Run the installer for your system. The installer will automatically add vagrant to your system path so that it is available in terminals. If it is not found, please try logging out and logging back in to your system (this is particularly necessary sometimes for Windows).
»
Verifying the Installation

After installing Vagrant, verify the installation worked by opening a new command prompt or console, and checking that vagrant is available:

$ vagrant

Usage: vagrant [options] []

-v, --version                    Print the version and exit.
-h, --help                       Print this help.

»
Caveats

Beware of system package managers! Some operating system distributions include a vagrant package in their upstream package repos. Please do not install Vagrant in this manner. Typically these packages are missing dependencies or include very outdated versions of Vagrant. If you install via your system’s package manager, it is very likely that you will experience issues. Please use the official installers on the downloads page.

Project Setup
The first step in configuring any Vagrant project is to create a Vagrantfile. The purpose of the Vagrantfile is twofold:

Mark the root directory of your project. Many of the configuration options in Vagrant are relative to this root directory.

Describe the kind of machine and resources you need to run your project, as well as what software to install and how you want to access it.

Vagrant has a built-in command for initializing a directory for usage with Vagrant: vagrant init. For the purpose of this getting started guide, please follow along in your terminal:

$ mkdir vagrant_getting_started
$ cd vagrant_getting_started
$ vagrant init hashicorp/precise64

This will place a Vagrantfile in your current directory. You can take a look at the Vagrantfile if you want, it is filled with comments and examples. Do not be afraid if it looks intimidating, we will modify it soon enough.

You can also run vagrant init in a pre-existing directory to set up Vagrant for an existing project.

The Vagrantfile is meant to be committed to version control with your project, if you use version control. This way, every person working with that project can benefit from Vagrant without any upfront work.

Boxes

Instead of building a virtual machine from scratch, which would be a slow and tedious process, Vagrant uses a base image to quickly clone a virtual machine. These base images are known as "boxes" in Vagrant, and specifying the box to use for your Vagrant environment is always the first step after creating a new Vagrantfile.

Installing a Box

If you ran the commands on the getting started overview page, then you’ve already installed a box before, and you do not need to run the commands below again. However, it is still worth reading this section to learn more about how boxes are managed.

Boxes are added to Vagrant with vagrant box add. This stores the box under a specific name so that multiple Vagrant environments can re-use it. If you have not added a box yet, you can do so now:

$ vagrant box add hashicorp/precise64

This will download the box named “hashicorp/precise64” from HashiCorp’s Vagrant Cloud box catalog, a place where you can find and host boxes. While it is easiest to download boxes from HashiCorp’s Vagrant Cloud you can also add boxes from a local file, custom URL, etc.

Boxes are globally stored for the current user. Each project uses a box as an initial image to clone from, and never modifies the actual base image. This means that if you have two projects both using the hashicorp/precise64 box we just added, adding files in one guest machine will have no effect on the other machine.

In the above command, you will notice that boxes are namespaced. *Boxes are broken down into two parts - the username and the box name - separated by a slash *. In the example above, the username is “hashicorp”, and the box is “precise64”. You can also specify boxes via URLs or local file paths, but that will not be covered in the getting started guide.

Namespaces do not guarantee canonical boxes! A common misconception is that a namespace like “ubuntu” represents the canonical space for Ubuntu boxes. This is untrue. Namespaces on Vagrant Cloud behave very similarly to namespaces on GitHub, for example. Just as GitHub’s support team is unable to assist with issues in someone’s repository, HashiCorp’s support team is unable to assist with third-party published boxes.

Using a Box

Now that the box has been added to Vagrant, we need to configure our project to use it as a base. Open the Vagrantfile and change the contents to the following:

Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/precise64"
end

The “hashicorp/precise64” in this case must match the name you used to add the box above. This is how Vagrant knows what box to use. If the box was not added before, Vagrant will automatically download and add the box when it is run.

You may specify an explicit version of a box by specifying config.vm.box_version for example:

Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/precise64"
  config.vm.box_version = "1.1.0"
end

You may also specify the URL to a box directly using config.vm.box_url:

Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/precise64"
  config.vm.box_url = "https://vagrantcloud.com/hashicorp/precise64"
end

Up And SSH

It is time to boot your first Vagrant environment. Run the following from your terminal:

$ vagrant up

In less than a minute, this command will finish and you will have a virtual machine running Ubuntu. You will not actually see anything though, since Vagrant runs the virtual machine without a UI. To prove that it is running, you can SSH into the machine:

$ vagrant ssh

This command will drop you into a full-fledged SSH session. Go ahead and interact with the machine and do whatever you want. Although it may be tempting, be careful about rm -rf /, since Vagrant shares a directory at /vagrant with the directory on the host containing your Vagrantfile, and this can delete all those files. Shared folders will be covered in the next section.

Take a moment to think what just happened: With just one line of configuration and one command in your terminal, we brought up a fully functional, SSH accessible virtual machine. Cool. The SSH session can be terminated with CTRL+D.

vagrant@precise64:~$ logout
Connection to 127.0.0.1 closed.

When you are done fiddling around with the machine, run vagrant destroy back on your host machine, and Vagrant will terminate the use of any resources by the virtual machine.

The vagrant destroy command does not actually remove the downloaded box file. To completely remove the box file, you can use the vagrant box remove command.

vagrant destory
vagrant box remove
Usage: vagrant [options] <command> [<args>]

    -v, --version                    Print the version and exit.
    -h, --help                       Print this help.

Common commands:
     box             manages boxes: installation, removal, etc.
     cloud           manages everything related to Vagrant Cloud
     destroy         stops and deletes all traces of the vagrant machine
     global-status   outputs status Vagrant environments for this user
     halt            stops the vagrant machine
     help            shows the help for a subcommand
     init            initializes a new Vagrant environment by creating a Vagrantfile
     login
     package         packages a running vagrant environment into a box
     plugin          manages plugins: install, uninstall, update, etc.
     port            displays information about guest port mappings
     powershell      connects to machine via powershell remoting
     provision       provisions the vagrant machine
     push            deploys code in this environment to a configured destination
     rdp             connects to machine via RDP
     reload          restarts vagrant machine, loads new Vagrantfile configuration
     resume          resume a suspended vagrant machine
     snapshot        manages snapshots: saving, restoring, etc.
     ssh             connects to machine via SSH
     ssh-config      outputs OpenSSH valid configuration to connect to the machine
     status          outputs status of the vagrant machine
     suspend         suspends the machine
     up              starts and provisions the vagrant environment
     upload          upload to machine via communicator
     validate        validates the Vagrantfile
     version         prints current and latest Vagrant version
     winrm           executes commands on a machine via WinRM
     winrm-config    outputs WinRM configuration to connect to the machine
Usage: vagrant box <subcommand> [<args>]

Available subcommands:
     add
     list
     outdated
     prune
     remove
     repackage
     update
Vagrant vs. Other Software
Getting Started
    Install
    Project Setup
    Boxes
    Up and SSH
    Synced Folders
    Provisioning
    Networking
    Share
    Teardown
    Rebuild
    Providers

Synced Folders

While it is cool to have a virtual machine so easily, not many people want to edit files using just plain terminal-based editors over SSH. Luckily with Vagrant you do not have to. By using synced folders, Vagrant will automatically sync your files to and from the guest machine.

By default, Vagrant shares your project directory (remember, that is the one with the Vagrantfile) to the /vagrant directory in your guest machine.

Note that when you vagrant ssh into your machine, you’re in /home/vagrant. /home/vagrant is a different directory from the synced /vagrant directory.

If your terminal displays an error about incompatible guest additions (or no guest additions), you may need to update your box or choose a different box such as hashicorp/precise64. Some users have also had success with the vagrant-vbguest plugin, but it is not officially supported by the Vagrant core team.

Run vagrant up again and SSH into your machine to see:

$ vagrant up
...
$ vagrant ssh
...
vagrant@precise64:~$ ls /vagrant
Vagrantfile

Believe it or not, that Vagrantfile you see inside the virtual machine is actually the same Vagrantfile that is on your actual host machine. Go ahead and touch a file to prove it to yourself:

vagrant@precise64:~$ touch /vagrant/foo
vagrant@precise64:~$ exit
$ ls
foo Vagrantfile

Whoa! “foo” is now on your host machine. As you can see, Vagrant kept the folders in sync.

With synced folders, you can continue to use your own editor on your host machine and have the files sync into the guest machine.

Provisioning

Alright, so we have a virtual machine running a basic copy of Ubuntu and we can edit files from our machine and have them synced into the virtual machine. Let us now serve those files using a webserver.

We could just SSH in and install a webserver and be on our way, but then every person who used Vagrant would have to do the same thing. Instead, Vagrant has built-in support for automated provisioning. Using this feature, Vagrant will automatically install software when you vagrant up so that the guest machine can be repeatably created and ready-to-use.
»
Installing Apache

We will just setup Apache for our basic project, and we will do so using a shell script. Create the following shell script and save it as bootstrap.sh in the same directory as your Vagrantfile:---->
(By default, Vagrant shares your project directory (remember, that is the one with the Vagrantfile) to the /vagrant directory in your guest machine.)
即在/vagrant目录下创建该脚本文件:bootstrap.sh

#!/usr/bin/env bash

apt-get update
apt-get install -y apache2
if ! [ -L /var/www ]; then
  rm -rf /var/www
  ln -fs /vagrant /var/www
fi

Next, we configure Vagrant to run this shell script when setting up our machine. We do this by editing the Vagrantfile, which should now look like this:

Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/precise64"
  config.vm.provision :shell, path: "bootstrap.sh"
end

The “provision” line is new, and tells Vagrant to use the shell provisioner to setup the machine, with the bootstrap.sh file. The file path is relative to the location of the project root (where the Vagrantfile is).
»
Provision!

After everything is configured, just run vagrant up to create your machine and Vagrant will automatically provision it. You should see the output from the shell script appear in your terminal. If the guest machine is already running from a previous step, run vagrant reload --provision, which will quickly restart your virtual machine, skipping the initial import step. The provision flag on the reload command instructs Vagrant to run the provisioners, since usually Vagrant will only do this on the first vagrant up.

After Vagrant completes running, the web server will be up and running. You cannot see the website from your own browser (yet), but you can verify that the provisioning works by loading a file from SSH within the machine:

$ vagrant ssh
...
vagrant@precise64:~$ wget -qO- 127.0.0.1

This works because in the shell script above we installed Apache and setup the default DocumentRoot of Apache to point to our /vagrant directory, which is the default synced folder setup by Vagrant.

You can play around some more by creating some more files and viewing them from the terminal, but in the next step we will cover networking options so that you can use your own browser to access the guest machine.

For complex provisioning scripts, it may be more efficient to package a custom Vagrant box with those packages pre-installed instead of building them each time. This topic is not covered by the getting started guide, but can be found in the packaging custom boxes documentation.

Networking

At this point we have a web server up and running with the ability to modify files from our host and have them automatically synced to the guest. However, accessing the web pages simply from the terminal from inside the machine is not very satisfying. In this step, we will use Vagrant’s networking features to give us additional options for accessing the machine from our host machine.
»
Port Forwarding

One option is to use port forwarding. Port forwarding allows you to specify ports on the guest machine to share via a port on the host machine. This allows you to access a port on your own machine, but actually have all the network traffic forwarded to a specific port on the guest machine.

Let us setup a forwarded port so we can access Apache in our guest. Doing so is a simple edit to the Vagrantfile, which now looks like this:

Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/precise64"
  config.vm.provision :shell, path: "bootstrap.sh"
  config.vm.network :forwarded_port, guest: 80, host: 4567
end

Run a vagrant reload or vagrant up (depending on if the machine is already running) so that these changes can take effect.

Once the machine is running again, load http://127.0.0.1:4567 in your browser. You should see a web page that is being served from the virtual machine that was automatically setup by Vagrant.
»
Other Networking

Vagrant also has other forms of networking, allowing you to assign a static IP address to the guest machine, or to bridge the guest machine onto an existing network. If you are interested in other options, read the networking page.

Share

Now that we have a web server up and running and accessible from your machine, we have a fairly functional development environment. But in addition to providing a development environment, Vagrant also makes it easy to share and collaborate on these environments. The primary feature to do this in Vagrant is called Vagrant Share.

Vagrant Share lets you share your Vagrant environment to anyone around the world with an Internet connection. It will give you a URL that will route directly to your Vagrant environment from any device in the world that is connected to the Internet.

Run vagrant share:

$ vagrant share
...
==> default: Creating Vagrant Share session...
==> default: HTTP URL: http://b1fb1f3f.ngrok.io
...

Your URL will be different, so do not try the URL above. Instead, copy the URL that vagrant share outputted for you and visit that in a web browser. It should load the Apache page we setup earlier.

If you modify the files in your shared folder and refresh the URL, you will see it update! The URL is routing directly into your Vagrant environment, and works from any device in the world that is connected to the internet.

To end the sharing session, hit Ctrl+C in your terminal. You can refresh the URL again to verify that your environment is no longer being shared.

Vagrant Share is much more powerful than simply HTTP sharing. For more details, see the complete Vagrant Share documentation.

Vagrant share is not designed to serve production traffic! Please do not rely on Vagrant share outside of development or Q/A. The Vagrant share service is not designed to carry production-level traffic.

Teardown

How do we clean up our development environment?

With Vagrant, you suspend, halt, or destroy the guest machine. Each of these options have pros and cons. Choose the method that works best for you.

Suspending the virtual machine by calling vagrant suspend will save the current running state of the machine and stop it. When you are ready to begin working again, just run vagrant up, and it will be resumed from where you left off. The main benefit of this method is that it is super fast, usually taking only 5 to 10 seconds to stop and start your work. The downside is that the virtual machine still eats up your disk space, and requires even more disk space to store all the state of the virtual machine RAM on disk.

Halting the virtual machine by calling vagrant halt will gracefully shut down the guest operating system and power down the guest machine. You can use vagrant up when you are ready to boot it again. The benefit of this method is that it will cleanly shut down your machine, preserving the contents of disk, and allowing it to be cleanly started again. The downside is that it’ll take some extra time to start from a cold boot, and the guest machine still consumes disk space.

Destroying the virtual machine by calling vagrant destroy will remove all traces of the guest machine from your system. It’ll stop the guest machine, power it down, and remove all of the guest hard disks. Again, when you are ready to work again, just issue a vagrant up. The benefit of this is that no cruft is left on your machine. The disk space and RAM consumed by the guest machine is reclaimed and your host machine is left clean. The downside is that vagrant up to get working again will take some extra time since it has to reimport the machine and re-provision it.

Rebuild

When you are ready to come back to your project, whether it is tomorrow, a week from now, or a year from now, getting it up and running is easy:

$ vagrant up

That’s it! Since the Vagrant environment is already all configured via the Vagrantfile, you or any of your coworkers simply have to run vagrant up at any time and Vagrant will recreate your work environment.

Providers

In this getting started guide, your project was always backed with VirtualBox. But Vagrant can work with a wide variety of backend providers, such as VMware, AWS, and more. Read the page of each provider for more information on how to set them up.

Once you have a provider installed, you do not need to make any modifications to your Vagrantfile, just vagrant up with the proper provider and Vagrant will do the rest:

$ vagrant up --provider=vmware_fusion

Ready to move that to the cloud? Take it to AWS:

$ vagrant up --provider=aws

Once you run vagrant up with another provider, every other Vagrant command does not need to be told what provider to use. Vagrant can automatically figure it out. So when you are ready to SSH or destroy or anything else, just run the commands like normal, such as vagrant destroy. No extra flags necessary.

2.VirtualBox

VirtualBox官网https://www.virtualbox.org/
VirtualBox下载https://www.virtualbox.org/wiki/Downloads
VirtualBox:开源虚拟化工具
虚拟化工具(虚拟机软件):有windows版,linux版,mac版。它的主要作用是在原有操作系统(windows或linux或mac)下,虚拟出一台电脑,你可以在这台虚拟电脑上再安装其他的操作系统,就等于不用重启电脑,而同时运行几个操作系统。至于能同时运行多少个操作系统,要视乎主机的性能和所安装的操作系统占用的资源而定。
下载并安装完VirtualBox后为了能让Vagrant使用VirtualBox,需将包含VBoxManage的目录加入到系统的PATH环境变量中。
当使用vagrant up命令出错时,根据提示运行vagrant up --provider=virtualbox可显示具体的出错提示信息:根据提示信息添加具体的环境变量。

vagrant up --provider=virtualbox

在这里插入图片描述

3.VMware Workstation

VMware Workstation:商用虚拟化工具
网络原理,以及对VMware Workstation虚拟网络VMnet0、VMnet1、VMnet8的图解
VMware Workstation 的安装和使用
使用VMware给虚拟机安装linux系统

猜你喜欢

转载自blog.csdn.net/qq_41875506/article/details/89529475