add variables for more config options maintenance

This commit is contained in:
Bartal Læarsson
2025-02-28 10:44:08 +00:00
parent eaafb90365
commit 20d1964db3
7 changed files with 85 additions and 20 deletions
+22
View File
@@ -0,0 +1,22 @@
---
# Kubernetes configuration
kubernetes_version: "1.32"
pod_cidr: "192.168.0.0/16"
service_cidr: "10.96.0.0/16"
# CNI plugin choice (options: calico, flannel, etc.)
cni_plugin: "calico"
calico_version: "v3.29.1"
# Node resources
control_plane_cpus: 2
control_plane_memory: 4G
control_plane_disk: 20G
worker_cpus: 2
worker_memory: 4G
worker_disk: 20G
# DNS configuration
dns_servers:
- "8.8.8.8"
- "8.8.4.4"
+4 -4
View File
@@ -6,15 +6,15 @@ all:
control_plane: control_plane:
hosts: hosts:
controlplane: controlplane:
ansible_host: 10.203.146.138 ansible_host: 10.203.146.40
workers: workers:
hosts: hosts:
node01: node01:
ansible_host: 10.203.146.17 ansible_host: 10.203.146.133
node02: node02:
ansible_host: 10.203.146.203 ansible_host: 10.203.146.11
node03: node03:
ansible_host: 10.203.146.70 ansible_host: 10.203.146.223
vars: vars:
ansible_user: ubuntu ansible_user: ubuntu
ansible_become: yes ansible_become: yes
+8 -2
View File
@@ -1,8 +1,9 @@
--- ---
# ansible/playbooks/kubernetes.yml
- name: Common configuration for all nodes - name: Common configuration for all nodes
hosts: k8s_cluster hosts: k8s_cluster
become: yes become: yes
vars_files:
- ../defaults/kubernetes.yml
tags: tags:
- common - common
tasks: tasks:
@@ -14,6 +15,8 @@
- name: Kubernetes setup for all nodes - name: Kubernetes setup for all nodes
hosts: k8s_cluster hosts: k8s_cluster
become: yes become: yes
vars_files:
- ../defaults/kubernetes.yml
tags: tags:
- k8s-setup - k8s-setup
tasks: tasks:
@@ -22,6 +25,8 @@
- name: Initialize Kubernetes control plane - name: Initialize Kubernetes control plane
hosts: control_plane hosts: control_plane
become: yes become: yes
vars_files:
- ../defaults/kubernetes.yml
tags: tags:
- control-plane - control-plane
tasks: tasks:
@@ -30,12 +35,13 @@
- name: Join worker nodes to cluster - name: Join worker nodes to cluster
hosts: workers hosts: workers
become: yes become: yes
vars_files:
- ../defaults/kubernetes.yml
tags: tags:
- workers - workers
tasks: tasks:
- include_tasks: ../tasks/kubernetes/worker-join.yml - include_tasks: ../tasks/kubernetes/worker-join.yml
- name: Validate Kubernetes cluster - name: Validate Kubernetes cluster
hosts: k8s_cluster hosts: k8s_cluster
become: yes become: yes
@@ -1,9 +1,9 @@
--- ---
# tasks/control-plane-init.yml # tasks/control-plane-init.yml
- name: Set Kubernetes network CIDRs for Calico - name: Set Kubernetes network CIDRs
set_fact: set_fact:
pod_cidr: "192.168.0.0/16" # Calico default CIDR pod_cidr: "{{ pod_cidr }}"
service_cidr: "10.96.0.0/16" service_cidr: "{{ service_cidr }}"
- name: Initialize Kubernetes control plane - name: Initialize Kubernetes control plane
command: > command: >
+1 -4
View File
@@ -126,10 +126,7 @@
- name: Install Kubernetes components - name: Install Kubernetes components
apt: apt:
name: name: "{{ kubernetes_packages }}"
- kubelet
- kubeadm
- kubectl
state: present state: present
- name: Hold Kubernetes packages - name: Hold Kubernetes packages
+2 -2
View File
@@ -5,11 +5,11 @@
state: started state: started
enabled: yes enabled: yes
- name: Configure systemd-resolved to use Google DNS - name: Configure systemd-resolved to use configurable DNS
lineinfile: lineinfile:
path: /etc/systemd/resolved.conf path: /etc/systemd/resolved.conf
regexp: '^#?DNS=' regexp: '^#?DNS='
line: 'DNS=8.8.8.8' line: 'DNS={{ dns_servers | join(" ") }}'
state: present state: present
- name: Restart systemd-resolved service - name: Restart systemd-resolved service
+45 -5
View File
@@ -1,6 +1,8 @@
#!/bin/bash #!/bin/bash
# provision-kubernetes.sh - Creates and configures a Kubernetes environment # provision-kubernetes.sh - Creates and configures a Kubernetes environment
set -e # Exit on error
# Set variables # Set variables
CONTROL_PLANE="controlplane" CONTROL_PLANE="controlplane"
NODE01="node01" NODE01="node01"
@@ -10,6 +12,44 @@ SSH_KEY_FILE="$(pwd)/multipass/cloud-init/ssh_key.txt"
CLOUD_INIT_TEMPLATE="$(pwd)/multipass/cloud-init/common.yaml" CLOUD_INIT_TEMPLATE="$(pwd)/multipass/cloud-init/common.yaml"
CLOUD_INIT_PROCESSED="$(pwd)/multipass/cloud-init/common_processed.yaml" CLOUD_INIT_PROCESSED="$(pwd)/multipass/cloud-init/common_processed.yaml"
INVENTORY_FILE="$(pwd)/ansible/inventories/kubernetes.yml" INVENTORY_FILE="$(pwd)/ansible/inventories/kubernetes.yml"
DEFAULTS_FILE="$(pwd)/ansible/defaults/kubernetes.yml"
# Load resource values from defaults file if it exists
if [ -f "$DEFAULTS_FILE" ] && command -v python3 >/dev/null && python3 -c "import yaml" 2>/dev/null; then
echo "Loading VM resource settings from defaults file..."
# Extract the VM resource values using Python
RESOURCES=$(python3 -c "
import yaml
try:
with open('$DEFAULTS_FILE', 'r') as f:
vars = yaml.safe_load(f)
cp_cpu = vars.get('control_plane_cpus', 2)
cp_mem = vars.get('control_plane_memory', '2G')
cp_disk = vars.get('control_plane_disk', '20G')
worker_cpu = vars.get('worker_cpus', 2)
worker_mem = vars.get('worker_memory', '2G')
worker_disk = vars.get('worker_disk', '20G')
print(f'{cp_cpu} {cp_mem} {cp_disk} {worker_cpu} {worker_mem} {worker_disk}')
except Exception as e:
print('2 2G 20G 2 2G 20G') # Default values if anything fails
")
read CP_CPU CP_MEM CP_DISK WORKER_CPU WORKER_MEM WORKER_DISK <<< "$RESOURCES"
echo "Control plane resources: $CP_CPU CPUs, $CP_MEM memory, $CP_DISK disk"
echo "Worker node resources: $WORKER_CPU CPUs, $WORKER_MEM memory, $WORKER_DISK disk"
else
# Default values if we can't load from the defaults file
CP_CPU=2
CP_MEM="2G"
CP_DISK="20G"
WORKER_CPU=2
WORKER_MEM="2G"
WORKER_DISK="20G"
echo "Using default VM resource settings"
echo "Control plane resources: $CP_CPU CPUs, $CP_MEM memory, $CP_DISK disk"
echo "Worker node resources: $WORKER_CPU CPUs, $WORKER_MEM memory, $WORKER_DISK disk"
fi
# Check if any VMs already exist # Check if any VMs already exist
VM_EXISTS=false VM_EXISTS=false
@@ -38,17 +78,17 @@ else
echo "Creating Kubernetes VMs with Multipass..." echo "Creating Kubernetes VMs with Multipass..."
# Control plane gets more resources # Control plane gets more resources
multipass launch --name $CONTROL_PLANE --cpus 2 --memory 2G --disk 20G --cloud-init "$CLOUD_INIT_PROCESSED" multipass launch --name $CONTROL_PLANE --cpus $CP_CPU --memory $CP_MEM --disk $CP_DISK --cloud-init "$CLOUD_INIT_PROCESSED"
multipass launch --name $NODE01 --cpus 2 --memory 2G --disk 20G --cloud-init "$CLOUD_INIT_PROCESSED" multipass launch --name $NODE01 --cpus $WORKER_CPU --memory $WORKER_MEM --disk $WORKER_DISK --cloud-init "$CLOUD_INIT_PROCESSED"
multipass launch --name $NODE02 --cpus 2 --memory 2G --disk 20G --cloud-init "$CLOUD_INIT_PROCESSED" multipass launch --name $NODE02 --cpus $WORKER_CPU --memory $WORKER_MEM --disk $WORKER_DISK --cloud-init "$CLOUD_INIT_PROCESSED"
multipass launch --name $NODE03 --cpus 2 --memory 2G --disk 20G --cloud-init "$CLOUD_INIT_PROCESSED" multipass launch --name $NODE03 --cpus $WORKER_CPU --memory $WORKER_MEM --disk $WORKER_DISK --cloud-init "$CLOUD_INIT_PROCESSED"
# Remove processed file after use # Remove processed file after use
rm "$CLOUD_INIT_PROCESSED" rm "$CLOUD_INIT_PROCESSED"
# Give VMs a moment to initialize # Give VMs a moment to initialize
echo "Waiting for VMs to initialize..." echo "Waiting for VMs to initialize..."
sleep 10 sleep 15
fi fi
echo "Extracting IP addresses..." echo "Extracting IP addresses..."