#!/bin/bash set -e echo "==================================================" echo " News API Kubernetes Deployment" echo "==================================================" echo "" # Color codes GREEN='\033[0;32m' BLUE='\033[0;34m' YELLOW='\033[1;33m' RED='\033[0;31m' NC='\033[0m' # No Color # Check if DOCKER_HUB_USER is set if [ -z "$DOCKER_HUB_USER" ]; then echo -e "${RED}Error: DOCKER_HUB_USER environment variable is not set${NC}" echo "Please run: export DOCKER_HUB_USER=your-username" exit 1 fi # Deployment option DEPLOYMENT_TYPE=${1:-local} echo -e "${BLUE}Deployment Type: ${DEPLOYMENT_TYPE}${NC}" echo "" # Step 1: Build Docker Image echo -e "${YELLOW}[1/4] Building News API Docker image...${NC}" docker build -t site11/news-api:latest services/news-api/backend/ echo -e "${GREEN}✓ Image built successfully${NC}" echo "" # Step 2: Push or Load Image if [ "$DEPLOYMENT_TYPE" == "dockerhub" ]; then echo -e "${YELLOW}[2/4] Tagging and pushing to Docker Hub...${NC}" docker tag site11/news-api:latest ${DOCKER_HUB_USER}/news-api:latest docker push ${DOCKER_HUB_USER}/news-api:latest echo -e "${GREEN}✓ Image pushed to Docker Hub${NC}" echo "" echo -e "${YELLOW}[3/4] Deploying to Kubernetes with Docker Hub image...${NC}" envsubst < k8s/news-api/news-api-dockerhub.yaml | kubectl apply -f - elif [ "$DEPLOYMENT_TYPE" == "kind" ]; then echo -e "${YELLOW}[2/4] Loading image to Kind cluster...${NC}" kind load docker-image site11/news-api:latest --name site11-cluster echo -e "${GREEN}✓ Image loaded to Kind${NC}" echo "" echo -e "${YELLOW}[3/4] Deploying to Kind Kubernetes...${NC}" kubectl apply -f k8s/news-api/news-api-deployment.yaml else echo -e "${YELLOW}[2/4] Using local image...${NC}" echo -e "${GREEN}✓ Image ready${NC}" echo "" echo -e "${YELLOW}[3/4] Deploying to Kubernetes...${NC}" kubectl apply -f k8s/news-api/news-api-deployment.yaml fi echo -e "${GREEN}✓ Deployment applied${NC}" echo "" # Step 4: Wait for Pods echo -e "${YELLOW}[4/4] Waiting for pods to be ready...${NC}" kubectl wait --for=condition=ready pod -l app=news-api -n site11-news --timeout=120s || true echo -e "${GREEN}✓ Pods are ready${NC}" echo "" # Display Status echo -e "${BLUE}==================================================" echo " Deployment Status" echo "==================================================${NC}" echo "" echo -e "${YELLOW}Pods:${NC}" kubectl -n site11-news get pods echo "" echo -e "${YELLOW}Service:${NC}" kubectl -n site11-news get svc echo "" echo -e "${YELLOW}HPA:${NC}" kubectl -n site11-news get hpa echo "" echo -e "${BLUE}==================================================" echo " Access the API" echo "==================================================${NC}" echo "" echo "Port forward to access locally:" echo -e "${GREEN}kubectl -n site11-news port-forward svc/news-api-service 8050:8000${NC}" echo "" echo "Then visit:" echo " - Health: http://localhost:8050/health" echo " - Docs: http://localhost:8050/docs" echo " - Korean Articles: http://localhost:8050/api/v1/ko/articles" echo " - Latest: http://localhost:8050/api/v1/en/articles/latest" echo "" echo -e "${GREEN}✓ Deployment completed successfully!${NC}"