装置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连贯虚拟机
毛病:其余团队成员不能拜访你的虚拟机
# 固定IPconfig.vm.network "private_network", ip: "192.168.50.4" #设置动静IP#config.vm.network "private_network", type: "dhcp"
- 端口转发
- 虚拟机参数配置
- 共享目录
Vagrantfile示例
# -*- 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 # SHELLend
驯服迷人的Vagrant!
Vagrant应用国内镜像装置插件和box镜像