From 994a857b5de1b1db6db2c84373fee9326957ef60 Mon Sep 17 00:00:00 2001 From: holden093 Date: Sun, 29 Jun 2025 16:46:20 +0200 Subject: [PATCH] added script and readme --- README.md | 29 +++++++++++++++++++++++++++++ ansible/k8s-init.yaml | 1 + ansible/longhorn-reqs.yaml | 36 ++++++++++++++++++++++++++++++++++++ ansible/master-k8s.yaml | 3 ++- init-deploy.sh | 25 +++++++++++++++++++++++++ 5 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 README.md create mode 100644 ansible/longhorn-reqs.yaml create mode 100755 init-deploy.sh diff --git a/README.md b/README.md new file mode 100644 index 0000000..4cf921f --- /dev/null +++ b/README.md @@ -0,0 +1,29 @@ +# Kubernetes Cluster Deployment Guide + +This guide describes how to deploy a Kubernetes cluster tailored to your environment using the provided `init-deploy.sh` script. + +## ⚙️ Pre-requisites + +Before running the deployment script, ensure the following are in place: + +- `kubectl` is installed on your local machine +- The remote user can execute `sudo` commands **without a password prompt** +- SSH host keys of the target nodes are already added to your system (to avoid interactive confirmation prompts) + +## 🧭 Deployment Goals + +This setup will create a Kubernetes cluster with the following topology and components: + +- **1 Control Plane Node** +- **2 Worker Nodes** +- **Calico** as the CNI (Container Network Interface) +- **MetalLB** as the cloud load balancer for bare metal services +- **Longhorn** as the persistent storage provider + +## 🚀 How to Deploy + +To initiate the deployment, run the provided script: + +```bash +chmod +x init-deploy.sh +./init-deploy.sh diff --git a/ansible/k8s-init.yaml b/ansible/k8s-init.yaml index 50b7b96..538ec13 100644 --- a/ansible/k8s-init.yaml +++ b/ansible/k8s-init.yaml @@ -2,6 +2,7 @@ hosts: k8s_control_plane become: true vars: + ## This is the default networkd of Calico pod_network_cidr: "192.168.0.0/16" apiserver_advertise_address: "10.50.3.21" cri_socket: "unix:///run/containerd/containerd.sock" diff --git a/ansible/longhorn-reqs.yaml b/ansible/longhorn-reqs.yaml new file mode 100644 index 0000000..8dcfc41 --- /dev/null +++ b/ansible/longhorn-reqs.yaml @@ -0,0 +1,36 @@ +--- +- name: Setup Longhorn Requirements + hosts: all + become: true + tasks: + + - name: Install required packages + apt: + name: + - open-iscsi + - nfs-common + state: present + update_cache: yes + + - name: Ensure iscsi_tcp kernel module is loaded + modprobe: + name: iscsi_tcp + state: present + + - name: Ensure iscsi_tcp module loads on boot + copy: + dest: /etc/modules-load.d/iscsi.conf + content: | + iscsi_tcp + owner: root + group: root + mode: '0644' + + - name: Disable and stop multipathd service + systemd: + name: multipathd + enabled: false + state: stopped + masked: true + daemon_reload: yes + ignore_errors: true # in case it's not installed diff --git a/ansible/master-k8s.yaml b/ansible/master-k8s.yaml index 5e3077d..0a83223 100644 --- a/ansible/master-k8s.yaml +++ b/ansible/master-k8s.yaml @@ -1,4 +1,5 @@ # master-k8s.yaml - import_playbook: k8s-install-deps.yaml - import_playbook: k8s-init.yaml -- import_playbook: install_cni.yaml \ No newline at end of file +- import_playbook: install_cni.yaml +- import_playbook: longhorn-reqs.yaml \ No newline at end of file diff --git a/init-deploy.sh b/init-deploy.sh new file mode 100755 index 0000000..07735c2 --- /dev/null +++ b/init-deploy.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +KUBE_USER="kevin" +KUBE_CP="k8s-control-plane" +KUBE_W1="k8s-worker1" +KUBE_W2="k8s-worker2" + +ANSIBLE_DIR="ansible" + +## Prepare all of the nodes with k8s using the ansible playbooks I prepared + +ansible-playbook -i ./"$ANSIBLE_DIR"/inventory.ini ./"$ANSIBLE_DIR"/master-k8s.yaml + +## Fetch the configuration from the freshly installed cluster. BEWARE THAT ANY EXISTING CONFIG WILL BE OVERWRITTEN + +scp "$KUBE_USER@$KUBE_CP":"$HOME"/.kube/config "$HOME"/.kube/config + +## Now join the workers to the cluster + +JOIN_TOKEN=$(ssh "$KUBE_USER@$KUBE_CP" "kubeadm token create --print-join-command") + +for NODE in "$KUBE_W1" "$KUBE_W2"; do + echo "Joining $NODE" + ssh "$KUBE_USER@$NODE" "sudo $JOIN_TOKEN" +done \ No newline at end of file