创建多台 KVM 虚拟机的配置文件

用于批量创建多台虚拟机的配置。


适用于 KVM 的配置文件

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

LINUX_BASE_BOX = "centos/7.9"

Vagrant.configure(2) do |config|
    config.vm.box = LINUX_BASE_BOX
    config.vm.box_url = "file://../common/metadata-kvm.json"
    config.ssh.username="echoxu"
    config.ssh.insert_key = false
    config.ssh.connect_timeout = 300
    config.ssh.private_key_path = "~/.ssh/id_rsa_vagrant"
    config.ssh.guest_port = 51888
    config.vm.network :forwarded_port, id: "ssh", guest: 22, host: 2222, disabled: true
    config.vm.synced_folder ".", "/vagrant", disabled: true
	
    config.vm.define "WEB", autostart: true, primary: true do |webCfg|
        webCfg.vm.hostname = "vue-viewfilx"
        #config.vm.network :private_network, :ip => "192.168.121.89",:libvirt__network_name => "vagrant-libvirt"
        
        webCfg.vm.provider "libvirt" do |libvirt|
            libvirt.memory = "1024"
            libvirt.cpus = 1
            libvirt.driver = "kvm"
            libvirt.nested = true
            libvirt.default_prefix="vue.viewfilx.com-"
            libvirt.management_network_mac = "52:54:00:40:e9:48"
        end
        
        # 安装 vue 
        webCfg.vm.provision "shell", privileged: true, path: './scripts/vue.sh'
    end

    config.vm.define "API", autostart: true, primary: true do |apiCfg|
        apiCfg.vm.hostname = "api-viewfilx"
        #apiCfg.vm.network :private_network, :ip => "192.168.121.90",:libvirt__network_name => "vagrant-libvirt"
        #apiCfg.vm.synced_folder "./share/api-viewfilx", "/home/echoxu/api-viewfilx", type: "rsync"
        
        apiCfg.vm.provider "libvirt" do |libvirt|
            libvirt.memory = "1024"
            libvirt.cpus = 1
            libvirt.driver = "kvm"
            libvirt.nested = true
            libvirt.default_prefix="api.viewfilx.com-"
            libvirt.management_network_mac = "52:54:00:e0:72:28"
        end
        
        # 安装 go,需要在 vagrant up 后执行 vagrant provision 才会执行此脚本
        apiCfg.vm.provision "shell", privileged: true, path: './scripts/go.sh'
    end
    
    config.vm.define "STORAGE", autostart: true, primary: true do |storageCfg|
        storageCfg.vm.hostname = "minio-viewfilx"
        #storageCfg.vm.network :private_network, :ip => "192.168.121.91",:libvirt__network_name => "vagrant-libvirt"
        
        storageCfg.vm.provider "libvirt" do |libvirt|
            libvirt.memory = "1024"
            libvirt.cpus = 1
            libvirt.driver = "kvm"
            libvirt.nested = true
            libvirt.default_prefix="minio.viewfilx.com-"
            libvirt.management_network_mac = "52:54:00:9b:f0:46"
        end
        
        # 安装 minio
        storageCfg.vm.provision "shell", privileged: true, path: './scripts/minio.sh'
        storageCfg.vm.provision "file", source: "./scripts/minio-startup.sh", destination: "/tmp/minio.sh"
        storageCfg.vm.provision "shell", privileged: true, inline: 'mv /tmp/minio.sh /etc/init.d/ && chmod +x /etc/init.d/minio.sh && chkconfig --add minio.sh'
    end
    
    config.vm.define "NGINX", autostart: true, primary: true do |nginxCfg|
        nginxCfg.vm.hostname = "nginx-viewfilx"
        #nginxCfg.vm.network :private_network, :ip => "192.168.121.92",:libvirt__network_name => "vagrant-libvirt"
        
        nginxCfg.vm.provider "libvirt" do |libvirt|
            libvirt.memory = "1024"
            libvirt.cpus = 1
            libvirt.driver = "kvm"
            libvirt.nested = true
            libvirt.default_prefix="nginx.viewfilx.com-"
            libvirt.management_network_mac = "52:54:00:ae:5d:9c"
        end
        
        # 安装 openresty 或者 nginx + lua
        nginxCfg.vm.provision "shell", privileged: true, path: './scripts/nginx.sh'
    end
    
    config.vm.define "DB", autostart: true, primary: true do |dbCfg|
        dbCfg.vm.hostname = "mysql-viewfilx"
        #dbCfg.vm.network :private_network, :ip => "192.168.121.93",:libvirt__network_name => "vagrant-libvirt"
        
        dbCfg.vm.provider "libvirt" do |libvirt|
            libvirt.memory = "1024"
            libvirt.cpus = 1
            libvirt.driver = "kvm"
            libvirt.nested = true
            libvirt.default_prefix="mysql.viewfilx.com-"
            libvirt.management_network_mac = "52:54:00:fa:74:93"
        end
        
        dbCfg.vm.provision "shell", privileged: true, path: './scripts/mysql.sh'
    end

end

适用于 Virtualbox 的配置文件

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

LINUX_BASE_BOX = "centos/7.9"

Vagrant.configure(2) do |config|
    config.vm.box = LINUX_BASE_BOX
    config.vm.box_url = "file://../common/metadata.json"
    config.ssh.username="echoxu"
    config.ssh.private_key_path = "C:\\Users\\echoxu\\.ssh\\id_rsa_vagrant"
    config.ssh.guest_port = 51888
    config.vm.network :forwarded_port, id: "ssh", guest: 22, host: 2222, disabled: true
	
    config.vm.define "WEB", autostart: true, primary: true do |webCfg|
        webCfg.vm.hostname = "vue-viewfilx"
        webCfg.vm.network :forwarded_port, guest: 51888, host: 2256
        webCfg.ssh.port = 2256
        webCfg.vm.network "private_network", ip: "192.168.56.95", netmask: "255.255.255.0"
        webCfg.vm.synced_folder ".", "/vagrant", disabled: true
        webCfg.vm.synced_folder "./share/vue-viewfilx", "/home/echoxu/vue-viewfilx"
        webCfg.vm.provider "virtualbox" do |vb|
            vb.name = "viewfilx.com"
            vb.memory = "1024"
            vb.cpus = 1
        end
        # 安装 vue 
        webCfg.vm.provision "shell", privileged: true, path: './scripts/vue.sh'
    end

    config.vm.define "API", autostart: true, primary: true do |apiCfg|
        apiCfg.vm.hostname = "api-viewfilx"
        apiCfg.vm.network :forwarded_port, guest: 51888, host: 2257
        apiCfg.ssh.port = 2257
        apiCfg.vm.network "private_network", ip: "192.168.56.96", netmask: "255.255.255.0"
        apiCfg.vm.synced_folder ".", "/vagrant", disabled: true
        apiCfg.vm.synced_folder "./share/api-viewfilx", "/home/echoxu/api-viewfilx"
        apiCfg.vm.provider "virtualbox" do |vb|
            vb.name = "api.viewfilx.com"
            vb.memory = "1024"
            vb.cpus = 1
        end
        # 安装 go,需要在 vagrant up 后执行 vagrant provision 才会执行此脚本
        apiCfg.vm.provision "shell", privileged: true, path: './scripts/go.sh'
    end
    
    config.vm.define "STORAGE", autostart: true, primary: true do |storageCfg|
        storageCfg.vm.hostname = "minio-viewfilx"
        storageCfg.vm.network :forwarded_port, guest: 51888, host: 2258
        storageCfg.ssh.port = 2258
        storageCfg.vm.network "private_network", ip: "192.168.56.97", netmask: "255.255.255.0"
        storageCfg.vm.synced_folder ".", "/vagrant", disabled: true
        # storageCfg.vm.disk :disk, size: "10GB", primary: true
        storageCfg.vm.provider "virtualbox" do |vb|
            vb.name = "minio.viewfilx.com"
            vb.memory = "1024"
            vb.cpus = 1
        end
        # 安装 minio
        storageCfg.vm.provision "shell", privileged: true, path: './scripts/minio.sh'
        storageCfg.vm.provision "file", source: "./scripts/minio-startup.sh", destination: "/tmp/minio.sh"
        storageCfg.vm.provision "shell", privileged: true, inline: 'mv /tmp/minio.sh /etc/init.d/ && chmod +x /etc/init.d/minio.sh && chkconfig --add minio.sh'
    end
    
    config.vm.define "NGINX", autostart: true, primary: true do |nginxCfg|
        nginxCfg.vm.hostname = "nginx-viewfilx"
        nginxCfg.vm.network :forwarded_port, guest: 51888, host: 2259
        nginxCfg.ssh.port = 2259
        nginxCfg.vm.network "private_network", ip: "192.168.56.98", netmask: "255.255.255.0"
        nginxCfg.vm.synced_folder ".", "/vagrant", disabled: true
        nginxCfg.vm.provider "virtualbox" do |vb|
            vb.name = "nginx.viewfilx.com"
            vb.memory = "1024"
            vb.cpus = 1
        end
        # 安装 openresty 或者 nginx + lua
        nginxCfg.vm.provision "shell", privileged: true, path: './scripts/nginx.sh'
    end
    
    config.vm.define "DB", autostart: true, primary: true do |dbCfg|
        dbCfg.vm.hostname = "mysql-viewfilx"
        dbCfg.vm.network :forwarded_port, guest: 51888, host: 2260
        dbCfg.ssh.port = 2260
        dbCfg.vm.network "private_network", ip: "192.168.56.99", netmask: "255.255.255.0"
        dbCfg.vm.synced_folder ".", "/vagrant", disabled: true
        dbCfg.vm.provider "virtualbox" do |vb|
            vb.name = "mysql.viewfilx.com"
            vb.memory = "1024"
            vb.cpus = 1
        end
        dbCfg.vm.provision "shell", privileged: true, path: './scripts/mysql.sh'
    end

