-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathVagrantfile
175 lines (139 loc) · 4.11 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Check for missing plugins
required_plugins = %w(vagrant-hostmanager vagrant-scp vagrant-env)
plugin_installed = false
required_plugins.each do |plugin|
unless Vagrant.has_plugin?(plugin)
system "vagrant plugin install #{plugin}"
plugin_installed = true
end
end
# If new plugins installed, restart Vagrant process
if plugin_installed === true
exec "vagrant #{ARGV.join' '}"
end
### configuration parameters ###
# Vagrant variables
VAGRANTFILE_API_VERSION = "2"
DEFAULT_BOX_NAME = "bento/ubuntu-22.04"
DEFAULT_BOX_VERSION = "202303.13.0"
kubespray_ver = ENV["KUBESPRAY_VER"] || "v2.24.0"
# control node
ctrlnodes = [
{
:hostname => "controller",
:ip => "192.168.56.10",
:ram => 2048,
:cpus => 2,
}
]
# cluster nodes
nodes = [
{
:hostname => "node01",
:ip => "192.168.56.21",
:ram => 4096,
:cpus => 2
},
{
:hostname => "node02",
:ip => "192.168.56.22",
:ram => 4096,
:cpus => 2
},
{
:hostname => "node03",
:ip => "192.168.56.23",
:ram => 4096,
:cpus => 2
},
]
### main ###
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# vagrant-hostmanager options
config.hostmanager.enabled = true
config.hostmanager.manage_host = false
config.hostmanager.manage_guest = true
config.hostmanager.ignore_private_ip = false
config.hostmanager.include_offline = true
# vagrant-env options
config.env.enable
# Forward ssh agent to easily ssh into the different machines
config.ssh.forward_agent = true
# cluster nodes
nodes.each do |node|
config.vm.define node[:hostname] do |config|
### vm definitions ###
config.vm.hostname = node[:hostname]
config.vm.boot_timeout = 900
config.vm.box = node[:box] ? node[:box] : DEFAULT_BOX_NAME;
config.vm.box_version = node[:box_version] ? node[:box_version] : DEFAULT_BOX_VERSION;
config.vm.network :private_network, ip: node[:ip]
memory = node[:ram];
cpus = node[:cpus];
### provider ###
config.vm.provider :virtualbox do |vb|
vb.customize [
"modifyvm", :id,
"--memory", memory.to_s,
"--cpus", cpus.to_s,
"--ioapic", "on",
"--natdnshostresolver1", "on",
"--natdnsproxy1", "on"
]
end
### provisioners ###
# preinst
config.vm.provision "preinst", type: "shell" do |m|
m.path = "scripts/preinst.sh"
end
# ssh public key
id_rsa_pub = File.read("#{Dir.home}/.ssh/id_rsa.pub")
config.vm.provision "copy ssh public key", type: "shell",
inline: "echo \"#{id_rsa_pub}\" >> /home/vagrant/.ssh/authorized_keys"
end
end
# control nodes
ctrlnodes.each do |ctrl|
config.vm.define ctrl[:hostname] do |config|
### vm definitions ###
config.vm.hostname = ctrl[:hostname]
config.vm.boot_timeout = 900
config.vm.box = ctrl[:box] ? ctrl[:box] : DEFAULT_BOX_NAME;
config.vm.box_version = ctrl[:box_version] ? ctrl[:box_version] : DEFAULT_BOX_VERSION;
config.vm.network :private_network, ip: ctrl[:ip]
memory = ctrl[:ram];
cpus = ctrl[:cpus];
### provider ###
config.vm.provider :virtualbox do |vb|
vb.customize [
"modifyvm", :id,
"--memory", memory.to_s,
"--cpus", cpus.to_s,
"--ioapic", "on",
"--natdnshostresolver1", "on",
"--natdnsproxy1", "on"
]
end
### provisioners ###
# preinst
config.vm.provision "preinst", type: "shell" do |m|
m.path = "scripts/preinst.sh"
end
# copy files
config.vm.provision "file", source: "~/.ssh/id_rsa", destination: "~/.ssh/id_rsa"
config.vm.provision "shell", inline: <<-SHELL
chmod 600 /home/vagrant/.ssh/id_rsa
SHELL
config.vm.provision "file", source: "./ansible.cfg", destination: "~/.ansible.cfg"
# kubernetes
config.vm.provision "k8s", type: "shell", run: "never", privileged: false do |m|
m.path = "scripts/k8s.sh"
m.env = {
"KUBESPRAY_VER" => kubespray_ver
}
end
end
end
end