From 91ac1ffb091073210ca14d8d72f3f955641225bb Mon Sep 17 00:00:00 2001 From: J-hoplin1 Date: Mon, 3 Jul 2023 13:43:02 +0900 Subject: [PATCH] enable k3s version select --- .env | 5 +++++ Readme.md | 10 ++++++++++ app.py | 10 +++++----- cluster.py | 5 +++-- scripts/common/master-node-util.sh | 19 +++++++++++++++++++ scripts/nodeInit.sh | 9 ++++++--- 6 files changed, 48 insertions(+), 10 deletions(-) create mode 100644 scripts/common/master-node-util.sh diff --git a/.env b/.env index f1d8ff2..bbae92f 100644 --- a/.env +++ b/.env @@ -1,5 +1,10 @@ +#Multipass Ubuntu Version MULTIPASS_INSTANCE=22.04 +# K3S Version +K3S_VERSION=v1.26.6+k3s1 + + MASTER_DEFAULT_CPU=2 MASTER_DEFAULT_MEMORY=2048M MASTER_DEFAULT_STORAGE=20G diff --git a/Readme.md b/Readme.md index 00c94bb..56462a6 100644 --- a/Readme.md +++ b/Readme.md @@ -58,6 +58,16 @@ cd K3S-Virtual-Cluster pip3 install -r requirements.txt ``` +3. Make your K3S Version of dotenv file. Default is `v1.26.6+k3s1`. Find available version [here](https://github.com/k3s-io/k3s/releases) + +``` +... + +K3S_VERSION=v1.26.6+k3s1 + +... +``` + --- ## Commands diff --git a/app.py b/app.py index 7780cd2..a23edb0 100644 --- a/app.py +++ b/app.py @@ -25,17 +25,17 @@ class Resolver(Utility): def __init__(self): pass - def cluster_init(self): + def cluster_init(self, version): os.system('clear') def NodeInit(name, cpu, memory, disk, tp, *args): if tp == NodeType.MASTER: subprocess.run(["bash", f"{Assets.SCRIPT_PATH}/nodeInit.sh", name, - cpu, memory, disk, tp], stdout=subprocess.PIPE) + cpu, memory, disk, tp, version], stdout=subprocess.PIPE) elif tp == NodeType.WORKER: token, ip = args subprocess.run(["bash", f"{Assets.SCRIPT_PATH}/nodeInit.sh", name, cpu, - memory, disk, tp, token, ip], stdout=subprocess.PIPE) + memory, disk, tp, token, ip, version], stdout=subprocess.PIPE) else: raise exceptions.IllegalControlException("Invalid node type") @@ -207,7 +207,7 @@ def terminate_cluster(self): print(self.getNormalMessage("Complete to terminate cluster!")) @InstanceNameAlreadyTakenChecker() - def add_node(self, name, **kwargs): + def add_node(self, name, version, **kwargs): masterConfig: dict = self.readConfig(Assets.MASTER_CONFIG) # Get worker node config workerConfig: dict = self.readConfig(Assets.WORKER_CONFIG) @@ -236,7 +236,7 @@ def add_node(self, name, **kwargs): print(self.getNormalMessage( f"Initiating worker node : {workerNodeName} ...")) subprocess.run(["bash", f"{Assets.SCRIPT_PATH}/nodeInit.sh", workerNodeName, workerNodeCPU, - workerNodeMemory, workerNodeStorage, NodeType.WORKER, token, ip], stdout=subprocess.PIPE) + workerNodeMemory, workerNodeStorage, NodeType.WORKER, token, ip, version], stdout=subprocess.PIPE) print(self.getNormalMessage( f"Complete to build worker node : {workerNodeName} ...")) workerConfig[name]["ip"] = subprocess.run( diff --git a/cluster.py b/cluster.py index 330e393..ad73e8d 100644 --- a/cluster.py +++ b/cluster.py @@ -1,4 +1,5 @@ import argparse +import os import dotenv from app import Resolver from exception import exceptions @@ -25,13 +26,13 @@ try: if argList.cluster == "init": - resolver.cluster_init() + resolver.cluster_init(os.environ["K3S_VERSION"]) elif argList.cluster == "terminate": resolver.terminate_cluster() elif argList.cluster == "add": if not argList.name: raise exceptions.RequiredCommandLineOptionLost('-n') - resolver.add_node(argList.name) + resolver.add_node(argList.name, os.environ["K3S_VERSION"]) elif argList.cluster == "shell": if not argList.name: raise exceptions.RequiredCommandLineOptionLost('-n') diff --git a/scripts/common/master-node-util.sh b/scripts/common/master-node-util.sh new file mode 100644 index 0000000..644c625 --- /dev/null +++ b/scripts/common/master-node-util.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +function master-node-install(){ + # Master node global installation script + + sudo snap install helm --classic + sudo snap install jq + + # Prometheus metrics for lens metrics collector + export KUBECONFIG=/etc/rancher/k3s/k3s.yaml + helm repo add prometheus-community https://prometheus-community.github.io/helm-charts + helm repo add stable https://charts.helm.sh/stable + helm repo update + helm install prometheus prometheus-community/kube-prometheus-stack +} + + +# Ignore stdin, stdout, stderr outputs to prevent frontend warning logs +master-node-install > /dev/null 2>&1 diff --git a/scripts/nodeInit.sh b/scripts/nodeInit.sh index 3113c36..a658ca7 100644 --- a/scripts/nodeInit.sh +++ b/scripts/nodeInit.sh @@ -7,17 +7,20 @@ node_disk=$4 node_type=$5 master_token=$6 master_ip=$7 +k3s_version=$8 common_path=$(dirname $(realpath $0))/common - if [ $node_type = "master" ]; then echo "Here" multipass launch --name $node_name --cpus $node_cpu --memory $node_memory --disk $node_disk - multipass exec $node_name -- /bin/bash -c "curl -sfL https://get.k3s.io | K3S_KUBECONFIG_MODE="644" sh -" + multipass exec $node_name -- /bin/bash -c "curl -sfL https://get.k3s.io | INSTALL_K3S_CHANNEL='$k3s_version' K3S_KUBECONFIG_MODE='644' INSTALL_K3S_EXEC='--disable traefik' sh -" + multipass transfer $common_path/master-node-util.sh $node_name:master-node-util.sh + multipass exec $node_name -- /bin/bash -c "chmod u+rwx ~/master-node-util.sh" + multipass exec $node_name -- /bin/bash -c "~/master-node-util.sh" else multipass launch --name $node_name --cpus $node_cpu --memory $node_memory --disk $node_disk - multipass exec $node_name -- /bin/bash -c "curl -sfL https://get.k3s.io | K3S_TOKEN=\"$master_token\" K3S_URL=https://$master_ip:6443 sh -" + multipass exec $node_name -- /bin/bash -c "curl -sfL https://get.k3s.io | INSTALL_K3S_CHANNEL='$k3s_version' K3S_TOKEN='$master_token' K3S_URL=https://$master_ip:6443 sh -" fi multipass transfer $common_path/util.sh $node_name:util.sh