Please enable Javascript to view the contents

Vagrant 入门使用案列

 ·  🕒 3 分钟  ·  ✍️ 加文 · 👀... 阅读

Vagrant

概述

Ruby 语言开发的多虚拟机(Multiple Hypervisors)自动化管理工具, 可以用来快速构建虚拟开发环境(封装一个 Linux 的开发环境,分发给团队成员)
支持 VMwareFusion、VirtualBox、HyperV、Libvirt等虚拟化技术, Vagrant与VMware的集成组件是收费的,而与VirtualBox、Libvirt、HyperV 集成是免费的
Vagrant 会把 Vagrantfile 文件所在目录下的所有文件同步复制到虚拟机中的 /vagrant 目录下

windows 端盒子box目录: C:\Users\way.vagrant.d\boxes
virtual box 虚拟机默认位置: C:\Users\way\VirtualBox VMs

BOX 镜像

vagrant 官方 box 镜像仓库: https://app.vagrantup.com/boxes/search

CentOS 官方 box 地址:http://cloud.centos.org/centos/7/vagrant/x86_64/images/
Ubuntu 官方 box 地址:http://cloud-images.ubuntu.com

Ubuntu版本代号: Ubuntu(14) Trusty 、Ubuntu(16) Xenial 、Ubuntu(18) Bionic 、Ubuntu(20) Focal

vagrant
http://mirrors.ustc.edu.cn/ubuntu-cloud-images/xenial/20210219/xenial-server-cloudimg-amd64-vagrant.box
http://mirrors.ustc.edu.cn/centos-cloud/centos/7/vagrant/x86_64/images/CentOS-7-x86_64-Vagrant-2002_01.VirtualBox.box

HyperV
http://mirrors.ustc.edu.cn/centos-cloud/centos/7/vagrant/x86_64/images/CentOS-7-x86_64-Vagrant-2002_01.HyperV.box

快速入门

部署环境 Windows 10

  1. virtual box 虚拟化工具及扩展包安装
    https://download.virtualbox.org/virtualbox/6.1.18/VirtualBox-6.1.18-142142-Win.exe
    https://download.virtualbox.org/virtualbox/6.1.18/Oracle_VM_VirtualBox_Extension_Pack-6.1.18.vbox-extpack

  2. 下载安装 Vagrant, 傻瓜式一步步安装
    https://releases.hashicorp.com/vagrant/2.2.14/vagrant_2.2.14_x86_64.msi

  3. 添加 vagrant 镜像
    vagrant box add centos7_virtualbox http://mirrors.ustc.edu.cn/centos-cloud/centos/7/vagrant/x86_64/images/CentOS-7-x86_64-Vagrant-2002_01.VirtualBox.box

vagrant box add ubuntu18 https://mirrors.ustc.edu.cn/ubuntu-cloud-images/xenial/current/xenial-server-cloudimg-amd64-vagrant.box

或先下载后离线添加vagrant镜像
vagrant box add ubuntu18 ./boxs/xenial-server-cloudimg-amd64-vagrant.box

  1. 查看本地 box 镜像列表
    vagrant box list

  2. 创建一个空目录, 初始化虚拟机, 生成 VagrantFile 文件(可自定义修改)
    mkdir demo02 && cd demo02
    vagrant init centos7

  3. 启动, 默认选项 –provider=virtualbox
    vagrant up demo02

  4. ssh登录
    虚机默认ssh 账号vagrant 密码为空,私钥认证
    vagrant ssh demo02

管理命令

# 进入项目目录执行
vagrant version
vagrant box list         # 查看本地镜像列表
vagrant init demo01      # 初始化环境,生成一个 Vagrantfile 的配置文件; 或手动配置
vagrant package          # 打包当前虚拟机配置环境,生成 package.box
vagrant ssh node1        # ssh 连接虚拟机 node1
vagrant validate Vagrantfile
vagrant up|halt|suspend|resume|reload|destroy [vm_name|vm_id]    # 开机、关机、暂停、恢复、重启、删除 vm
vagrant snapshot save|list|restore|delete node1 snap01  # 快照创建、查看、恢复、删除

Vagrantfile

Vagrantfile 主要配置项:
config.vm.define
config.vm.box 虚拟机镜像
config.vm.network 虚拟机网络配置, 虚拟化virtualbox时, 有三种网络模式: nat 模式(默认), 桥接网络(公网), 仅主机模式(私网)
config.vm.provider 虚拟化技术, 如 virtualbox(推荐)、hyperv等
config.vm.provision

单虚拟机模式

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

Vagrant.configure("2") do |config|
  # 虚机名字或ID
  config.vm.define "demo02"
  config.vm.box = "centos7_virtualbox"
  config.vm.box_check_update = false
  # config.vm.hostname = "node1"   # 系统主机名
  # config.ssh.password = "password"
  # config.ssh.insert_key = false
  
  # nat模式
  # config.vm.network "private_network"
  # config.vm.network "private_network", ip: "192.168.33.10"
  # 端口映射
  # config.vm.network "forwarded_port", guest: 80, host: 8080
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
  
  # 桥模式
  # config.vm.network "public_network"
  config.vm.network "public_network", ip: "192.168.31.188"

  # 文件夹同步
  # config.vm.synced_folder "../data", "/vagrant_data"
  
  config.vm.provider "virtualbox" do |vb|
    # Display the VirtualBox GUI when booting the machine
    vb.gui = false
    # Customize the amount of memory on the VM:
    vb.memory = "1024"
    # virtualbox 界面显示的虚机名字
    vb.name = "demo02"
  end

  # config.vm.provision "shell", inline: <<-SHELL
  #   yum install -y epel-release
  #   yum install -y nginx
  #   sudo systemctl start nginx
  # SHELL
end

多虚拟机模式

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

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

  config.vm.box_check_update = false
  config.vm.provider "virtualbox" do |vb|
    # Display the VirtualBox GUI when booting the machine
    vb.gui = false
    # Customize the amount of memory on the VM:
    vb.memory = "1024"
  end

  config.vm.define "node1" do |n1|
    n1.vm.box = "centos7_virtualbox"
    n1.vm.hostname = "node1"   # 系统主机名
    n1.vm.network "public_network"
    n1.vm.network "private_network", ip: "172.16.0.100"
  end

  config.vm.define "node2" do |n2|
    n2.vm.box = "centos7_virtualbox"
    n2.vm.hostname = "node2"   # 系统主机名
    n2.vm.network "private_network", ip: "172.16.0.200"
  end

  # config.vm.provision "shell", inline: <<-SHELL
  #   yum install -y epel-release
  #   yum install -y nginx
  #   sudo systemctl start nginx
  # SHELL

end

参考

https://www.cnblogs.com/yangjian2006/p/12403888.html
https://www.vagrantup.com/docs
https://github.com/whorusq/learning-vagrant
vm ssh登录失败参考: https://www.cnblogs.com/csliwei/p/5860005.html
vagrant 虚机网络配置
https://www.jianshu.com/p/a1bc23bc7892


加文
作者: 加文
运维工程师
版权声明:自由转载-非商用-非衍生-转载请注明出处!


目录