在现代化云原生架构中,Kubernetes与Docker的协同工作模式已经成为行业标准实践。我曾在多个生产级集群中部署过Nginx作为入口控制器和服务网格组件,这种技术组合能够实现:
三者配合使用时,Nginx作为K8s集群的"交通警察",负责将外部请求精准路由到对应Docker容器;Kubernetes则扮演"调度中心"角色,动态管理Nginx实例和业务容器的生命周期。这种架构下,单个Nginx Pod可以处理数万个并发连接,同时通过K8s的Horizontal Pod Autoscaler实现自动扩容。
Docker在技术栈中承担着标准化交付单元的角色。通过Dockerfile构建的镜像包含Nginx或应用服务的完整运行环境,例如典型的Nginx镜像构建:
dockerfile复制FROM nginx:1.21-alpine
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY static/ /usr/share/nginx/html
EXPOSE 80
关键配置要点:
实践经验:生产环境建议使用
nginx:1.21-alpine而非latest标签,避免版本漂移问题
在K8s环境中,Nginx通常以两种形态存在:
典型Ingress配置示例:
yaml复制apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: app-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
rules:
- host: demo.example.com
http:
paths:
- path: /api/(.*)
pathType: Prefix
backend:
service:
name: api-service
port:
number: 8080
K8s通过以下核心资源对象管理协作流程:
资源分配示例:
yaml复制resources:
limits:
cpu: "2"
memory: "1Gi"
requests:
cpu: "500m"
memory: "512Mi"
bash复制helm upgrade --install ingress-nginx ingress-nginx \
--repo https://kubernetes.github.io/ingress-nginx \
--set controller.kind=DaemonSet
yaml复制affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values: [nginx-ingress]
topologyKey: kubernetes.io/hostname
通过ConfigMap实现配置热更新:
bash复制kubectl create configmap nginx-conf --from-file=nginx.conf
yaml复制volumes:
- name: nginx-config
configMap:
name: nginx-conf
volumeMounts:
- mountPath: /etc/nginx
name: nginx-config
bash复制kubectl exec -it nginx-pod -- nginx -s reload
| 参数项 | 推荐值 | 说明 |
|---|---|---|
| worker_processes | auto | 自动匹配CPU核心数 |
| worker_connections | 10240 | 单个worker最大连接数 |
| keepalive_timeout | 75s | 长连接保持时间 |
| client_max_body_size | 20m | 文件上传大小限制 |
调整内核参数提升性能:
bash复制sysctl -w net.core.somaxconn=32768
sysctl -w net.ipv4.tcp_tw_reuse=1
bash复制kubectl describe pod nginx-pod | grep -A10 Events
kubectl logs --tail=100 nginx-pod
kubectl exec -it nginx-pod -- nginx -T
bash复制kubectl get endpoints api-service
kubectl describe service api-service
限制Nginx容器的网络访问范围:
yaml复制apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: nginx-firewall
spec:
podSelector:
matchLabels:
app: nginx
policyTypes:
- Ingress
- Egress
ingress:
- ports:
- protocol: TCP
port: 80
- protocol: TCP
port: 443
egress:
- to:
- podSelector:
matchLabels:
app: backend
使用Cert-Manager自动化证书签发:
yaml复制apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
name: example-com
spec:
secretName: example-com-tls
issuerRef:
name: letsencrypt-prod
dnsNames:
- example.com
- www.example.com
配置Nginx暴露Metrics端点:
yaml复制annotations:
prometheus.io/scrape: "true"
prometheus.io/port: "9113"
prometheus.io/path: "/metrics"
关键监控指标告警阈值:
Fluentd收集日志的配置示例:
xml复制<source>
@type tail
path /var/log/nginx/access.log
pos_file /var/log/nginx/access.log.pos
tag nginx.access
format /^(?<remote>[^ ]*) (?<host>[^ ]*) (?<user>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^\"]*?)(?: +\S*)?)?" (?<code>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>[^\"]*)" "(?<agent>[^\"]*)")?$/
</source>
在K8s环境中调试Nginx配置时,我习惯使用kubectl port-forward快速验证配置变更:
bash复制kubectl port-forward svc/nginx-service 8080:80
这样可以直接在本地通过http://localhost:8080测试,避免频繁发布到测试环境。当遇到复杂的路由规则时,建议先用nginx -T输出完整配置进行检查,再结合curl -v逐步验证每个location块的行为。