• vagrant学习指南


    一、vagrant介绍

    Vagrant是一种用于在单个工作流中构建和管理虚拟机环境的工具。
    Vagrant提供了基于行业标准技术构建的易于配置、可复制和可移植的工作环境,并由一个统一的工作流控制,以帮助最大限度地提高您和您的团队的生产力和灵活性。vagrant管理的虚拟机是在VirtualBox、VMware、AWS或任何其他提供商的基础上配置的。
    特点:
      高效方便地创建虚拟化环境,以便进行开发、测试机运维人员进行相关测试;
      使用预定义声明式配置创建虚拟机,不会牺牲其他使用的工具性能(如:编辑器,浏览器,调试器等);
      快捷复制虚拟机环境;
      可以与虚拟化软件多个版本(如virtualbox的多个版本)兼容运行;

    Vagrant vs. CLI Tools
      VirtualBox和VMware等虚拟化软件使用自带的CLI tools,用于管理其平台上机器的生命周期,并进行创建自动化实用程序。Vagrant也内置了类似CLI tools的工具。这些CLI工具和Vagrant之间的区别在于,Vagrant以多种方式构建在这些实用程序之上,同时仍然提供一致的工作流。

    Vagrant vs. Terraform
      Vagrant是一种专注于管理开发环境的工具,Terraform是一种用于构建基础设施的工具。
      Terraform可以描述本地或远程存在的复杂基础设施集。它的重点是随着时间的推移建设和改变基础设施。虚拟机生命周期的最小方面可以在Terraform中复制,有时会导致与Vagrant的混淆。
      Vagrant提供了许多Terraform没有的更高级别的特性。Vagrant提供了同步文件夹、自动联网、HTTP隧道等功能,以简化开发环境的使用。因为Terraform专注于基础设施管理,而不是开发环境,所以这些功能超出了该项目的范围。
      Terraform的主要用途是管理云提供商(如AWS)中的远程资源。Terraform旨在管理跨越多个云提供商的超大基础设施。Vagrant主要是为最多只使用少数虚拟机的本地开发环境设计的。

    二、vagrant入门手册

     1)下载vagrant

      下载地址:https://www.vagrantup.com/downloads;

     

    2)windows平台下安装vagrant
      直接按照提示执行安装即可。

     

     3)vagrant基本操作

    Windows进入dos命令行,执行vagrant命令。

    vagrant --verson                #查看vagrant版本;
    vagrant box list                #查看虚拟机清单;
    
    ::创建一个Vagrant虚拟环境以及工程:
    f:
    mkdir vagrant_test1
    cd vagrant_test1
    vagrant init centos/7  ::初始化一个config.vm.box为centos/7 的Vagrantfile;
    
    ::下载一个centos/7.box,地址为:https://app.vagrantup.com/centos/boxes/7;本文用的virtualbox提供的box;
    vagrant box add base https://app.vagrantup.com/laravel/boxes/homestead/versions/8.0.0/providers/virtualbox.box  rem 从官网添加基础box;
    :: vagrant box add base D:virtualbox.box  rem 添加本地基础box;
    
    ::进入f:修改Vagrantfile,内容如下: 
    Vagrant.configure("2") do |config|
      config.vm.box = "centos/7"
    end
    ::事实上,在vagrant init centos/7已完成该内容修改;
    
    vagrant up ::启动虚拟机;

     

     

    vagrant status      ::查看所有虚拟机状态;
    ::vagrant status [vm-name] 
    vagrant ssh         ::访问虚拟机;
    

      

     二、vagrant命令介绍

    Usage: vagrant [options] <command> [<args>]
    
        -h, --help                       Print this help.
    
    Common commands:
         autocomplete    manages autocomplete installation on host
         box             manages boxes: installation, removal, etc.
         cloud           manages everything related to Vagrant Cloud
         destroy         stops and deletes all traces of the vagrant machine
         global-status   outputs status Vagrant environments for this user
         halt            stops the vagrant machine
         help            shows the help for a subcommand
         init            initializes a new Vagrant environment by creating a Vagrantfile
         login
         package         packages a running vagrant environment into a box
         plugin          manages plugins: install, uninstall, update, etc.
         port            displays information about guest port mappings
         powershell      connects to machine via powershell remoting
         provision       provisions the vagrant machine
         push            deploys code in this environment to a configured destination
         rdp             connects to machine via RDP
         reload          restarts vagrant machine, loads new Vagrantfile configuration
         resume          resume a suspended vagrant machine
         snapshot        manages snapshots: saving, restoring, etc.
         ssh             connects to machine via SSH
         ssh-config      outputs OpenSSH valid configuration to connect to the machine
         status          outputs status of the vagrant machine
         suspend         suspends the machine
         up              starts and provisions the vagrant environment
         upload          upload to machine via communicator
         validate        validates the Vagrantfile
         version         prints current and latest Vagrant version
         winrm           executes commands on a machine via WinRM
         winrm-config    outputs WinRM configuration to connect to the machine
    
    For help on any individual command run `vagrant COMMAND -h`
    
    Additional subcommands are available, but are either more advanced
    or not commonly used. To see all subcommands, run the command
    `vagrant list-commands`.
            --[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
    

      

    三、vagrant故障排查

    问题1:vagrant虚拟机启动时,无法使用本地box?

    f:vagrant_test1>vagrant up
    Bringing machine 'default' up with 'virtualbox' provider...
    ==> default: Box 'centos/7' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
    ==> default: Loading metadata for box 'centos/7'
    default: URL: https://vagrantcloud.com/centos/7
    ==> default: Adding box 'centos/7' (v2004.01) for provider: virtualbox
    default: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/2004.01/providers/virtualbox.box
    ==> default: Box download is resuming from prior download progress
    ^C==> default: Waiting for cleanup before exiting...

    Download redirected to host: cloud.centos.org
    default:
    ==> default: Box download was interrupted. Exiting.
    default: Calculating and comparing box checksum...
    The checksum of the downloaded box did not match the expected
    value. Please verify that you have the proper URL setup and that
    you're downloading the proper file.

    Expected: 7e83943defcb5c4e9bebbe4184cce4585c82805a15e936b01b1e893b63dee2c5
    Received: fa428b507895ac98f969896acd749680595f5ae7bca80d9563421412c8fe8cc4

    原因分析:virtualbox已经从本地添加完毕,但启动虚拟机仍不能发现该box;

    解决方法:自行使用网络资源进行启动虚拟机。

    f:vagrant_test1>vagrant up
    Bringing machine 'default' up with 'virtualbox' provider...
    ==> default: Box 'centos/7' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
    ==> default: Loading metadata for box 'centos/7'
    default: URL: https://vagrantcloud.com/centos/7
    ==> default: Adding box 'centos/7' (v2004.01) for provider: virtualbox
    default: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/2004.01/providers/virtualbox.box
    ==> default: Box download is resuming from prior download progress
    Download redirected to host: cloud.centos.org
    default:
    default: Calculating and comparing box checksum...
    ==> default: Successfully added box 'centos/7' (v2004.01) for 'virtualbox'!
    ==> default: Importing base box 'centos/7'...
    ==> default: Matching MAC address for NAT networking...
    ==> default: Checking if box 'centos/7' version '2004.01' is up to date...
    ==> default: Setting the name of the VM: vagrant_test1_default_1628102314718_11903
    ==> default: Clearing any previously set network interfaces...
    ==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    ==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
    ==> default: Booting VM...
    ==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
    ==> default: Machine booted and ready!
    ==> default: Checking for guest additions in VM...
    default: No guest additions were detected on the base box for this VM! Guest
    default: additions are required for forwarded ports, shared folders, host only
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default:
    default: This is not an error message; everything may continue to work properly,
    default: in which case you may ignore this message.
    ==> default: Rsyncing folder: /cygdrive/f/vagrant_test1/ => /vagrant

    问题2:启动多个虚拟机失败?

    f:vagrant_test2>vagrant up
    Bringing machine 'swarm-manager' up with 'virtualbox' provider...
    Bringing machine 'swarm-worker1' up with 'virtualbox' provider...
    Bringing machine 'swarm-worker2' up with 'virtualbox' provider...
    ==> swarm-manager: Importing base box 'centos/7'...
    ==> swarm-manager: Matching MAC address for NAT networking...
    ==> swarm-manager: Checking if box 'centos/7' version '2004.01' is up to date...
    ==> swarm-manager: Setting the name of the VM: vagrant_test2_swarm-manager_1628119322659_41402
    ==> swarm-manager: Fixed port collision for 22 => 2222. Now on port 2200.
    Vagrant is currently configured to create VirtualBox synced folders with
    the `SharedFoldersEnableSymlinksCreate` option enabled. If the Vagrant
    guest is not trusted, you may want to disable this option. For more
    information on this option, please refer to the VirtualBox manual:

    https://www.virtualbox.org/manual/ch04.html#sharedfolders

    This option can be disabled globally with an environment variable:

    VAGRANT_DISABLE_VBOXSYMLINKCREATE=1

    or on a per folder basis within the Vagrantfile:

    config.vm.synced_folder '/host/path', '/guest/path', SharedFoldersEnableSymlinksCreate: false
    ==> swarm-manager: Clearing any previously set network interfaces...
    ==> swarm-manager: Preparing network interfaces based on configuration...
    swarm-manager: Adapter 1: nat
    swarm-manager: Adapter 2: hostonly
    ==> swarm-manager: Forwarding ports...
    swarm-manager: 22 (guest) => 2200 (host) (adapter 1)
    ==> swarm-manager: Running 'pre-boot' VM customizations...
    ==> swarm-manager: Booting VM...
    ==> swarm-manager: Waiting for machine to boot. This may take a few minutes...
    swarm-manager: SSH address: 127.0.0.1:2200
    swarm-manager: SSH username: vagrant
    swarm-manager: SSH auth method: private key
    swarm-manager:
    swarm-manager: Vagrant insecure key detected. Vagrant will automatically replace
    swarm-manager: this with a newly generated keypair for better security.
    swarm-manager:
    swarm-manager: Inserting generated public key within guest...
    swarm-manager: Removing insecure key from the guest if it's present...
    swarm-manager: Key inserted! Disconnecting and reconnecting using new SSH key...
    ==> swarm-manager: Machine booted and ready!
    ==> swarm-manager: Checking for guest additions in VM...
    swarm-manager: No guest additions were detected on the base box for this VM! Guest
    swarm-manager: additions are required for forwarded ports, shared folders, host only
    swarm-manager: networking, and more. If SSH fails on this machine, please install
    swarm-manager: the guest additions and repackage the box to continue.
    swarm-manager:
    swarm-manager: This is not an error message; everything may continue to work properly,
    swarm-manager: in which case you may ignore this message.
    ==> swarm-manager: Setting hostname...
    ==> swarm-manager: Configuring and enabling network interfaces...
    ==> swarm-manager: Rsyncing folder: /cygdrive/f/vagrant_test2/ => /vagrant
    ==> swarm-manager: Mounting shared folders...
    swarm-manager: /home/vagrant/labs => F:/vagrant_test2/labs
    Vagrant was unable to mount VirtualBox shared folders. This is usually
    because the filesystem "vboxsf" is not available. This filesystem is
    made available via the VirtualBox Guest Additions and kernel module.
    Please verify that these guest additions are properly installed in the
    guest. This is not a bug in Vagrant and is usually caused by a faulty
    Vagrant box. For context, the command attempted was:

    mount -t vboxsf -o uid=1000,gid=1000,_netdev home_vagrant_labs /home/vagrant/labs

    The error output from the command was:

    mount: unknown filesystem type 'vboxsf'

    f:vagrant_test2>
    f:vagrant_test2>
    f:vagrant_test2>vagrant status
    Current machine states:

    swarm-manager running (virtualbox)
    swarm-worker1 not created (virtualbox)
    swarm-worker2 not created (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`.

    原因分析:vagrantfile存在问题。

    解决方法:修改vagrantfile即可。

    问题3:查看虚拟机状态失败?

    C:Userschalon>vagrant status
    A Vagrant environment or target machine is required to run this
    command. Run `vagrant init` to create a new Vagrant environment. Or,
    get an ID of a target machine from `vagrant global-status` to run
    this command on. A final option is to change to a directory with a
    Vagrantfile and to try again.

    原因分析:无vagrantfile。

    解决方法:切换至含有vagrantfile目录即可。

  • 相关阅读:
    Executors源码之线程池
    Java序列化对字段名的影响
    Spring Cloud Alibaba(二)
    Security版本冲突,老版本共用服务接入新版本服务
    记一次虚拟机崩溃事件和解决方法(CentOS7)
    vue-cli 项目构建学习笔记(Vue3)
    IDEA插件-IDE Eval Reset
    Docker的学习
    Spring Security的学习
    Spring MVC框架的设计理念
  • 原文地址:https://www.cnblogs.com/chalon/p/15098037.html
Copyright © 2020-2023  润新知