一、安裝virtualBox 進入 VirtualBox 的主頁,即可進入下載頁面. VirtualBox 是一個跨平臺的虛擬化工具,支持多個操作系統,根據自己的情況選擇對應的版本下載即可。 在安裝完主程式後,直接雙擊擴展包文件即可安裝擴展包。 二、安裝Vagrant 在 Vagant 網站下載最新 ...
一、安裝virtualBox
進入 VirtualBox 的主頁,即可進入下載頁面.
VirtualBox 是一個跨平臺的虛擬化工具,支持多個操作系統,根據自己的情況選擇對應的版本下載即可。
在安裝完主程式後,直接雙擊擴展包文件即可安裝擴展包。
二、安裝Vagrant
在 Vagant 網站下載最新的版本,根據自己的操作系統選擇對應的版本下載即可。
註意,Vagrant 是沒有圖形界面的,所以安裝完成後也沒有桌面快捷方式。具體使用方法,接下來會詳細說明。
Vagrant 的安裝程式會自動把安裝路徑加入到 PATH 環境變數,所以,這時候可以通過命令行執行 vagrant version
檢查是否安裝成功:
三、下載虛擬機鏡像
使用 Vagrant 創建虛機時,需要指定一個鏡像,也就是 box
。開始這個 box 不存在,所以 Vagrant 會先從網上下載,然後緩存在本地目錄中。
Vagrant 有一個鏡像網站,裡面列出了都有哪些鏡像可以用,並且提供了操作文檔。
但是這裡預設下載往往會比較慢,所以下麵我會介紹如何在其它地方下載到基礎鏡像,然後按照自己的需要重置。如果網速較好,下載順利的朋友可以選擇性地跳過部分內容。
下麵我給出最常用的兩個 Linux 操作系統鏡像的下載地址:
CentOS
CentOS 的鏡像下載網站是: http://cloud.centos.org/centos/
在其中選擇自己想要下載的版本,列表中有一個 vagrant
目錄,裡面是專門為 vagrant 構建的鏡像。選擇其中的 .box
尾碼的文件下載即可。這裡可以使用下載工具,以較快的速度下載下來。
Ubuntu
Ubuntu 的鏡像下載網站是: http://cloud-images.ubuntu.com/
同樣先選擇想要的版本,然後選擇針對 vagrant 的 .box
文件即可。
如果這裡官網的速度較慢,還可以從 清華大學的鏡像站 下載。
四、添加box
接下來我們需要將下載後的 .box
文件添加到 vagrant 中。
Vagrant 沒有 GUI,只能從命令行訪問,先啟動一個命令行,然後執行:
指令1:vagrant box list 查詢vagrant 已經管理的 Box 有哪些
houlei@houleideMacBook-Pro ubuntu % vagrant box list
There are no installed boxes! Use `vagrant box add` to add some.
指令2:vagrant box add 將 box 添加到vagrant 中, 命令後面跟著的是box文件路徑,並且通過 --name ubuntu
為這個 box 指定一個名字。
houlei@houleideMacBook-Pro ubuntu % vagrant box add /Users/houlei/Desktop/vagrant/box/xenial-server-cloudimg-amd64-vagrant.box --name ubuntu ==> box: Box file was not detected as metadata. Adding it directly... ==> box: Adding box 'ubuntu' (v0) for provider: box: Unpacking necessary files from: file:///Users/houlei/Desktop/vagrant/box/xenial-server-cloudimg-amd64-vagrant.box ==> box: Successfully added box 'ubuntu' (v0) for 'virtualbox'! # 安裝成功 houlei@houleideMacBook-Pro ubuntu % vagrant box list ubuntu (virtualbox, 0) # 剛安裝成功的box,在安裝的時候,我去的名字叫ubuntu houlei@houleideMacBook-Pro ubuntu %
指令3:vagrant box remove NAME 根據名字刪除指定的box
五、Vagrant基本操作
1、新建虛擬機
我們在創建虛擬機的時候,會生產一些文件,所以我們為每個虛擬機最好都創建一個獨立的文件。然後進入文件中
/Users/houlei/Desktop/vagrant/ubuntu
houlei@houleideMacBook-Pro ubuntu %
我在桌面上創建了一個vagrant文件夾,在裡面有創建了ubuntu文件夾,專門用來存放創建的而這個虛擬機的東西
新建虛擬機指令:vagrant init [boxname] 加上boxname 表示使用哪個box 創建虛擬機
houlei@houleideMacBook-Pro ubuntu % vagrant init ubuntu 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. houlei@houleideMacBook-Pro ubuntu %
創建成功後,會在文件夾中多一個“Vagrantfile”的文件。
2、啟動虛擬機
註意: 在當前這個小例子中,上面所有的 vagrant
命令都需要在 Vagrantfile
所在的目錄下執行。
啟動虛擬機的指令:vagrant up
只要是沒有報錯,就說明啟動成功了
3、查看虛擬機的狀態
指令:vagrant status
如果是running 就說明我們的虛擬機,啟動成功了。
4、鏈接虛擬機
如果啟動沒問題,接下來執行 vagrant ssh
就能以 vagrant
用戶直接登入虛機中。
root
用戶沒有預設密碼,也不能直接登錄。需要 root 許可權的命令可以通過在命令前添加 sudo
來執行,也可以執行 sudo -i
直接切換到 root
用戶。
這時候打開 VirtualBox 程式,可以看到自動創建的虛機:
我們也可以在 VirtualBox 的終端上登錄系統,預設的登錄用戶名和密碼都是 vagrant,但是個人覺得不是很方便。
更推薦大家使用 vagrant ssh
5、停止虛擬機:
指令:vagrant halt
6、 暫停虛擬機
指令:vagrant suspend
7、恢復虛擬機
指令:vagrant resume
註意: 不管虛機是關閉還是暫停狀態,甚至是 error 狀態,都可以執行 vagrant up
來讓虛機恢復運行。
8、刪除虛擬機
指令:vagrant destroy
六、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://vagrantcloud.com/search. config.vm.box = "ubuntu" # 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. # Enable provisioning with a shell script. Additional provisioners such as # Ansible, Chef, Docker, Puppet and Salt 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
這是一個 Ruby 語法的文件,因為 Vagrant 就是用 Ruby 編寫的。如果編輯器沒有語法高亮可以手動設置文件類型為 Ruby。
這個預設文件內容幾乎都是註釋,提示有哪些配置項可以修改,我們不需要去學 Ruby 編程也可以照葫蘆畫瓢的完成基本的配置。
刨除註釋,這個文件的實際生效內容只有3行
Vagrant.configure("2") do |config| config.vm.box = "ubuntu" end
這裡的 config.vm.box
對應的就是虛機的鏡像,也就是 box 文件,這是唯一必填的配置項。
特別提醒,Vagrantfile
文件名是固定的寫法,大小寫也要完全一樣,修改了就不認識了
七、自定義配置Vagrantfile
下麵我將針對這份預設的 Vagrantfile
內容,逐個講解其中的配置含義和如何根據實際情況修改。
1、配置埠轉發
埠轉發(Port forward)又叫埠映射,就是把虛機的某個埠,映射到宿主機的埠上。這樣就能在宿主機上訪問到虛擬機中的服務。
例如啟動虛機時,預設的 22 (guest) => 2222 (host) (adapter 1)
就是把虛機的 SSH 服務埠(22
)映射到宿主機的 2222
埠,這樣直接在宿主機通過 ssh 客戶端訪問 127.0.0.1:2222
埠就等價於訪問虛擬機的 22
埠。
下麵這兩段配置就是教我們如何配置額外的埠轉發規則,例如把 Web 服務也映射出來:
# 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"
實際上設置埠轉發這個功能並不實用,一個很明顯的問題就是如果啟動多個虛機,很容易就出現宿主機上埠衝突的問題。即使沒有埠衝突,使用起來也不方便,我個人不推薦使用的,可以把這部分配置直接刪掉。直接使用下麵的私有網路。
這個功能是虛擬機軟體提供的,可以在虛機的網卡設置中展開高級選項,找到相關的配置:
還有個地方需要註意,預設的 SSH 埠映射在這裡沒法直接修改。比如像我這樣,2222 埠出現莫名問題,如果想要把 22 埠轉發到其它埠如 22222,直接添加下麵這樣的配置是沒用的:
config.vm.network "forwarded_port", guest: 22, host: 22222 它會在原來的基礎上新加一個埠轉發規則,而不是替代原來的,必須要先強制關閉掉預設的那條規則:
config.vm.network "forwarded_port", guest: 22, host: 2222, id: "ssh", disabled: "true" config.vm.network "forwarded_port", guest: 22, host: 22222
2、配置私有網路
下麵這段配置用來配置私有網路,實際上對應的是 VirtualBox 的主機網路,也就是 HostOnly 網路。
# 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"
取消註釋最下麵一行,就可以為虛機設置指定的私有網路地址:
config.vm.network "private_network", ip: "192.168.33.10"
如果這個網段的主機網路在 VirtualBox 中不存在,Vagrant 會在啟動虛機時自動創建。所以,如果你想要利用已有的網路,請查看現有主機網路配置:
最好這個網路也不要啟用 DHCP,完全由自己來分配地址,這樣更加清楚。
config.vm.network "private_network", ip: "192.168.56.10"
修改完成後,執行 vagrant reload
命令重建虛機,就能看到多出來的網卡了。
私有網路實際也可以直接使用 DHCP,但是並不推薦:
config.vm.network "private_network", type: "dhcp"
3、配置同步文件夾
houlei@houleideMacBook-Pro ubuntu % vagrant reload ==> default: Attempting graceful shutdown of VM... ==> default: Clearing any previously set forwarded ports... ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat default: Adapter 2: hostonly ==> default: Forwarding ports... default: 22 (guest) => 2222 (host) (adapter 1) ==> default: Running 'pre-boot' VM customizations... ==> default: Booting VM... ==> default: Waiting for machine to boot. This may take a few minutes... default: SSH address: 127.0.0.1:2222 default: SSH username: vagrant default: SSH auth method: private key default: Warning: Connection reset. Retrying... ==> default: Machine booted and ready! ==> default: Checking for guest additions in VM... default: The guest additions on this VM do not match the installed version of default: VirtualBox! In most cases this is fine, but in rare cases it can default: prevent things such as shared folders from working properly. If you see default: shared folder errors, please make sure the guest additions within the default: virtual machine match the version of VirtualBox you have installed on default: your host and reload your VM. default: default: Guest Additions Version: 5.1.38 default: VirtualBox Version: 6.1 ==> default: Configuring and enabling network interfaces... ==> default: Mounting shared folders... default: /vagrant => /Users/houlei/Desktop/vagrant/ubuntu # /vagrant 對應的事虛擬機上的路徑, =>對應的是本機上的路徑。 ==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision` ==> default: flag to force provisioning. Provisioners marked to run always will still run.
侯哥語錄:我曾經是一個職業教育者,現在是一個自由開發者。我希望我的分享可以和更多人一起進步。分享一段我喜歡的話給大家:"我所理解的自由不是想乾什麼就乾什麼,而是想不幹什麼就不幹什麼。當你還沒有能力說不得時候,就努力讓自己變得強大,擁有說不得權利。"