Skip to content

Commit 0692ab9

Browse files
CodFrmclaude
andcommitted
🚀 K8s 部署优化:添加 startupProbe + 启动预热,消除冷启动延迟
添加 entrypoint.sh 预热脚本,在 Pod 接收流量前预先渲染关键页面; 配置 startupProbe 确保预热期间不被误杀,readinessProbe 确保预热完成后才切入流量。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 969d24b commit 0692ab9

5 files changed

Lines changed: 51 additions & 2 deletions

File tree

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Thumbs.db
3636
.github
3737
.gitea
3838
deploy
39+
!deploy/docker/entrypoint.sh
3940

4041
# Documentation
4142
README.md

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,6 @@ next-env.d.ts
4646
/public/assets/monaco-editor/*
4747
/public/assets/prismjs/*
4848

49+
.claude
50+
51+
CLAUDE.md

deploy/docker/Dockerfile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,12 @@ COPY --from=builder /app/.next/standalone ./
6262
COPY --from=builder /app/.next/static ./.next/static
6363
COPY --from=builder /app/public ./public
6464

65+
# 复制启动脚本
66+
COPY deploy/docker/entrypoint.sh ./entrypoint.sh
67+
RUN chmod +x ./entrypoint.sh
68+
6569
# 暴露端口
6670
EXPOSE 3000
6771

68-
# 启动应用(standalone 模式使用 server.js
69-
CMD ["node", "server.js"]
72+
# 启动应用(包含预热
73+
CMD ["./entrypoint.sh"]

deploy/docker/entrypoint.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/sh
2+
set -e
3+
4+
# Start Next.js server in the background
5+
node server.js &
6+
SERVER_PID=$!
7+
8+
# Wait for the server to be ready
9+
echo "Waiting for Next.js server to start..."
10+
for i in $(seq 1 30); do
11+
if wget -q --spider http://localhost:3000/ 2>/dev/null; then
12+
echo "Server is up, starting warmup..."
13+
break
14+
fi
15+
if [ "$i" = "30" ]; then
16+
echo "Server failed to start within 30s"
17+
exit 1
18+
fi
19+
sleep 1
20+
done
21+
22+
# Warmup key pages to trigger SSR compilation and caching
23+
echo "Warming up pages..."
24+
wget -q -O /dev/null http://localhost:3000/zh-CN 2>/dev/null || true
25+
wget -q -O /dev/null http://localhost:3000/zh-CN/search 2>/dev/null || true
26+
wget -q -O /dev/null http://localhost:3000/en 2>/dev/null || true
27+
echo "Warmup complete."
28+
29+
# Wait for the server process
30+
wait $SERVER_PID

deploy/helm/templates/deployment.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,25 @@ spec:
3737
- name: http
3838
containerPort: 3000
3939
protocol: TCP
40+
startupProbe:
41+
httpGet:
42+
path: /
43+
port: http
44+
initialDelaySeconds: 5
45+
periodSeconds: 5
46+
failureThreshold: 12
4047
livenessProbe:
4148
httpGet:
4249
path: /
4350
port: http
51+
periodSeconds: 30
52+
failureThreshold: 3
4453
readinessProbe:
4554
httpGet:
4655
path: /
4756
port: http
57+
periodSeconds: 10
58+
failureThreshold: 3
4859
resources:
4960
{{- toYaml .Values.resources | nindent 12 }}
5061
{{- with .Values.env }}

0 commit comments

Comments
 (0)