第 5 章 · 接入、搜索与对象存储:Nginx / Elasticsearch / MinIO
5.1 Nginx 1.27
适用场景
| 选用 | 作用 |
|---|---|
| 静态资源(HTML/CSS/JS/图片) | 高性能文件服务 |
| 反向代理 | 隐藏后端 API,统一入口 |
| 负载均衡 | upstream 多后端 Pod |
| URL 重写、限流 | location、limit_req |
TLS 终止、多域名路由:生产建议 集群插件 Nginx Ingress + Ingress 规则;Nginx 容器适合 NodePort 直出 或集群内 Sidecar。
商店部署参数
| 项 | 值 |
|---|---|
| id | nginx |
| 镜像 | nginx:1.27-alpine |
| 端口 | 80 |
| PVC | 可选;静态站可挂载 /usr/share/nginx/html |
核心配置语法(nginx.conf / conf.d/*.conf)
# 上游(K8s Service 名)
upstream api_backend {
server api:8080;
keepalive 32;
}
server {
listen 80;
server_name _;
# 静态
location / {
root /usr/share/nginx/html;
index index.html;
try_files $uri $uri/ /index.html;
}
# API 反代
location /api/ {
proxy_pass http://api_backend/;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Connection "";
}
# 健康检查
location /healthz {
return 200 'ok';
add_header Content-Type text/plain;
}
}
常用指令
| 指令 | 用途 |
|---|---|
proxy_pass | 反向代理 |
rewrite | URL 重写 |
limit_req_zone | 限流 |
gzip on | 压缩 |
client_max_body_size | 上传大小限制 |
部署与生效
- 商店部署,NodePort 暴露 80
- 改配置:终端 编辑
/etc/nginx/conf.d/default.conf - 校验:
nginx -t - 热载:
nginx -s reload或 应用部署 → 重启 - 生产:配置写入 PVC 或 ConfigMap(工作负载 YAML 挂载)
业务集成
前端构建产物 dist/ 拷入 html 目录;API 基址设为相对路径 /api/,由 Nginx 转发到 api:8080。
5.2 Elasticsearch 8.14
适用场景
| 选用 | 场景 |
|---|---|
| 全文检索 | 商品搜索、文档搜索 |
| 日志分析(ELK) | 配合 Kibana、Filebeat |
| 聚合分析 | 销售报表、用户行为统计 |
不选用:主业务事务库;小数据量 LIKE 够用(SQL 即可)。
商店部署参数
| 项 | 值 |
|---|---|
| id | elasticsearch |
| 镜像 | elasticsearch:8.14.0 |
| 端口 | 9200(HTTP API) |
| PVC | 30 Gi → /usr/share/elasticsearch/data |
| 资源 | 大型 · 4 核 8Gi |
环境变量
env:
- name: discovery.type
value: "single-node"
- name: xpack.security.enabled
value: "false"
- name: ES_JAVA_OPTS
value: "-Xms2g -Xmx2g"
- name: cluster.name
value: "es-app"
- name: node.name
value: "es-node-1"
生产开启安全:
- name: xpack.security.enabled
value: "true"
- name: ELASTIC_PASSWORD
value: "强密码"
连接语法
HTTP
http://elasticsearch:9200
带认证
https://elastic:密码@elasticsearch:9200
Python elasticsearch 客户端
from elasticsearch import Elasticsearch
es = Elasticsearch(["http://elasticsearch:9200"])
REST API 语法
# 集群健康
curl http://elasticsearch:9200/_cluster/health?pretty
# 创建索引
curl -X PUT http://elasticsearch:9200/products -H 'Content-Type: application/json' -d'