一、VirtualBox和vagrant的安裝
VirtualBox 是一款開源的虛擬機軟件,和VMWare是同類型的軟件,用於在當前的電腦上構建一台虛擬機,在這台虛擬機上可以安裝系統和軟件,與真實的電腦一般無二。
Vagrant用於創建和部署虛擬化開發環境。它使用Oracle的開源VirtualBox虛擬化系統.。
注意的是這兩個的版本,如果匹配不上在運行vagrant命令時就會出現版本問題,筆者使用的是windows7系統,這里提供上面兩個安裝包的百度雲的地址:
1、VirtualBox
鏈接:https://pan.baidu.com/s/1Qcb9DIYJV7LUG32VAMPLCQ
提取碼:y2sx
2、Vagrant
鏈接:https://pan.baidu.com/s/1AlWX6mkBvem4XYsLS_Ze_Q
提取碼:0xn9
(一)VirtualBox的安裝
1、下載完畢后如下
2、點擊安裝
不需要改變什么,只需要按照步驟,一直next進行安裝即可。
(二)Vagrant的安裝
1、下載后如下
2、點擊安裝
這與VirtualBox的安裝一樣,只是不斷的next就可以進行安裝完畢,這個安裝完畢后會進行重啟電腦,在重啟電腦后可以打開cmd命令行窗口進行驗證看是否已經可以使用了。
可以看到並沒有出現什么問題,可以正常的使用了,下面是vagrant對應的命令。
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. connect connect to a remotely shared Vagrant environment 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 Vagrant file login log in to HashiCorp's Atlas 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 destinatio n rdp connects to machine via RDP reload restarts vagrant machine, loads new Vagrantfile configurati on resume resume a suspended vagrant machine share share your Vagrant environment with anyone in the world snapshot manages snapshots: saving, restoring, etc. ssh connects to machine via SSH ssh-config outputs OpenSSH valid configuration to connect to the machi ne status outputs status of the vagrant machine suspend suspends the machine up starts and provisions the vagrant environment validate validates the Vagrantfile version prints current and latest Vagrant version
二、創建虛擬機
(一)使用Vagrant創建centos虛擬機
1、新建文件夾
可以先創建一個文件夾,比如centos7:
2、Vagrant創建虛擬機
- 下載box
box就是我們需要安裝的虛擬機操作系統,Vagrant開源社區提供了許多已經打包好的操作系統:
官方鏡像:https://vagrantcloud.com/boxes/search
第三方倉庫:http://www.vagrantbox.es/
當從這上面下載box后,將其拷貝到上述新建文件夾中,如下所示:
- vagrant box add --name base vagrant-centos-7.box
將下載好的box通過下面命令進行添加:
- vagrant box list
查看是否添加成功
- vagrant init base
在命令行下進入到該目錄,然后執行
vagrant init base
此時會在該目錄下生成一個Vagrantfile這樣的一個文件,它是對你將要創建的虛擬機的一個描述信息
# -*- 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 = "base" #base就是剛才的鏡像名稱 # 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. # NOTE: This will enable public access to the opened port # config.vm.network "forwarded_port", guest: 80, host: 8080 # Create a forwarded port mapping which allows access to a specific port # within the machine from a port on the host machine and only allow access # via 127.0.0.1 to disable public access # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1" # 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| # # Display the VirtualBox GUI when booting the machine # vb.gui = true # # # Customize the amount of memory on the VM: # vb.memory = "1024" # 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 install -y apache2 # SHELL end
- vagrant up
啟動虛擬機
3、連接虛擬機
其實當你創建好虛擬機后,在你的VirtureBox中已經有這個虛擬機的信息了。
此時你直接點擊登陸即可,默認的用戶名和密碼是:vagrant
或者你也可以通過xshell進行連接,這些都是可以的。