Skip to content

Commit

Permalink
♻️ Rethink @mmumshad's Vagrant configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Searge committed Apr 6, 2024
1 parent c8ba5c2 commit 20e1f40
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 330 deletions.
170 changes: 137 additions & 33 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,160 @@

# Define how much memory your computer has in GB (e.g. 8, 16)
# Larger nodes will be created if you have more.
RAM_SIZE = 16
RAM_SIZE = 8

# Define how mnay CPU cores you have.
# More powerful workers will be created if you have more
CPU_CORES = 8
CPU_CORES = 4

# Internal network prefix for the VM network
# See the documentation before changing this
IP_NW = "192.168.56."

Vagrant.configure("2") do |config|
config.vm.box = "debian/bookworm64"
config.vm.boot_timeout = 900
config.vm.box_check_update = true
# Calculate resource amounts
# based on RAM/CPU
ram_selector = (RAM_SIZE / 4) * 4
if ram_selector < 8
raise "Unsufficient memory #{RAM_SIZE}GB. min 8GB"
end
RESOURCES = {
"control" => {
1 => {
# controlplane01 bigger since it may run e2e tests.
"ram" => [ram_selector * 128, 2048].max(),
"cpu" => CPU_CORES >= 12 ? 4 : 2,
},
2 => {
# All additional masters get this
"ram" => [ram_selector * 128, 2048].min(),
"cpu" => CPU_CORES > 8 ? 2 : 1,
},
},
"worker" => {
"ram" => [ram_selector * 128, 4096].min(),
"cpu" => CPU_CORES > 8 ? 2 : 1,
},
}

# Sets up hosts file and DNS
def setup_dns(node)
# Set up /etc/hosts
node.vm.provision "setup-hosts", :type => "shell", :path => "vm/utils/setup-hosts.sh" do |s|
s.args = ["enp0s8", node.vm.hostname]
end
# Set up DNS resolution
# node.vm.provision "setup-dns", type: "shell", :path => "vm/utils/update-dns.sh"
end

# Runs provisioning steps that are required by masters and workers
def provision_kubernetes_node(node)
# Set up kernel parameters, modules and tunables
node.vm.provision "setup-kernel", :type => "shell", :path => "vm/utils/setup-kernel.sh"
# Set up ssh
node.vm.provision "setup-ssh", :type => "shell", :path => "vm/utils/ssh.sh"
# Set up DNS
setup_dns node
# Set up with Ansible
node.vm.provision "ansible" do |ansible|
ansible.compatibility_mode = "2.0"
ansible.verbose = "v"
ansible.playbook = "ansible/provision.yml"
end
end

# Define the number of master and worker nodes. You should not change this
NUM_CONTROL_NODES = 2
NUM_WORKER_NODE = 2

# Network
config.vm.hostname = PRJ_HOSTNAME
# FIXME: Change bridge adapter name and IP address according to your environment
# For exanple, I use eno1 and IP address 10.0.0.128/28
config.vm.network "public_network",
bridge: "eno1", ip: "10.0.0.131", hostname: true
# Host address start points
MASTER_IP_START = 10
NODE_IP_START = 20
LB_IP_START = 30

config.vm.network "forwarded_port", guest: 80, host: 8080,
auto_correct: true
config.vm.network "forwarded_port", guest: 443, host: 8443,
auto_correct: true
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.

config.vm.synced_folder "../drupal8-website", PRJ_DIR
# Every Vagrant development environment requires a box. You can search for
# boxes at https://vagrantcloud.com/search.
# config.vm.box = "base"
config.vm.box = "debian/bookworm64"
config.vm.boot_timeout = 900

# Config for hypervisor
# config.vm.provider "hyperv" do |hv|
# and change all vb. to hv.
config.vm.provider "virtualbox" do |vb|
vb.name = PRJ_NAME
# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
config.vm.box_check_update = false

vb.memory = "2048"
vb.cpus = "2"
# Provision Control Nodes
(1..NUM_CONTROL_NODES).each do |i|
config.vm.define "controlplane0#{i}" do |node|
# Name shown in the GUI
node.vm.provider "virtualbox" do |vb|
vb.name = "kubernetes-ha-controlplane-#{i}"
vb.memory = RESOURCES["control"][i > 2 ? 2 : i]["ram"]
vb.cpus = RESOURCES["control"][i > 2 ? 2 : i]["cpu"]
# Disable gui & audio for headless operation in VirtualBox
vb.gui = false
vb.customize ["modifyvm", :id, "--audio", "none"]
end
node.vm.hostname = "controlplane0#{i}"
node.vm.network :private_network, ip: IP_NW + "#{MASTER_IP_START + i}"
node.vm.network "forwarded_port", guest: 22, host: "#{2710 + i}"
provision_kubernetes_node node
if i == 1
# Add cetificate verification scripts
node.vm.provision "file", source: "vm/utils/cert_verify.sh", destination: "$HOME/cert_verify.sh"
node.vm.provision "file", source: "vm/utils/approve-csr.sh", destination: "$HOME/approve-csr.sh"
end
end
end

# Provision Load Balancer Node
config.vm.define "loadbalancer" do |node|
node.vm.provider "virtualbox" do |vb|
vb.name = "kubernetes-ha-lb"
vb.memory = 512
vb.cpus = 1
# Disable gui & audio for headless operation in VirtualBox
vb.gui = false
vb.customize ["modifyvm", :id, "--audio", "none"]
end
node.vm.hostname = "loadbalancer"
node.vm.network :private_network, ip: IP_NW + "#{LB_IP_START}"
node.vm.network "forwarded_port", guest: 22, host: 2730
# Set up ssh
node.vm.provision "setup-ssh", :type => "shell", :path => "vm/utils/ssh.sh"
setup_dns node
end

config.vm.provision "file",
source: "~/.ssh/id_ed25519.pub", destination: "~/.ssh/me.pub"

config.vm.provision "shell", inline: <<-SHELL
sudo apt-get update
sudo apt-get install -y openssh-server
cat /home/vagrant/.ssh/me.pub >> /home/vagrant/.ssh/authorized_keys
cat /home/vagrant/.ssh/me.pub >> /root/.ssh/authorized_keys
SHELL
# Provision Worker Nodes
(1..NUM_WORKER_NODE).each do |i|
config.vm.define "node0#{i}" do |node|
node.vm.provider "virtualbox" do |vb|
vb.name = "kubernetes-ha-node-#{i}"
vb.memory = RESOURCES["worker"]["ram"]
vb.cpus = RESOURCES["worker"]["cpu"]
vb.gui = false
vb.customize ["modifyvm", :id, "--audio", "none"]
end
node.vm.hostname = "node0#{i}"
node.vm.network :private_network, ip: IP_NW + "#{NODE_IP_START + i}"
node.vm.network "forwarded_port", guest: 22, host: "#{2720 + i}"
provision_kubernetes_node node
end
end

# Modify all nodes to not use GUI and audio
config.vm.define "all" do |node|
node.vm.provider "virtualbox" do |vb|
vb.gui = false
vb.customize ["modifyvm", :id, "--audio", "none"]
end
end
end
4 changes: 2 additions & 2 deletions ansible.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ remote_tmp = "/tmp/ansible-${USER}"
# Tweaks for readability and debug
callbacks_enabled = profile_tasks
stdout_callback = community.general.yaml
# cow_selection = bunny
cow_selection = bunny
nocows = True
force_color = True

Expand All @@ -30,7 +30,7 @@ force_color = True
# no_log = True

[inventory]
enable_plugins = auto, yaml, kubernetes.core.k8s
enable_plugins = auto, yaml, kubernetes.core.k8s, ini

[ssh_connection]
# SSH CONFIG
Expand Down
142 changes: 0 additions & 142 deletions vm/Vagrantfile

This file was deleted.

5 changes: 0 additions & 5 deletions vm/debian/ssh.sh

This file was deleted.

Loading

0 comments on commit 20e1f40

Please sign in to comment.