1

安装vagrant

vagrant_2.2.10

安装virtualbox

virtualbox-6.1.14

安装vagrant插件

vagrant-vbguest插件

vagrant plugin install vagrant-vbguest

vagrant-disksize插件

vagrant plugin install vagrant-disksize

vagrant-share插件

vagrant plugin install vagrant-share 
    
  • 查看vagrant插件
vagrant plugin list

vagrant-disksize (0.1.3, global)
vagrant-share (1.1.11, global)
vagrant-vbguest (0.25.0, global)

vagrant常用命令

#列出本地的box文件
vagrant box list
#在空文件夹初始化虚拟机
vagrant init NAME [URL]
#在初始化完的文件夹内启动虚拟机
vagrant up
#重启虚拟机
vagrant reload
#以默认账号vagrant连接虚拟机
vagrant ssh
#关闭虚拟机
vagrant halt
#挂起启动的虚拟机
vagrant suspend
#查找虚拟机的运行状态
vagrant status
#销毁当前虚拟机
vagrant destory

常用的镜像

Vagrant boxes search

Ubuntu 18.04

https://mirrors.tuna.tsinghua.edu.cn/ubuntu-cloud-images/bionic/current/bionic-server-cloudimg-amd64-vagrant.box

CentOS 7

https://mirrors.ustc.edu.cn/centos-cloud/centos/7/vagrant/x86_64/images/CentOS-7.box

使用举例

#启动一个 Ubuntu 18.04 的虚拟机
vagrant init ubuntu-bionic https://mirrors.tuna.tsinghua.edu.cn/ubuntu-cloud-images/bionic/current/bionic-server-cloudimg-amd64-vagrant.box

#启动一个 CentOS 7 的虚拟机
vagrant init centos7 https://mirrors.ustc.edu.cn/centos-cloud/centos/7/vagrant/x86_64/images/CentOS-7.box

Vagrantfile常用配置说明

  • 网络配置
    Private network(私有网络)
    优点:可以使用一个固定IP连接虚拟机
    缺点:其他团队成员不能访问你的虚拟机
# 固定IP
config.vm.network "private_network", ip: "192.168.50.4" 

#设置动态IP
#config.vm.network "private_network", type: "dhcp"
  • 端口转发
  • 虚拟机参数配置
  • 共享目录

Vagrantfile示例

  • demo 1
# -*- 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起始配置块
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.
  #定义box名称
  config.vm.box = "ubuntu18"
  #定义虚拟机名字
  config.vm.hostname = "rtm"
  #设置磁盘容量,需要安装vagrant-disksize插件
  config.disksize.size = "80GB"

  #---------------------SSH相关配置--------------------------------------------------------
  # config.ssh.username = "vagrant"   #设置默认ssh用户(默认用户是vagrant)
  # config.ssh.password = "vagrant"   #设置默认ssh密码(默认密码是vagrant)
  # config.ssh.port = 22              #设置ssh端口

  # 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"
  # ---------------------基于virtualbox的一些配置--------------------------------------------
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "8192"
    # 在virtualbox中显示的名字
    vb.name = "rtm"
    # 指定虚拟机内核数
    vb.cpus = 2
  end

  # 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 "../data", "/vagrant_data"

  # 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 machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # 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
  # Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL
end
  • 循环创建3个node
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  (1..3).each do |i|
    config.vm.define "node-#{i}" do |node|
      # 使用的box
      node.vm.box = "ubuntu/1804"
      # 对应的boxw文件路径
      node.vm.box_url = "file:///xxx.box"
      # 创建的虚拟机的hostname
      node.vm.hostname = "node#{i}"
      # 设置虚拟机的IP
      node.vm.network "private_network", ip: "192.168.56.#{i}"

      node.vm.provider "virtualbox" do |v|
        v.name = "ubuntu#{i}"
        v.cpus = 2
        v.memory = 2048
        # Vagrant exposes a way to call any command against VBoxManage just prior to booting the machine
        # v.customize ["modifyvm", :id, "--vram", 32]
        v.gui = false
      end
    end
end

vagrant打包

  • 查看virtualbox中虚拟机的名字

找到virtualbox可执行文件虚拟机所在得目录, 然后执行命令vboxmanage list vms, 列出所有的虚拟机
image.png

  • 打包

打包命令的各个参数说明

$ vagrant package --help
Usage: vagrant package [options] [name|id]

Options:

        --base NAME                  Name of a VM in VirtualBox to package as a base box (VirtualBox Only)
        --output NAME                Name of the file to output
        --include FILE,FILE..        Comma separated additional files to package with the box
        --vagrantfile FILE           Vagrantfile to package with the box
        --[no-]color                 Enable or disable color output
        --machine-readable           Enable machine readable output
    -v, --version                    Display Vagrant version
        --debug                      Enable debug output
        --timestamp                  Enable timestamps on log output
        --debug-timestamp            Enable debug output with timestamps
        --no-tty                     Enable non-interactive output
    -h, --help                       Print this help

执行打包命令

vagrant package --base rtm --vagrantfile Vagrantfile --output rtm.v1.box
  • 打包成功

image.png

使用自己打包的vagrant box

  • 将box安装到vagrant容器
vagrant box add -name [虚拟机名称] [box放置的位置]
vagrant box add -name rtm ./rtm.v1.box
  • 本地新建一个文件夹rtm-v1,然后进行该目录
mkdir rtm-v1 &&  cd rtm-v1

初始化上面添加的box

vagrant init rtm
  • 在本地Vagrantfile中修改配置

vi Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|

  config.vm.box = "rtm"
  config.vm.hostname = "rtm-v1"

  # using a specific IP.
  config.vm.network "private_network", ip: "192.168.33.88"

  config.ssh.username = "vagrant"
  config.ssh.password = "vagrant"
  
  config.vm.provider "virtualbox" do |vb|
    # Display the VirtualBox GUI when booting the machine
    # vb.gui = true
  
    # Customize the amount of memory on the VM:
    vb.memory = "8192"
    vb.name = "rtm-v1"
    vb.cpus = 2
  end
 
end
  • 启动即可
vagrant up

vagrant 用户安装 ssh 公钥

vagrant 通过 ssh 与虚拟机交互,我们需要从 github 下载公钥安装到 vagrant 用户

mkdir -p /home/vagrant/.ssh
chmod 0700 /home/vagrant/.ssh
wget --no-check-certificate 
    https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub 
    -O /home/vagrant/.ssh/authorized_keys
chmod 0600 /home/vagrant/.ssh/authorized_keys
chown -R vagrant /home/vagrant/.ssh

设置 vagrant 用户无密码运行 sudo

需 vagrant 用户需自由运行 sudo 命令而不用输入密码

sudo visudo -f /etc/sudoers.d/vagrant

添加下面内容到文件

vagrant ALL=(ALL) NOPASSWD:ALL

这样在使用 sudo 时就不必在输入密码了 可以通过下面命令确认配置是否生效

sudo pwd

2.3 创建 Vagrant Box

  1. 查询虚拟机名称,VBoxManage list vms
  2. 指定虚拟机名称来创建 Box,vagrant package --base ubuntu-18.04 --output ubuntu.box
  3. 添加创建的Box到Vagrant环境中,vagrant box add ubuntu-18.04 ubuntu.box
  4. 初始化运行环境并设置Vagrantfilevagrant init ubuntu-18.04
  5. 使用Vagrant运行虚拟机,vagrant up

征服诱人的Vagrant!
Vagrant使用国内镜像安装插件和box镜像
Vagrant - 常用命令总结
https://www.jianshu.com/p/9ff...


alits
7 声望0 粉丝

引用和评论

0 条评论