zkt25/z2/start-app.sh

82 lines
2.7 KiB
Bash

#!/bin/bash
# Script to deploy the app locally using Docker Desktop + Kubernetes
# Set default region (optional but kept for consistency)
if [ -z "$REGION" ]; then
export REGION="local"
echo "No region specified. Using default region: $REGION"
else
echo "Deploying to region: $REGION"
fi
# Build Docker image
echo "Building Docker image..."
docker build -t antonin193/simple-web-app:latest .
# Push to Docker Hub (optional for local, but kept if needed across machines)
echo "Pushing image to Docker Hub..."
docker push antonin193/simple-web-app:latest
# Create Kubernetes Namespace
echo "Creating namespace..."
kubectl apply -f namespace.yaml
# Create PersistentVolume and PersistentVolumeClaim
echo "Creating persistent storage..."
kubectl apply -f persistent-storage.yaml
# Wait briefly for resources to be established
sleep 2
# Deploy PostgreSQL first
echo "Creating PostgreSQL Deployment and Service..."
kubectl apply -f postgres-deployment.yaml
kubectl apply -f postgres-service.yaml
# Wait for PostgreSQL to be ready
echo "Waiting for PostgreSQL to be ready..."
kubectl wait --for=condition=ready pod -l app=postgres -n my-app --timeout=120s
# Deploy application
echo "Creating Deployment..."
kubectl apply -f deployment.yaml
echo "Creating StatefulSet..."
kubectl apply -f statefulset.yaml
echo "Creating Service..."
kubectl apply -f service.yaml
# Wait for LoadBalancer IP (Docker Desktop uses host network so it's usually localhost)
echo "Waiting for LoadBalancer to obtain an external IP (or localhost for Docker Desktop)..."
external_ip=""
attempt=0
max_attempts=4
while [ -z "$external_ip" ] && [ $attempt -lt $max_attempts ]; do
sleep 10
attempt=$((attempt+1))
external_ip=$(kubectl get svc web-app-service -n my-app --template="{{range .status.loadBalancer.ingress}}{{.ip}}{{end}}" 2>/dev/null)
if [ -z "$external_ip" ]; then
echo "Waiting for external IP... Attempt $attempt of $max_attempts"
fi
done
# Fallback to localhost if no external IP is found (common in Docker Desktop)
if [ -z "$external_ip" ]; then
external_ip="localhost"
echo ""
echo "=========================================================="
echo "Could not get external IP from LoadBalancer. Defaulting to localhost."
echo "You can try accessing your app at: http://localhost:80"
echo "Or check service status manually with:"
echo "kubectl get svc web-app-service -n my-app"
echo "=========================================================="
else
echo ""
echo "=========================================================="
echo "Application deployed successfully!"
echo "You can try accessing your app at: http://localhost:80"
echo "=========================================================="
fi