Hashicorp Vagrant 使用学习小记 2024-04-01 02:28:09 Steven Xeldax [TOC] > Vagrant 是用来管理虚拟机的,如 VirtualBox、VMware、AWS 等,主要好处是可以提供一个可配置、可移植和复用的软件环境,可以使用 shell、chef、puppet 等工具部署。所以 vagrant 不能单独使用,如果你用它来管理自己的开发环境的话,必须在自己的电脑里安装了虚拟机软件,我使用的是virtualbox。 Vagrant 提供一个命令行工具vagrant,通过这个命令行工具可以直接启动一个虚拟机,当然你需要提前定义一个 Vagrantfile 文件,这有点类似 Dockerfile 之于 docker 了。 # Hashicorp Vagrant 环境初始化 azure 选择支持嵌套虚拟化的 VM 系列。例如,Dv3 和 Ev3 系列(D系列,E系列)  查看是否支持虚拟化 cat /proc/cpuinfo | grep -i svm cat /proc/cpuinfo | grep -i vmx apt install cpu-checker kvm-ok 安装virtualbox apt-get -y install gcc make linux-headers-$(uname -r) dkms apt install virtualbox virtualbox-ext-pack VBoxManage -v  安装vargent apt install vargent ``` $ vagrant init # Tweak a Vagrant Little # Open Vagrantfile $ nano ./Vagrantfile --- Vagrant.configure("2") do |config| config.ssh.insert_key = false config.vm.define "ubuntu" do |ubuntu| ubuntu.vm.box = "ubuntu/bionic64" end config.vm.network "public_network" end --- $ vagrant up # Check Status $ vagrant status Current machine states: ubuntu running (virtualbox) # SSH To VM ( Without any hussle ) $ vagrant ssh ``` ``` # 创建一个文件夹 mkdir -p /tmp/test-vm # 创建 Vagrantfile cat >/tmp/test-vm/Vagrantfile <<EOF Vagrant.configure("2") do |config| # The most common configuration options are documented and commented below. # For a complete reference, please see the online documentation at # https://docs.vagrantup.com. # Every Vagrant development environment requires a box. You can search for # boxes at https://vagrantcloud.com/search. config.vm.box = "debian10-base" config.ssh.username = "vagrant" config.vm.synced_folder ".", "/vagrant", disabled: true #config.hostmanager.enabled = true #config.hostmanager.manage_host = false #config.hostmanager.manage_guest = true #config.hostmanager.ignore_private_ip = false config.vm.hostname = "vm-test-101" config.vm.network "private_network", ip: "192.168.168.101" config.vm.network "forwarded_port", guest: 22, host: 10122, protocol: "tcp", host_ip: "127.0.0.1" config.vm.provider "virtualbox" do |vb| vb.cpus = 2 vb.memory = "1024" end end EOF # 启动 cd /tmp/test-vm vagrant up # 测试 ssh vagrant@127.0.0.1 -p10122 ``` # Vagrant 常见命令 ``` # 添加 box $ vagrant box add new-box-name box文件地址(本地、远程) # 删除 box $ vagrant box remove box-name # 以指定的 box 初始化 $ vagrant init new-box-name # 启动 $ vagrant up # 关闭 $ vagrant halt # 重启,重新加载配置文件 $ vagrant reload # 挂起 $ vagrant suspend # 创建快照(vm 名称使用 vagrant status 查看) $ vagrant snapshot save [vm名称] [快照名称] # 查看快照列表 $ vagrant snapshot list # 还原到指定快照 $ vagrant snapshot restore [vm名称] [快照名称] # 删除快照 $ vagrant snapshot delete [快照名称] # 关闭、删除 Vagrant 创建的虚拟机资源 $ vagrant destroy default: Are you sure you want to destroy the 'default' VM? [y/N] y ==> default: Forcing shutdown of VM... ==> default: Destroying VM and associated drives... # 打包 $ vagrant package --output 自定义的包名.box # 更多详细说明 $ vagrant -h $ vagrant 命令名 -h ``` ``` Typing `vagrant` from the command line will display a list of all available commands. Be sure that you are in the same directory as the Vagrantfile when running these commands! # Creating a VM - `vagrant init` -- Initialize Vagrant with a Vagrantfile and ./.vagrant directory, using no specified base image. Before you can do vagrant up, you'll need to specify a base image in the Vagrantfile. - `vagrant init <boxpath>` -- Initialize Vagrant with a specific box. To find a box, go to the [public Vagrant box catalog](https://app.vagrantup.com/boxes/search). When you find one you like, just replace it's name with boxpath. For example, `vagrant init ubuntu/trusty64`. # Starting a VM - `vagrant up` -- starts vagrant environment (also provisions only on the FIRST vagrant up) - `vagrant resume` -- resume a suspended machine (vagrant up works just fine for this as well) - `vagrant provision` -- forces reprovisioning of the vagrant machine - `vagrant reload` -- restarts vagrant machine, loads new Vagrantfile configuration - `vagrant reload --provision` -- restart the virtual machine and force provisioning # Getting into a VM - `vagrant ssh` -- connects to machine via SSH - `vagrant ssh <boxname>` -- If you give your box a name in your Vagrantfile, you can ssh into it with boxname. Works from any directory. # Stopping a VM - `vagrant halt` -- stops the vagrant machine - `vagrant suspend` -- suspends a virtual machine (remembers state) # Cleaning Up a VM - `vagrant destroy` -- stops and deletes all traces of the vagrant machine - `vagrant destroy -f` -- same as above, without confirmation # Boxes - `vagrant box list` -- see a list of all installed boxes on your computer - `vagrant box add <name> <url>` -- download a box image to your computer - `vagrant box outdated` -- check for updates vagrant box update - `vagrant box remove <name>` -- deletes a box from the machine - `vagrant package` -- packages a running virtualbox env in a reusable box # Saving Progress -`vagrant snapshot save [options] [vm-name] <name>` -- vm-name is often `default`. Allows us to save so that we can rollback at a later time # Tips - `vagrant -v` -- get the vagrant version - `vagrant status` -- outputs status of the vagrant machine - `vagrant global-status` -- outputs status of all vagrant machines - `vagrant global-status --prune` -- same as above, but prunes invalid entries - `vagrant provision --debug` -- use the debug flag to increase the verbosity of the output - `vagrant push` -- yes, vagrant can be configured to [deploy code](http://docs.vagrantup.com/v2/push/index.html)! - `vagrant up --provision | tee provision.log` -- Runs `vagrant up`, forces provisioning and logs all output to a file # Plugins - [vagrant-hostsupdater](https://github.com/cogitatio/vagrant-hostsupdater) : `$ vagrant plugin install vagrant-hostsupdater` to update your `/etc/hosts` file automatically each time you start/stop your vagrant box. # Notes - If you are using [VVV](https://github.com/varying-vagrant-vagrants/vvv/), you can enable xdebug by running `vagrant ssh` and then `xdebug_on` from the virtual machine's CLI. ``` # VagrantFile 常用配置 ``` Vagrant.configure("2") do |config| config.ssh.insert_key = false config.vm.box = "mynewbox" config.vm.network "public_network", use_dhcp_assigned_default_route: true,bridge: "eth0" config.vm.hostname = "vm-test-101" end ``` ``` Vagrant.configure("2") do |config| config.vm.define "vm01" do |config| config.ssh.insert_key = false config.vm.box = "mynewbox" config.vm.network "public_network", use_dhcp_assigned_default_route: true,bridge: "eth0" config.vm.hostname = "vm-test-01" end end ``` # 参考文章 https://github.com/whorusq/learning-vagrant https://blog.csdn.net/m0_50546016/article/details/119176009 https://jimmysong.io/blog/vagrant-intro/ https://developer.hashicorp.com/vagrant/docs/cli