首页 > 其他分享 >Vagrantfile - 自定义配置

Vagrantfile - 自定义配置

时间:2023-02-09 17:36:08浏览次数:67  
标签:vb network Vagrantfile 虚拟机 配置 ################# vm config 自定义

1、Vagrantfile参数说明:

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  ################# Box配置 ################# 
  config.vm.box = "debian_buster"
  config.vm.box_version = "3.4.00"
  config.vm.box_url = "https://vagrantcloud.com/debian/buster"


  ################# 网络配置 ################# 
  # 端口转发配置
  config.vm.network "forwarded_port", guest: 80, host: 8080
  config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
  
  config.vm.network "forwarded_port", guest: 2004, host: 33214, protocol: "tcp"
  config.vm.network "forwarded_port", guest: 2004, host: 33214, protocol: "udp"

  config.vm.network "forwarded_port", guest: 2004, host: 8080, auto_correct: true
  config.vm.usable_port_rage = 8000..8999
  
  config.vm.network "forwarded_port", guest: 22, host: 2222, id: "ssh", disabled: "true"
  config.vm.network "forwarded_port", guest: 22, host: 22222
  
  # NAT 网络
  # 启动时会默认起 1 个 NAT 网络,保证了虚机可以上互联网
  
  # 主机网络配置
  config.vm.network "private_network", type: "dhcp" # DHCP获取IP地址(不推荐)
  config.vm.network "private_network", ip: "192.168.56.10" # 保证了宿主机和虚机,以及虚机和虚机之间的通信
  config.vm.network "private_network", ip: "fea3:4442:82f1::c3", netmask: "48"
  
  # 桥接网络配置
  config.vm.network "public_network"
  config.vm.network "public_network", auto_config: false
  
  config.vm.network "public_network", ip: 192.168.56.100
  
  config.vm.network "public_network", bridge: "en1: Wi-Fi (AirPort)"
  config.vm.network "public_network", bridge: [
	  "en1: Wi-Fi (AirPort)",
	  "en2: Broadcom NetXtreme Gigabit Ethernet Controller",
  ]

  # config.vm.network "public_network", auto_config: false
  # config.vm.provision "shell",
  # 	  run: "always",
  # 	  inline: "ifconfig eth1 192.168.56.100 network 255.255.255.0 up"
  # config.vm.provision "shell",
  # 	  run: "always",
  # 	  inline: "ifconfig eth1 inet6 f303::100/48"

  # config.vm.network "public_network", ip: "192.168.56.100"
  # config.vm.provision "shell",
  #    run: "always",
  #    inline: "route add default gw 192.168.56.1"
  # config.vm.provision "shell",
  #    run: "always",
  #    inline: "route -A inet6 add default gw fc00::1 eth1"
  # config.vm.provision "shell",
  #    run: "always",
  #    inline: "eval `route -n | awk '{ if ($8 ==\"eth0\" && $2 != \"0.0.0.0\") print \"route del default gw \" $2; }'`"

  ################# SSH配置 ################# 
  config.ssh.username = "vagrant"
  config.ssh.password = "vagrant"
  config.ssh.insert_key = false  # 以后创建出来的虚拟机都是用统一的 insecure_private_key 来登录
  # 默认每个虚拟机会生成自己的 key

  ################# 虚拟机主机名称 ################# 
  config.vm.hostname = "deb"

  ################# 同步目录配置 ################# 
  config.vm.synced_folder ".", "/vagrant",create: true
  config.vm.synced_folder "../data", "/vagrant_data",create: true,owner: "root",group: "root"
  #第一个参数是主机上共享的目录,第二个参数是vagrant虚拟机的挂载目录,第三个参数在虚拟机里面没有挂载目录则创建挂载目录,第四个参数所属用户,第五个参数所属用户组
  
  ################# Virtualbox 配置 ################# 
  config.vm.provider "virtualbox" do |vb|
       vb.gui = false # 启动虚拟机时不启用GUI界面,节省宿主机资源
       vb.name = "Debian_11" # Virtuablbox主机名称
       vb.memory = "1024" # 虚拟机内存
       vb.cpus = "2" # 虚拟机CPU
       # 磁盘设置需要安装插件
  end
  
  ################# Provision 配置 #################  
  # 启动虚拟机运行的脚本
  config.vm.provision "shell", path: "DNS.sh"
  # 在与Vagrantfile同级的目录下创建一个`bootstrap.sh`文件
  
  config.vm.provision "shell", "echo hello-world!"
  
  config.vm.provision "shell", inline: <<-SHELL
	   apt-get update
	   apt-get install -y apache2
  SHELL
