add html resource
All checks were successful
Build NGINX on Ubuntu / build-and-deploy (push) Successful in 1m53s
All checks were successful
Build NGINX on Ubuntu / build-and-deploy (push) Successful in 1m53s
This commit is contained in:
parent
4ae6507af9
commit
3d9574c26d
152
k8s/Makefile
152
k8s/Makefile
@ -1,152 +0,0 @@
|
||||
# NGINX Kubernetes 部署 Makefile
|
||||
# 简化部署操作和管理
|
||||
|
||||
# 默认配置
|
||||
HARBOR_REGISTRY ?= harbor.example.com
|
||||
HARBOR_USERNAME ?= your-username
|
||||
HARBOR_PASSWORD ?= your-password
|
||||
NGINX_IMAGE_TAG ?= latest
|
||||
NAMESPACE ?= default
|
||||
DEPLOYMENT_FILE ?= ./nginx-deployment.yaml
|
||||
|
||||
# 颜色输出
|
||||
RED := \033[0;31m
|
||||
GREEN := \033[0;32m
|
||||
YELLOW := \033[1;33m
|
||||
BLUE := \033[0;34m
|
||||
NC := \033[0m # No Color
|
||||
|
||||
.PHONY: help deploy clean status logs port-forward scale update rollback check-env install-deps
|
||||
|
||||
# 默认目标
|
||||
help: ## 显示帮助信息
|
||||
@echo "$(BLUE)NGINX Kubernetes 部署工具$(NC)"
|
||||
@echo ""
|
||||
@echo "$(YELLOW)可用命令:$(NC)"
|
||||
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf " $(GREEN)%-15s$(NC) %s\n", $$1, $$2}' $(MAKEFILE_LIST)
|
||||
@echo ""
|
||||
@echo "$(YELLOW)环境变量:$(NC)"
|
||||
@echo " HARBOR_REGISTRY Harbor 仓库地址 (当前: $(HARBOR_REGISTRY))"
|
||||
@echo " HARBOR_USERNAME Harbor 用户名 (当前: $(HARBOR_USERNAME))"
|
||||
@echo " HARBOR_PASSWORD Harbor 密码 (已设置: $(if $(HARBOR_PASSWORD),是,否))"
|
||||
@echo " NGINX_IMAGE_TAG 镜像标签 (当前: $(NGINX_IMAGE_TAG))"
|
||||
@echo " NAMESPACE 命名空间 (当前: $(NAMESPACE))"
|
||||
|
||||
check-env: ## 检查环境变量
|
||||
@echo "$(BLUE)[INFO]$(NC) 检查必要的环境变量..."
|
||||
@if [ -z "$(HARBOR_REGISTRY)" ]; then echo "$(RED)[ERROR]$(NC) HARBOR_REGISTRY 未设置"; exit 1; fi
|
||||
@if [ -z "$(HARBOR_USERNAME)" ]; then echo "$(RED)[ERROR]$(NC) HARBOR_USERNAME 未设置"; exit 1; fi
|
||||
@if [ -z "$(HARBOR_PASSWORD)" ]; then echo "$(RED)[ERROR]$(NC) HARBOR_PASSWORD 未设置"; exit 1; fi
|
||||
@if [ -z "$(NGINX_IMAGE_TAG)" ]; then echo "$(RED)[ERROR]$(NC) NGINX_IMAGE_TAG 未设置"; exit 1; fi
|
||||
@echo "$(GREEN)[SUCCESS]$(NC) 环境变量检查通过"
|
||||
|
||||
install-deps: ## 安装必要的依赖工具
|
||||
@echo "$(BLUE)[INFO]$(NC) 检查并安装必要的工具..."
|
||||
@which kubectl > /dev/null || (echo "$(RED)[ERROR]$(NC) kubectl 未安装" && exit 1)
|
||||
@echo "$(GREEN)[SUCCESS]$(NC) 所有必要工具已安装"
|
||||
|
||||
deploy: check-env install-deps ## 部署 NGINX 到 Kubernetes
|
||||
@echo "$(BLUE)[INFO]$(NC) 开始部署 NGINX 到 Kubernetes..."
|
||||
@cd k8s && chmod +x deploy.sh && \
|
||||
HARBOR_REGISTRY=$(HARBOR_REGISTRY) \
|
||||
HARBOR_USERNAME=$(HARBOR_USERNAME) \
|
||||
HARBOR_PASSWORD=$(HARBOR_PASSWORD) \
|
||||
NGINX_IMAGE_TAG=$(NGINX_IMAGE_TAG) \
|
||||
NAMESPACE=$(NAMESPACE) \
|
||||
./deploy.sh
|
||||
|
||||
status: ## 查看部署状态
|
||||
@echo "$(BLUE)[INFO]$(NC) 查看 NGINX 部署状态..."
|
||||
@echo ""
|
||||
@echo "$(YELLOW)=== Pods ===$(NC)"
|
||||
@kubectl get pods -l app=nginx -n $(NAMESPACE) -o wide || true
|
||||
@echo ""
|
||||
@echo "$(YELLOW)=== Services ===$(NC)"
|
||||
@kubectl get services -l app=nginx -n $(NAMESPACE) || true
|
||||
@echo ""
|
||||
@echo "$(YELLOW)=== Deployments ===$(NC)"
|
||||
@kubectl get deployments -l app=nginx -n $(NAMESPACE) || true
|
||||
@echo ""
|
||||
@echo "$(YELLOW)=== HPA ===$(NC)"
|
||||
@kubectl get hpa -l app=nginx -n $(NAMESPACE) 2>/dev/null || echo "HPA 未启用"
|
||||
|
||||
logs: ## 查看 NGINX Pod 日志
|
||||
@echo "$(BLUE)[INFO]$(NC) 查看 NGINX Pod 日志..."
|
||||
@kubectl logs -l app=nginx -n $(NAMESPACE) -f --all-containers=true
|
||||
|
||||
port-forward: ## 设置端口转发以便本地访问
|
||||
@echo "$(BLUE)[INFO]$(NC) 设置端口转发 localhost:8080 -> nginx-service:80"
|
||||
@echo "$(YELLOW)访问 http://localhost:8080 来测试 NGINX$(NC)"
|
||||
@kubectl port-forward service/nginx-service 8080:80 -n $(NAMESPACE)
|
||||
|
||||
scale: ## 扩缩容 NGINX 部署 (用法: make scale REPLICAS=5)
|
||||
@if [ -z "$(REPLICAS)" ]; then echo "$(RED)[ERROR]$(NC) 请指定副本数: make scale REPLICAS=5"; exit 1; fi
|
||||
@echo "$(BLUE)[INFO]$(NC) 扩缩容 NGINX 到 $(REPLICAS) 个副本..."
|
||||
@kubectl scale deployment nginx-deployment --replicas=$(REPLICAS) -n $(NAMESPACE)
|
||||
@kubectl rollout status deployment/nginx-deployment -n $(NAMESPACE)
|
||||
|
||||
update: check-env ## 更新 NGINX 镜像版本
|
||||
@echo "$(BLUE)[INFO]$(NC) 更新 NGINX 镜像到 $(NGINX_IMAGE_TAG)..."
|
||||
@kubectl set image deployment/nginx-deployment nginx=$(HARBOR_REGISTRY)/test/nginx:$(NGINX_IMAGE_TAG) -n $(NAMESPACE)
|
||||
@kubectl rollout status deployment/nginx-deployment -n $(NAMESPACE)
|
||||
@echo "$(GREEN)[SUCCESS]$(NC) 镜像更新完成"
|
||||
|
||||
rollback: ## 回滚到上一个版本
|
||||
@echo "$(BLUE)[INFO]$(NC) 回滚 NGINX 部署到上一个版本..."
|
||||
@kubectl rollout undo deployment/nginx-deployment -n $(NAMESPACE)
|
||||
@kubectl rollout status deployment/nginx-deployment -n $(NAMESPACE)
|
||||
@echo "$(GREEN)[SUCCESS]$(NC) 回滚完成"
|
||||
|
||||
restart: ## 重启 NGINX 部署
|
||||
@echo "$(BLUE)[INFO]$(NC) 重启 NGINX 部署..."
|
||||
@kubectl rollout restart deployment/nginx-deployment -n $(NAMESPACE)
|
||||
@kubectl rollout status deployment/nginx-deployment -n $(NAMESPACE)
|
||||
@echo "$(GREEN)[SUCCESS]$(NC) 重启完成"
|
||||
|
||||
clean: ## 清理 NGINX 部署
|
||||
@echo "$(BLUE)[INFO]$(NC) 清理 NGINX 部署..."
|
||||
@kubectl delete -f k8s/nginx-deployment.yaml -n $(NAMESPACE) --ignore-not-found=true
|
||||
@kubectl delete secret harbor-registry-secret -n $(NAMESPACE) --ignore-not-found=true
|
||||
@echo "$(GREEN)[SUCCESS]$(NC) 清理完成"
|
||||
|
||||
config: ## 显示当前配置
|
||||
@echo "$(BLUE)[INFO]$(NC) 当前配置:"
|
||||
@echo " Harbor 仓库: $(HARBOR_REGISTRY)"
|
||||
@echo " Harbor 用户: $(HARBOR_USERNAME)"
|
||||
@echo " 镜像标签: $(NGINX_IMAGE_TAG)"
|
||||
@echo " 命名空间: $(NAMESPACE)"
|
||||
@echo " 部署文件: $(DEPLOYMENT_FILE)"
|
||||
|
||||
test: ## 测试 NGINX 服务连接
|
||||
@echo "$(BLUE)[INFO]$(NC) 测试 NGINX 服务连接..."
|
||||
@POD_NAME=$$(kubectl get pods -l app=nginx -n $(NAMESPACE) -o jsonpath='{.items[0].metadata.name}'); \
|
||||
if [ -n "$$POD_NAME" ]; then \
|
||||
echo "测试 Pod: $$POD_NAME"; \
|
||||
kubectl exec $$POD_NAME -n $(NAMESPACE) -- curl -s -o /dev/null -w "HTTP状态码: %{http_code}\n" http://localhost/; \
|
||||
else \
|
||||
echo "$(RED)[ERROR]$(NC) 未找到运行中的 NGINX Pod"; \
|
||||
fi
|
||||
|
||||
debug: ## 调试部署问题
|
||||
@echo "$(BLUE)[INFO]$(NC) 调试 NGINX 部署..."
|
||||
@echo ""
|
||||
@echo "$(YELLOW)=== 检查 Secrets ===$(NC)"
|
||||
@kubectl get secret harbor-registry-secret -n $(NAMESPACE) -o yaml 2>/dev/null || echo "Secret 不存在"
|
||||
@echo ""
|
||||
@echo "$(YELLOW)=== 检查事件 ===$(NC)"
|
||||
@kubectl get events --sort-by=.metadata.creationTimestamp -n $(NAMESPACE) | grep nginx | tail -10
|
||||
@echo ""
|
||||
@echo "$(YELLOW)=== Pod 详细信息 ===$(NC)"
|
||||
@POD_NAME=$$(kubectl get pods -l app=nginx -n $(NAMESPACE) -o jsonpath='{.items[0].metadata.name}' 2>/dev/null); \
|
||||
if [ -n "$$POD_NAME" ]; then \
|
||||
kubectl describe pod $$POD_NAME -n $(NAMESPACE); \
|
||||
else \
|
||||
echo "未找到 NGINX Pod"; \
|
||||
fi
|
||||
|
||||
# 为方便使用,创建一些简短的别名
|
||||
s: status ## 别名: status
|
||||
l: logs ## 别名: logs
|
||||
d: deploy ## 别名: deploy
|
||||
c: clean ## 别名: clean
|
||||
u: update ## 别名: update
|
295
k8s/deploy.bat
295
k8s/deploy.bat
@ -1,295 +0,0 @@
|
||||
@echo off
|
||||
REM Kubernetes 自动部署脚本 (Windows 版本)
|
||||
REM 用于从私有 Harbor 仓库拉取 NGINX 镜像并部署到 K8s 集群
|
||||
|
||||
setlocal EnableDelayedExpansion
|
||||
|
||||
REM 设置颜色代码(Windows 10/11 支持 ANSI 转义序列)
|
||||
set "RED=[91m"
|
||||
set "GREEN=[92m"
|
||||
set "YELLOW=[93m"
|
||||
set "BLUE=[94m"
|
||||
set "NC=[0m"
|
||||
|
||||
REM 打印带颜色的信息
|
||||
:print_info
|
||||
echo %BLUE%[INFO]%NC% %~1
|
||||
goto :eof
|
||||
|
||||
:print_success
|
||||
echo %GREEN%[SUCCESS]%NC% %~1
|
||||
goto :eof
|
||||
|
||||
:print_warning
|
||||
echo %YELLOW%[WARNING]%NC% %~1
|
||||
goto :eof
|
||||
|
||||
:print_error
|
||||
echo %RED%[ERROR]%NC% %~1
|
||||
goto :eof
|
||||
|
||||
REM 检查必要的环境变量
|
||||
:check_env_vars
|
||||
call :print_info "检查环境变量..."
|
||||
|
||||
set "missing_vars="
|
||||
if "%HARBOR_REGISTRY%"=="" set "missing_vars=%missing_vars% HARBOR_REGISTRY"
|
||||
if "%HARBOR_USERNAME%"=="" set "missing_vars=%missing_vars% HARBOR_USERNAME"
|
||||
if "%HARBOR_PASSWORD%"=="" set "missing_vars=%missing_vars% HARBOR_PASSWORD"
|
||||
if "%NGINX_IMAGE_TAG%"=="" set "missing_vars=%missing_vars% NGINX_IMAGE_TAG"
|
||||
|
||||
if not "%missing_vars%"=="" (
|
||||
call :print_error "缺少必要的环境变量: %missing_vars%"
|
||||
echo.
|
||||
echo 请设置以下环境变量:
|
||||
echo set HARBOR_REGISTRY=^<你的Harbor仓库地址^>
|
||||
echo set HARBOR_USERNAME=^<Harbor用户名^>
|
||||
echo set HARBOR_PASSWORD=^<Harbor密码^>
|
||||
echo set NGINX_IMAGE_TAG=^<镜像标签,如: %%GITHUB_SHA%% 或 latest^>
|
||||
echo.
|
||||
echo 或者创建 .env 文件包含这些变量
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
call :print_success "环境变量检查通过"
|
||||
goto :eof
|
||||
|
||||
REM 检查 kubectl 连接
|
||||
:check_kubectl
|
||||
call :print_info "检查 kubectl 连接..."
|
||||
|
||||
kubectl version --client >nul 2>&1
|
||||
if errorlevel 1 (
|
||||
call :print_error "kubectl 未安装或不在 PATH 中"
|
||||
echo 请从 https://kubernetes.io/docs/tasks/tools/install-kubectl-windows/ 安装 kubectl
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
kubectl cluster-info >nul 2>&1
|
||||
if errorlevel 1 (
|
||||
call :print_error "无法连接到 Kubernetes 集群"
|
||||
echo 请确保:
|
||||
echo 1. kubectl 已正确配置
|
||||
echo 2. 能够访问 Kubernetes 集群
|
||||
echo 3. 当前用户有适当的权限
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
call :print_success "kubectl 连接正常"
|
||||
kubectl cluster-info --short
|
||||
goto :eof
|
||||
|
||||
REM 创建命名空间(如果不存在)
|
||||
:create_namespace
|
||||
set "namespace=%~1"
|
||||
if "%namespace%"=="" set "namespace=default"
|
||||
|
||||
call :print_info "检查命名空间: %namespace%"
|
||||
|
||||
kubectl get namespace %namespace% >nul 2>&1
|
||||
if errorlevel 1 (
|
||||
call :print_info "创建命名空间: %namespace%"
|
||||
kubectl create namespace %namespace%
|
||||
if errorlevel 1 (
|
||||
call :print_error "创建命名空间失败"
|
||||
exit /b 1
|
||||
)
|
||||
call :print_success "命名空间 '%namespace%' 创建成功"
|
||||
) else (
|
||||
call :print_success "命名空间 '%namespace%' 已存在"
|
||||
)
|
||||
goto :eof
|
||||
|
||||
REM 创建或更新 Harbor 仓库访问凭据
|
||||
:create_harbor_secret
|
||||
set "namespace=%~1"
|
||||
if "%namespace%"=="" set "namespace=default"
|
||||
|
||||
call :print_info "创建/更新 Harbor 仓库访问凭据..."
|
||||
|
||||
REM 删除已存在的 secret(如果存在)
|
||||
kubectl delete secret harbor-registry-secret -n %namespace% --ignore-not-found=true >nul 2>&1
|
||||
|
||||
REM 创建新的 secret
|
||||
kubectl create secret docker-registry harbor-registry-secret --docker-server=%HARBOR_REGISTRY% --docker-username=%HARBOR_USERNAME% --docker-password=%HARBOR_PASSWORD% --namespace=%namespace%
|
||||
if errorlevel 1 (
|
||||
call :print_error "创建 Harbor 仓库访问凭据失败"
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
call :print_success "Harbor 仓库访问凭据创建成功"
|
||||
goto :eof
|
||||
|
||||
REM 更新部署文件中的镜像标签
|
||||
:update_deployment_image
|
||||
set "deployment_file=%~1"
|
||||
set "image_tag=%~2"
|
||||
|
||||
call :print_info "更新部署文件中的镜像标签: %image_tag%"
|
||||
|
||||
set "full_image=%HARBOR_REGISTRY%/test/nginx:%image_tag%"
|
||||
|
||||
REM 备份原文件
|
||||
copy "%deployment_file%" "%deployment_file%.bak" >nul
|
||||
|
||||
REM 使用 PowerShell 替换镜像标签
|
||||
powershell -Command "(Get-Content '%deployment_file%') -replace '%HARBOR_REGISTRY%/test/nginx:[^\s]*', '%full_image%' | Set-Content '%deployment_file%'"
|
||||
if errorlevel 1 (
|
||||
call :print_error "更新镜像标签失败"
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
call :print_success "镜像标签更新为: %full_image%"
|
||||
goto :eof
|
||||
|
||||
REM 应用 Kubernetes 配置
|
||||
:apply_k8s_config
|
||||
set "deployment_file=%~1"
|
||||
call :print_info "应用 Kubernetes 配置..."
|
||||
|
||||
if not exist "%deployment_file%" (
|
||||
call :print_error "部署文件不存在: %deployment_file%"
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
kubectl apply -f "%deployment_file%"
|
||||
if errorlevel 1 (
|
||||
call :print_error "应用 Kubernetes 配置失败"
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
call :print_success "Kubernetes 配置应用成功"
|
||||
goto :eof
|
||||
|
||||
REM 等待部署就绪
|
||||
:wait_for_deployment
|
||||
set "deployment_name=nginx-deployment"
|
||||
set "namespace=%~1"
|
||||
set "timeout=%~2"
|
||||
if "%namespace%"=="" set "namespace=default"
|
||||
if "%timeout%"=="" set "timeout=300"
|
||||
|
||||
call :print_info "等待部署就绪..."
|
||||
|
||||
kubectl wait --for=condition=available deployment/%deployment_name% --namespace=%namespace% --timeout=%timeout%s
|
||||
if errorlevel 1 (
|
||||
call :print_error "部署超时,请检查部署状态"
|
||||
kubectl describe deployment %deployment_name% -n %namespace%
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
call :print_success "部署已就绪"
|
||||
goto :eof
|
||||
|
||||
REM 显示部署状态
|
||||
:show_deployment_status
|
||||
set "namespace=%~1"
|
||||
if "%namespace%"=="" set "namespace=default"
|
||||
|
||||
call :print_info "部署状态:"
|
||||
|
||||
echo.
|
||||
echo === Pods ===
|
||||
kubectl get pods -l app=nginx -n %namespace% -o wide
|
||||
|
||||
echo.
|
||||
echo === Services ===
|
||||
kubectl get services -l app=nginx -n %namespace%
|
||||
|
||||
echo.
|
||||
echo === Deployments ===
|
||||
kubectl get deployments -l app=nginx -n %namespace%
|
||||
|
||||
echo.
|
||||
echo === HPA ===
|
||||
kubectl get hpa -l app=nginx -n %namespace% 2>nul || echo HPA 未启用
|
||||
|
||||
echo.
|
||||
echo === Events ===
|
||||
kubectl get events --sort-by=.metadata.creationTimestamp -n %namespace% | findstr /C:"nginx"
|
||||
goto :eof
|
||||
|
||||
REM 获取访问信息
|
||||
:get_access_info
|
||||
set "namespace=%~1"
|
||||
if "%namespace%"=="" set "namespace=default"
|
||||
|
||||
call :print_info "获取访问信息..."
|
||||
|
||||
for /f "tokens=*" %%i in ('kubectl get service nginx-service -n %namespace% -o jsonpath^="{.spec.type}"') do set "service_type=%%i"
|
||||
|
||||
if "%service_type%"=="NodePort" (
|
||||
for /f "tokens=*" %%i in ('kubectl get service nginx-service -n %namespace% -o jsonpath^="{.spec.ports[0].nodePort}"') do set "node_port=%%i"
|
||||
for /f "tokens=*" %%i in ('kubectl get nodes -o jsonpath^="{.items[0].status.addresses[?(@.type==\"InternalIP\")].address}"') do set "node_ip=%%i"
|
||||
call :print_success "NodePort 访问地址: http://!node_ip!:!node_port!"
|
||||
) else if "%service_type%"=="LoadBalancer" (
|
||||
call :print_info "等待 LoadBalancer 外部 IP..."
|
||||
for /f "tokens=*" %%i in ('kubectl get service nginx-service -n %namespace% -o jsonpath^="{.status.loadBalancer.ingress[0].ip}" 2^>nul') do set "external_ip=%%i"
|
||||
if not "!external_ip!"=="" (
|
||||
call :print_success "LoadBalancer 访问地址: http://!external_ip!"
|
||||
) else (
|
||||
call :print_warning "LoadBalancer 外部 IP 仍在分配中"
|
||||
)
|
||||
) else if "%service_type%"=="ClusterIP" (
|
||||
for /f "tokens=*" %%i in ('kubectl get service nginx-service -n %namespace% -o jsonpath^="{.spec.clusterIP}"') do set "cluster_ip=%%i"
|
||||
call :print_success "ClusterIP 访问地址: http://!cluster_ip!"
|
||||
call :print_info "注意: ClusterIP 只能在集群内部访问"
|
||||
)
|
||||
goto :eof
|
||||
|
||||
REM 清理函数
|
||||
:cleanup
|
||||
call :print_info "执行清理操作..."
|
||||
if exist ".\nginx-deployment.yaml.bak" (
|
||||
move ".\nginx-deployment.yaml.bak" ".\nginx-deployment.yaml" >nul
|
||||
)
|
||||
goto :eof
|
||||
|
||||
REM 主函数
|
||||
:main
|
||||
call :print_info "开始 NGINX Kubernetes 自动部署..."
|
||||
|
||||
REM 设置默认值
|
||||
if "%NAMESPACE%"=="" set "NAMESPACE=default"
|
||||
if "%DEPLOYMENT_FILE%"=="" set "DEPLOYMENT_FILE=.\nginx-deployment.yaml"
|
||||
if "%NGINX_IMAGE_TAG%"=="" set "NGINX_IMAGE_TAG=latest"
|
||||
|
||||
REM 检查是否有 .env 文件
|
||||
if exist ".env" (
|
||||
call :print_info "加载 .env 文件..."
|
||||
for /f "usebackq tokens=1,2 delims==" %%a in (".env") do (
|
||||
set "%%a=%%b"
|
||||
)
|
||||
)
|
||||
|
||||
REM 执行部署步骤
|
||||
call :check_env_vars
|
||||
if errorlevel 1 exit /b 1
|
||||
|
||||
call :check_kubectl
|
||||
if errorlevel 1 exit /b 1
|
||||
|
||||
call :create_namespace "%NAMESPACE%"
|
||||
if errorlevel 1 exit /b 1
|
||||
|
||||
call :create_harbor_secret "%NAMESPACE%"
|
||||
if errorlevel 1 exit /b 1
|
||||
|
||||
call :update_deployment_image "%DEPLOYMENT_FILE%" "%NGINX_IMAGE_TAG%"
|
||||
if errorlevel 1 exit /b 1
|
||||
|
||||
call :apply_k8s_config "%DEPLOYMENT_FILE%"
|
||||
if errorlevel 1 exit /b 1
|
||||
|
||||
call :wait_for_deployment "%NAMESPACE%"
|
||||
if errorlevel 1 exit /b 1
|
||||
|
||||
call :show_deployment_status "%NAMESPACE%"
|
||||
call :get_access_info "%NAMESPACE%"
|
||||
|
||||
call :print_success "🎉 NGINX 部署完成!"
|
||||
goto :eof
|
||||
|
||||
REM 如果直接运行此脚本,执行主函数
|
||||
call :main
|
||||
call :cleanup
|
@ -230,6 +230,51 @@ get_access_info() {
|
||||
esac
|
||||
}
|
||||
|
||||
# 处理HTML_DIR变量
|
||||
process_html_dir() {
|
||||
print_info "处理HTML目录变量..."
|
||||
|
||||
# 如果HTML_DIR变量不存在,设置默认值
|
||||
if [[ -z "${HTML_DIR}" ]]; then
|
||||
export HTML_DIR="/mnt/nginx-html"
|
||||
print_warning "HTML_DIR环境变量未设置,使用默认值: $HTML_DIR"
|
||||
else
|
||||
print_info "使用HTML目录: $HTML_DIR"
|
||||
fi
|
||||
|
||||
# 检查PV配置文件是否存在
|
||||
local pv_file="./nginx-pv.yaml"
|
||||
if [[ -f "$pv_file" ]]; then
|
||||
print_info "更新持久卷配置中的HTML目录路径..."
|
||||
|
||||
# 备份原文件
|
||||
cp "$pv_file" "${pv_file}.bak"
|
||||
|
||||
# 替换变量
|
||||
sed -i.tmp "s|\"\${{ vars.HTML_DIR }}\"|\"${HTML_DIR}\"|g" "$pv_file"
|
||||
|
||||
# 清理临时文件
|
||||
rm -f "${pv_file}.tmp"
|
||||
|
||||
print_success "持久卷配置已更新,使用HTML目录: $HTML_DIR"
|
||||
else
|
||||
print_warning "持久卷配置文件不存在: $pv_file"
|
||||
fi
|
||||
}
|
||||
|
||||
# 应用持久卷配置
|
||||
apply_pv_config() {
|
||||
print_info "应用持久卷配置..."
|
||||
|
||||
local pv_file="./nginx-pv.yaml"
|
||||
if [[ -f "$pv_file" ]]; then
|
||||
kubectl apply -f "$pv_file"
|
||||
print_success "持久卷配置应用成功"
|
||||
else
|
||||
print_warning "持久卷配置文件不存在: $pv_file,跳过应用"
|
||||
fi
|
||||
}
|
||||
|
||||
# 主函数
|
||||
main() {
|
||||
print_info "开始 NGINX Kubernetes 自动部署..."
|
||||
@ -244,6 +289,11 @@ main() {
|
||||
check_kubectl
|
||||
create_namespace "$namespace"
|
||||
create_harbor_secret "$namespace"
|
||||
|
||||
# 处理HTML_DIR变量并应用持久卷配置
|
||||
process_html_dir
|
||||
apply_pv_config
|
||||
|
||||
update_deployment_image "$deployment_file" "$image_tag"
|
||||
apply_k8s_config "$deployment_file"
|
||||
wait_for_deployment "$namespace"
|
||||
@ -260,6 +310,9 @@ cleanup() {
|
||||
if [[ -f "./nginx-deployment.yaml.bak" ]]; then
|
||||
mv "./nginx-deployment.yaml.bak" "./nginx-deployment.yaml"
|
||||
fi
|
||||
if [[ -f "./nginx-pv.yaml.bak" ]]; then
|
||||
mv "./nginx-pv.yaml.bak" "./nginx-pv.yaml"
|
||||
fi
|
||||
}
|
||||
|
||||
# 设置清理陷阱
|
||||
|
@ -207,6 +207,35 @@ spec:
|
||||
- name: nginx-logs
|
||||
mountPath: /usr/local/nginx/logs
|
||||
|
||||
# 添加初始化容器检查HTML目录权限
|
||||
- name: init-html-permissions
|
||||
image: busybox
|
||||
command: ["/bin/sh", "-c"]
|
||||
args:
|
||||
- |
|
||||
echo "检查HTML目录权限...";
|
||||
if [ ! -r /usr/local/nginx/html ]; then
|
||||
echo "警告: HTML目录不可读";
|
||||
chmod -R 755 /usr/local/nginx/html;
|
||||
fi;
|
||||
if [ ! -r /usr/local/nginx/html/index.html ] && [ ! -r /usr/local/nginx/html/index.htm ]; then
|
||||
echo "警告: 没有找到index文件,创建默认页面";
|
||||
cat > /usr/local/nginx/html/index.html << 'EOF'
|
||||
<!DOCTYPE html>
|
||||
<html><head><title>Welcome to Nginx</title></head>
|
||||
<body><h1>Welcome to Nginx on Kubernetes!</h1>
|
||||
<p>If you see this page, the persistent volume is mounted but no HTML files were found.</p>
|
||||
</body></html>
|
||||
EOF
|
||||
fi;
|
||||
echo "设置目录权限...";
|
||||
chown -R 65534:65534 /usr/local/nginx/html || true;
|
||||
chmod -R 755 /usr/local/nginx/html || true;
|
||||
echo "HTML目录权限设置完成";
|
||||
volumeMounts:
|
||||
- name: nginx-html-content
|
||||
mountPath: /usr/local/nginx/html
|
||||
|
||||
containers:
|
||||
- name: nginx
|
||||
# 使用私有仓库中的镜像(需要根据实际情况修改)
|
||||
@ -276,6 +305,9 @@ spec:
|
||||
# 添加日志目录挂载
|
||||
- name: nginx-logs
|
||||
mountPath: /usr/local/nginx/logs
|
||||
# 添加HTML内容挂载
|
||||
- name: nginx-html-content
|
||||
mountPath: /usr/local/nginx/html
|
||||
|
||||
volumes:
|
||||
- name: nginx-config-volume
|
||||
@ -289,6 +321,10 @@ spec:
|
||||
# 添加日志目录卷
|
||||
- name: nginx-logs
|
||||
emptyDir: {}
|
||||
# 添加HTML内容卷
|
||||
- name: nginx-html-content
|
||||
persistentVolumeClaim:
|
||||
claimName: nginx-html-pvc
|
||||
|
||||
# 节点选择器(可选)
|
||||
nodeSelector:
|
||||
|
39
k8s/nginx-pv.yaml
Normal file
39
k8s/nginx-pv.yaml
Normal file
@ -0,0 +1,39 @@
|
||||
# 持久卷和持久卷声明定义
|
||||
# 用于存储HTML静态文件
|
||||
|
||||
# 1. 持久卷 - 基于主机路径
|
||||
apiVersion: v1
|
||||
kind: PersistentVolume
|
||||
metadata:
|
||||
name: nginx-html-pv
|
||||
labels:
|
||||
app: nginx
|
||||
type: local
|
||||
spec:
|
||||
storageClassName: manual
|
||||
capacity:
|
||||
storage: 1Gi
|
||||
accessModes:
|
||||
- ReadWriteMany
|
||||
hostPath:
|
||||
path: "${{ vars.HTML_DIR }}"
|
||||
persistentVolumeReclaimPolicy: Retain
|
||||
|
||||
---
|
||||
# 2. 持久卷声明
|
||||
apiVersion: v1
|
||||
kind: PersistentVolumeClaim
|
||||
metadata:
|
||||
name: nginx-html-pvc
|
||||
namespace: default
|
||||
spec:
|
||||
storageClassName: manual
|
||||
accessModes:
|
||||
- ReadWriteMany
|
||||
resources:
|
||||
requests:
|
||||
storage: 1Gi
|
||||
selector:
|
||||
matchLabels:
|
||||
app: nginx
|
||||
type: local
|
Loading…
x
Reference in New Issue
Block a user