Vagrant 快速入门

用户头像
FeiLong
关注
发布于: 2020 年 07 月 12 日
Vagrant 快速入门

使用 Vagrant 可以管理本地虚拟机。

环境

  • 操作系统:Microsoft Windows 10 企业版 LTSC

  • 版本:10.0.17763

安装

Vagrant - 用于管理虚拟机生命周期的命令行实用程序。

choco install vagrant -y

使用

  • 入门

mkdir vgtest
cd vgtest
# 启动一个 CentOS 7
vagrant init centos/7
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.
vagrant up
Bringing machine 'default' up with 'hyperv' provider...
==> default: Verifying Hyper-V is enabled...
==> default: Verifying Hyper-V is accessible...
==> default: Importing a Hyper-V instance
default: Creating and registering the VM...
default: Successfully imported VM
default: Configuring the VM...
==> default: Starting the machine...
==> default: Waiting for the machine to report its IP address...
default: Timeout: 120 seconds
default: IP: 192.168.227.214
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 192.168.227.214:22
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!
# ssh 连接
vagrant ssh



  • 自动配置

bootstrap.sh 文件内容如下:

#!/usr/bin/env bash
yum update -y
yum install -y vim telnet



Vagrantfile 文件内容如下:

Vagrant.configure("2") do |config|
config.vm.box = "centos/7"
config.vm.provision :shell, path: "bootstrap.sh"
end



# 校验配置
vagrant validate
Vagrantfile validated successfully.
# 重启并自动配置
vagrant reload --provision



  • 清理环境



# 查看状态
vagrant status
Current machine states:
default running (hyperv)
# 清理
vagrant destroy
default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Stopping the machine...
==> default: Deleting the machine...



参考



  • https://www.vagrantup.com/docs/index

  • https://app.vagrantup.com/boxes/search



发布于: 2020 年 07 月 12 日 阅读数: 40
用户头像

FeiLong

关注

还未添加个人签名 2018.09.17 加入

还未添加个人简介

评论

发布
暂无评论
Vagrant 快速入门