安装Vagrant
vagrant是HashiCorp的虚拟机管理软件,可以管理virtualbox,VMware,docker等镜像, 在vagrant中统一被称之为box。
对于搭建和配置集群开发环境很方便
下载vagrant
下载地址:https://www.vagrantup.com/downloads.html
文档地址: https://www.vagrantup.com/docs/
根据不同的操作系统可以选择对应的版本下载
下载完成之后直接点击安装即可,安装完成之后检查版本信息
$ vagrant -v
Vagrant 2.2.7
$ vagrant box list
There are no installed boxes! Use `vagrant box add` to add some.
下载更新powershell
执行添加centos命令,我目前使用的是Windows7,可能出现powershell版本不够,报一个错误
$ vagrant box add centos/7
Vagrant failed to initialize at a very early stage:
The version of powershell currently installed on this host is less than
the required minimum version. Please upgrade the installed version of
powershell to the minimum required version and run the command again.
Installed version: 2
Minimum required version: 3
查看powershell版本命令:$psversiontable
> $psversiontable
Name Value
---- -----
CLRVersion 2.0.50727.5420
BuildVersion 6.1.7601.17514
PSVersion 2.0
WSManStackVersion 2.0
PSCompatibleVersions {1.0, 2.0}
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.1
下载安装新版powershell:
下载地址:https://docs.microsoft.com/en-us/powershell/scripting/wmf/setup/install-configure?view=powershell-7
根据【安装说明】信息中找到对应的操作系统版本的安装文件,如Windows7 SP1 64位对应的是:Win7AndW2K8R2-KB3191566-x64.ZIP
注意:如果安装的时候长时间卡住不动,可能需要修复下更新程序:
参考地址:https://docs.microsoft.com/zh-cn/windows/deployment/update/windows-update-resources
安装后:
> $psversiontable
Name Value
---- -----
PSVersion 5.1.14409.1005
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14409.1005
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Vagrant基本使用
Vagrant提供很多已经打包好的box仓库,仓库搜索地址:https://app.vagrantup.com/boxes/search
找到自己需要的box,执行命令下载到本地,根据box的信息,可以有几种格式可以选择,VMware或者virtualbox等
$ vagrant box add centos/7
==> box: Loading metadata for box 'centos/7'
box: URL: https://vagrantcloud.com/centos/7
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.
1) hyperv
2) libvirt
3) virtualbox
4) vmware_desktop
Enter your choice: 3
==> box: Adding box 'centos/7' (v1905.1) for provider: virtualbox
box: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/1905.1/providers/virtualbox.box
box: Download redirected to host: cloud.centos.org
下载速度比较慢,可以把地址复制下来,然后使用下载工具下载
手动下载后添加
$ vagrant box add centos/7 ./DownloadBoxes/centos7.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos/7' (v0) for provider:
box: Unpacking necessary files from: file://E:/vagrant/DownloadBoxes/centos7.box
box:
==> box: Successfully added box 'centos/7' (v0) for 'virtualbox'!
初始化虚拟机,启动虚拟机
vagrant init centos/7
vagrant up
如果没有安装virtualbox等provider,启动会提示
No usable default provider could be found for your system.
安装virtualbox
下载地址:https://www.virtualbox.org/wiki/Downloads
安装好virtualbox之后,再次运行
vagrant up
vagrant ssh
登录系统,可以执行其他操作了
如果直接使用virtualbox登录,默认账户:vagrant/vagrant
Vagrantfile配置
Vagrant最主要的配置文件就是Vagrantfile,配置虚拟机相关的属性,网络,以及脚本执行等操作。
Vagrant的配置语法是Ruby的,每个项目都需要有一个Vagrantfile,在执行vagrant init的目录下可以找到该文件,可以复制此文件直接初始化vagrant项目。
Vagrant.configure("2") do |config|
# box源
config.vm.box = "centos/7"
# 配置主机名
config.vm.hostname = "test.centos.local"
# 配置文件夹同步
# config.vm.synced_folder "../data", "/vagrant_data"
# 端口映射
# config.vm.network "forwarded_port", guest: 80, host: 8080
# 配置私有网络,如果是private_network,虚拟机网络和主机网络隔离, 外部不能ping通虚拟机
# config.vm.network "private_network", ip: "192.168.1.11"
# 桥接模式,主机之外可以访问到
config.vm.network "public_network", ip: "192.168.31.101"
config.vm.provider "virtualbox" do |vb|
# vb.gui = true
# 配置虚拟机名字,不配置的话,自动会产生虚拟机名字,配置虚拟机内存大小
vb.name = "test-vm1"
vb.memory = "1024"
end
# 配置初始化需要执行的脚本,比如更新软件包,安装常用软件等
config.vm.provision "shell", inline: <<-SHELL
yum update
yum install -y gcc
yum install -y net-tools.x86_64
SHELL
end
网友评论