# 02-05: Nginx + 控制节点 + Ingress(M1) # 路径 /demo-m1,随机一台控制节点(nodeSelector + toleration,控制节点常有 NoSchedule 污点) # ConfigMap:首页 + default.conf(单文件 subPath 挂载,与 M2~M4 一致,便于 nginx 后续扩展) --- apiVersion: v1 # ConfigMap 使用的 API 版本 kind: ConfigMap # 配置资源类型:ConfigMap metadata: # 对该 ConfigMap 的标识信息 name: nginx-m1-html # ConfigMap 名称 namespace: default # 命名空间 data: # ConfigMap 数据键值区 index.html: | # HTML 内容:会挂载到 nginx 的网页目录
控制节点 + Ingress
Backend: M1
default.conf: | # nginx 配置:通过 subPath 单文件挂载到 conf.d/default.conf server { listen 80 default_server; server_name _; root /usr/share/nginx/html; index index.html; location / { add_header X-Backend "M1"; try_files $uri $uri/ /index.html; } } --- apiVersion: apps/v1 # Deployment 使用的 API 版本 kind: Deployment # 工作负载:Deployment metadata: # Deployment 标识信息 name: nginx-m1 # Deployment 名称 namespace: default # 部署命名空间 labels: # 额外标签(用于检索/筛选) app: nginx-m1 # 应用标签 matrix: "02-05-m1" # 矩阵编号标签(用于你后续调试/统计) spec: # Deployment 期望状态 replicas: 1 # 副本数:本例为 1(便于对应路径验证) selector: # Deployment 用于选择 Pod 的条件 matchLabels: # 标签匹配集合(用于选中模板 Pod) app: nginx-m1 # 必须与 template.metadata.labels 对上 template: # Pod 模板 metadata: # Pod 的元信息 labels: # Pod 标签 app: nginx-m1 # Pod 标签 spec: # Pod 规范 nodeSelector: # 节点选择:固定跑在 control-plane 上 node-role.kubernetes.io/control-plane: "" # 选择带 control-plane 角色标签的节点 tolerations: # 容忍污点:让 Pod 能调度到 control-plane - key: node-role.kubernetes.io/control-plane # 污点 key operator: Exists # 存在即匹配 effect: NoSchedule # 匹配 NoSchedule 污点效果 volumes: # Pod 内卷定义 - name: html # 卷名:给 volumeMounts 引用 configMap: # 卷来源:ConfigMap name: nginx-m1-html # 引用的 ConfigMap 名称 containers: # 容器列表 - name: nginx # 容器名 image: nginx:alpine # nginx 镜像 ports: # 容器端口列表 - containerPort: 80 # nginx HTTP 端口 volumeMounts: # 容器内挂载点列表 - name: html # 对应 volumes[].name mountPath: /usr/share/nginx/html/index.html # 挂载到网页文件路径 subPath: index.html # 从 ConfigMap 里选取单个 key readOnly: true # 只读挂载(配置文件更安全) - name: html # 第二处也使用同一个卷 mountPath: /etc/nginx/conf.d/default.conf # nginx 配置文件路径 subPath: default.conf # 从 ConfigMap 里选取对应 key readOnly: true # 只读挂载 --- apiVersion: v1 # Service 使用的 API 版本 kind: Service # 网络抽象:把 Pod 暴露成稳定访问入口 metadata: # Service 标识 name: nginx-m1 # Service 名称 namespace: default # Service 所在命名空间 spec: # Service 期望状态 selector: # Service 按标签选择后端 Pod app: nginx-m1 # 选择 nginx-m1 Pod ports: # Service 端口映射 - port: 80 # Service 端口 targetPort: 80 # 转发到 Pod 的端口 --- apiVersion: traefik.io/v1alpha1 # Traefik Middleware 使用的 API 版本 kind: Middleware # 路由中间件:stripPrefix metadata: # Middleware 标识 name: stripprefix-m1 # Middleware 名称 namespace: default # 命名空间 spec: # Middleware 配置 stripPrefix: # 去掉前缀 prefixes: # 要剔除的前缀列表 - /demo-m1 # 本矩阵的路径前缀 --- apiVersion: networking.k8s.io/v1 # Ingress 使用的 API 版本 kind: Ingress # 入口资源:把路径转发到 Service metadata: # Ingress 标识 name: nginx-m1 # Ingress 名称 namespace: default # 命名空间 annotations: # Ingress 注解:Traefik 用来绑定中间件 traefik.ingress.kubernetes.io/router.middlewares: default-stripprefix-m1@kubernetescrd # 绑定 stripprefix-m1 spec: # Ingress 规则 rules: # 规则列表 - http: # HTTP 规则 paths: # 路径匹配列表 - path: /demo-m1 # 匹配路径 pathType: Prefix # 前缀匹配类型 backend: # 后端目标 service: # 后端 Service name: nginx-m1 # Service 名 port: # Service 端口 number: 80 # 端口号