Install containerd and kubeadm

This commit is contained in:
Kris Lamoureux 2022-11-12 02:38:39 -05:00
commit d4fe1f6667
Signed by: kris
GPG Key ID: 3EDA9C3441EDA925
7 changed files with 178 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.vagrant
.k8s-ips
*.log

12
LICENSE Normal file
View File

@ -0,0 +1,12 @@
Copyright (C) 2022 by Kris Lamoureux <kris@lamoureux.io>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.

11
Makefile Normal file
View File

@ -0,0 +1,11 @@
all: vagrant
vagrant:
vagrant up --no-destroy-on-error --no-color | tee ./vagrantup.log
./scripts/vagrant-ips.sh | tee -a ./vagrantup.log
vagrant rsync | tee -a ./vagrantup.log
vagrant provision --no-color | tee -a ./vagrantup.log
clean:
vagrant destroy -f --no-color
rm -rf .vagrant .k8s-ips *.log

10
README.md Normal file
View File

@ -0,0 +1,10 @@
# vagrant-k8s
A test environment to play with Kubernetes (k8s) using kubeadm and containerd on Debian Virtual Machines using Vagrant. A simple `make` command will create three Debian 11 nodes with 2 vCPUs and 2 GB of RAM each, allocating 6 threads—depending on CPU—and 6 GB of system RAM.
This project is a work in progress.
## Quick Start
1. `make clean`
2. `make`
3. `grep node1 vagrantup.log | less`

30
Vagrantfile vendored Normal file
View File

@ -0,0 +1,30 @@
CPU = 2
MEM = 2048
HOSTS = Array(1..3)
Vagrant.configure(2) do |vm_config|
HOSTS.each do |count|
vm_config.vm.define "node".concat("#{count}") do |config|
config.vm.box = "debian/bullseye64"
config.vm.network "private_network", type: "dhcp"
config.vm.hostname = "node".concat("#{count}")
config.vm.synced_folder ".", "/vagrant", type: "rsync",
rsync__exclude: [".git/", "*.log"]
# Libvirt
config.vm.provider :libvirt do |virt|
virt.memory = MEM
virt.cpus = CPU
end
# VirtualBox
config.vm.provider :virtualbox do |vbox|
vbox.memory = MEM
vbox.cpus = CPU
end
config.vm.provision "shell", path: "scripts/provision.sh"
end
end
end

73
scripts/provision.sh Executable file
View File

@ -0,0 +1,73 @@
#!/bin/bash
################
### Settings ###
################
set -x
DEBIAN_FRONTEND=noninteractive
KEYRING_PATH="/usr/share/keyrings/kubernetes-archive-keyring.gpg"
KEYRING_URL="https://packages.cloud.google.com/apt/doc/apt-key.gpg"
#########################################################
### Install containerd, kubeadm, kubelet, and kubectl ###
#########################################################
# Install prerequsite packages
apt-get install -y apt-transport-https ca-certificates curl
# Install Google Cloud public signing key
curl -fsSLo "$KEYRING_PATH" "$KEYRING_URL"
if [ ! -f /etc/sysctl.d/keepalived.conf ]; then
cat <<- EOF | tee /etc/apt/sources.list.d/kubernetes.list
deb [signed-by=${KEYRING_PATH}] https://apt.kubernetes.io/ kubernetes-xenial main
EOF
fi
# Update package index and install
apt-get update
apt-get install -y containerd kubelet kubeadm kubectl
apt-mark hold kubelet kubeadm kubectl
# Forwarding IPv4 and letting iptables see bridged traffic
if [ ! -f /etc/modules-load.d/k8s.conf ]; then
cat <<- EOF | tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
modprobe overlay
modprobe br_netfilter
fi
# sysctl params required by setup, params persist across reboots
if [ ! -f /etc/sysctl.d/k8s.conf ]; then
cat <<- EOF | tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF
sudo sysctl --system
fi
# Prevent further execution until floating IP is found (not a failure)
if [ ! -f /vagrant/.k8s-ips ]; then
echo "NOTICE: /vagrant/.k8s-ips not found"
echo "NOTICE: Rerun 'vagrant provision' after running vagrant-ips.sh"
exit 0
fi
##########################################
### Install keepalived for floating IP ###
##########################################
apt-get update
apt-get install -y keepalived
if [ ! -f /etc/sysctl.d/keepalived.conf ]; then
cat <<- EOF | tee /etc/sysctl.d/keepalived.conf
net.ipv4.ip_nonlocal_bind = 1
EOF
sysctl -p
fi

39
scripts/vagrant-ips.sh Executable file
View File

@ -0,0 +1,39 @@
#!/bin/bash
set -x
# IPs file already exist
if [ -f .k8s-ips ]; then
echo "NOTICE: .k8s-ips already exists"
exit 0
fi
# Create file with vagrant-k8s private DHCP IPs
echo "declare -a K8S_NODES" > .k8s-ips
for i in {1..3}; do
IP=$(vagrant ssh "node$i" -c "hostname -I | cut -d' ' -f2" 2>/dev/null)
echo "K8S_NODES[$i]=$IP" >> .k8s-ips
done
# Source new IP file
# shellcheck disable=SC1091
source .k8s-ips
# Grab last octet on IP addresses (assuming /24)
last_octets=()
for i in {1..3}; do
last_octets+=("$(echo "${K8S_NODES[$i]}" | rev | cut -d. -f1 | rev)")
done
# Generate random octet and ensure it's not taken
while true
do
available=true
random_octet="$(shuf -i2-254 -n1)"
for i in "${last_octets[@]}"; do
[ "$random_octet" == "${i::-1}" ] && available=false
done
[ "$available" == true ] && break
done
# Add keepalived IP address in order
sed -i "/declare/a K8S_NODES[0]=${K8S_NODES[1]%.*}.$random_octet" .k8s-ips