initial commit with basic vagrant stuff

This commit is contained in:
2024-05-30 14:30:09 +02:00
parent 8b4e92fbae
commit 18662bae87
6 changed files with 234 additions and 0 deletions

51
vagrant/README.md Normal file
View File

@@ -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
```

76
vagrant/Vagrantfile vendored Normal file
View File

@@ -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

70
vagrant/bootstrap.sh Normal file
View File

@@ -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<<EOF
overlay
br_netfilter
EOF
modprobe overlay
modprobe br_netfilter
echo "[TASK 4] Add Kernel settings"
cat >>/etc/sysctl.d/kubernetes.conf<<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
sysctl --system >/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<<EOF
172.16.16.100 kmaster.example.com kmaster
172.16.16.101 kworker1.example.com kworker1
172.16.16.102 kworker2.example.com kworker2
EOF

View File

@@ -0,0 +1,14 @@
#!/bin/bash
echo "[TASK 1] Pull required containers"
kubeadm config images pull >/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

View File

@@ -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