Skip to content

Commit

Permalink
Merge pull request #18 from Xtigyro/feature/custom-label-for-nodes
Browse files Browse the repository at this point in the history
Optional Custom Labels for K8s Nodes. README Improvements.
  • Loading branch information
Xtigyro authored Jul 24, 2020
2 parents 34685a9 + 3b2f429 commit f764d81
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,30 @@ Create a local one or multi-node K8s cluster preset for development with one com

## Quick Start

To create a local K8s cluster in Docker container(s) with properly configured Helm v3, Ingress Controller, MetalLB, and Metrics Server - please run:
To create a local one or multi-node K8s cluster in Docker container(s) with properly configured Helm v3, Ingress Controller, MetalLB, and Metrics Server - please run:

```bash
cd local-cluster
bash prerequisites-cmds.sh --helm_ver=3.[x].[x] # Helm ver. is optional.
bash create-cluster.sh --nodes=[1-99] --k8s_ver=1.[x].[x] # Only no. of K8s nodes is mandatory.
```

## Helper Menu

```console
# bash create-cluster.sh -h

Usage:
--k8s_ver,-v Set K8s version to be deployed.
--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.
--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'
```

## Prerequisite Notes

The `prerequisites-cmds.sh` can be used either like a true Shell script, or the commands which are part of it can be executed one by one. It depends on your preference.
Expand Down
29 changes: 20 additions & 9 deletions local-cluster/create-cluster.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ while [ $# -gt 0 ]; do
fi
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
;;
--half-labelled=*|-hl=*)
if [[ "$1" != *=* ]]; then shift; fi # Value is next arg if no `=`
NODE_LABEL="${1#*=}"
COEFFICIENT=0.5
HALF_LABELLED=true
;;
--k8s_ver=*|-v=*)
if [[ "$1" != *=* ]]; then shift; fi
if [[ "$1" != *=1.*.* ]]; then
Expand All @@ -41,7 +53,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}--reset,-r${NC} Resets any old temporary configuration.\n ${LIGHT_GREEN}--help,-h${NC} Prints this message.\nExample:\n ${LIGHT_GREEN}bash $0 -n=1 -v=1.18.2${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}--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
exit 0
;;
*)
Expand Down Expand Up @@ -82,13 +94,12 @@ helmfile -f ./helmfile.yaml apply > /dev/null
CLUSTER_WRKS=$(kubectl get nodes | tail -n +2 | cut -d' ' -f1)
IFS=$'\n' CLUSTER_WRKS=(${CLUSTER_WRKS})
# Put node labels
for ((i=0;i<="${#CLUSTER_WRKS[@]}";i++));
do
if [ -n "${CLUSTER_WRKS[i]}" ] ; then
kubectl label node "${CLUSTER_WRKS[i]}" nodeType=devops
else
break
fi
done
if [[ ! -z "$ALL_LABELLED" ]] || [[ ! -z "$HALF_LABELLED" ]]; then
NO_NODES_LABELLED="$(bc -l <<<"${#CLUSTER_WRKS[@]} * $COEFFICIENT" | 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

0 comments on commit f764d81

Please sign in to comment.