add kubernetes
This commit is contained in:
Executable
+8
@@ -0,0 +1,8 @@
|
||||
#!/bin/bash
|
||||
# cleanup-postgres.sh - Removes PostgreSQL testing environment
|
||||
|
||||
echo "Deleting PostgreSQL VMs..."
|
||||
multipass delete pg-primary pg-replica1 pg-replica2 pg-lb
|
||||
multipass purge
|
||||
|
||||
echo "Cleanup complete."
|
||||
Regular → Executable
+106
@@ -0,0 +1,106 @@
|
||||
#!/bin/bash
|
||||
# provision-kubernetes.sh - Creates and configures a Kubernetes environment
|
||||
|
||||
# Set variables
|
||||
CONTROL_PLANE="controlplane"
|
||||
NODE01="node01"
|
||||
NODE02="node02"
|
||||
NODE03="node03"
|
||||
SSH_KEY_FILE="$(pwd)/multipass/cloud-init/ssh_key.txt"
|
||||
CLOUD_INIT_TEMPLATE="$(pwd)/multipass/cloud-init/common.yaml"
|
||||
CLOUD_INIT_PROCESSED="$(pwd)/multipass/cloud-init/common_processed.yaml"
|
||||
INVENTORY_FILE="$(pwd)/ansible/inventories/kubernetes.yml"
|
||||
|
||||
# Check if any VMs already exist
|
||||
VM_EXISTS=false
|
||||
if multipass info $CONTROL_PLANE &>/dev/null || \
|
||||
multipass info $NODE01 &>/dev/null || \
|
||||
multipass info $NODE02 &>/dev/null || \
|
||||
multipass info $NODE03 &>/dev/null; then
|
||||
VM_EXISTS=true
|
||||
fi
|
||||
|
||||
if [ "$VM_EXISTS" = true ]; then
|
||||
echo "Some Kubernetes VMs already exist, proceeding to Ansible configuration..."
|
||||
else
|
||||
# Verify SSH key file exists
|
||||
if [ ! -f "$SSH_KEY_FILE" ]; then
|
||||
echo "Error: SSH key file not found at $SSH_KEY_FILE"
|
||||
echo "Please create this file with your public SSH key."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Read SSH key
|
||||
SSH_KEY=$(cat "$SSH_KEY_FILE")
|
||||
|
||||
# Process the cloud-init template and replace the placeholder
|
||||
sed "s|\$SSH_PUBLIC_KEY|$SSH_KEY|g" "$CLOUD_INIT_TEMPLATE" > "$CLOUD_INIT_PROCESSED"
|
||||
|
||||
echo "Creating Kubernetes VMs with Multipass..."
|
||||
# Control plane gets more resources
|
||||
multipass launch --name $CONTROL_PLANE --cpus 2 --memory 2G --disk 20G --cloud-init "$CLOUD_INIT_PROCESSED"
|
||||
multipass launch --name $NODE01 --cpus 2 --memory 2G --disk 20G --cloud-init "$CLOUD_INIT_PROCESSED"
|
||||
multipass launch --name $NODE02 --cpus 2 --memory 2G --disk 20G --cloud-init "$CLOUD_INIT_PROCESSED"
|
||||
multipass launch --name $NODE03 --cpus 2 --memory 2G --disk 20G --cloud-init "$CLOUD_INIT_PROCESSED"
|
||||
|
||||
# Remove processed file after use
|
||||
rm "$CLOUD_INIT_PROCESSED"
|
||||
|
||||
# Give VMs a moment to initialize
|
||||
echo "Waiting for VMs to initialize..."
|
||||
sleep 10
|
||||
fi
|
||||
|
||||
echo "Extracting IP addresses..."
|
||||
# Extract IPs correctly
|
||||
CP_IP=$(multipass info $CONTROL_PLANE | grep "IPv4" | head -n 1 | awk '{print $2}')
|
||||
NODE01_IP=$(multipass info $NODE01 | grep "IPv4" | head -n 1 | awk '{print $2}')
|
||||
NODE02_IP=$(multipass info $NODE02 | grep "IPv4" | head -n 1 | awk '{print $2}')
|
||||
NODE03_IP=$(multipass info $NODE03 | grep "IPv4" | head -n 1 | awk '{print $2}')
|
||||
|
||||
# Print the IPs to verify
|
||||
echo "Control Plane IP: $CP_IP"
|
||||
echo "Node01 IP: $NODE01_IP"
|
||||
echo "Node02 IP: $NODE02_IP"
|
||||
echo "Node03 IP: $NODE03_IP"
|
||||
|
||||
echo "Generating Ansible inventory..."
|
||||
# Create inventory file
|
||||
cat > "$INVENTORY_FILE" << EOF
|
||||
---
|
||||
all:
|
||||
children:
|
||||
k8s_cluster:
|
||||
children:
|
||||
control_plane:
|
||||
hosts:
|
||||
controlplane:
|
||||
ansible_host: $CP_IP
|
||||
workers:
|
||||
hosts:
|
||||
node01:
|
||||
ansible_host: $NODE01_IP
|
||||
node02:
|
||||
ansible_host: $NODE02_IP
|
||||
node03:
|
||||
ansible_host: $NODE03_IP
|
||||
vars:
|
||||
ansible_user: ubuntu
|
||||
ansible_become: yes
|
||||
ansible_ssh_private_key_file: ~/.ssh/id_rsa
|
||||
ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
|
||||
EOF
|
||||
|
||||
echo "Verifying inventory file contents:"
|
||||
cat "$INVENTORY_FILE"
|
||||
|
||||
echo "Running Ansible playbook for Kubernetes deployment..."
|
||||
ansible-playbook -i "$INVENTORY_FILE" kubernetes/main.yml
|
||||
|
||||
echo "Kubernetes environment setup complete."
|
||||
echo "Control Plane: $CP_IP"
|
||||
echo "Workers: $NODE01_IP, $NODE02_IP, $NODE03_IP"
|
||||
echo ""
|
||||
echo "To access the cluster, run:"
|
||||
echo "multipass shell $CONTROL_PLANE"
|
||||
echo "And then: kubectl get nodes"
|
||||
|
||||
Regular → Executable
+100
@@ -0,0 +1,100 @@
|
||||
#!/bin/bash
|
||||
# provision-postgres.sh - Creates and configures a PostgreSQL environment
|
||||
|
||||
# Set variables
|
||||
PRIMARY_NAME="pg-primary"
|
||||
REPLICA1_NAME="pg-replica1"
|
||||
REPLICA2_NAME="pg-replica2"
|
||||
LB_NAME="pg-lb"
|
||||
SSH_KEY_FILE="$(pwd)/multipass/cloud-init/ssh_key.txt"
|
||||
CLOUD_INIT_TEMPLATE="$(pwd)/multipass/cloud-init/common.yaml"
|
||||
CLOUD_INIT_PROCESSED="$(pwd)/multipass/cloud-init/common_processed.yaml"
|
||||
INVENTORY_FILE="$(pwd)/ansible/inventories/postgres.yml"
|
||||
|
||||
# Check if any VMs already exist
|
||||
VM_EXISTS=false
|
||||
if multipass info $PRIMARY_NAME &>/dev/null || \
|
||||
multipass info $REPLICA1_NAME &>/dev/null || \
|
||||
multipass info $REPLICA2_NAME &>/dev/null || \
|
||||
multipass info $LB_NAME &>/dev/null; then
|
||||
VM_EXISTS=true
|
||||
fi
|
||||
|
||||
if [ "$VM_EXISTS" = true ]; then
|
||||
echo "Some PostgreSQL VMs already exist, proceeding to Ansible configuration..."
|
||||
else
|
||||
# Verify SSH key file exists
|
||||
if [ ! -f "$SSH_KEY_FILE" ]; then
|
||||
echo "Error: SSH key file not found at $SSH_KEY_FILE"
|
||||
echo "Please create this file with your public SSH key."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Read SSH key
|
||||
SSH_KEY=$(cat "$SSH_KEY_FILE")
|
||||
|
||||
# Process the cloud-init template and replace the placeholder
|
||||
sed "s|\$SSH_PUBLIC_KEY|$SSH_KEY|g" "$CLOUD_INIT_TEMPLATE" > "$CLOUD_INIT_PROCESSED"
|
||||
|
||||
echo "Creating PostgreSQL VMs with Multipass..."
|
||||
multipass launch --name $PRIMARY_NAME --cpus 1 --mem 1G --disk 5G --cloud-init "$CLOUD_INIT_PROCESSED"
|
||||
multipass launch --name $REPLICA1_NAME --cpus 1 --mem 1G --disk 5G --cloud-init "$CLOUD_INIT_PROCESSED"
|
||||
multipass launch --name $REPLICA2_NAME --cpus 1 --mem 1G --disk 5G --cloud-init "$CLOUD_INIT_PROCESSED"
|
||||
multipass launch --name $LB_NAME --cpus 1 --mem 1G --disk 5G --cloud-init "$CLOUD_INIT_PROCESSED"
|
||||
|
||||
# Remove processed file after use
|
||||
rm "$CLOUD_INIT_PROCESSED"
|
||||
|
||||
# Give VMs a moment to initialize
|
||||
echo "Waiting for VMs to initialize..."
|
||||
sleep 10
|
||||
fi
|
||||
|
||||
echo "Extracting IP addresses..."
|
||||
# Extract IPs correctly
|
||||
PRIMARY_IP=$(multipass info pg-primary | grep "IPv4" | head -n 1 | awk '{print $2}')
|
||||
REPLICA1_IP=$(multipass info pg-replica1 | grep "IPv4" | head -n 1 | awk '{print $2}')
|
||||
REPLICA2_IP=$(multipass info pg-replica2 | grep "IPv4" | head -n 1 | awk '{print $2}')
|
||||
LB_IP=$(multipass info pg-lb | grep "IPv4" | head -n 1 | awk '{print $2}')
|
||||
|
||||
# Print the IPs to verify
|
||||
echo "Primary IP: $PRIMARY_IP"
|
||||
echo "Replica1 IP: $REPLICA1_IP"
|
||||
echo "Replica2 IP: $REPLICA2_IP"
|
||||
echo "Load Balancer IP: $LB_IP"
|
||||
|
||||
echo "Generating Ansible inventory..."
|
||||
# Create inventory file - using a simpler format to avoid issues
|
||||
cat > "$INVENTORY_FILE" << EOF
|
||||
[postgres_primary]
|
||||
pg-primary ansible_host=$PRIMARY_IP
|
||||
|
||||
[postgres_replicas]
|
||||
pg-replica1 ansible_host=$REPLICA1_IP
|
||||
pg-replica2 ansible_host=$REPLICA2_IP
|
||||
|
||||
[postgres_lb]
|
||||
pg-lb ansible_host=$LB_IP
|
||||
|
||||
[postgres:children]
|
||||
postgres_primary
|
||||
postgres_replicas
|
||||
postgres_lb
|
||||
|
||||
[all:vars]
|
||||
ansible_user=ubuntu
|
||||
ansible_ssh_private_key_file=~/.ssh/id_rsa
|
||||
ansible_python_interpreter=/usr/bin/python3
|
||||
ansible_ssh_common_args='-o StrictHostKeyChecking=no'
|
||||
EOF
|
||||
|
||||
echo "Verifying inventory file contents:"
|
||||
cat "$INVENTORY_FILE"
|
||||
|
||||
echo "Running Ansible playbook..."
|
||||
ansible-playbook -i "$INVENTORY_FILE" ansible/playbooks/postgres.yml
|
||||
|
||||
echo "PostgreSQL environment is ready!"
|
||||
echo "Primary: $PRIMARY_IP"
|
||||
echo "Replicas: $REPLICA1_IP, $REPLICA2_IP"
|
||||
echo "Load Balancer: $LB_IP"
|
||||
|
||||
Reference in New Issue
Block a user