#!/bin/bash ## ## Script to set up the kubernetes on Google Cloud environment settings ## main () { echo "This script will create a file .gce_kubernetes.config in the current directory that will contain the variables that you are about to set to set up the Kubernetes cluster on Google Compute Engine. You should also have already authenticated to the Google Cloud via the gcloud command line interface." if [ -f .gce_kubernetes.config ]; then read -p "Kubernetes configuration file exists. Do you wish to display the contents? [y]" show_config show_config=${show_config:-y} if [ "$show_config" == "y" ]; then cat .gce_kubernetes.config read -p "Do you wish to set up the configuration again? [n]" reset_config reset_config=${reset_config:-n} if [ "$reset_config" == "n" ]; then echo "Exiting" else config_gather fi else echo "Setting up the configuration ..." config_gather fi else echo "Setting up the configuration ..." config_gather fi } config_gather () { read -p "Please enter the Google Compute Region you want to use? [us-west1]" gce_region gce_region=${gce_region:-us-west1} read -p "Please enter the Google Compute Zone you want to use? [us-west1-c]" gce_zone gce_zone=${gce_zone:-us-west1-c} read -p "Please enter the Google Compute Project you are going to use? (This should already exist)" gce_project read -p "What version of Kubernetes do you want to install? Tested versions: 1.10.3, 1.10.5, and [1.10.6]" kube_ver=${kube_ver:-1.10.6} read -p "Please enter the name of GCE network you want to create? [kubernetes]" kube_network kube_network=${kube_network:-kubernetes} read -p "Please enter the name of the GCE subnet within the $kube_network you want to create? [kubernetes-subnet]" kube_subnet kube_subnet=${kube_subnet:-kubernetes-subnet} read -p "Please enter the name used for the public IP address object in Google Compute? [kube-public-ip]" kube_pub_ip kube_pub_ip=${kube_pub_ip:-kube-public-ip} read -p "Please enter the CIDR network address range of the kubernetes subnet? [10.240.0.0/24]" kube_subnet_cidr kube_subnet_cidr=${kube_subnet_cidr:-10.240.0.0/24} kube_subnet_addr_calc=`ipcalc -n $kube_subnet_cidr | cut -c 9- | rev | cut -c 3- | rev` read -p "Please enter the lowest network component of the IP address range [$kube_subnet_addr_calc]?" kube_subnet_addr kube_subnet_addr=${kube_subnet_addr:-$kube_subnet_addr_calc} read -p "Please enter the pod CIDR network address to be used? [10.200.0.0/16]" kube_pod_cidr kube_pod_cidr=${kube_pod_cidr:-10.200.0.0/16} kube_pod_addr_calc=`ipcalc -n $kube_pod_cidr | cut -c 9- | rev | cut -c 5- | rev` read -p "Please enter the lowest network component of the pod address range [$kube_pod_addr_calc]?" kube_pod_addr kube_pod_addr=${kube_pod_addr:-$kube_pod_addr_calc} kube_pod_cidr_prefix=`echo $kube_pod_cidr | rev| cut -c -2 | rev` kube_pod_node_prefix=$(($kube_pod_cidr_prefix + 8)) read -p "Best guess at the CIDR prefix used on the nodes for the pod subnets [$kube_pod_node_prefix]" kube_node_pod_prefix kube_node_pod_prefix=${kube_node_pod_prefix:-$kube_pod_node_prefix} read -p "Firewall rules will be created for internal traffic (all allowed) and external traffic (ssh, ping, kubernetes). You can provide a name for the internal traffic rule: [kubernetes-allow-internal]" kube_int_fw_name kube_int_fw_name=${kube_int_fw_name:-kubernetes-allow-internal} read -p " You can provide a name for the external traffic rule: [kubernetes-allow-external]" kube_ext_fw_name kube_ext_fw_name=${kube_ext_fw_name:-kubernetes-allow-external} read -p "Please enter the name used for the Kubernetes Cluster object? [kube-cluster]" kube_cluster kube_cluster=${kube_cluster:-kube-cluster} read -p "How many master (controller) nodes in the Kubernetes cluster do you want to create (currently a maximum of 9)? [2]" kube_masters kube_masters=${kube_masters:-2} read -p "How many worker nodes in the Kubernetes cluster do you want to create (currently a maximum of 9)? [2]" kube_workers kube_workers=${kube_workers:-2} echo "The following variables will be committed to the configuration file: GCE region: $gce_region GCE zone: $gce_zone GCE project: $gce_project Kubernetes Version: $kube_ver GCE VPC network name: $kube_network GCE VPC subnet name: $kube_subnet Kubernetes public IP address object name: $kube_pub_ip Kubernetes subnet CIDR address: $kube_subnet_cidr Kubernetes subnet address: $kube_subnet_addr Kubernetes pod subnet CIDR address: $kube_pod_cidr Kubernetes pod address: $kube_pod_addr Kubernetes node pod CIDR prefix: $kube_node_pod_prefix Kubernetes internal firewall rule: $kube_int_fw_name Kubernetes external firewall rule: $kube_ext_fw_name Kubernetes cluster object name: $kube_cluster Number of controller nodes: $kube_masters Number of worker nodes: $kube_workers" read -p "Please confirm that these values are correct (y/n) [y]" confirm_values confirm_values=${confirm_values:-y} if [ "$confirm_values" == "y" ]; then cat > .gce_kubernetes.config << EOM GCE_REGION=$gce_region GCE_ZONE=$gce_zone GCE_PROJECT=$gce_project KUBE_VER=$kube_ver KUBE_NETWORK=$kube_network KUBE_SUBNET=$kube_subnet KUBE_PUB_IP=$kube_pub_ip KUBE_SUBNET_CIDR=$kube_subnet_cidr KUBE_SUBNET_ADDR=$kube_subnet_addr KUBE_POD_CIDR=$kube_pod_cidr KUBE_POD_ADDR=$kube_pod_addr KUBE_NODE_POD_PREFIX=$kube_node_pod_prefix KUBE_INT_FW_NAME=$kube_int_fw_name KUBE_EXT_FW_NAME=$kube_ext_fw_name KUBE_CLUSTER=$kube_cluster KUBE_CONTROLLERS=$kube_masters KUBE_WORKERS=$kube_workers EOM fi } main