From 18662bae87186f3136b9dafc0a0f6be694c640e1 Mon Sep 17 00:00:00 2001 From: Jan Novak Date: Thu, 30 May 2024 14:30:09 +0200 Subject: [PATCH] initial commit with basic vagrant stuff --- ansible/readme.md | 16 ++++++++ vagrant/README.md | 51 ++++++++++++++++++++++++ vagrant/Vagrantfile | 76 ++++++++++++++++++++++++++++++++++++ vagrant/bootstrap.sh | 70 +++++++++++++++++++++++++++++++++ vagrant/bootstrap_kmaster.sh | 14 +++++++ vagrant/bootstrap_kworker.sh | 7 ++++ 6 files changed, 234 insertions(+) create mode 100644 ansible/readme.md create mode 100644 vagrant/README.md create mode 100644 vagrant/Vagrantfile create mode 100644 vagrant/bootstrap.sh create mode 100644 vagrant/bootstrap_kmaster.sh create mode 100644 vagrant/bootstrap_kworker.sh diff --git a/ansible/readme.md b/ansible/readme.md new file mode 100644 index 0000000..77dea76 --- /dev/null +++ b/ansible/readme.md @@ -0,0 +1,16 @@ +```bash +# install some prerequisities for kvm +sudo apt -y install bridge-utils cpu-checker libvirt-clients libvirt-daemon qemu qemu-kvm + +# create LVM and filesystem +lvcreate -L 500G -n vms ubuntu-vg +mkfs.ext4 /dev/mapper/ubuntu--vg-vms + +# mount it in fstab +# /dev/mapper/ubuntu--vg-vms /srv/vms ext4 defaults 0 0 + +# install vagrant +apt install vagrant + + +``` \ No newline at end of file diff --git a/vagrant/README.md b/vagrant/README.md new file mode 100644 index 0000000..79a75de --- /dev/null +++ b/vagrant/README.md @@ -0,0 +1,51 @@ +## Provisioning the Kubernetes cluster +### Clone the repo +``` +$ git clone https://github.com/justmeandopensource/kubernetes +$ cd kubernetes/vagrant-provisioning +``` +### Bring up the cluster +For VirtualBox environment +``` +$ vagrant up +``` +For KVM/Libvirt environment +``` +$ vagrant up --provider libvirt +``` +### Copy the kubeconfig file from kmaster +Password for root user is _kubeadmin_ +``` +$ mkdir ~/.kube +$ scp root@172.16.16.100:/etc/kubernetes/admin.conf ~/.kube/config +``` +### Destroy the cluster +``` +$ vagrant destroy -f +``` + +## Deploying Add Ons +### Deploy dynamic nfs volume provisioning +``` +$ cd kubernetes/vagrant-provisioning/misc/nfs-subdir-external-provisioner +$ cat setup_nfs | vagrant ssh kmaster +$ cat setup_nfs | vagrant ssh kworker1 +$ cat setup_nfs | vagrant ssh kworker2 +$ kubectl create -f 01-setup-nfs-provisioner.yaml + +###### for testing +$ kubectl create -f 02-test-claim.yaml +$ kubectl delete -f 02-test-claim.yaml +``` +### Deploy metalLB load balancing +``` +$ cd kubernetes/vagrant-provisioning/misc/metallb +$ kubectl create -f 01_metallb.yaml + +###### wait for 10 seconds or so for the pods to run +$ kubectl create -f 02_metallb-config.yaml + +###### for testing +$ kubectl create -f 03_test-load-balancer.yaml +$ kubectl delete -f 03_test-load-balancer.yaml +``` diff --git a/vagrant/Vagrantfile b/vagrant/Vagrantfile new file mode 100644 index 0000000..3b8c978 --- /dev/null +++ b/vagrant/Vagrantfile @@ -0,0 +1,76 @@ +# -*- mode: ruby -*- +# vi: set ft=ruby : + +ENV['VAGRANT_NO_PARALLEL'] = 'yes' + +VAGRANT_BOX = "generic/ubuntu2204" +VAGRANT_BOX_VERSION = "4.2.10" +CPUS_MASTER_NODE = 2 +CPUS_WORKER_NODE = 2 +MEMORY_MASTER_NODE = 2048 +MEMORY_WORKER_NODE = 1024 +WORKER_NODES_COUNT = 2 + + +Vagrant.configure(2) do |config| + + config.vm.provision "shell", path: "bootstrap.sh" + + # Kubernetes Master Server + config.vm.define "kmaster" do |node| + + node.vm.box = VAGRANT_BOX + node.vm.box_check_update = false + node.vm.box_version = VAGRANT_BOX_VERSION + node.vm.hostname = "kmaster.home.lab" + + node.vm.network "private_network", ip: "172.16.16.100" + + node.vm.provider :virtualbox do |v| + v.name = "kmaster" + v.memory = MEMORY_MASTER_NODE + v.cpus = CPUS_MASTER_NODE + end + + node.vm.provider :libvirt do |v| + v.memory = MEMORY_MASTER_NODE + v.nested = true + v.cpus = CPUS_MASTER_NODE + end + + node.vm.provision "shell", path: "bootstrap_kmaster.sh" + + end + + + # Kubernetes Worker Nodes + (1..WORKER_NODES_COUNT).each do |i| + + config.vm.define "kworker#{i}" do |node| + + node.vm.box = VAGRANT_BOX + node.vm.box_check_update = false + node.vm.box_version = VAGRANT_BOX_VERSION + node.vm.hostname = "kworker#{i}.home.lab" + + node.vm.network "private_network", ip: "172.16.16.10#{i}" + + node.vm.provider :virtualbox do |v| + v.name = "kworker#{i}" + v.memory = MEMORY_WORKER_NODE + v.cpus = CPUS_WORKER_NODE + end + + node.vm.provider :libvirt do |v| + v.memory = MEMORY_WORKER_NODE + v.nested = true + v.cpus = CPUS_WORKER_NODE + end + + node.vm.provision "shell", path: "bootstrap_kworker.sh" + + end + + end + +end \ No newline at end of file diff --git a/vagrant/bootstrap.sh b/vagrant/bootstrap.sh new file mode 100644 index 0000000..f2a2f71 --- /dev/null +++ b/vagrant/bootstrap.sh @@ -0,0 +1,70 @@ +#!/bin/bash + +## !IMPORTANT ## +# +## This script is tested only in the generic/ubuntu2204 Vagrant box +## If you use a different version of Ubuntu or a different Ubuntu Vagrant box test this again +# + +echo "[TASK 1] Disable and turn off SWAP" +sed -i '/swap/d' /etc/fstab +swapoff -a + +echo "[TASK 2] Stop and Disable firewall" +systemctl disable --now ufw >/dev/null 2>&1 + +echo "[TASK 3] Enable and Load Kernel modules" +cat >>/etc/modules-load.d/containerd.conf<>/etc/sysctl.d/kubernetes.conf</dev/null 2>&1 + +echo "[TASK 5] Install containerd runtime" +export DEBIAN_FRONTEND=noninteractive +apt-get update -qq >/dev/null +apt-get install -qq -y apt-transport-https ca-certificates curl gnupg lsb-release >/dev/null +mkdir -p /etc/apt/keyrings +curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg +echo \ + "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \ + $(lsb_release -cs) stable" > /etc/apt/sources.list.d/docker.list +apt-get update -qq >/dev/null +apt-get install -qq -y containerd.io >/dev/null +containerd config default > /etc/containerd/config.toml +sed -i 's/SystemdCgroup \= false/SystemdCgroup \= true/g' /etc/containerd/config.toml +systemctl restart containerd +systemctl enable containerd >/dev/null + +echo "[TASK 6] Set up kubernetes repo" +curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.29/deb/Release.key | gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg +echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.29/deb/ /' > /etc/apt/sources.list.d/kubernetes.list + +echo "[TASK 7] Install Kubernetes components (kubeadm, kubelet and kubectl)" +apt-get update -qq >/dev/null +apt-get install -qq -y kubeadm kubelet kubectl >/dev/null + +echo "[TASK 8] Enable ssh password authentication" +sed -i 's/^PasswordAuthentication .*/PasswordAuthentication yes/' /etc/ssh/sshd_config +echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config +systemctl reload sshd + +echo "[TASK 9] Set root password" +echo -e "kubeadmin\nkubeadmin" | passwd root >/dev/null 2>&1 +echo "export TERM=xterm" >> /etc/bash.bashrc + +echo "[TASK 10] Update /etc/hosts file" +cat >>/etc/hosts</dev/null + +echo "[TASK 2] Initialize Kubernetes Cluster" +kubeadm init --apiserver-advertise-address=172.16.16.100 --pod-network-cidr=192.168.0.0/16 >> /root/kubeinit.log 2>/dev/null + +echo "[TASK 3] Deploy Calico network" +kubectl --kubeconfig=/etc/kubernetes/admin.conf create -f https://raw.githubusercontent.com/projectcalico/calico/v3.27.0/manifests/tigera-operator.yaml >/dev/null +kubectl --kubeconfig=/etc/kubernetes/admin.conf create -f https://raw.githubusercontent.com/projectcalico/calico/v3.27.0/manifests/custom-resources.yaml >/dev/null + +echo "[TASK 4] Generate and save cluster join command to /joincluster.sh" +kubeadm token create --print-join-command > /joincluster.sh diff --git a/vagrant/bootstrap_kworker.sh b/vagrant/bootstrap_kworker.sh new file mode 100644 index 0000000..c897aae --- /dev/null +++ b/vagrant/bootstrap_kworker.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +echo "[TASK 1] Join node to Kubernetes Cluster" +export DEBIAN_FRONTEND=noninteractive +apt-get install -qq -y sshpass >/dev/null +sshpass -p "kubeadmin" scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no kmaster.example.com:/joincluster.sh /joincluster.sh >/dev/null 2>&1 +bash /joincluster.sh >/dev/null