Files
Deploy-Laboratory/ansible/files/nodejs-demo/04-10-nodejs-demo.yaml
jack be97836e0d chore: 清理调试脚本并收敛到 Ansible 流程
移除已废弃的调试/验证脚本与空目录,统一文档与脚本说明到 ansible-playbook 的部署方式,避免失效引用和误用路径。

Made-with: Cursor
2026-03-23 19:18:55 +08:00

129 lines
5.0 KiB
YAML
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.
# 对应文档docs/04-10-nodejs-Ingress与Traefik.md
# 累积04-09 + Ingress 增加 host、path 改为 /api访问需 Host: app.example.local
apiVersion: v1 # PVC API 版本
kind: PersistentVolumeClaim # 持久卷声明
metadata: # PVC 元信息
name: nodejs-demo-data # PVC 名称
namespace: default # 命名空间
spec: # PVC 规格
accessModes: # 访问模式
- ReadWriteOnce # RWO同一时间仅单节点挂载读写
storageClassName: local-path # 存储类
resources: # 资源请求
requests: # 配额请求
storage: 1Gi # 申请容量
---
apiVersion: v1 # ConfigMap API 版本
kind: ConfigMap # 配置资源
metadata: # ConfigMap 元信息
name: nodejs-demo-config # ConfigMap 名称
namespace: default # 命名空间
data: # 配置键值
APP_MSG: "Hello from ConfigMap" # 示例消息内容
---
apiVersion: apps/v1 # Deployment API 版本
kind: Deployment # 工作负载Deployment
metadata: # Deployment 元信息
name: nodejs-demo # Deployment 名称
namespace: default # 命名空间
spec: # Deployment 规格
replicas: 1 # 副本数
selector: # Pod 选择器
matchLabels: # 标签匹配集合
app: nodejs-demo # 匹配 app=nodejs-demo 的 Pod
template: # Pod 模板
metadata: # Pod 元信息
labels: # Pod 标签
app: nodejs-demo # 与 selector.matchLabels 对齐
spec: # Pod 规格
nodeSelector: # 节点选择
kubernetes.io/hostname: ylc62 # 固定到指定节点(按实际修改)
securityContext: # Pod 级安全上下文
fsGroup: 1000 # 挂载卷文件组 ID
containers: # 容器列表
- name: nodejs-demo # 容器名
image: node:18.20-alpine # Node.js 镜像
imagePullPolicy: IfNotPresent # 拉取策略
securityContext: # 容器级安全上下文
allowPrivilegeEscalation: false # 禁止提权
runAsNonRoot: true # 非 root 运行
runAsUser: 1000 # 运行用户 UID
readOnlyRootFilesystem: true # 根文件系统只读
env: # 环境变量
- name: APP_MSG # 环境变量名
valueFrom: # 从引用源取值
configMapKeyRef: # 从 ConfigMap key 读取
name: nodejs-demo-config # ConfigMap 名称
key: APP_MSG # ConfigMap 键名
command: # 启动命令
- node # 执行 node
- "-e" # 执行内联脚本
- | # 多行 JS 脚本(内容保持原样)
const http=require('http');
const msg=process.env.APP_MSG||'no env';
http.createServer((q,s)=>s.end(msg)).listen(8080);
ports: # 容器端口
- containerPort: 8080 # 监听端口
resources: # 资源请求与限制
requests: # 最小资源请求
cpu: "50m" # 请求 CPU
memory: "64Mi" # 请求内存
limits: # 资源上限
cpu: "500m" # CPU 限制
memory: "256Mi" # 内存限制
livenessProbe: # 存活探针
httpGet: # HTTP 探测
path: / # 探测路径
port: 8080 # 探测端口
initialDelaySeconds: 3 # 初始延迟
periodSeconds: 10 # 探测周期
readinessProbe: # 就绪探针
httpGet: # HTTP 探测
path: / # 探测路径
port: 8080 # 探测端口
initialDelaySeconds: 2 # 初始延迟
periodSeconds: 5 # 探测周期
volumeMounts: # 卷挂载
- name: tmp # 临时卷名称
mountPath: /tmp # 容器内临时目录
- name: data # 数据卷名称
mountPath: /data # 容器内数据目录
volumes: # 卷定义
- name: tmp # 临时卷
emptyDir: {} # 空目录卷
- name: data # 数据卷
persistentVolumeClaim: # 卷来源为 PVC
claimName: nodejs-demo-data # 绑定 PVC 名称
---
apiVersion: v1 # Service API 版本
kind: Service # Service 资源
metadata: # Service 元信息
name: nodejs-demo # Service 名称
namespace: default # 命名空间
spec: # Service 规格
selector: # 选择后端 Pod
app: nodejs-demo # 选中 app=nodejs-demo
ports: # 端口映射
- port: 80 # Service 暴露端口
targetPort: 8080 # 转发到容器端口
---
apiVersion: networking.k8s.io/v1 # Ingress API 版本
kind: Ingress # Ingress 资源
metadata: # Ingress 元信息
name: nodejs-demo # Ingress 名称
namespace: default # 命名空间
annotations: # Traefik 注解
traefik.ingress.kubernetes.io/router.entrypoints: web # 使用 web(HTTP) 入口
spec: # Ingress 规则
rules: # 规则列表
- host: app.example.local # 主机名匹配
http: # HTTP 路由
paths: # 路径列表
- path: /api # 匹配 API 路径前缀
pathType: Prefix # 前缀匹配
backend: # 后端目标
service: # 后端 Service
name: nodejs-demo # Service 名称
port: # Service 端口
number: 80 # 端口号