golang開發:(一)開發環境搭建vagrant+VirtualBox

開發環境介紹

不管何種開發語言,目前用的比較多的開發環境基本就是Vagrant+VirtualBox搭建的虛擬開發環境,這種開發環境的好處就是一次搭建處處可用,各個平臺和系統都可以使用。開發團隊中,可以自己製作一個box,讓團隊的成員方便安裝,保證每個人的開發環境都是一致的。

Vagrant可以創建一些共享目錄,讓物理機和虛擬機使用共享的目錄,虛擬機只提供開發環境。這樣的話,開發環境隨處可用。代碼目錄只要在物理機上共享就可以使用這套開發環境。

Vagrant 安裝

官網下載合適的安裝包傻瓜式安裝。官網下載地址:http://www.vagrantup.com/downloads.html
安裝完成後試下 命令

vagrant -h
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

就表示安裝成功了。

VirtualBox 安裝

跟Vagrant類似傻瓜式安裝。官網下載地址:https://www.virtualbox.org/wiki/Downloads/
下載合適的平臺版本安裝
VirtualBox 軟件只要可以打開就表示安裝成功了

各種box的下載

先進入官網box的列表 https://app.vagrantup.com/boxes/search
查找自己需要虛擬機 系統 和版本,我們下載的基本都是VirtualBox版本的,可以點擊菜單的VirtualBox標籤
在這裏插入圖片描述
我們下載的是 Ubuntu 16.04 LTS
https://app.vagrantup.com/ubuntu/boxes/xenial64
找到它最近的版本,點擊鏈接 https://app.vagrantup.com/ubuntu/boxes/xenial64/versions/20190521.0.0
URL + /providers/ + 虛擬機就是需要下載的box

我們下載的包就是下面的鏈接
https://app.vagrantup.com/ubuntu/boxes/xenial64/versions/20190521.0.0/providers/virtualbox.box

然後迅雷下載上面的鏈接

添加box

執行 vagrant box add 名稱 box地址
名稱–box名稱,任意取名,默認 base
box地址–已經下載好的box地址或者遠端的box地址

我的機器執行的是

vagrant box add base virtualboxubuntu.box

添加完成後執行
sudo vagrant init
我的執行

    sudo vagrant init
    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.

啓動 vagrant

     sudo vagrant up
/opt/vagrant/embedded/gems/2.2.2/gems/vagrant-2.2.2/lib/vagrant/util/which.rb:37: warning: Insecure world writable dir /data/code/go/bin in PATH, mode 040777
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'base'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: ubuntubox_default_1558766226465_14530
==> default: Fixed port collision for 22 => 2222. Now on port 2200.

連接登錄虛擬機

sudo vagrant ssh
/opt/vagrant/embedded/gems/2.2.2/gems/vagrant-2.2.2/lib/vagrant/util/which.rb:37: warning: Insecure world writable dir /data/code/go/bin in PATH, mode 040777
Welcome to Ubuntu 16.04.6 LTS (GNU/Linux 4.4.0-148-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

0 packages can be updated.
0 updates are security updates.

可以看到是Ubuntu的系統

vagrantup 常用的開發配置

打開box的配置。vim Vagrantfile

config.vm.box = "base" --box 名字
config.vm.synced_folder "../data", "/vagrant_data"
--把本機的../data 掛載到虛擬機的 /vagrant_data
ned port
config.vm.network "forwarded_port", guest: 80, host: 8080
--把本機的8080端口請求轉發到虛擬機的 80端口
config.vm.network "private_network", ip: "192.168.33.10"
--網絡設置,主機虛擬機網絡互訪,主機通過192.168.33.10 可以訪問虛擬機
config.ssh.username = "vagrant"
--登錄用戶名 
config.ssh.password = "vagrant"
--登錄密碼
config.ssh.insert_key = "true"
--賬戶密碼鍵值存儲(一直沒明白是啥意思)
config.ssh.private_key_path = "/Users/XXX/.ssh/id_rsa"
--密鑰登錄的時候,密鑰地址。
onfig.vm.provision "shell", inline: <<-SHELL
 apt-get update
 apt-get install -y apache2
 SHELL
 --虛擬機啓動的時候需要執行的腳本

瞭解一些簡單vagrantup,平常開發就夠用了。

vagrant box add/remove 添加移除 box
vagrant halt 停止虛擬機
vagrant init 初始化虛擬機
vagrant up 啓動虛擬機
vagrant reload 重載虛擬機
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章