Files
Deploy-Laboratory/docs/04-01-k3s-nodejs-高级部署.md
2026-03-29 09:08:01 +08:00

97 lines
4.0 KiB
Markdown
Raw Permalink 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.
# 04-01-k3s-nodejs-高级部署
> Node.js 属于 `04` 高级部署序列。
> 本文作为 Node.js 主入口:先跑通基础链路,再扩展到自定义端口、存储与构建。
## TL;DR
- **自动化验收**`./ansible/bin/verify.sh run 04-01`
- **关键前置**:按本文「前置条件」准备环境变量/Secret/入口 IP
- **成功判据**:达到本文「预期」且 playbook 断言通过
- **排障**:见本文「排障」
## 前置条件
- 已完成 `01-02-k3s-工作节点.md`
- 已完成 `03-01-k3s-traefik-dashboard.md`(可选,便于观察路由)
- 已完成 `03-04-k3s-cloudflare-tunnel-配置接入.md`(如需外网入口)
## 基础部署步骤
1. 创建 Node.js Deployment默认监听 `3000`
2. 创建 Service`80 -> 3000`
3. 创建 Ingress 或 IngressRoute路径建议 `/node`
## 清单路径(唯一真源)
完整 YAML 不在本文重复粘贴,请以仓库内文件为准(与 Ansible 共用):
| 项 | 路径 / 命令 |
|----|-------------|
| 清单文件 | [`ansible/files/04-01/04-01-nodejs-demo.yaml`](../ansible/files/04-01/04-01-nodejs-demo.yaml) |
| 手工应用 | `kubectl apply -f ansible/files/04-01/04-01-nodejs-demo.yaml` |
| Ansible | `ansible-playbook -i ansible/inventory.ini ansible/playbooks/verify/04-01.yml -e nodejs_demo_manifest=04-01-nodejs-demo.yaml` |
索引与累积说明见 `ansible/files/04-01/` 目录内的累积清单文件(`04-01``04-14`)。
### 相对上游
本文为 **基线**,无上游清单;以下为资源摘要(便于检索,**以 YAML 文件为准**)。
| 资源 | 要点 |
|------|------|
| Deployment `nodejs-demo` | `node:18-alpine`,监听 **3000**`replicas: 1` |
| Service | `80``targetPort: 3000` |
| Ingress | `entrypoints: web`,路径 **`/node`**,无 `host` |
应用方式:
```bash
kubectl apply -f ansible/files/04-01/04-01-nodejs-demo.yaml
```
## 基础验证
```bash
kubectl get pod,svc,ing -n default -o wide
curl -s --max-time 3 http://192.168.2.61/node/
curl -s --max-time 3 http://192.168.2.62/node/
```
预期:返回应用内容(如 `Hello World from Node.js`)。
## 失败排查
统一看:`06-01-k3s-networkpolicy-故障排查.md`
## 部署阶段扩展(分项导航)
在本文 `nodejs-demo` 基线上按主题增量实践(**`04-02``04-14` 已按 Core→Plus→Pro、从简到繁编号**)。**`ansible/files/04-01/`** 保留**全系列累积清单**便于对照 diff**`04-02``04-13`** 各篇在 **`ansible/files/04-XX/`** 另有**同构副本**专供分课解耦与手工复制。每篇文档附 **相对上一篇的变更表**;基线与 [`ansible/playbooks/verify/04-01.yml`](../ansible/playbooks/verify/04-01.yml) 共用。
- `04-02-nodejs-端口与Service.md``containerPort` 与 Service/Ingress 端口对应
- `04-03-nodejs-镜像与运行命令.md`:镜像 tag、`imagePullPolicy``command`/`args`
- `04-04-nodejs-环境变量与配置注入.md`ConfigMap/Secret、`env`/`envFrom`
- `04-05-nodejs-探针与健康检查.md`:存活/就绪/启动探针
- `04-06-nodejs-副本与滚动发布.md``replicas`、滚动策略
- `04-07-nodejs-Ingress与Traefik.md`:路径、主机名、`web`/`websecure`
- `04-08-nodejs-资源请求与限制.md``resources`、OOM/CPU 节流
- `04-09-nodejs-调度与亲和.md``nodeSelector`、亲和、容忍
- `04-10-nodejs-安全上下文.md``securityContext`、非 root、只读根等
- `04-11-nodejs-存储与卷.md``emptyDir`、PVC、配置卷挂载
- `04-12-nodejs-TLS与证书.md`Ingress `tls`、HTTPS`03-02` ACME 配合)
- `04-13-nodejs-HPA.md`:水平自动扩缩容
- `04-14-nodejs-GitOps与CI流水线.md`构建镜像、GitOps/CI 闭环
## 下一步
- 返回 `00-00-构建总览.md`,按导航继续。
## 排障
- **先看 playbook 输出**:失败时先定位是 deploy/wait/http_check 哪一步。
- **集群侧总览**`kubectl get nodes -o wide``kubectl -n kube-system get pods -o wide`
- **事件与日志**`kubectl -n <ns> describe ...``kubectl -n <ns> logs ... --tail=200`