Files
brunoandClaude 4606a575d0 feat: 重构部署架构 - 简化K8s部署流程
主要变更:
- 重构CI/CD流程: 移除Harbor依赖,直接部署到K8s
- 优化命名空间: 从claude-stock简化为stock
- 新增一键部署脚本: deploy.sh支持完整K8s部署流程
- 分离健康检查端点:
  - /health: K8s探针专用(快速响应)
  - /api/health: 详细健康检查(包含数据库/缓存状态)
- 简化Nginx配置: API路由交由Ingress处理
- 新增K8s部署清单: k8s/stock-deployment.yaml(PostgreSQL+Redis+Backend+Frontend+Ingress)
- 新增完整部署文档: DEPLOYMENT.md(一键部署/手动部署/CI CD/故障排查)
- 添加前端重构总结文档: frontend/REFACTOR_SUMMARY.md

技术改进:
✓ 简化CI/CD流程(5个stage: build, test, deploy, verify, notify)
✓ 使用artifact在job间传递镜像(避免重复构建)
✓ K8s健康检查配置优化(liveness 45s, readiness 20s)
✓ 资源限制合理化(backend 2Gi, frontend 512Mi)
✓ Ingress统一路由(/api → backend, / → frontend)

部署方式:
1. 一键部署: ./deploy.sh
2. CI/CD自动部署: git push origin main
3. 手动部署: kubectl apply -f k8s/stock-deployment.yaml

访问地址: https://stock.omniverseai.net

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
2025-11-25 20:01:57 +08:00

150 lines
5.9 KiB
Bash
Executable File

#!/bin/bash
# ============================================================================
# Claude Stock Application - One-Click Deployment Script
# Deploys to stock namespace in Kubernetes
# ============================================================================
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
NAMESPACE="stock"
BACKEND_IMAGE="stock-backend:latest"
FRONTEND_IMAGE="stock-frontend:latest"
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
echo -e "${BLUE}╔══════════════════════════════════════════════════════════════╗${NC}"
echo -e "${BLUE}║ Claude Stock Application Deployment Tool ║${NC}"
echo -e "${BLUE}╚══════════════════════════════════════════════════════════════╝${NC}"
echo ""
# Function to print step
print_step() {
echo -e "${YELLOW}▶ $1${NC}"
}
# Function to print success
print_success() {
echo -e "${GREEN}✓ $1${NC}"
}
# Function to print error
print_error() {
echo -e "${RED}✗ $1${NC}"
}
# Check if kubectl is available
if ! command -v kubectl &> /dev/null; then
print_error "kubectl is not installed or not in PATH"
exit 1
fi
# Check if docker is available
if ! command -v docker &> /dev/null; then
print_error "Docker is not installed or not in PATH"
exit 1
fi
# Step 1: Build Docker images
print_step "Step 1/5: Building Docker images..."
echo " Building backend image..."
cd "$PROJECT_ROOT/backend"
docker build -t $BACKEND_IMAGE . || {
print_error "Backend Docker build failed"
exit 1
}
print_success "Backend image built: $BACKEND_IMAGE"
echo " Building frontend image..."
cd "$PROJECT_ROOT/frontend"
docker build -t $FRONTEND_IMAGE . || {
print_error "Frontend Docker build failed"
exit 1
}
print_success "Frontend image built: $FRONTEND_IMAGE"
# Step 2: Create namespace if it doesn't exist
print_step "Step 2/5: Setting up Kubernetes namespace..."
kubectl create namespace $NAMESPACE --dry-run=client -o yaml | kubectl apply -f - >/dev/null 2>&1
print_success "Namespace '$NAMESPACE' ready"
# Step 3: Apply Kubernetes configurations
print_step "Step 3/5: Deploying to Kubernetes..."
cd "$PROJECT_ROOT/k8s"
# Deploy in correct order
echo " Applying configurations..."
kubectl apply -f stock-deployment.yaml -n $NAMESPACE || {
print_error "Failed to apply Kubernetes configurations"
exit 1
}
print_success "Kubernetes configurations applied"
# Step 4: Wait for deployments
print_step "Step 4/5: Waiting for deployments to be ready..."
echo " Waiting for PostgreSQL..."
kubectl wait --for=condition=Ready pod -l app=stock-postgres -n $NAMESPACE --timeout=180s 2>/dev/null || echo " (PostgreSQL timeout, continuing...)"
echo " Waiting for Redis..."
kubectl wait --for=condition=Ready pod -l app=stock-redis -n $NAMESPACE --timeout=180s 2>/dev/null || echo " (Redis timeout, continuing...)"
echo " Waiting for backend deployment..."
kubectl rollout status deployment/stock-backend -n $NAMESPACE --timeout=5m 2>&1 | grep -v "Waiting for" | tail -1
if [ ${PIPESTATUS[0]} -eq 0 ]; then
print_success "Backend deployment ready"
else
print_error "Backend deployment timeout"
fi
echo " Waiting for frontend deployment..."
kubectl rollout status deployment/stock-frontend -n $NAMESPACE --timeout=5m 2>&1 | grep -v "Waiting for" | tail -1
if [ ${PIPESTATUS[0]} -eq 0 ]; then
print_success "Frontend deployment ready"
else
print_error "Frontend deployment timeout"
fi
# Step 5: Display deployment status
print_step "Step 5/5: Deployment Summary"
echo ""
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${BLUE}Pods:${NC}"
kubectl get pods -n $NAMESPACE
echo ""
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${BLUE}Services:${NC}"
kubectl get svc -n $NAMESPACE
echo ""
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${BLUE}Ingress:${NC}"
kubectl get ingress -n $NAMESPACE
echo ""
echo -e "${GREEN}╔══════════════════════════════════════════════════════════════╗${NC}"
echo -e "${GREEN}║ Deployment Complete! ║${NC}"
echo -e "${GREEN}╚══════════════════════════════════════════════════════════════╝${NC}"
echo ""
echo -e "${YELLOW}Access the application at:${NC}"
echo -e " ${BLUE}https://stock.omniverseai.net${NC}"
echo ""
echo -e "${YELLOW}Useful commands:${NC}"
echo -e " View logs (backend): ${GREEN}kubectl logs -n $NAMESPACE -l app=stock-backend -f${NC}"
echo -e " View logs (frontend): ${GREEN}kubectl logs -n $NAMESPACE -l app=stock-frontend -f${NC}"
echo -e " View all pods: ${GREEN}kubectl get pods -n $NAMESPACE${NC}"
echo -e " Restart backend: ${GREEN}kubectl rollout restart deployment/stock-backend -n $NAMESPACE${NC}"
echo -e " Restart frontend: ${GREEN}kubectl rollout restart deployment/stock-frontend -n $NAMESPACE${NC}"
echo -e " Access database: ${GREEN}kubectl exec -it -n $NAMESPACE stock-postgres-0 -- psql -U stockai -d stock_ai${NC}"
echo ""