zkt25/sk1/prepare-app.sh

97 lines
3.1 KiB
Bash
Executable File

#!/bin/bash
# filepath: prepare-app.sh
set -e
# Set variables
PROJECT_ID=$(gcloud config get-value project)
REGION=europe-central2
ZONE=europe-central2-a
CLUSTER_NAME=sayed-cluster-1
# Build and push the Docker image
echo "[1/4] Building and pushing Docker image..."
docker build -t gcr.io/$PROJECT_ID/contact-app:latest .
gcloud auth configure-docker
docker push gcr.io/$PROJECT_ID/contact-app:latest
# Check if cluster exists, if not create it
echo "[2/4] Checking for GKE cluster..."
if ! gcloud container clusters describe $CLUSTER_NAME --region $REGION --project $PROJECT_ID &>/dev/null; then
echo "Cluster $CLUSTER_NAME not found. Creating cluster..."
gcloud container clusters create-auto $CLUSTER_NAME \
--region $REGION \
--project $PROJECT_ID
echo "Cluster created successfully."
else
echo "Cluster $CLUSTER_NAME already exists."
fi
# Connect to the cluster
echo "[3/4] Connecting to GKE cluster..."
gcloud container clusters get-credentials $CLUSTER_NAME --region $REGION --project $PROJECT_ID
kubectl config set-context --current --namespace=default
# Create namespace and apply MongoDB resources
echo "[4/4] Deploying Kubernetes resources..."
kubectl create namespace contact-app --dry-run=client -o yaml | kubectl apply -f -
kubectl apply -n contact-app -f k8s/mongo-pvc.yaml
kubectl apply -n contact-app -f k8s/mongo-deployment.yaml
kubectl apply -n contact-app -f k8s/mongo-service.yaml
# Apply app with image substitution and create LoadBalancer service
echo "Deploying application..."
sed "s|<YOUR_GCR_IMAGE>|gcr.io/$PROJECT_ID/contact-app:latest|g" k8s/app-deployment.yaml | kubectl apply -n contact-app -f -
kubectl apply -n contact-app -f k8s/app-service.yaml
kubectl apply -n contact-app -f k8s/contact-app-lb.yaml
# Wait for LoadBalancer to get external IP
echo "Waiting for LoadBalancer to get external IP..."
while [[ -z $(kubectl get service contact-app-lb -n contact-app -o jsonpath='{.status.loadBalancer.ingress[0].ip}' 2>/dev/null) ]]; do
echo -n "."
sleep 5
done
# Get the external IP
EXTERNAL_IP=$(kubectl get service contact-app-lb -n contact-app -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
echo "External IP: $EXTERNAL_IP"
# Create a domain using nip.io
DOMAIN="${EXTERNAL_IP}.nip.io"
echo "Domain: $DOMAIN"
# Create and apply ingress with the domain
cat <<EOF > k8s/ingress-with-domain.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: contact-app-ingress
annotations:
networking.gke.io/managed-certificates: contact-app-cert
kubernetes.io/ingress.class: "gce"
spec:
rules:
- host: ${DOMAIN}
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: contact-app
port:
number: 80
---
apiVersion: networking.gke.io/v1
kind: ManagedCertificate
metadata:
name: contact-app-cert
spec:
domains:
- ${DOMAIN}
EOF
kubectl apply -n contact-app -f k8s/ingress-with-domain.yaml
echo "Deployment started. It may take a few minutes for the HTTPS certificate to be provisioned."
echo "Once provisioned, your application will be available at:"
echo "https://${DOMAIN}"