2019-年-最简单最通俗的-vagrant-安装使用说明附带示例Vagrantfile

39次阅读

共计 4136 个字符,预计需要花费 11 分钟才能阅读完成。

2019 年 最简单最通俗的 vagrant 安装使用说明,附带示例 vagrantfile

Vagrant 是一个基于 Ruby 的工具,用于创建和部署虚拟化开发环境。它 使用 Oracle 的开源 VirtualBox 虚拟化系统,使用 Chef 创建自动化虚拟环境。

老套路,更新留坑

vagrant 安装

  • 官网链接

vagrant windows 下载

  • 64 位下载:https://releases.hashicorp.co…
  • 32 位下载:https://releases.hashicorp.co…
  • 百度云 64 位下载:

     链接:https://pan.baidu.com/s/1oiztOlj0S_h6AfQ6WdUb_w 
    提取码:aoph 

vagrant mac 下载

  • 64 位下载: https://releases.hashicorp.co…

vagrant box 下载

  • 官网下载:https://app.vagrantup.com/box…
  • 百度云下载:提取码:aoph

vagrant box 添加到本地镜像

  • 下载好的镜像添加
vagrant box add {镜像名称} {镜像地址}

例如:

vagrant box add C:/box/centos7.box --name centos/7.5 
  • 使用远程镜像
vagrant box add https://mirrors.tuna.tsinghua.edu.cn/ubuntu-cloud-images/bionic/current/bionic-server-cloudimg-amd64-vagrant.box --name ubuntu/bionic

vagrant 命令说明

vagrant up 启动 / 创建虚拟机

G:\project
λ vagrant up  ci1.cn && vagrant ssh ci1.cn
Bringing machine 'ci1.cn' up with 'virtualbox' provider...
==> ci1.cn: Clearing any previously set forwarded ports...
==> ci1.cn: Vagrant has detected a configuration issue which exposes a
.....
Last login: Mon Jun 24 06:40:46 2019 from 10.0.2.2
[vagrant@ci1 ~]$ ls
node-v12.3.1  node-v12.3.1.tar.gz  project
[vagrant@ci1 ~]$

vagrant ssh 进行虚拟机交互命令行

G:\project
λ vagrant ssh ci1.cn
Last login: Mon Jun 24 06:40:46 2019 from 10.0.2.2
[vagrant@ci1 ~]$

vagrant reload 重新启动虚拟机

vagrant reload ci1.cn

vagrant up/reload –provision 重新创建或重新执行脚本

vagrant up --provision
vagrant reload --provision

vagrant status 查看当前目录下的虚拟机状态

vagrant status

例如:

G:\project
λ vagrant status
Current machine states:

ci1.cn                    running (virtualbox)
ci2.cn                    poweroff (virtualbox)

This environment represents multiple VMs. The VMs are all listed
above with their current state. For more information about a specific
VM, run `vagrant status NAME`.

vagrant 使用样例

我们举例以 lin-cms-tp 的全家桶进行举例,其他目录一样哈,别太注重目录

  • 进入 lin-cms-tp 全家桶 平级目录
λ ls

lin-cms-tp
  • 初始化 Vagrantfile
G:\project\open-source
λ 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.

λ ls

lin-cms-tp  Vagrantfile
  • 添加 box 镜像
vagrant box add https://mirrors.tuna.tsinghua.edu.cn/ubuntu-cloud-images/bionic/current/bionic-server-cloudimg-amd64-vagrant.box --name ubuntu/bionic
  • 然后替换 Vagrantfile 内容
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.require_version ">= 1.6.0"

boxes = [
    {
        :name => "lincms",
        :eth1 => "10.10.1.10",
        :mem => "1024",
        :cpu => "1"
    }
]

Vagrant.configure(2) do |config|

  config.vm.box = "ubuntu/bionic"

  boxes.each do |opts|
      config.vm.define opts[:name] do |config|
        config.vm.hostname = opts[:name]
        config.vm.provider "vmware_fusion" do |v|
          v.vmx["memsize"] = opts[:mem]
          v.vmx["numvcpus"] = opts[:cpu]
        end

        config.vm.provider "virtualbox" do |v|
          v.customize ["modifyvm", :id, "--memory", opts[:mem]]
          v.customize ["modifyvm", :id, "--cpus", opts[:cpu]]
        end

        config.vm.network :private_network, ip: opts[:eth1]
      end
  end

  config.vm.synced_folder "./lin-cms-tp", "/home/vagrant/lin-cms-tp"
  config.vm.provision "shell", privileged: true, path: "./setup.sh"

end
  • 新建脚本文件:setup.sh , 内容如下:
# Timezone
sudo /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && echo 'Asia/Shanghai' > /etc/timezone

# 设置国内源
sudo mv /etc/apt/sources.list /etc/apt/sources.list.back && \
     echo '# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释 \n \
     deb http://mirrors.aliyun.com/debian/ stretch main non-free contrib \n \
     deb-src http://mirrors.aliyun.com/debian/ stretch main non-free contrib \n \
     deb http://mirrors.aliyun.com/debian-security stretch/updates main \n \
     deb-src http://mirrors.aliyun.com/debian-security stretch/updates main \n \
     deb http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib \n \
     deb-src http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib \n \
     deb http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib \n \
     deb-src http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib' >> /etc/apt/sources.list
     
# Libs
sudo apt-get update && sudo apt-get install -y git vim gcc glibc-static telnet bridge-utils

# install docker

sudo curl -fsSL https://get.docker.com | sudo bash -s docker --mirror Aliyun

sudo groupadd docker
sudo gpasswd -a vagrant docker
sudo systemctl start docker

rm -rf get-docker.sh

#  配置镜像加速器
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{"registry-mirrors": ["https://dt77flbr.mirror.aliyuncs.com"]
}
EOF

sudo systemctl enable docker
sudo systemctl daemon-reload
sudo systemctl restart docker


# 打印 IP 地址信息

ip a
  • 最后一步,创建并启动虚拟器
# 创建
vagrant up lincms

# 查看状态
vagrant status


# 进入
vagrant ssh lincms

如果在以上过程出现错误,请到搜索引擎搜索解决,或者提交评论和留言

错误收集文档【点我】

正文完
 0