vagrant
是一个虚拟机管理工具 (虚拟机编排工具),使用方式有点类似于 docker-compose
,这个工具的优势就是可以把手动安装虚拟机这个操作转换成通过代码来控制虚拟机
有点类似于使用 dockerfile
来构建 docker 镜像的感觉。
常用命令
命令 |
作用 |
vagrant init |
生成 vagrantfile 文件 |
vagrant up |
启动虚拟机 |
vagent halt |
关闭虚拟机 |
vagrant box list |
查看安装的 box(虚拟机镜像) |
vagrant box add centos/7 |
安装 box(虚拟机镜像) |
vagrant ssh (hostname) |
通过 ssh 连接 |
常用 vagrantfile
批量创建虚拟机
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
servers = { :client => '172.16.2.10', :master1 => '172.16.2.11', :master2 => '172.16.2.12', :master3 => '172.16.2.13', :worker1 => '172.16.2.21', :worker2 => '172.16.2.22' }
Vagrant.configure("2") do |config| config.vm.box = "ubuntu/focal64" config.vm.box_check_update = false
config.hostmanager.enabled = true config.hostmanager.manage_host = true config.hostmanager.manage_guest = true config.hostmanager.ignore_private_ip = false config.hostmanager.include_offline = true
servers.each do |server_name, server_ip| config.vm.define server_name do |server_config| server_config.vm.hostname = "#{server_name.to_s}" server_config.vm.network :private_network, ip: server_ip server_config.vm.provider "virtualbox" do |vb| vb.name = server_name.to_s vb.memory = "4096" vb.cpus = 4 end end end end
|
循环创建虚拟机
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| Vagrant.configure("2") do |config|
(1..3).each do |i| config.vm.define "node#{i}" do |node| node.vm.box = "ubuntu/trusty64" node.vm.hostname="node#{i}" node.vm.network "private_network", ip: "192.168.59.#{i}" node.vm.provider "virtualbox" do |v| v.name = "node#{i}" v.memory = 2048 v.cpus = 1 end node.vm.provision "shell", inline: <<-SHELL wget -qO- https://get.docker.com/ | sed 's/docker-engine/docker-engine=1.11.0-0~trusty/' | sh usermod -aG docker vagrant SHELL end end end
|
多次定义安装虚拟机
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
Vagrant.configure("2") do |config| config.vm.define "master" do |master| master.vm.box = "centos/7" master.vm.provider "virtualbox" master.vm.network "private_network", ip: "10.10.0.10" master.vm.synced_folder "/home/iuxt/code/system-deployment/configuration/puppet/modules", "/etc/puppetlabs/code/modules",create: true,owner: "root", group: "root",mount_options:["dmode=775","fmode=644"] master.vm.synced_folder "/home/iuxt/code/system-deployment/configuration/puppet/environments/production/manifests", "/etc/puppetlabs/code/environments/production/manifests",create: true,owner: "root", group: "root",mount_options:["dmode=775","fmode=644"] master.vm.hostname = "puppet-master" master.vm.provider "virtualbox" do |vb| vb.gui = false vb.memory = "4096" vb.cpus = "2" end master.vm.provision "shell", inline: <<-SHELL sudo rpm -Uvh https://yum.puppet.com/puppet6-release-el-7.noarch.rpm yum install puppetserver -y echo "127.0.0.1 puppet" >> /etc/hosts SHELL end
config.vm.define "agent" do |agent| agent.vm.box = "centos/7" agent.vm.provider "virtualbox" agent.vm.network "private_network", ip: "10.10.0.11" agent.vm.hostname = "puppet-agent" agent.vm.provider "virtualbox" do |vb| vb.gui = false vb.memory = "4096" vb.cpus = "2" end agent.vm.provision "shell", inline: <<-SHELL sudo rpm -Uvh https://yum.puppet.com/puppet6-release-el-7.noarch.rpm yum install puppet -y echo "10.0.0.10 puppet" >> /etc/hosts SHELL end end
|
常用插件
自动管理 hosts
插件官网:https://github.com/devopsgroup-io/vagrant-hostmanager
安装插件
1
| vagrant plugin install vagrant-hostmanager
|
启用
1 2 3 4 5 6 7 8 9 10 11 12
| Vagrant.configure("2") do |config| config.hostmanager.enabled = true config.hostmanager.manage_host = true config.hostmanager.manage_guest = true config.hostmanager.ignore_private_ip = false config.hostmanager.include_offline = true config.vm.define 'example-box' do |node| node.vm.hostname = 'example-box-hostname' node.vm.network :private_network, ip: '192.168.42.42' node.hostmanager.aliases = %w(example-box.localdomain example-box-alias) end end
|
挂载本机目录到虚拟机
1 2 3 4 5 6
| Vagrant.configure("2") do |config| config.vm.define "master" do |master| master.vm.box = "centos/7" master.vm.provider "virtualbox" master.vm.network "private_network", ip: "10.10.0.10" master.vm.synced_folder "/home/iuxt/code/system-deployment/configuration/puppet/modules", "/etc/puppetlabs/code/modules",create: true,owner: "root", group: "root",mount_options:["dmode=775","fmode=644"]
|
这里可能会提示:mount: unknown filesystem type 'vboxsf'
,这是因为虚拟机没有自带 vbox guest agent
, 以前的 centos 是 vagrant 自己打包的,都是带 vbox guest agent 的,但是现在 vagrant 下载 centos 的 box 的时候会重定向到 centos 官网,这个 box 是不带 vbox guest agent 的,解决办法有以下:
- 更换镜像为
generic/centos7
, 镜像官网:https://roboxes.org/
- 自己安装好再重新打包 box
- 安装
vagrant plugin install vagrant-vbguest
,这个插件可以自动挂载 iso,然后安装。
- 更换增加
type
参数,使用 nfs 或 smb 来和虚拟机共享,需要主机安装 nfs 或 smb 服务。