Kubernetes Cluster Setup Script
This script automates the setup of a Kubernetes cluster using Multipass for virtual machines and HAProxy for load balancing. It allows you to create a highly available Kubernetes cluster with customizable resources for master and worker nodes.
Prerequisites
- Multipass: Ensure Multipass is installed on your machine. See Multipass Install.
- kubectl: Install
kubectlto interact with your Kubernetes cluster. See kubectl installation guide. - jq: Install
jqfor JSON parsing. Runsudo apt-get install jqor follow the official guide.
References
- Multipass
- Docker Install
- Creating Highly Available Clusters with kubeadm
- Installing kubeadm
- CRI-O
- Weaveworks
- LUNXXtips YouTube Tutorials
Usage
Clone the Repository:
git clone https://github.com/eliasmeireles/k8s.git cd k8s/learning/cluster-generateRun the Script:
./setup
Options
| Option | Description | Default Value |
|---|---|---|
--base-name | Base name for the cluster (required) | - |
--cluster-version | Kubernetes cluster version (e.g., v1.27) | - |
--num-masters | Number of master nodes (required) | - |
--num-workers | Number of worker nodes (required) | - |
--master-mem | Memory for master nodes | 2G |
--master-cpus | CPUs for master nodes | 2 |
--master-disk | Disk size for master nodes | 22G |
--worker-mem | Memory for worker nodes | 4G |
--worker-cpus | CPUs for worker nodes | 2 |
--worker-disk | Disk size for worker nodes | 32G |
--haproxy-disk | Disk size for HAProxy node | 24G |
--haproxy-cpu | CPUs for HAProxy node | 2 |
--haproxy-memory | Memory for HAProxy node | 4G |
--help | Display help message | - |
Example
./scripts/setup --base-name dev-k8s --cluster-version v1 --num-masters 3 --num-workers 4 --master-mem 3G --worker-mem 6G
- This command will create a Kubernetes cluster named
dev-k8s, with versionv1, 3 master nodes, and 3 worker nodes. Master nodes will have 3GB of memory, and worker nodes will have 6GB of memory.
Network Setup
The script automatically applies a network plugin. For non-ARM architectures, Weave is used:
kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml
For ARM architectures, Calico is applied:
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
Logging
- Logs for each execution are stored in the
./.tempdirectory with filenames based on the timestamp of execution.
Notes
- The script assumes a Unix-like environment.
- Ensure you have sufficient resources (CPU, memory, disk space) on your host machine to run the virtual machines.
- Each node created will have the necessary configuration and Kubernetes setup executed via a transferred script (
kube-config.sh).
Example Output
After completing the steps described in Master Node Setup, you will see the output below:



Installing kube-prometheus
To monitor your cluster, you can install kube-prometheus:
Make sure that you checked the expected cluster context
make apply-kube-prometheus
When all pods in the monitoring namespace are running, access the Prometheus dashboard:
Access the dashboard at http://monitor.v1.k8s.local and use the default credentials (admin/admin).
Cleanup
To delete all instances created by the script, use:
./scripts/delete-instances.sh <pattern>
Replace <pattern> with a keyword that matches the instance names to delete.
Author
Created by Elias Meireles.