From d4fe1f666719b3cdc59f7caf6f4ccd540b8b57fa Mon Sep 17 00:00:00 2001 From: Kris Lamoureux Date: Sat, 12 Nov 2022 02:38:39 -0500 Subject: [PATCH] Install containerd and kubeadm --- .gitignore | 3 ++ LICENSE | 12 +++++++ Makefile | 11 +++++++ README.md | 10 ++++++ Vagrantfile | 30 +++++++++++++++++ scripts/provision.sh | 73 ++++++++++++++++++++++++++++++++++++++++++ scripts/vagrant-ips.sh | 39 ++++++++++++++++++++++ 7 files changed, 178 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 Vagrantfile create mode 100755 scripts/provision.sh create mode 100755 scripts/vagrant-ips.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8f89140 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.vagrant +.k8s-ips +*.log diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..196e2f4 --- /dev/null +++ b/LICENSE @@ -0,0 +1,12 @@ +Copyright (C) 2022 by Kris Lamoureux + +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. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d4e1606 --- /dev/null +++ b/Makefile @@ -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 diff --git a/README.md b/README.md new file mode 100644 index 0000000..e1540b8 --- /dev/null +++ b/README.md @@ -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` diff --git a/Vagrantfile b/Vagrantfile new file mode 100644 index 0000000..ef5a2a7 --- /dev/null +++ b/Vagrantfile @@ -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 diff --git a/scripts/provision.sh b/scripts/provision.sh new file mode 100755 index 0000000..3dc2a53 --- /dev/null +++ b/scripts/provision.sh @@ -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 diff --git a/scripts/vagrant-ips.sh b/scripts/vagrant-ips.sh new file mode 100755 index 0000000..598204b --- /dev/null +++ b/scripts/vagrant-ips.sh @@ -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