vagrant 常用配置
# 基础镜像
基于哪个镜像来配置
Vagrant.configure("2") do |config|
config.vm.box = "centos/7"
config.vm.box_check_update = false
end
1
2
3
4
2
3
4
# 资源配置
官网 Virtualbox 的配置 (opens new window)
config.vm.provider "virtualbox" do |vb|
vb.name = "node-#{i}"
vb.memory = "1024"
vb.cpus = 2
v.gui = false
end
1
2
3
4
5
6
2
3
4
5
6
# 网络配置
# 公有网络
主机与虚拟机,虚拟机与虚拟机都可以互通,也就是桥接模式。
config.vm.network "public_network"
1
# 私有网络
各个机器之间网络独立。也称 host-ohly。虚拟机能够与主机网络互通,但是各个机器不能互通。
config.vm.network "private_network"
1
# 静态 IP
不使用默认的 DHCP 分配 IP。每次启动机器都时同一个 IP。
config.vm.network "public_network", ip: "192.168.0.17"
1
# 默认网卡
如果不配置默认网卡那么会每次都弹出一个选择的输出。指定后即可。需要保证网卡名称完全一致才行。
config.vm.network "public_network", bridge: "en1: Wi-Fi (AirPort)"
# 多个,有一个匹配上了就会使用。适合换了机器不换配置。
config.vm.network "public_network", bridge: [
"en1: Wi-Fi (AirPort)",
"en6: Broadcom NetXtreme Gigabit Ethernet Controller",
]
1
2
3
4
5
6
7
2
3
4
5
6
7
# 手动配置
自己通过 ifconfig 来配置网络。
Vagrant.configure("2") do |config|
config.vm.network "public_network", auto_config: false
# manual ip
config.vm.provision "shell",
run: "always",
inline: "ifconfig eth1 192.168.0.17 netmask 255.255.255.0 up"
# manual ipv6
config.vm.provision "shell",
run: "always",
inline: "ifconfig eth1 inet6 add fc00::17/7"
end
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# NAT防火墙配置
虚拟机到主机需要在主机配置防火墙。route -n
即可查看到 IP 地址。如下图则配置为 192.168.128.0/24
# 批量创建虚拟机
# 批量方式
# -*- mode: ruby -*-
# vi: set ft=ruby :
servers = {
:client => '192.168.31.10',
:master1 => '192.168.31.11',
:master2 => '192.168.31.12',
:master3 => '192.168.31.13',
:worker1 => '192.168.31.14',
:worker2 => '192.168.31.15'
}
Vagrant.configure("2") do |config|
config.vm.box = "my-centos7"
config.vm.box_check_update = false
servers.each do |server_name, server_ip|
config.vm.define server_name do |node|
node.vm.hostname = "#{server_name.to_s}"
node.vm.provider "virtualbox" do |vb|
vb.name = server_name.to_s
vb.memory = "1024"
vb.cpus = 1
end
node.ssh.username="root"
node.ssh.password="9999"
node.hostmanager.enabled = true
node.hostmanager.manage_host = true
node.hostmanager.manage_guest = true
node.hostmanager.ignore_private_ip = false
node.hostmanager.include_offline = true
node.vm.network "public_network",
ip: server_ip,
bridge: [
"Intel(R) I211 Gigabit Network Connection",
"Intel(R) Wi-Fi 6 AX200 160MHz"
],
adapter: 2
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
42
43
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
42
43
# 循环方式
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "centos/7"
config.vm.box_check_update = false
(1..2).each do |i|
config.vm.define "node-#{i}" do |node|
node.vm.provider "virtualbox" do |vb|
vb.name = "centos7-node-#{i}"
vb.memory = "1024"
vb.cpus = 2
end
# 定义网络配置
node.vm.allow_hosts_modification = true
node.vm.hostname = "node-#{i}"
node.vm.network "public_network",
ip: "192.168.31.#{10+i}",
bridge: "en1: Intel(R) I211 Gigabit Network Connection"
# ssh 配置
# config.ssh.port = 22
#config.ssh.username = "root"
#config.ssh.password = "9524"
#config.ssh.private_key_path = ""
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
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
# 多次定义方式
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.define "master" do |master|
master.vm.box = "centos/7"
end
config.vm.define "agent" do |agent|
master.vm.box = "centos/7"
end
end
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
在 GitHub 编辑此页 (opens new window)
上次更新: 2024/02/25, 12:11:11