end

2、Vagrantfile配置案例 单个虚拟机配置(这个创建后的虚拟机装默认关闭防火墙、Selinux、SWAP分区以及安装curl软件包)

Vagrant.configure(2) do |config|
  config.vm.box = "centos"
  config.vm.box = "ling218"

  config.vm.network "private_network", ip: "192.168.56.10"

  config.vm.synced_folder ".", "/vagrant",create: true

  config.ssh.username = "vagrant"
  config.ssh.password = "vagrant"
  config.ssh.insert_key = false

  config.vm.provider "virtualbox" do |vb|
     vb.gui = false
     vb.name = "CentOS7"
     vb.memory = "1024"
     vb.cpus = "2" 
  end

  config.vm.provision "shell", inline: <<-SHELL
     # 安装curl软件
     yum clean all
     yum makecache fast
     yum install -y curl
     # 关闭防火墙
     systemctl stop firewalld
     systemctl disable firewalld
     # 关闭Selinux
     setenforce 0
     sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
     # 关闭swap
     swapoff -a
     sed -i 's/.*swap.*/#&/' /etc/fstab
  SHELL
end

多个虚拟机配置(执行 vagrant up 命令后将创建两个虚拟机 web和db)

Vagrant.configure(2) do |config|
  config.vm.box = "centos"
  config.vm.synced_folder ".", "/vagrant", create: true

  config.vm.define "web-server" do |web|
    web.vm.hostname = "web"
    web.vm.network "private_network", ip: "192.168.56.10"
    web.vm.provider "virtualbox" do |vb|
      vb.name = "web"
      vb.cpus = "2"
      vb.memory = "1024"
      vb.gui = false
    end

  config.vm.define "db-server" do |web|
    web.vm.hostname = "db"
    web.vm.network "private_network", ip: "192.168.56.11"
    web.vm.provider "virtualbox" do |vb|
      vb.name = "db"
      vb.cpus = "2"
      vb.memory = "1024"
      vb.gui = false
    end  

标签:vb,network,Vagrantfile,虚拟机,配置,#################,vm,config,自定义
From: https://blog.51cto.com/u_15551039/6047188

相关文章

  • nvue页面自定义字体
    addRule函数 1@原型:addRule(type,contentObject)2@参数:3type:[StringfontFace必需]协议名称,不可修改,固定值为:fontFace。4contentObject:[Ob......
  • springboot开发日记(9)——YAML配置文件
    YAML——适合用来做以数据为中心的配置文件基本语法使用缩进表示层级关系。缩进时不允许使用Tab键,只允许使用空格。(实际使用idea开发中,Tab也有效)缩进的空格数不重......
  • 如何配置Docker代理
    目录下载clash运行clash更新docker.service重启docker下载clashwgethttps://github.com/Dreamacro/clash/releases/download/v1.13.0/clash-linux-amd64-v1.13.0.gzgzi......
  • Spring45 - 自定义校验
    实现自定义校验第一步自定义校验注解importjakarta.validation.Constraint;importjakarta.validation.Payload;importjava.lang.annotation.*;@Target({ElementT......
  • Spring Boot自定义注释制作API接口拦截器进行TOKEN验证
    一:自定义注解@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)@Documentedpublic@interfaceChackToken{booleanvalidate()defaulttrue;}......
  • 【Logback】自定义日志输出格式
    logback官方文档logback(二)springboot配置日志文件格式、logback-spring配置文件详解、logback为日志自定义颜色【史上最详细】logback(三)mybatis-plus结合logback将sql语......
  • SpringBoot自动配置原理
    传统的Spring项目,需要我们对每个引入的组件进行手动配置。这需要开发者对组件有深入的了解,否则很容易遗漏某些细节。对于业务开发人员/公司来说,他们只需要知道如何使用组......
  • activemq配置无法访问8161
    首先先检查防火墙有没有打开,我是腾讯云,已经打开仍无法访问ok继续添加开放端口查看防火墙状态systemctlstatusfirewalld如果未启用,则启用防火墙systemctlstartfirewa......
  • MYSQL——真实生产环境的数据库机器配置
    摘要介绍真实项目中数据库配置选型,机器压测指标等,以帮助项目设计构建MYSQL集群能够符合你的业务。以下是个人在真实生产环境总结的的相关的参数,仅仅供大家参考。一、生产环......
  • 配置Monitoring监控日志
    https://help.aliyun.com/document_detail/68017.html通过配置Monitoring监控日志,您可以查看阿里云Elasticsearch实例的监控日志并配置监控索引,避免因监控日志占用空间过......