vagrant是一個工具,用於創建和部署虛擬化開發環境的。它用起來非常方便。它可以很方便地幫我們部署一個虛機。在我們很多的情況裏,我們可以利用Linux系統來部署我們的beats及web服務。在今天的教程裏,我們來簡單地介紹一下如何在Mac OS的環境下部署一個Ubuntu 18.04的系統。
安裝
Homebrew
對於沒有安裝Homebrew的開發者來說,我們需要按照如下的方式來安裝:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
一旦安裝完畢,我們可以進行如下的操作:
brew doctor
brew update
Virtualbox
我們可以按照如下的命令來安裝virtualbox:
brew cask install virtualbox
依賴於不同的Mac OS的版本,你可能需要如下的命令來進行安裝:
brew install virtualbox
Vagrant
我們按照如下的命令來進行安裝:
brew cask install vagrant
這樣就完成了我們的安裝。
創建 Vagrant 文件
我想將虛擬機保存在主目錄中的單獨文件夾中,標題爲“ vms”:
mkdir ~/vms
在這裏,我爲每個項目創建一個文件夾(將<project-name>替換爲項目的實際名稱):
mkdir ~/vms/<project-name>
針對我的情況,我創建了一個叫做sample的文件夾:
/Users/liuxg/vms/sample
在這個目錄下, 我們創建一個叫做Vagrantfile的文件:
Vagrantfile
它的內容如下:
# -*- mode: ruby -*-
# vi: set ft=ruby :
VM_BOX = 'ubuntu/bionic64'
NETWORK = 'forwarded_port'
GUEST_PORT = 80
HOST_PORT = 9000
Vagrant.configure(2) do |config|
config.vm.box = VM_BOX
config.vm.network NETWORK, guest: GUEST_PORT, host: HOST_PORT
config.vm.provider "virtualbox" do |vb|
vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
vb.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
vb.memory = 4096
end
config.vm.provision 'shell', inline: <<-SHELL
echo 'ubuntu:ubuntu' | sudo chpasswd
SHELL
end
在上面我們可根據自己的需要來修改上面的GUEST_PORT及HOST_PORT。這樣我們可以暴露我們的VM的口地址給Host。我們同時也創建了一個叫做ubuntu/ubuntu的用戶名及密碼。在上面,我們部署了一個bionic的Ubuntu 系統。它是18.04的版本。我們保存好這個文件。
Vagrant及SSH
現在該我們開始部部署我們的Virtualbox的虛機的時候了。我們在terminal中打入如下的命令:
vagrant up
我們會發現如下的輸出:
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'ubuntu/bionic64' could not be found. Attempting to find and install...
default: Box Provider: virtualbox
default: Box Version: >= 0
==> default: Loading metadata for box 'ubuntu/bionic64'
default: URL: https://vagrantcloud.com/ubuntu/bionic64
==> default: Adding box 'ubuntu/bionic64' (v20200402.0.0) for provider: virtualbox
default: Downloading: https://vagrantcloud.com/ubuntu/boxes/bionic64/versions/20200402.0.0/providers/virtualbox.box
==> default: Box download is resuming from prior download progress
default: Download redirected to host: cloud-images.ubuntu.com
default: Progress: 0% (Rate: 9660/s, Estimated time remaining: 8:34:32)
上面顯示我們的安裝非常地緩慢,慢的難以接受。那麼我們如何解決這個問題呢?我們甚至嘗試不同的網路配置及技巧,可能效果還是不是你想要的。最重要的一點,我們可以發現上面的下載地址:
https://vagrantcloud.com/ubuntu/boxes/bionic64/versions/20200402.0.0/providers/virtualbox.box
我們可以通過一些技巧直接從瀏覽器器中下載這個image,並存於一個目錄中。針對我的情況:
$ pwd
/Users/liuxg/vms/ubuntu-18.04
liuxg:ubuntu-18.04 liuxg$ ls
bionic-server-cloudimg-amd64-vagrant.box
接下來,我們可以通過如下的方法來添加:
vagrant box add --name ubuntu/bionic64 /Users/liuxg/vms/ubuntu-18.04/bionic-server-cloudimg-amd64-vagrant.box
等我們添加完後,我們使用如下的命令來顯示:
vagrant box list
$ vagrant box list
ubuntu/bionic64 (virtualbox, 0)
顯然,ubuntu/bionic64已經成功地被添加了。
接下來,我們再次運行:
vagrant up
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu/bionic64'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: sample_default_1585992906421_91628
Vagrant is currently configured to create VirtualBox synced folders with
the `SharedFoldersEnableSymlinksCreate` option enabled. If the Vagrant
guest is not trusted, you may want to disable this option. For more
information on this option, please refer to the VirtualBox manual:
https://www.virtualbox.org/manual/ch04.html#sharedfolders
This option can be disabled globally with an environment variable:
VAGRANT_DISABLE_VBOXSYMLINKCREATE=1
or on a per folder basis within the Vagrantfile:
config.vm.synced_folder '/host/path', '/guest/path', SharedFoldersEnableSymlinksCreate: false
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 80 (guest) => 9000 (host) (adapter 1)
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:
default: Vagrant insecure key detected. Vagrant will automatically replace
default: this with a newly generated keypair for better security.
default:
default: Inserting generated public key within guest...
default: Removing insecure key from the guest if it's present...
default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> 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.2.34
default: VirtualBox Version: 6.1
==> default: Mounting shared folders...
default: /vagrant => /Users/liuxg/vms/sample
==> default: Running provisioner: shell...
default: Running: inline script
這一次它很快地幫我們完成了安裝。
我們接下直接使用如下的方法來進入到ubuntu系統中去:
vagrant ssh
很顯然,我們已經進入到Ubuntu系統中去了。我們可以在這裏面安裝我們需要的任何一個軟件,並啓動你自己的服務。
在默認的情況下,在vagrant的環境中,它可以訪問到主機的Vagrantfile所在的目錄。我們可以直接訪問/vagrant目錄就可以訪問到主機裏的文件了。
一下是Vagrant的更多的一些命令:
命令 | 解釋 |
---|---|
vagrant box list | 查看目前已有的box |
vagrant box add | 新增加一個box |
vagrant box remove | 刪除指定box |
vagrant init | 初始化配置vagrantfile |
vagrant up | 啓動虛擬機 |
vagrant ssh | ssh登錄虛擬機 |
vagrant suspend | 掛起虛擬機 |
vagrant reload | 重啓虛擬機 |
vagrant halt | 關閉虛擬機 |
vagrant status | 查看虛擬機狀態 |
vagrant destroy | 刪除虛擬機 |
參考:
【1】https://blog.csdn.net/weixin_33713503/article/details/93300761
【2】https://www.jianshu.com/p/0cabd5072b86