Files
Deploy-Laboratory/docs/04-02-nodejs-镜像与运行命令.md
jack 8c43761962 feat: 按 doc_id 重组 ansible/files 与验证框架
- ansible/files 改为与文档 XX-YY 对齐的目录结构,更新相关 playbook 路径
- 新增 scripts/verify.sh 与 ansible/playbooks/verify/*.yml,移除单体 verify-matrix.yml
- 补充 docs/00-02 矩阵状态、00-05 验证框架与流程、00-04 环境与 ylc65 工作机说明
- 增加 k3s 存储准备、Longhorn、local-path 等 playbook 与辅助脚本

Made-with: Cursor
2026-03-26 07:01:14 +08:00

68 lines
3.0 KiB
Markdown
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.
# 04-02-nodejs-镜像与运行命令
> 在 [`04-01-k3s-nodejs-高级部署.md`](04-01-k3s-nodejs-高级部署.md) 的 `nodejs-demo` 基线上,调整**镜像**与**进程启动方式**。
## 前置条件
- 已按 `04-01` 部署并验证 `curl` 可达。
## 清单路径(唯一真源)
| 项 | 路径 / 命令 |
|----|-------------|
| 本篇完整清单(累积至 04-02 | [`ansible/files/04-01-nodejs-demo/04-02-nodejs-demo.yaml`](../ansible/files/04-01-nodejs-demo/04-02-nodejs-demo.yaml) |
| 手工应用 | `kubectl apply -f ansible/files/04-01-nodejs-demo/04-02-nodejs-demo.yaml` |
| Ansible | `ansible-playbook -i ansible/inventory.ini ansible/playbooks/nodejs-demo-apply.yml -e nodejs_demo_manifest=04-02-nodejs-demo.yaml` |
若你更喜欢命令行换镜像,文末也给了 **`kubectl set image`**,可不改仓库清单。
## 场景说明(白话)
- **换镜像版本**:就像本地 `docker pull node:18.20-alpine`K8s 里改 `image:` 一行即可;写死版本号比总写 `latest` 更容易排查「昨天还能跑今天不行」。
- **何时拉镜像imagePullPolicy**:节点上还没有这个镜像时肯定要拉;若 CI 总往同一个 tag 里覆盖推送,一般要 **`Always`**,否则会用到旧层。
- **改启动命令**:镜像自带的入口不满足时,用 `command` / `args` 告诉 K8s「用哪条命令起 Node」和 Docker 里覆盖 `ENTRYPOINT`/`CMD` 一个意思。
- **NODE_OPTIONS 等**:适合放在环境变量里,见 [`04-03-nodejs-环境变量与配置注入.md`](04-03-nodejs-环境变量与配置注入.md)。
### 相对 `04-01` 的变更(原文 → 新文)
| 位置 | 原文(`04-01` | 新文(`04-02` |
|------|-----------------|-----------------|
| `containers[].image` | `node:18-alpine` | `node:18.20-alpine` |
| `containers[].imagePullPolicy` | (默认) | `IfNotPresent` |
| `containers[].command` / `args` | 单行 `["node","-e","...Hello World...listen(3000)"]` | `command: ["node"]` + `args` 两段,`res.end('Hello from pinned image')` |
应用:
```bash
kubectl apply -f ansible/files/04-01-nodejs-demo/04-02-nodejs-demo.yaml
# 或仅打补丁(示意)
kubectl set image deployment/nodejs-demo nodejs-demo=node:18.20-alpine -n default
```
## 验证
```bash
kubectl describe pod -l app=nodejs-demo -n default | sed -n '/Image:/,/Port:/p'
kubectl get pod -l app=nodejs-demo -n default
curl -s --max-time 3 http://<节点IP>/node/
```
预期Pod 为 Running响应体与当前 `command`/`args` 一致。
## 删除 / 回滚
```bash
kubectl rollout undo deployment/nodejs-demo -n default
# 或恢复 04-01 原始 YAML 后 kubectl apply -f nodejs-demo.yaml
```
## 失败排查
- **ImagePullBackOff**:镜像名/tag 错误、私有仓库未配置 `imagePullSecrets`、节点无法访问 registry。
- 统一网络与策略:`06-01-k3s-networkpolicy-故障排查.md`
## 相关文档
- [`04-03-nodejs-环境变量与配置注入.md`](04-03-nodejs-环境变量与配置注入.md)
- [`04-05-nodejs-资源请求与限制.md`](04-05-nodejs-资源请求与限制.md)