zkt25/sk1/not use/complete-ingress-setup.sh

167 lines
4.3 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# Get project ID
PROJECT=$(gcloud config get-value project)
echo "🔍 Checking if certificate exists..."
if ! kubectl get managedcertificate sk1-cert -n sk1 2>/dev/null; then
echo "🔒 Creating managed certificate..."
# Create certificate
cat > certificate.yaml << EOF
apiVersion: networking.gke.io/v1
kind: ManagedCertificate
metadata:
name: sk1-cert
namespace: sk1
spec:
domains:
- nudges.works
EOF
kubectl apply -f certificate.yaml
echo "✅ Certificate created"
else
echo "✅ Certificate already exists"
fi
echo "🔍 Checking if static IP exists..."
if ! gcloud compute addresses describe sk1-static-ip --global &>/dev/null; then
echo "🌐 Creating static IP..."
# Create static IP
gcloud compute addresses create sk1-static-ip --global
echo "✅ Static IP created"
else
echo "✅ Static IP already exists"
fi
# Get the static IP
IP=$(gcloud compute addresses describe sk1-static-ip --global --format="value(address)")
echo "📝 Using static IP: $IP"
echo "🏗️ Creating ingress resource..."
# Create ingress resource
cat > complete-ingress.yaml << EOF
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: sk1-ingress
namespace: sk1
annotations:
kubernetes.io/ingress.global-static-ip-name: sk1-static-ip
networking.gke.io/managed-certificates: sk1-cert
kubernetes.io/ingress.class: "gce"
spec:
rules:
- host: nudges.works
http:
paths:
- path: /api/
pathType: Prefix
backend:
service:
name: backend-service
port:
number: 4000
- path: /
pathType: Prefix
backend:
service:
name: frontend-service
port:
number: 80
EOF
# Apply the ingress configuration
kubectl apply -f complete-ingress.yaml
echo "⏱️ Waiting for ingress to initialize..."
sleep 10
echo "🔍 Checking ingress status..."
kubectl get ingress -n sk1
# Create a special nginx configuration for frontend to handle direct requests to backend
echo "📝 Updating frontend nginx configuration to correctly handle API requests..."
cat > fixed-nginx-conf.yaml << EOF
apiVersion: v1
kind: ConfigMap
metadata:
name: fixed-nginx-conf
namespace: sk1
data:
nginx.conf: |
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.html;
# All API requests should go to backend service
location /api/ {
proxy_pass http://backend-service:4000/;
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_cache_bypass \$http_upgrade;
}
# All other requests go to the React app
location / {
try_files \$uri \$uri/ /index.html;
}
}
EOF
kubectl apply -f fixed-nginx-conf.yaml
# Update frontend to use this config
kubectl patch deployment frontend -n sk1 --type=strategic --patch '
{
"spec": {
"template": {
"spec": {
"containers": [
{
"name": "frontend",
"volumeMounts": [
{
"name": "nginx-config",
"mountPath": "/etc/nginx/conf.d/default.conf",
"subPath": "nginx.conf"
}
]
}
],
"volumes": [
{
"name": "nginx-config",
"configMap": {
"name": "fixed-nginx-conf"
}
}
]
}
}
}
}
'
echo "🔄 Restarting pods to ensure clean configuration..."
kubectl rollout restart deployment frontend -n sk1
kubectl rollout restart deployment backend -n sk1
echo "⏱️ Waiting for deployments to restart..."
kubectl rollout status deployment frontend -n sk1
kubectl rollout status deployment backend -n sk1
echo "✅ Setup complete!"
echo ""
echo "Your application should now be accessible at: https://nudges.works"
echo "Note: It may take 5-10 minutes for the DNS and certificate to propagate completely."
echo ""
echo "You can test the API directly with: curl https://nudges.works/api"
echo ""
echo "If you still encounter issues, try clearing your browser cache or using an incognito window."