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