Last active
November 22, 2024 00:02
-
-
Save alexolinux/f011d53366434713dccb0e2097a6be3c to your computer and use it in GitHub Desktop.
K3d Cluster control as a service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#-- --------------------------- | |
#-- Author: alexolinux ------- | |
#-- --------------------------- | |
k3dCheck(){ | |
command -v k3d > /dev/null | |
if [ $? -eq 0 ]; then | |
bool="true" | |
else | |
bool="false" | |
fi | |
} | |
createCluster(){ | |
k3dCheck | |
if [ "$bool" == "true" ]; then | |
if [ -z "$CLUSTER_NAME" ]; then | |
echo "Error: Cluster name not provided. Usage: $0 create <cluster_name>" | |
exit 1 | |
fi | |
echo "Creating k3d cluster..." | |
k3d cluster create "$CLUSTER_NAME" \ | |
--servers 1 \ | |
--agents 3 \ | |
--k3s-node-label topology.kubernetes.io/zone=zone-a@agent:0 \ | |
--k3s-node-label topology.kubernetes.io/zone=zone-a@agent:1 \ | |
--k3s-node-label topology.kubernetes.io/zone=zone-a@agent:2 \ | |
--api-port 6550 -p "80:80@loadbalancer" \ | |
--volume "${HOME}/kubernetes/volume:/data@agent:*" | |
else | |
echo "k3d has not found." | |
fi | |
} | |
deleteCluster(){ | |
k3dCheck | |
if [ "$bool" == "true" ]; then | |
if [ -z "$CLUSTER_NAME" ]; then | |
echo "Error: Cluster name not provided. Usage: $0 delete <cluster_name>" | |
exit 1 | |
fi | |
echo "Removing k3d cluster..." | |
k3d cluster delete $CLUSTER_NAME | |
else | |
echo "k3d has not found." | |
fi | |
} | |
startCluster(){ | |
k3dCheck | |
if [ "$bool" == "true" ]; then | |
if [ -z "$CLUSTER_NAME" ]; then | |
echo "Error: Cluster name not provided. Usage: $0 start <cluster_name>" | |
exit 1 | |
fi | |
echo "Starting k3d cluster..." | |
k3d cluster start $CLUSTER_NAME | |
else | |
echo "k3d has not found." | |
fi | |
} | |
stopCluster(){ | |
k3dCheck | |
if [ "$bool" == "true" ]; then | |
if [ -z "$CLUSTER_NAME" ]; then | |
echo "Error: Cluster name not provided. Usage: $0 stop <cluster_name>" | |
exit 1 | |
fi | |
echo "Stopping k3d cluster..." | |
k3d cluster stop $CLUSTER_NAME | |
else | |
echo "k3d has not found." | |
fi | |
} | |
if [ "$#" -lt 2 ]; then | |
echo "Usage: $0 [create|delete|start|stop] <cluster_name>" | |
exit 1 | |
fi | |
ACTION=$1 | |
CLUSTER_NAME=$2 | |
case $ACTION in | |
"create") | |
createCluster | |
;; | |
"delete") | |
deleteCluster | |
;; | |
"start") | |
startCluster | |
;; | |
"stop") | |
stopCluster | |
;; | |
*) | |
echo "Invalid action. Usage: $0 [create|delete|start|stop] <cluster_name>" | |
exit 1 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment