@daduizhang
2021-03-24T02:07:52.000000Z
字数 10609
阅读 1205
Linux
vagrant 虚拟开发环境,和线上环境保持一致,已预装git, vim, python, go, docker, mysql 等, 并完成以下配置:
开箱即用,轻松避免开发环境和生产环境不一致的问题。
使用vagrant的好处:
- 业界流行的开发方式
- 避免折腾环境问题,提升开发效率
- 用windows和mac的用户可以在享受原生系统的易用性的同时,享受使用linux的快感。
- 占用资源少,和原生linux速度相当
- 与host系统隔离,vm被搞坏了可以随时删掉重建,还可以创建多个虚拟环境。系统崩了,不用担心重要文件丢失。
- 方便本地调试,端口无限制,软件安装无需申请权限
- 避免在一台机器上测试通过,在另一台机器上无法通过的问题
vagrant box add ubuntu-16.04 ./ubuntu-16.04.boxvagrant init ubuntu-16.04
此时会在目录下生成配置文件 Vagrantfile,修改以下配置:
# 添加私有IP,和host机通信config.vm.network "private_network", ip: "192.168.33.10"# 映射文件夹,可自行修改config.vm.synced_folder "../workspace", "/home/vagrant/workspace"config.vm.provider "virtualbox" do |vb|# Display the VirtualBox GUI when booting the machinevb.gui = false# Customize the amount of memory on the VM:vb.memory = "2048"vb.cpus = "2"vb.customize [ "modifyvm", :id, "--uartmode1", "disconnected" ]end
继续执行以下命令即可
vagrant up # 启动虚拟机vagrant ssh # ssh到虚拟机vagrant halt # 关闭虚拟机,通常情况下,无需关闭
详细使用方法可参考 https://segmentfault.com/a/1190000008729625 。
用户可运行以下 Vagrantfile 自行编译生成 ubuntu-16.04-v1.0.0.box
# -*- 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/xenial64"config.vm.hostname = "shannonai.local"# 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 "/Users/shibo/shannon_workspace", "/home/vagrant/workspace"# 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 machinevb.gui = false# Customize the amount of memory on the VM:vb.memory = "2048"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# 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: <<-SHELLset -exexport DEBIAN_FRONTEND=noninteractiveUSER_HOME=/home/vagrantCONDA_HOME=$USER_HOME/software/condaGO_HOME=$USER_HOME/softwarePYTHON_VERSION=3.6.6# use aliyun sourceecho 'deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse' > /etc/apt/sources.listecho 'deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse' >> /etc/apt/sources.listecho 'deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse' >> /etc/apt/sources.listecho 'deb http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse' >> /etc/apt/sources.listecho 'deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse' >> /etc/apt/sources.listecho 'deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse' >> /etc/apt/sources.listecho 'deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse' >> /etc/apt/sources.listecho 'deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse' >> /etc/apt/sources.listecho 'deb-src http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse' >> /etc/apt/sources.list# install packageapt-get -yqq update# install mysql and allow remote access, default password of root user is 123123echo "mysql-server mysql-server/root_password password 123123" | debconf-set-selectionsecho "mysql-server mysql-server/root_password_again password 123123" | debconf-set-selectionsapt-get install -yqq --no-install-recommends mysql-server mysql-clientsed -i 's/^bind-address/# bind-address/' /etc/mysql/mysql.conf.d/mysqld.cnfmysql -u root -p123123 -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123123' WITH GRANT OPTION; FLUSH PRIVILEGES;"sed -i '31i collation-server = utf8_unicode_ci' /etc/mysql/mysql.conf.d/mysqld.cnfsed -i '32i character-set-server = utf8' /etc/mysql/mysql.conf.d/mysqld.cnfsed -i '33i skip-character-set-client-handshake' /etc/mysql/mysql.conf.d/mysqld.cnfsystemctl restart mysql# install common packagesapt-get install -yqq --no-install-recommends gcc build-essential apt-utils rsync netcat procps libssl-devapt-get install -yqq --no-install-recommends vim htop git tree pv libmysqlclient-dev language-pack-zh-hans ack-grep curl# set gitgit config --system push.default simplegit config --system alias.co checkoutgit config --system alias.br branchgit config --system alias.ci commitgit config --system alias.st statusgit config --system core.editor "vim"git config --system alias.unstage 'reset HEAD --'git config --system alias.lg 'log --graph --abbrev-commit --decorate --format=format:"%C(cyan)%h%C(reset) - %C(white)%s%C(reset) %C(yellow)%d%C(reset) %C(dim white) - %anC(reset) %C(dim green)(%c i)%C(reset)" --all'# set localesgrep -q -F 'LANG=zh_CN.UTF-8' /etc/environment || echo 'LANG=zh_CN.UTF-8' >> /etc/environmentgrep -q -F 'LC_ALL=zh_CN.UTF-8' /etc/environment || echo 'LC_ALL=zh_CN.UTF-8' >> /etc/environmentgrep -q -F 'LANGUAGE=zh_CN.UTF-8' /etc/environment || echo 'LANGUAGE=zh_CN.UTF-8' >> /etc/environment# install golangmkdir -p $GO_HOMEif [ ! -d $GO_HOME/go ]; thencurl -sSL -o /tmp/golang.tar.gz https://code.aliyun.com/k9kdqvbb/files/raw/master/go1.10.3.linux-amd64.tar.gztar -C $GO_HOME -xzf /tmp/golang.tar.gzfigrep -q -F "export GOROOT=$GO_HOME/go" $USER_HOME/.profile || echo "export GOROOT=$GO_HOME/go" >> $USER_HOME/.profilegrep -q -F 'export PATH=$GOROOT/bin:$PATH' $USER_HOME/.profile || echo 'export PATH=$GOROOT/bin:$PATH' >> $USER_HOME/.profilegrep -q -F "export GOPATH=~/workspace/go" $USER_HOME/.profile || echo "export GOPATH=~/workspace/go" >> $USER_HOME/.profilegrep -q -F 'export PATH=$GOPATH/bin:$PATH' $USER_HOME/.profile || echo 'export PATH=$GOPATH/bin:$PATH' >> $USER_HOME/.profilechown -R vagrant: $GO_HOMErm -rf /tmp/golang.tar.gz# install condaif [ ! -d $CONDA_HOME ]; thencurl -sS -o /tmp/conda.sh https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.shbash /tmp/conda.sh -b -p $CONDA_HOME$CONDA_HOME/bin/conda install -yq python==$PYTHON_VERSIONrm -rf /tmp/conda.shfigrep -q -F "export CONDA_HOME=$CONDA_HOME" $USER_HOME/.profile || echo "export CONDA_HOME=$CONDA_HOME" >> $USER_HOME/.profilegrep -q -F 'export PATH=$CONDA_HOME/bin:$PATH' $USER_HOME/.profile || echo 'export PATH=$CONDA_HOME/bin:$PATH' >> $USER_HOME/.profilemkdir -p $USER_HOME/.pipmkdir -p /root/.pipecho '[global]' | tee $USER_HOME/.pip/pip.conf /root/.pip/pip.confecho 'trusted-host = mirrors.aliyun.com' | tee --append $USER_HOME/.pip/pip.conf /root/.pip/pip.confecho 'index-url = https://mirrors.aliyun.com/pypi/simple' | tee --append $USER_HOME/.pip/pip.conf /root/.pip/pip.confchown -R vagrant: $USER_HOME/.pip$CONDA_HOME/bin/pip install --no-cache-dir --default-timeout=120 ipython glances mycli tqdm pymysql mysqlclient fabric argparse pyyamlchown -R vagrant: $CONDA_HOME# install docker-ceapt-get install -yqq --no-install-recommends apt-transport-https ca-certificates software-properties-commoncurl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"apt-get -yqq updateapt-get install -yqq --no-install-recommends docker-ce=18.03.1~ce-0~ubuntuusermod -a -G docker vagrantchmod 666 /var/run/docker.sock# aliyun speed up docker image downloadmkdir -p /etc/dockerecho "{" > /etc/docker/daemon.jsonecho ' "registry-mirrors": ["https://ht6aappv.mirror.aliyuncs.com"]' >> /etc/docker/daemon.jsonecho "}" >> /etc/docker/daemon.jsonsystemctl daemon-reloadsystemctl restart docker# install docker-composeDOCKER_COMPOSE=/usr/local/bin/docker-composeif [ ! -f $DOCKER_COMPOSE ]; thencurl -sSL -o $DOCKER_COMPOSE https://code.aliyun.com/k9kdqvbb/files/raw/master/docker-compose-Linux-x86_64chmod +x $DOCKER_COMPOSEfi# cleanapt-get autoremove -yqq --purgeapt-get clean && apt-get autoclean# set bashrcgrep -q -F "HISTSIZE=1000" $USER_HOME/.bashrc || sed -i 's/^HISTSIZE=1000/HISTSIZE=100000/' $USER_HOME/.bashrcgrep -q -F "HISTFILESIZE=2000" $USER_HOME/.bashrc || sed -i 's/^HISTFILESIZE=2000/HISTFILESIZE=200000/' $USER_HOME/.bashrc# set prefix sensitive command promptecho '"\e[A": history-search-backward' > $USER_HOME/.inputrcecho '"\e[B": history-search-forward' >> $USER_HOME/.inputrcchown vagrant: $USER_HOME/.inputrc# config vimrcecho 'filetype plugin indent on' > $USER_HOME/.vimrcecho 'set nocompatible' >> $USER_HOME/.vimrcecho 'filetype off' >> $USER_HOME/.vimrcecho 'set mouse-=a' >> $USER_HOME/.vimrcecho 'set fileencodings=utf-8' >> $USER_HOME/.vimrcecho 'set termencoding=utf-8' >> $USER_HOME/.vimrcecho 'set fileencoding=utf-8' >> $USER_HOME/.vimrcecho 'syntax on' >> $USER_HOME/.vimrcecho 'set nu' >> $USER_HOME/.vimrcecho 'set tabstop=4' >> $USER_HOME/.vimrcecho 'set shiftwidth=4' >> $USER_HOME/.vimrcecho 'set expandtab' >> $USER_HOME/.vimrcecho 'set cindent' >> $USER_HOME/.vimrcecho 'set autoindent' >> $USER_HOME/.vimrcecho 'set showmatch' >> $USER_HOME/.vimrcecho 'set cursorline' >> $USER_HOME/.vimrcecho 'set cursorcolumn' >> $USER_HOME/.vimrcecho 'set incsearch' >> $USER_HOME/.vimrcecho 'set hlsearch' >> $USER_HOME/.vimrcecho 'set ignorecase smartcase' >> $USER_HOME/.vimrcecho 'set nowrapscan' >> $USER_HOME/.vimrcecho 'set laststatus=2' >> $USER_HOME/.vimrcecho 'set nostartofline' >> $USER_HOME/.vimrcecho 'set ruler' >> $USER_HOME/.vimrcecho ':color desert' >> $USER_HOME/.vimrcchown vagrant: $USER_HOME/.vimrcSHELLend