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


References


Usage

  1. Clone the Repository:

    git clone https://github.com/eliasmeireles/k8s.git
    cd k8s/learning/cluster-generate
    
  2. Run the Script:

    ./setup
    

Options

OptionDescriptionDefault Value
--base-nameBase name for the cluster (required)-
--cluster-versionKubernetes cluster version (e.g., v1.27)-
--num-mastersNumber of master nodes (required)-
--num-workersNumber of worker nodes (required)-
--master-memMemory for master nodes2G
--master-cpusCPUs for master nodes2
--master-diskDisk size for master nodes22G
--worker-memMemory for worker nodes4G
--worker-cpusCPUs for worker nodes2
--worker-diskDisk size for worker nodes32G
--haproxy-diskDisk size for HAProxy node24G
--haproxy-cpuCPUs for HAProxy node2
--haproxy-memoryMemory for HAProxy node4G
--helpDisplay 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 version v1, 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 ./.temp directory 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:

Cluster setup run example

Multipass filtering created instances

k8s nodes


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.