From a52d9c49b7f4166b18a49b4907a2b0afba3aac05 Mon Sep 17 00:00:00 2001 From: makefunstuff Date: Thu, 18 Jul 2024 22:04:29 +0300 Subject: kvm cluster --- scripts/make-cluster | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ scripts/setup-kvm | 48 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 scripts/make-cluster create mode 100644 scripts/setup-kvm (limited to 'scripts') diff --git a/scripts/make-cluster b/scripts/make-cluster new file mode 100644 index 0000000..90ba009 --- /dev/null +++ b/scripts/make-cluster @@ -0,0 +1,64 @@ +#!/bin/bash + +# Number of nodes to create +NUM_NODES=3 +BASE_IMAGE="ubuntu-18.04-minimal-cloudimg-amd64.img" +NETWORK="default" +MEMORY=2048 +VCPUS=2 + +# Generate cloud-init ISO for each node +for i in $(seq 1 $NUM_NODES); do + VM_NAME="k3s-node-$i" + CLOUD_INIT_DIR="cloud-init-$VM_NAME" + mkdir -p $CLOUD_INIT_DIR + + cat < $CLOUD_INIT_DIR/meta-data +instance-id: $VM_NAME +local-hostname: $VM_NAME +EOF + + cp user-data $CLOUD_INIT_DIR/ + + # Create cloud-init ISO + genisoimage -output $CLOUD_INIT_DIR/cloud-init.iso -volid cidata -joliet -rock $CLOUD_INIT_DIR/user-data $CLOUD_INIT_DIR/meta-data + + # Create VM + virt-install \ + --name $VM_NAME \ + --memory $MEMORY \ + --vcpus $VCPUS \ + --disk size=10,backing_store=$BASE_IMAGE \ + --disk path=$CLOUD_INIT_DIR/cloud-init.iso,device=cdrom \ + --os-type linux \ + --os-variant ubuntu18.04 \ + --network network=$NETWORK \ + --graphics none \ + --console pty,target_type=serial \ + --import \ + --noautoconsole + + echo "Created VM $VM_NAME" +done + +# Wait for VMs to boot up and install K3s +sleep 60 + +# Get the IP address of the master node +MASTER_IP=$(virsh domifaddr k3s-node-1 | grep -oP '(\d+\.\d+\.\d+\.\d+)' | head -1) + +# Set up the master node +ssh ubuntu@$MASTER_IP "curl -sfL https://get.k3s.io | sh -" + +# Get the K3s token from the master node +K3S_TOKEN=$(ssh ubuntu@$MASTER_IP "sudo cat /var/lib/rancher/k3s/server/node-token") + +# Set up the worker nodes +for i in $(seq 2 $NUM_NODES); do + NODE_IP=$(virsh domifaddr k3s-node-$i | grep -oP '(\d+\.\d+\.\d+\.\d+)' | head -1) + ssh ubuntu@$NODE_IP "curl -sfL https://get.k3s.io | K3S_URL=https://$MASTER_IP:6443 K3S_TOKEN=$K3S_TOKEN sh -" + echo "Joined k3s-node-$i to the cluster" +done + +echo "K3s cluster setup completed. Master node IP: $MASTER_IP" + diff --git a/scripts/setup-kvm b/scripts/setup-kvm new file mode 100644 index 0000000..fe17d26 --- /dev/null +++ b/scripts/setup-kvm @@ -0,0 +1,48 @@ +#!/bin/bash + +# Function to print messages +print_message() { + echo "====================================================================" + echo "$1" + echo "====================================================================" +} + +# Update the system +print_message "Updating the system..." +sudo pacman -Syu --noconfirm + +# Install necessary packages +print_message "Installing necessary packages..." +sudo pacman -S --noconfirm qemu libvirt virt-manager dnsmasq bridge-utils + +# Enable and start the libvirtd service +print_message "Enabling and starting libvirtd service..." +sudo systemctl enable libvirtd.service +sudo systemctl start libvirtd.service + +# Add the current user to the libvirt group +CURRENT_USER=$(whoami) +print_message "Adding $CURRENT_USER to the libvirt group..." +sudo usermod -aG libvirt $CURRENT_USER + +# Verify virtualization support +print_message "Verifying virtualization support..." +VIRT_SUPPORT=$(egrep -c '(vmx|svm)' /proc/cpuinfo) +if [ "$VIRT_SUPPORT" -gt 0 ]; then + echo "Virtualization is supported." +else + echo "Virtualization is not supported. Please enable it in your BIOS/UEFI settings." + exit 1 +fi + +# Check if the default network is active and start it if not +print_message "Checking and starting default network..." +DEFAULT_NET_STATUS=$(sudo virsh net-list --all | grep default | awk '{print $2}') +if [ "$DEFAULT_NET_STATUS" != "active" ]; then + sudo virsh net-start default + sudo virsh net-autostart default +fi + +# Print final message +print_message "KVM setup is complete. Please log out and log back in to apply the user group changes." + -- cgit 1.4.1-2-gfad0