Skip to content

Instantly share code, notes, and snippets.

@serverwentdown
Last active April 2, 2024 12:56
Show Gist options
  • Save serverwentdown/787d7f63ce64c23c3ddf0f4bd0bd2749 to your computer and use it in GitHub Desktop.
Save serverwentdown/787d7f63ce64c23c3ddf0f4bd0bd2749 to your computer and use it in GitHub Desktop.
A dual-stack embedded-etcd development k3s cluster (originally from this discussion: https://github.com/k3s-io/k3s/discussions/9807)
# -*- mode: ruby -*-
# vi: set ft=ruby :
# 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.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://vagrantcloud.com/search.
config.vm.box = "generic/ubuntu2204"
config.vm.box_version = "4.3.12"
# 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
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# NOTE: This will enable public access to the opened port
# config.vm.network "forwarded_port", guest: 80, host: 8080
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine and only allow access
# via 127.0.0.1 to disable public access
# config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network "private_network", ip: "192.168.33.10"
# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
# config.vm.synced_folder "../data", "/vagrant_data"
# Disable the default share of the current code directory. Doing this
# provides improved isolation between the vagrant box and your host
# by making sure your Vagrantfile isn't accessible to the vagrant box.
# If you use this you may want to enable additional shared subfolders as
# shown above.
# config.vm.synced_folder ".", "/vagrant", disabled: true
# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
# config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
#
# # Customize the amount of memory on the VM:
# vb.memory = "2048"
# end
#
# View the documentation for the provider you are using for more
# information on available options.
# Enable provisioning with a shell script. Additional provisioners such as
# Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
# documentation for more information about their specific syntax and use.
# config.vm.provision "shell", inline: <<-SHELL
# apt-get update
# apt-get install -y apache2
# SHELL
ipv6_prefix = 'fd00'
ipv6_node_prefix = "#{ipv6_prefix}:10::"
ipv6_cluster_network = "#{ipv6_prefix}:42::/56"
ipv6_service_network = "#{ipv6_prefix}:43::/112"
ipv4_node_prefix = "10.10.0."
ipv4_node_offset = 100
ipv4_cluster_network = "10.42.0.0/16"
ipv4_service_network = "10.43.0.0/16"
%w[libvirt virtualbox vmware_desktop].each do |p|
config.vm.provider p do |v|
v.cpus = 2
v.memory = 1024 * 2
end
end
packages = <<-SHELL
sed -i 's,https://mirrors.edge.kernel.org/ubuntu/,http://sg.archive.ubuntu.com/ubuntu/,' /etc/apt/sources.list
export DEBIAN_FRONTEND=noninteractive
#apt-get update
#apt-get upgrade -y
#apt-get install -y nfs-common
SHELL
net_write = <<-SHELL
# Undo Vagrant setting
sed -i 's/net.ipv6.conf.all.disable_ipv6 = 1//' /etc/sysctl.conf
sysctl net.ipv6.conf.all.disable_ipv6=0
# Set up the second network interface
cat <<EOF > /etc/netplan/20-private.yaml
---
network:
version: 2
renderer: networkd
ethernets:
eth1:
addresses:
- $1
- $2
routes:
- to: default
via: "#{ipv6_node_prefix}0"
EOF
netplan apply
SHELL
ipv4_node_1 = "#{ipv4_node_prefix}#{ipv4_node_offset+1}"
(1..3).each do |i|
config.vm.define "node-#{i}" do |node|
node.vm.hostname = "node-#{i}"
ipv4 = "#{ipv4_node_prefix}#{ipv4_node_offset+i}/24"
ipv6 = "#{ipv6_node_prefix}#{i.to_s(16)}/64"
node.vm.network "private_network", ip: ipv4, auto_config: false
# Install packages
node.vm.provision "shell", inline: packages
# Configure networking
node.vm.provision "shell", inline: net_write, args: [ipv4, ipv6]
# Install k3s
node.vm.provision :k3s, run: "once" do |k3s|
# This section runs the installation script with a configuration file,
# environmental variables and a specific version.
k3s.args = %w[server]
k3s.env = %w[
INSTALL_K3S_VERSION=v1.28.7+k3s1
K3S_KUBECONFIG_MODE=0644
]
k3s.config_mode = '0644'
k3s_config = {
:token => 'vagrant-k3s',
:'flannel-iface' => 'eth1',
:'flannel-backend' => 'host-gw',
:'flannel-ipv6-masq' => 'true',
:'cluster-cidr' => "#{ipv4_cluster_network},#{ipv6_cluster_network}",
:'service-cidr' => "#{ipv4_service_network},#{ipv6_service_network}",
:disable => %w[local-storage],
}
if i == 1
k3s.config = k3s_config.merge(:'cluster-init' => 'true')
else
k3s.config = k3s_config.merge(:server => "https://#{ipv4_node_1}:6443")
k3s.skip_start = true # We'll manually start other servers later
end
end
# Enable bash completion
node.vm.provision "shell", inline: "kubectl completion bash > /etc/bash_completion.d/kubectl"
if i == 1
# Apply default manifest
node.vm.provision "file", source: "manifests", destination: "$HOME/manifests"
node.vm.provision "shell", inline: "sleep 15; kubectl apply -Rf $HOME/manifests/", privileged: false
else
# Wait for boot
node.vm.provision "shell", inline: "sleep #{5*i}; while ! curl -o /dev/null --insecure https://#{ipv4_node_1}:6443; do sleep 2; done"
# Start k3s
node.vm.provision "shell", inline: "systemctl enable --now k3s"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment