2025-08-14 22:10:42 +01:00
#!/bin/sh
# 1st Step - Ask user for no. of cluster nodes
echo "How many nodes would you like your cluster to have?"
read NODECOUNT
# 2nd Step - Ask user for node details & store in tmp files
for ( ( i = 0; i < $NODECOUNT ; i++) )
do
echo " Please enter the IP address of the ${ i } th node: "
read IPADDR_$i
echo " ${ IPADDR_ $i } " > /tmp/node${ i } _ip.txt
echo " Please enter the username for SSH access to the ${ i } th node (defaults to 'root'): "
read USERNAME_$i
if [ -z $USERNAME ]
then USERNAME = "root" ;
fi
echo " ${ USERNAME } " > /tmp/node${ i } _user.txt
echo " Please enter the password for SSH access to the ${ i } th node (defaults to 'root'): "
read PASSWORD_$i
if [ -z $PASSWORD ]
then PASSWORD = "root" ;
fi
echo " ${ PASSWORD } " > /tmp/node${ i } _pass.txt
done
# 3rd Step - Confirm node details with user & allow them to edit if necessary
echo "The following IP addresses were entered by the user:"
for ( ( i = 0; i < $NODECOUNT ; i++) )
do
echo " ${ IPADDR_ $i } "
done
read -p "Please confirm these are correct, or type 'edit' to make changes: " USERCONFIRMATION
if [ " $USERCONFIRMATION " = = "edit" ]
then
for ( ( i = 0; i < $NODECOUNT ; i++) )
do
echo " Please enter the IP address of the ${ i } th node: "
read IPADDR_$i
echo " ${ IPADDR_ $i } " > /tmp/node${ i } _ip.txt
echo " Please enter the username for SSH access to the ${ i } th node (defaults to 'root'): "
read USERNAME_$i
if [ -z $USERNAME ]
then USERNAME = "root" ;
fi
echo " ${ USERNAME } " > /tmp/node${ i } _user.txt
echo " Please enter the password for SSH access to the ${ i } th node (defaults to 'root'): "
read PASSWORD_$<s> i
if [ -z $PASSWORD ]
then PASSWORD = "root" ;
fi
echo " ${ PASSWORD } " > /tmp/node${ i } _pass.txt
done
fi
# 4th Step - Ask user to define master node & store selection in tmp files
echo "Please enter the number of the node you would like to define as your control plane (master):"
read MASTERNODE_SELECTION
echo " ${ MASTERNODE_SELECTION } " > /tmp/master.txt
# 5th Step - Add K8S repo & relevant keys to each node, install K8S & restart services
for ( ( i = 0; i < $NODECOUNT ; i++) )
do
echo " Adding Kubernetes repo to the system sources list on ${ IPADDR_ $i } ... "
ssh -n $USERNAME_ $i @${ IPADDR_ $i } 'sudo sh -c "echo deb http://apt.kubernetes.io/ kubernetes-xenial main > /etc/apt/sources.list.d/kubernetes.list"'
echo " Adding GPG key to the system on ${ IPADDR_ $i } ... "
ssh -n $USERNAME_ $i @${ IPADDR_ $i } 'sudo curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -'
echo " Updating package list on ${ IPADDR_ $i } ... "
ssh -n $USERNAME_ $i @${ IPADDR_ $i } 'sudo sh -c "apt update"'
echo " Upgrading packages on ${ IPADDR_ $i } ... "
ssh -n $USERNAME_ $i @${ IPADDR_ $i } 'sudo sh -c "apt upgrade"'
done
# 6th Step - Install K8S on master node and ask for configuration options from user
echo "Installing Kubernetes on the master node..."
ssh -n $USERNAME_ $MASTERNODE_SELECTION @${ IPADDR_ $MASTERNODE_SELECTION } 'sudo sh -c "apt install kubeadm; apt-get install kubelet; apt-get install kubernetes-cni"'
echo "Please enter your preferred port number for the control plane (master node) (defaults to 6443): "
read PORT_SELECTION
if [ -z $PORT_SELECTION ]
then PORT_SELECTION = "6443" ;
fi
echo "Please enter your preferred IP address or FQDN for the control plane (master node) (defaults to '0.0.0.0'): "
read IPADDRESS_SELECTION
if [ -z $IPADDRESS_SELECTION ]
then IPADDRESS_SELECTION = "0.0.0.0" ;
fi
echo " Configuring Kubernetes on the master node, using port ${ PORT_SELECTION } and listening IP/domain ${ IPADDRESS_SELECTION } ... "
ssh -n $USERNAME_ $MASTERNODE_SELECTION @${ IPADDR_ $MASTERNODE_SELECTION } 'sudo sh -c "kubeadm init --apiserver-advertise-address=$IPADDRESS_SELECTION --apiserver-bind-port=$PORT_SELECTION"'
echo "Please enter the following options to configure the Kubernetes control plane (master node)..."
echo "1. Pod Network CIDR: "
read NETWORKCIDR_SELECTION
echo " ${ NETWORKCIDR_SELECTION } " > /tmp/network.txt
echo "2. Service Subnet: "
read SERVICESUBNET_SELECTION
echo " ${ SERVICESUBNET_SELECTION } " > /tmp/servicesubnet.txt
echo "3. Kubernetes DNS Domain: "
read K8SDNS_SELECTION
echo " ${ K8SDNS_SELECTION } " > /tmp/kube-dns.txt
echo "Please enter your preferred Kubernetes version (defaults to '1.20.1'): "
read K8SVERSION_SELECTION
if [ -z $K8SVERSION_SELECTION ]
then K8SVERSION_SELECTION = "1.20.1" ;
fi
echo "Please enter your preferred pod Network provider (defaults to 'calico'): "
read NETWORKPROVIDER_SELECTION
if [ -z $NETWORKPROVIDER_SELECT <s> ION_SELECTION ]
then NETWORKPROVIDER_SELECTION = "calico" ;
fi
echo " Configuring Kubernetes on the master node with pod Network CIDR: ${ NETWORKCIDR_SELECTION } , service subnet: ${ SERVICESUBNET_SELECTION } , K8S DNS domain: ${ K8SDNS_SELECTION } , K8S version: ${ K8SVERSION_SELECTION } , and pod network provider: ${ NETWORKPROVIDER_SELECTION } ... "
ssh -n $USERNAME_ $MASTERNODE_SELECTION @${ IPADDR_ $MASTERNODE_SELECTION } 'sudo sh -c "kubectl apply --kubelet-extra-args ' --node-ip= $IPADDRESS_SELECTION ' -f https://docs.projectcalico.org/manifests/kube-flannel.yaml; kubeadm init phase addon all; kubectl apply --kubelet-extra-args ' --node-ip= $IPADDRESS_SELECTION ' -f https://docs.projectcalico.org/manifests/kube-flannel.yaml; kubectl create deployment nginx-deployment --image=nginx; kubectl expose deployments nginx-deployment --port 80 --type LoadBalancer"'
echo "Restarting Kubernetes services on the master node..."
ssh -n $USERNAME_ $MASTERNODE_SELECTION @${ IPADDR_ $MASTERNODE_SELECTION } 'sudo sh -c "systemctl restart kubelet.service"'
echo "Restarting Kubernetes services on all nodes..."
for ( ( i = 0; i < $NODECOUNT ; i++) )
do
echo " Restarting Kubernetes services on the ${ IPADDR_ $i } node... "
ssh -n $USERNAME_ $i @${ IPADDR_ $i } 'sudo sh -c "systemctl restart kubelet.service"'
done