end

批量创建相同配置的虚拟机

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

# 虚拟机相同配置时可用此方法
LINUX_BASE_BOX = "centos/7.9"

Host_count = 1

mysql_servers = {
    :'mysql-master' => '192.168.56.80',
    :'mysql-slave1' => '192.168.56.81',
    :'mysql-slave2' => '192.168.56.82'
}

Vagrant.configure(2) do |config|
    config.vm.box = LINUX_BASE_BOX
    config.vm.box_url = "file://../common/metadata.json"
    config.ssh.username="echoxu"
    config.ssh.private_key_path = "C:\\Users\\echoxu\\.ssh\\id_rsa_vagrant"
    config.vm.network :forwarded_port, id: "ssh", guest: 22, host: 2222, disabled: true
    config.ssh.guest_port = 51888

    mysql_servers.each do |mysql_server_name, mysql_server_ip|
        config.vm.define mysql_server_name do |mysql_config|
            mysql_config.vm.hostname = "#{mysql_server_name.to_s}.vagrant.internal"
            mysql_config.vm.network :forwarded_port, guest: 51888, host: "#{2280+Host_count}"
            mysql_config.ssh.port = "#{2280+Host_count}"
            Host_count += 1
            mysql_config.vm.network "private_network", ip: mysql_server_ip, netmask: "255.255.255.0"
            mysql_config.vm.synced_folder ".", "/vagrant", disabled: true
            mysql_config.vm.provider "virtualbox" do |vb|
                vb.name = mysql_server_name.to_s
                vb.memory = "512"
            end
        end       
    end
    
end

或者

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

Vagrant.configure("2") do |config|
  (1..3).each do |i|
    config.vm.define "k8s-node#{i}" do |node|
      node.vm.box = "centos7.8.2003"
      node.vm.hostname = "k8s-node#{i}"
      node.vm.network "private_network", ip: "192.168.56.#{90+i}", netmask: "255.255.255.0"
      node.vm.provider "virtualbox" do |vb|
        vb.name = "k8s-node#{i}"
        vb.memory = "512"
        vb.cpus = 1
      end
    end
  end
end
上次更新:
贡献者: iEchoxu