Files
k3s_auto_deploy/scripts/deploy-nginx-app.sh

187 lines
6.9 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "🚀 Nginx测试应用 - 自动化部署"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
# 检查依赖
echo "🔍 检查依赖..."
command -v kubectl >/dev/null 2>&1 || { echo "❌ kubectl未安装"; exit 1; }
command -v yq >/dev/null 2>&1 || { echo "❌ yq未安装"; exit 1; }
echo "✅ 依赖检查通过"
echo ""
# 检查kubectl连接
echo "🔍 检查K3s集群连接..."
if ! kubectl cluster-info >/dev/null 2>&1; then
echo "❌ 无法连接到K3s集群"
echo "💡 请确保已配置kubectl访问权限"
exit 1
fi
echo "✅ K3s集群连接正常"
echo ""
# 检查Gitea是否运行
echo "🔍 检查Gitea服务..."
if ! kubectl get svc gitea-http -n gitea >/dev/null 2>&1; then
echo "❌ Gitea服务未运行"
echo "💡 请先运行: ./scripts/deploy-gitea.sh"
exit 1
fi
echo "✅ Gitea服务运行正常"
echo ""
# 检查ArgoCD是否运行
echo "🔍 检查ArgoCD服务..."
if ! kubectl get namespace argocd >/dev/null 2>&1; then
echo "❌ ArgoCD未安装"
echo "💡 请先运行: ./scripts/deploy-argocd.sh"
exit 1
fi
echo "✅ ArgoCD服务运行正常"
echo ""
# 步骤1: 创建Gitea仓库
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "📦 步骤 1/3: 创建Gitea仓库"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
# 读取配置
CONFIG_FILE="$PROJECT_DIR/config/cluster-vars.yml"
GITEA_USER=$(yq eval '.gitea_user_name' "$CONFIG_FILE")
GITEA_PASSWORD=$(yq eval '.gitea_user_password' "$CONFIG_FILE")
GITEA_ORG=$(yq eval '.gitea_org_name' "$CONFIG_FILE")
NGINX_REPO=$(yq eval '.nginx_app_repo_name' "$CONFIG_FILE")
# 获取Gitea访问地址
GITEA_NODEPORT=$(kubectl get svc gitea-http -n gitea -o jsonpath='{.spec.ports[0].nodePort}')
NODE_IP=$(kubectl get nodes -o jsonpath='{.items[0].status.addresses[?(@.type=="ExternalIP")].address}')
if [ -z "$NODE_IP" ]; then
NODE_IP=$(kubectl get nodes -o jsonpath='{.items[0].status.addresses[?(@.type=="InternalIP")].address}')
fi
GITEA_URL="http://$NODE_IP:$GITEA_NODEPORT"
# 检查仓库是否已存在
echo "🔍 检查仓库是否存在..."
REPO_EXISTS=$(curl -s -o /dev/null -w "%{http_code}" \
-u "$GITEA_USER:$GITEA_PASSWORD" \
"$GITEA_URL/api/v1/repos/$GITEA_ORG/$NGINX_REPO")
if [ "$REPO_EXISTS" = "200" ]; then
echo "⚠️ 仓库已存在: $GITEA_ORG/$NGINX_REPO"
echo ""
read -p "是否删除并重新创建?(y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "🗑️ 删除现有仓库..."
curl -s -X DELETE \
-u "$GITEA_USER:$GITEA_PASSWORD" \
"$GITEA_URL/api/v1/repos/$GITEA_ORG/$NGINX_REPO"
echo "✅ 仓库已删除"
else
echo "⏭️ 跳过仓库创建"
SKIP_PUSH=true
fi
fi
if [ "$SKIP_PUSH" != "true" ]; then
echo "📝 创建新仓库..."
curl -s -X POST \
-u "$GITEA_USER:$GITEA_PASSWORD" \
-H "Content-Type: application/json" \
-d "{\"name\":\"$NGINX_REPO\",\"description\":\"Nginx test application for GitOps demo\",\"private\":false,\"auto_init\":false}" \
"$GITEA_URL/api/v1/org/$GITEA_ORG/repos" > /dev/null
echo "✅ 仓库创建成功: $GITEA_ORG/$NGINX_REPO"
fi
echo ""
# 步骤2: 推送应用到Gitea
if [ "$SKIP_PUSH" != "true" ]; then
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "📤 步骤 2/3: 推送应用到Gitea"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
"$SCRIPT_DIR/push-nginx-app.sh"
echo ""
else
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "⏭️ 步骤 2/3: 跳过推送(仓库已存在)"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
fi
# 步骤3: 创建ArgoCD Application
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "🎯 步骤 3/3: 创建ArgoCD Application"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
# 检查Application是否已存在
if kubectl get application nginx-app -n argocd >/dev/null 2>&1; then
echo "⚠️ ArgoCD Application已存在"
echo ""
read -p "是否删除并重新创建?(y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "🗑️ 删除现有Application..."
kubectl delete application nginx-app -n argocd
echo "✅ Application已删除"
sleep 2
else
echo "⏭️ 跳过Application创建"
SKIP_ARGOCD=true
fi
fi
if [ "$SKIP_ARGOCD" != "true" ]; then
"$SCRIPT_DIR/create-nginx-argocd-app.sh"
fi
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "🎉 Nginx测试应用部署完成"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo "📊 部署信息:"
echo " - 应用名称: nginx-test"
echo " - 命名空间: default"
echo " - 域名: https://ng.jpc.net3w.com"
echo " - Git仓库: $GITEA_URL/$GITEA_ORG/$NGINX_REPO"
echo ""
echo "🔍 验证命令:"
echo " # 查看Pod状态"
echo " kubectl get pods -l app=nginx-test -n default"
echo ""
echo " # 查看Service"
echo " kubectl get svc nginx-test -n default"
echo ""
echo " # 查看Ingress"
echo " kubectl get ingress nginx-test -n default"
echo ""
echo " # 查看ArgoCD Application"
echo " kubectl get application nginx-app -n argocd"
echo ""
echo "🌐 访问地址:"
echo " - 应用: https://ng.jpc.net3w.com"
echo " - ArgoCD: https://argocd.jpc.net3w.com"
echo " - Gitea: $GITEA_URL/$GITEA_ORG/$NGINX_REPO"
echo ""
echo "💡 更新应用:"
echo " 1. SSH到master节点"
echo " 2. cd /home/fei/k3s/nginx-app"
echo " 3. ./update-app.sh v2.0"
echo " 4. 等待ArgoCD自动同步约3分钟"
echo ""
echo "📝 注意事项:"
echo " - 确保DNS已配置: ng.jpc.net3w.com -> $NODE_IP"
echo " - 首次HTTPS访问需等待证书签发1-2分钟"
echo " - ArgoCD每3分钟检查一次Git仓库更新"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"