From 1577283a8918accb9bf147e3acf1245bb6dc329b Mon Sep 17 00:00:00 2001 From: Xtigyro Date: Sun, 26 Jul 2020 23:34:45 +0300 Subject: [PATCH 1/3] optional node tainting --- README.md | 4 ++- local-cluster/create-cluster.sh | 46 ++++++++++++++++++++--------- local-cluster/prerequisites-cmds.sh | 1 - 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 09a147d..fce06e6 100644 --- a/README.md +++ b/README.md @@ -28,10 +28,12 @@ Usage: --nodes,-n Set number of K8s nodes to be created. --all-labelled,-al Set labels on all K8s nodes. --half-labelled,-hl Set labels on half K8s nodes. + --all-tainted,-at Set taints on all K8s nodes. A different label can be defined. + --half-tainted,-ht Set taints on half K8s nodes. A different label can be defined. --reset,-r Resets any old temporary configuration. --help,-h Prints this message. Example: - bash create-cluster.sh -n=1 -v=1.18.2 -hl='nodeType=devops' + bash create-cluster.sh -n=2 -v=1.18.2 -hl='nodeType=devops' -ht ``` ## Prerequisite Notes diff --git a/local-cluster/create-cluster.sh b/local-cluster/create-cluster.sh index bd048be..73e7e7b 100644 --- a/local-cluster/create-cluster.sh +++ b/local-cluster/create-cluster.sh @@ -15,7 +15,6 @@ fi while [ $# -gt 0 ]; do case "$1" in --nodes=*|-n=*) - if [[ "$1" != *=* ]]; then shift; fi # Value is next arg if no `=` if [[ "$1" != *=[1-9] ]] && [[ "$1" != *=[1-9][1-9] ]]; then printf "\nNo. of K8s nodes must be: ${LIGHT_GREEN}1-99${NC}.\n" exit 1 @@ -23,19 +22,22 @@ while [ $# -gt 0 ]; do NO_NODES="${1#*=}" ;; --all-labelled=*|-al=*) - if [[ "$1" != *=* ]]; then shift; fi # Value is next arg if no `=` NODE_LABEL="${1#*=}" - COEFFICIENT=1 - ALL_LABELLED=true + COEFFICIENT_LABEL=1 ;; --half-labelled=*|-hl=*) - if [[ "$1" != *=* ]]; then shift; fi # Value is next arg if no `=` NODE_LABEL="${1#*=}" - COEFFICIENT=0.5 - HALF_LABELLED=true + COEFFICIENT_LABEL=0.5 + ;; + --all-tainted=*|-at=*|--all-tainted|-at) + if [[ "$1" == *=* ]]; then NODE_TAINT_LABEL="${1#*=}"; fi + COEFFICIENT_TAINT=1 + ;; + --half-tainted=*|-ht=*|--half-tainted|-ht) + if [[ "$1" == *=* ]]; then NODE_TAINT_LABEL="${1#*=}"; fi + COEFFICIENT_TAINT=0.5 ;; --k8s_ver=*|-v=*) - if [[ "$1" != *=* ]]; then shift; fi if [[ "$1" != *=1.*.* ]]; then printf "\nIncompatible K8s node ver.\nCorrect syntax/version: ${LIGHT_GREEN}1.[x].[x]${NC}\n" exit 2 @@ -53,7 +55,7 @@ while [ $# -gt 0 ]; do fi ;; --help|-h) - printf "\nUsage:\n ${LIGHT_GREEN}--k8s_ver,-v${NC} Set K8s version to be deployed.\n ${LIGHT_GREEN}--nodes,-n${NC} Set number of K8s nodes to be created.\n ${LIGHT_GREEN}--all-labelled,-al${NC} Set labels on all K8s nodes.\n ${LIGHT_GREEN}--half-labelled,-hl${NC} Set labels on half K8s nodes.\n ${LIGHT_GREEN}--reset,-r${NC} Resets any old temporary configuration.\n ${LIGHT_GREEN}--help,-h${NC} Prints this message.\nExample:\n ${LIGHT_GREEN}bash $0 -n=2 -v=1.18.2 -hl='nodeType=devops' ${NC}\n" # Flag argument + printf "\nUsage:\n ${LIGHT_GREEN}--k8s_ver,-v${NC} Set K8s version to be deployed.\n ${LIGHT_GREEN}--nodes,-n${NC} Set number of K8s nodes to be created.\n ${LIGHT_GREEN}--all-labelled,-al${NC} Set labels on all K8s nodes.\n ${LIGHT_GREEN}--half-labelled,-hl${NC} Set labels on half K8s nodes.\n ${LIGHT_GREEN}--all-tainted,-at${NC} Set taints on all K8s nodes. A different label can be defined.\n ${LIGHT_GREEN}--half-tainted,-ht${NC} Set taints on half K8s nodes. A different label can be defined.\n ${LIGHT_GREEN}--reset,-r${NC} Resets any old temporary configuration.\n ${LIGHT_GREEN}--help,-h${NC} Prints this message.\nExample:\n ${LIGHT_GREEN}bash $0 -n=2 -v=1.18.2 -hl='nodeType=devops' -ht ${NC}\n" # Flag argument exit 0 ;; *) @@ -84,22 +86,38 @@ for (( i=0; i<"${NO_NODES_CREATE}"; ++i)); do echo -e "${KIND_WRKR_CFG}" >> "${KIND_CFG}" done + # Create kINd cluster kind create cluster --config "${KIND_CFG}" --name kind-"${NO_NODES}" + # Revert the kINd config yes | mv "${KIND_CFG}.backup" "${KIND_CFG}" + # Deploy desired svc-s -helmfile -f ./helmfile.yaml apply > /dev/null +# helmfile -f ./helmfile.yaml apply > /dev/null + # Get node names CLUSTER_WRKS=$(kubectl get nodes | tail -n +2 | cut -d' ' -f1) IFS=$'\n' CLUSTER_WRKS=(${CLUSTER_WRKS}) + # Put node labels -if [[ ! -z "$ALL_LABELLED" ]] || [[ ! -z "$HALF_LABELLED" ]]; then - NO_NODES_LABELLED="$(bc -l <<<"${#CLUSTER_WRKS[@]} * $COEFFICIENT" | awk '{printf("%d\n",$1 - 0.5)}')" +if [[ ! -z "$COEFFICIENT_LABEL" ]]; then + NO_NODES_LABELLED="$(bc -l <<<"${#CLUSTER_WRKS[@]} * $COEFFICIENT_LABEL" | awk '{printf("%d\n",$1 - 0.5)}')" for ((i=1;i<="$NO_NODES_LABELLED";i++)); do kubectl label node "${CLUSTER_WRKS[i]}" "$NODE_LABEL" done fi -# Taint the node -# kubectl taint node -l nodeType=devops nodeType=devops:NoExecute + +# Taint the nodes with "NoExecute" +if [[ ! -z "$COEFFICIENT_TAINT" ]]; then + NO_NODES_TAINTED="$(bc -l <<<"${#CLUSTER_WRKS[@]} * $COEFFICIENT_TAINT" | awk '{printf("%d\n",$1 - 0.5)}')" + for ((i=1;i<="$NO_NODES_TAINTED";i++)); + do + if [[ ! -z "$NODE_LABEL" ]] && [[ -z "$NODE_TAINT_LABEL" ]] ; then + kubectl taint node "${CLUSTER_WRKS[i]}" "$NODE_LABEL":NoExecute + else + kubectl taint node "${CLUSTER_WRKS[i]}" "$NODE_TAINT_LABEL":NoExecute + fi + done +fi diff --git a/local-cluster/prerequisites-cmds.sh b/local-cluster/prerequisites-cmds.sh index bf6a687..ed94abe 100644 --- a/local-cluster/prerequisites-cmds.sh +++ b/local-cluster/prerequisites-cmds.sh @@ -10,7 +10,6 @@ HELM_VER='3.2.4' while [ $# -gt 0 ]; do case "$1" in --helm_ver=*|-hv=*) - if [[ "$1" != *=* ]]; then shift; fi if [[ "$1" != *=3.*.* ]]; then printf "\nIncompatible Helm ver.\nSupported syntax/version: ${LIGHT_GREEN}3.[x].[x]${NC}\n" exit 1 From 05cc523a54c46f2c89a698cdefdd44f5cfa85947 Mon Sep 17 00:00:00 2001 From: Xtigyro Date: Sun, 26 Jul 2020 23:50:38 +0300 Subject: [PATCH 2/3] new demo --- local-cluster/create-cluster.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/local-cluster/create-cluster.sh b/local-cluster/create-cluster.sh index 73e7e7b..ffb54c6 100644 --- a/local-cluster/create-cluster.sh +++ b/local-cluster/create-cluster.sh @@ -94,7 +94,7 @@ kind create cluster --config "${KIND_CFG}" --name kind-"${NO_NODES}" yes | mv "${KIND_CFG}.backup" "${KIND_CFG}" # Deploy desired svc-s -# helmfile -f ./helmfile.yaml apply > /dev/null +helmfile -f ./helmfile.yaml apply > /dev/null # Get node names CLUSTER_WRKS=$(kubectl get nodes | tail -n +2 | cut -d' ' -f1) From ea4ba9139887ddecefaed9a966b1d12ced82fc18 Mon Sep 17 00:00:00 2001 From: Xtigyro Date: Sun, 26 Jul 2020 23:53:24 +0300 Subject: [PATCH 3/3] small fixes