Vagrant Profile
- Vagrant is a Ruby-based tool for creating and deploying virtualized development environment. Vagrant easily configurable, repeatable, provide a consistent working environment for the team, to solve environmental problems such as isolation depend, to maximize production efficiency.
- Developers only need to create and configure Vagrantfile, creating the required development environment by vagrant up command. You can also share Vagrantfile file, build the same development environment Vagrant on any other machine.
Vagrant installation environment
Vagrant run, we need to rely for some specific virtualization technology, the most common are the following:
- virtualbox:
- vmware:
- libvirt: virtual technology on linux
- hyperv: win10 comes with virtualization technology
- docker: The most popular container hottest technology
1. Preparation Before Installation
Open Task Manager -> Properties -> CPU view virtualization is turned on.
If it is not turned on, turned into the bios interface CPU virtualization.
2. Install virtualbox
Select this article do virtualbox example
Download: https://www.virtualbox.org/wiki/Downloads
3. Install vagrant
Download: https://www.vagrantup.com/downloads.html
Vagrant basic commands
In the Vagrant, is achieved by the virtual machine to a packing box consistent, repeatable environment, based on the same box, the same environment can be constructed. Vagrant is also divided into two parts: one box lifecycle management, the second is the virtual machine lifecycle management.
1.box management command
PS C:\Users\wangjie> vagrant box help
Usage: vagrant box <subcommand> [<args>]
Available subcommands:
add 添加box到本地vagrant环境
list 列出本地环境中所有的box
outdated 检查并更新所有本地环境中的box
prune 清理本地环境中的box
remove 删除本地环境中指定的box
repackage 重新打包本地环境中指定的box
update 更新打包本地环境中指定的box
2. Virtual Machine Manager command
PS C:\Users\wangjie> vagrant -h
Usage: vagrant [options] <command> [<args>]
-v, --version Print the version and exit.
-h, --help Print this help.
Common commands:
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`
Virtual Machine Manager command is typically run in a project under the directory where the file Vagrantfile, commonly used commands are as follows
vagrant init [box] 当前目录创建 Vagrantfile 文件,可以指定box
vagrant up [name] 通过 Vagrantfile 文件启动虚拟机
vagrant status [name] 查看虚拟机状态
vagrant ssh [name] ssh登录虚拟机
vagrant suspend [name] 挂起虚拟机
vagrant resume [name] 恢复挂起的虚拟机
vagrant reload [name] 重启虚拟机,应用新 Vagrantfile 文件
vagrant package [name] 把一个运行的虚拟机打包成 box
vagrant halt [name] 关闭虚拟机
vagrant destroy [name] 销毁虚拟机
Vagrantfile file configuration
Vagrant command is very simple to use, the key is how to configure complex environment is reflected in the Vagrant. As can be seen from the above, Vagrant management of all the virtual machines Vagrantfile depending on the configuration file. When you run any vagrant command, Vagrant up from the current directory to find the first Vagrantfile it can find. E.g:
D:\Study\Easy_install\vagrant_demo\Vagrantfile
D:\Study\Easy_install\Vagrantfile
D:\Study\Vagrantfile
D:\Vagrantfile
Write the following to learn Vagrantfile file:
1. The simplest configuration
Vagrant.configure("2") do |config|
config.vm.box = "centos/7"
end
This is the simplest of Vagrantfile init centos / 7 created by vagrant.
- Vagrant.configure ( "2") represents the current version 1.1+ to support 2.0.x
- config.vm.box = "centos / 7" designated box
After starting the virtual machine configuration is as follows:
- CPU: 1 core
- Memory: 512M
- Network: Network Address Translation
- HDD: 40G
2. environment variable configuration
ENV["LC_ALL"] = "en_US.UTF-8"
Vagrant.configure("2") do |config|
# ...
end
3. Virtual machine configuration
- Define a virtual machine
config.vm.define "test" do |node| # test Vagrant 识别的虚拟机名称
node.vm.box = "centos/7" # node Vagrantfile 识别的节点名称
node.vm.hostname="vmtest" # 虚拟机 hostname
end
- VirtualBox configuration
node.vm.provider "virtualbox" do |vb| # 使用 VirtualBox 提供虚拟化
vb.name = "vbtest" # VirtualBox 识别的虚拟机名称
vb.memory = 2048 # 内存 2G
vb.cpus = 2 # CPU 2核
vb.gui = true # 启动图形化界面
vb.linked_clone = true # 链接克隆
end
- ssh configuration
node.ssh.username = "wangjie"
node.ssh.password = "123456"
- Network Configuration
Port forwarding: Network Address Translation
node.vm.network "forwarded_port", guest: 80, host: 8080, auto_correct: true
node.vm.usable_port_range = 8000..8999
Private network: virtualbox__intnet internal network using virtualbox
# 使用 host-only 网络
config.vm.network "private_network", ip: "192.168.99.14", name: "VirtualBox Host-Only Ethernet Adapter #2"
# 使用内部网络
config.vm.network "private_network", type: "dhcp", , virtualbox__intnet: true
Public network: bridge card
config.vm.network "public_network"
config.vm.network "public_network", use_dhcp_assigned_default_route: true
config.vm.network "public_network", bridge: [
"en1: Intel(R) Wireless-AC 9560",
"en6: Realtek Gaming GBE Family Controller",
]
- Shared directory
Support type: nfs, rsync, smb
# 第一个主机路径,可以是相对路径。第二个虚拟机路径,必须绝对路径
node.vm.synced_folder "src/", "/srv/website"
# 禁用同步
node.vm.synced_folder "src/", "/srv/website", disabled: true
node.vm.synced_folder ".", "/vagrant", disabled: true
# 设置所属用户/组
node.vm.synced_folder "src/", "/srv/website", owner: "root", group: "root"
- provision setting
File Upload
# 文件上传
node.vm.provision "file", type: "file", source: "~/.gitconfig", destination: ".gitconfig"
# 目录上传,不加尾部斜杠
node.vm.provision "file", source: "~/path/to/host/folder", destination: "$HOME/remote/newfolder"
shell command execution
# 内联脚本
node.vm.provision "bootstrap", type: "shell", inline: "echo Hello, World"
# 脚本参数
node.vm.provision "shell" do |s|
s.inline = "echo $1"
s.args = ["hello, world!"]
end
# 脚本定义
$script = <<-SCRIPT
echo I am provisioning...
date > /etc/vagrant_provisioned_at
SCRIPT
node.vm.provision "shell", inline: $script
# 外部脚本
node.vm.provision "shell", path: "script.sh"
# 普通用户执行
node.vm.provision "shell", privileged: false, path: "script.sh"
对于 provision 设置,在运行 vagrant up,vagrant provision和vagrant reload --provision 时可以应用 provision 配置。provision 默认只运行一次,可以设置标志 run: "always", 每次都运行。设置 run 为 never, 则运行 vagrant provision --provision-with [provision name]。
end
Work often used in a virtual machine, always tossing to and fro, although you can clone a virtual machine to shorten the time, but inside the virtual machine configuration there is no effective way to save. Take the time to learn about the Vagrant, will work and study required for effective virtual machine environment preserved, to avoid duplication of work, but also saves time, so that the limited time spent on meaningful things.
This article example
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.define "test" do |node|
node.vm.box = "centos/7"
node.vm.hostname="vmtest"
node.vm.network "forwarded_port", guest: 80, host: 8080, auto_correct: true
node.vm.usable_port_range = 8000..8999
node.vm.network "private_network", ip: "192.168.99.150", name: "VirtualBox Host-Only Ethernet Adapter #2"
node.vm.network "public_network"
node.vm.provider "virtualbox" do |vb|
vb.name = "vb_test"
vb.memory = 2048
vb.cpus = 2
# vb.gui = true
vb.linked_clone = true
end
node.vm.synced_folder "share", "/home/vagrant/share", disabled: true
node.vm.synced_folder ".", "/vagrant", disabled: true
node.vm.provision "file", source: "test.sh", destination: "/home/vagrant/test.sh", run: "always"
node.vm.provision "shell", inline: "bash /home/vagrant/test.sh", run: "always"
node.vm.provision "shell", path: "test.sh", run: "always"
end
end