今天分享工具vagrant(https://www.vagrantup.com/)
vagrant是什么?使用场景?如何安装?如何使用?今天围绕这四个问题简单说一下
vagrant是什么?
vagrant是一个自动化管理虚拟机的实用的工具,可以快速创建一个或多个虚拟机及虚拟机中的系统,系统中的镜像都可以实现。开发语言是用ruby语言写的。适用于windows、Linux、macOS系统。节省了搭建的时间。
vagrant的使用场景?
vagrant的使用场景有很多,首先是虚拟机的搭建、虚拟机镜像搭建、其次是自动化部署。
vagrant如何安装?
首先需要安装一项vagrant,安装步骤直接区官网进行下载就行,根据需要不同的系统版本下载相应的vagrant,其中Windows下载AMD64的就行安装后可以通过vagrant -v 命令进行测试是否安装成功。
vagrant的神奇离不开两个,一个是(Install Vagrant VMware Utility)监听9922端口,另一个是(VMware provider plugin);其中Utility是一个单独的服务,安装的话可以通过vagrant的官网,具体如下:
在vagrant下载页面找到如图所示,并点击EXPlore:
可以看到关于Utility相关的信息,点击相应的版本进行安装即可。(!!!需要注意的是macOS-ARM64苹果本系统,只能手动安装。官网不提供)
因为Utility这个服务有监听9922端口,所以可以通过查看监听端口看是否安装:
sudo netstat -an | grep 9922
tcp4 0 0 127.0.0.1.9922 *.* LISTEN
安装VMWARE provider plugin可以使用国内站点进行:
#vagrant plugin install vagrant-vmware-desktop --plugin-clean-sources --plugin-source https://gems.ruby-china.com
国外站点:
#vagrant plugin install vagrant-vmware-desktop
验证:vagrant plugin list
最后如果验证都没有问题,那vagrant就安装好了。
vagrant如何安装?
1.创建一个文件夹vagrant-docker,进入文件夹后创建一个Vagrantfile文件和一个settings.yaml文件。其中Vagrantfile文件如下:
# -*- mode: ruby -*-
# vi: set ft=ruby :
## VARS =begin
# 变量文件(加载yaml文件模块,读取变量)
require "yaml"
settings = YAML.load_file "settings.yaml"
# 默认 PROVIDER
VAGRANT_DEFAULT_PROVIDER = "vmware_desktop"
# 虚拟机 IP
VB_IP = settings["network"]["control_ip"]
# 虚拟机 NAME
VB_NAME = settings["nodes"]["control"]["name"]
# 虚拟机 DIRECTORY
VMWARE_DIRECTORY = settings["vmware"]["directory"] + "/VM-" + VB_NAME
## VARS =end
## Configuration Version =begin vagrant的配置版本,这里选择第二个版本
Vagrant.configure("2") do |config|
## This configures what box the machine will be brought up against
# The value here should be the name of an installed box or a shorthand name of a box in HashiCorp's Vagrant Cloud;判断系统的架构
if `uname -m`.strip == "arm64"
config.vm.box = settings["vmware"]["box"]["arm64"]
APT_MIRROR = settings["software"]["apt_mirror"]["arm64"]
GUEST_OS = "arm-ubuntu-64"
else
config.vm.box = settings["vmware"]["box"]["amd64"]
APT_MIRROR = settings["software"]["apt_mirror"]["amd64"]
GUEST_OS = "ubuntu-64"
end
## If true, Vagrant will check for updates to the configured box on every vagrant up
# If an update is found, Vagrant will tell the user 系统镜像是否更新
config.vm.box_check_update = true
## Configures synced folders on the machine,
# so that folders on your host machine can be synced to and from the guest machine 当前所在目录共享到虚拟机这个不需要所以关闭
config.vm.synced_folder ".", "/vagrant", disabled: true
## Machine Settings =begin 虚拟机配置
config.vm.define VB_NAME do |vm_1|
## Configures networks on the machine 网络配置,创建成功后会新增加一块网卡
vm_1.vm.network :private_network, ip: VB_IP
## The hostname the machine should have
# If set to a string, the hostname will be set on boot
# If set, Vagrant will update /etc/hosts on the guest with the configured hostname 主机名称
vm_1.vm.hostname = VB_NAME
## Provider settings =begin
vm_1.vm.provider VAGRANT_DEFAULT_PROVIDER do |v|
## HGFS is functional within the guest
# This defaults to detected capability of the guest 共享默认不启用
v.functional_hgfs = false
## Launch guest with a GUI
# This defaults to false 默认可以看到变量
v.gui = true
## Path for storing VMware clones
# This defaults to ./.vagrant 默认是否克隆,目录在哪
v.clone_directory = VMWARE_DIRECTORY
## Use linked clones instead of full copy clones
v.linked_clone = true
## VMX key/value pairs to set or unset
# If the value is nit, the key will be deleted vm虚拟机的值
v.vmx["displayName"] = VB_NAME
v.vmx["memsize"] = settings["nodes"]["control"]["memory"]
v.vmx["numvcpus"] = settings["nodes"]["control"]["cpu"]
v.vmx["annotation"] = settings["nodes"]["annotation"]
v.vmx["guestOS"] = GUEST_OS
v.vmx["cpuid.coresPerSocket"] = "1"
end
## Provider settings =end
end
## Machine Settings =end
## The Vagrant Shell provisioner =begin
# allows you to upload and execute a script within the guest machine 在vm虚拟机中做了什么事情,修改镜像仓库
config.vm.provision "shell",
env: {
"DNS_SERVERS" => settings["network"]["dns_servers"].join(" "),
"APT_MIRROR" => APT_MIRROR
},
inline: <<-SHELL
# dns
sed -i /etc/systemd/resolved.conf \
-e '/^DNS=/s/=.*/='"${DNS_SERVERS}"'/'
systemctl restart systemd-resolved.service
ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
# apt_mirror
if ! curl --connect-timeout 2 google.com &>/dev/null; then
# C. 国内
CODE_NAME=$(lsb_release -cs)
COMPONENT="main restricted universe multiverse"
tee /etc/apt/sources.list >/dev/null<<-EOF
deb ${APT_MIRROR} ${CODE_NAME} ${COMPONENT}
deb ${APT_MIRROR} ${CODE_NAME}-updates ${COMPONENT}
deb ${APT_MIRROR} ${CODE_NAME}-backports ${COMPONENT}
deb ${APT_MIRROR} ${CODE_NAME}-security ${COMPONENT}
EOF
fi
# reset_tty1
if pgrep -l agetty &>/dev/null; then
kill -9 $(pgrep -l agetty | awk '{print $1}')
fi
SHELL
end
## Configuration Version =end
settings.yaml文件其中最重要的是vmware的安装位置,具体如下:
vmware:
# 1. 虚拟机存放位置
directory: F:\xxx
box:
amd64: generic/ubuntu2004
arm64: tknerr/ubuntu2004-desktop
software:
apt_mirror:
amd64: 个人喜欢的远程仓库地址/ubuntu
arm64: 个人喜欢的远程仓库地址/ubuntu-ports
nodes:
control:
name: Docker
cpu: 2
memory: 2048
annotation: "- OS: Ubuntu 20.04|0A - USERNAME: vagrant|0A - PASSWORD: vagrant|0A- SOFTWARE: docker"
network:
control_ip: 192.168.8.7
dns_servers:
- 114.114.114.114
- 8.8.8.8
- 8.8.4.4
最后,在vagrant-docker 文件夹中 操作vagrant up 命令即可创建。
常用命令
vagrant up 创建虚拟机,启动vagrant
vagrant ssh 链接虚拟机
vagrant halt 虚拟机关机