在 Kubernetes 的世界里,Nginx Ingress 不仅是流量的门卫,更是一把强大的调控利器。我们已经领略了其基础面貌,现在让我们踏上深度之旅,揭示 Nginx Ingress 的高级功能,助你在 Kubernetes 中创造流量掌控的艺术。
Annotations 是 Nginx Ingress 提供的瑰宝,通过添加注释,你可以像艺术家一样精雕细琢。重写路径、添加自定义配置片段,一切尽在掌握。
# 示例:使用 Annotations 进行路径重写
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$1
nginx.ingress.kubernetes.io/server-snippet: |
location / {
rewrite ^/oldpath/(.*)$ /newpath/$1 permanent;
}
spec:
rules:
- host: myapp.example.com
http:
paths:
- path: /somepath/(.*)
pathType: Prefix
backend:
service:
name: my-app-service
port:
number: 80
Nginx Ingress 巧妙支持不同主机名的流量分发,如同一场魔法表演,每个域名都是独一无二的服务入口。
# 示例:基于主机名的多域名支持
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
spec:
rules:
- host: app1.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: app1-service
port:
number: 80
- host: app2.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: app2-service
port:
number: 80
通过 Ingress Class,你如同指挥家一样,在同一集群中指挥不同配置的 Ingress Controller,灵活应对不同场景。
# 示例:使用 Ingress Class 进行多 Ingress Controller 支持
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
annotations:
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- host: myapp.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-app-service
port:
number: 80
通过配置 SSL/TLS 终端,为你的应用程序搭建安全的通信桥梁,每个域名都有其独特的保卫者。
# 示例:SSL/TLS 终端配置
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
spec:
rules:
- host: secure.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: secure-app-service
port:
number: 443
tls:
- hosts:
- secure.example.com
secretName: my-tls-secret
通过 Annotations,定制 Nginx Ingress 显示自定义的错误页面,为用户呈现更为精致的错误信息。
# 示例:自定义错误页面配置
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
annotations:
nginx.ingress.kubernetes.io/custom-http-errors: "404,500"
spec:
rules:
- host: myapp.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-app-service
port:
number: 80
Nginx Ingress 提供的 Annotations 让你能够实现流量的限速和控制,确保应用程序不受过多流量的干扰。
# 示例:限速和流量控制配置
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
annotations:
nginx.ingress.kubernetes.io/limit-rate: "10r/s"
spec:
rules:
- host: myapp.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-app-service
port:
number: 80
如果你的应用程序使用了 WebSockets 协议,Nginx Ingress 轻松支持,通过添加 nginx.ingress.kubernetes.io/websocket-services
注释,将 WebSockets 转发到后端服务。
# 示例:WebSockets 支持配置
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
annotations:
nginx.ingress.kubernetes.io/websocket-services: "my-websocket-service"
spec:
rules:
- host: myapp.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-websocket-service
port:
number: 80
Nginx Ingress 让你可以配置详细的日志记录,助你更好地监控和调试流量。通过 Annotations 添加自定义的 Nginx 配置,包括日志格式和目标。
# 示例:日志配置
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
annotations:
nginx.ingress.kubernetes.io/server-snippet: |
location / {
access_log /var/log/nginx/myapp.log;
}
spec:
rules:
- host: myapp.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-app-service
port:
number: 80
Nginx Ingress 提供的 Annotations 允许你进行动态负载均衡的配置,包括连接保持时间、负载均衡算法等。让你的应用程序在高峰时期依然优雅。
# 示例:动态负载均衡配置
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
annotations:
nginx.ingress.kubernetes.io/upstream-hash-by: "$remote_addr$request_uri"
spec:
rules:
- host: myapp.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-app-service
port:
number: 80
通过 Annotations,你可以配置 Nginx Ingress 提供基本身份验证、OAuth 认证等自定义认证和授权方式,保护你的应用程序免受未经授权的访问。
# 示例:自定义认证和授权配置
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
annotations:
nginx.ingress.kubernetes.io/auth-type: "basic"
nginx.ingress.kubernetes.io/auth-secret: "my-auth-secret"
spec:
rules:
- host: myapp.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-app-service
port:
number: 80
通过深入理解并灵活运用这些高级功能,你可以更好地定制和优化 Nginx Ingress,以满足复杂应用场景的需求。深入研究文档和实践是掌握这些高级功能的关键,希望这些示例能够为你提供更多的启发。