当前位置: 首页 > news >正文

Kubernetes Ingress 深度解析

Kubernetes Ingress 深度解析

一、Ingress 基本概念

Ingress 是 Kubernetes 中管理外部访问集群服务的 API 对象,提供 HTTP/HTTPS 路由规则,实现以下功能:

  • 基于域名/路径的路由
  • TLS/SSL 终止
  • 负载均衡
  • 流量控制

与传统服务的区别

特性IngressService (NodePort/LoadBalancer)
协议支持HTTP/HTTPS/GRPC所有TCP/UDP协议
路由能力基于主机名和路径的复杂路由简单的端口转发
实现层级L7 (应用层)L4 (传输层)
外部依赖需要Ingress Controller不需要额外组件

二、核心架构组成

请求
配置
Client
Ingress资源
Ingress Controller
负载均衡器
后端Service
Pod

1. Ingress 资源 (YAML定义)

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: example-ingressannotations:nginx.ingress.kubernetes.io/rewrite-target: /
spec:rules:- host: "example.com"http:paths:- pathType: Prefixpath: "/shop"backend:service:name: shop-serviceport:number: 80

2. Ingress Controller

常见实现:

  • Nginx Ingress Controller
  • Traefik
  • HAProxy Ingress
  • AWS ALB Ingress Controller
  • Istio Gateway

三、Ingress 详细配置

1. 路由规则类型

路径匹配方式
paths:
- path: /staticpathType: Prefix  # 前缀匹配
- path: /exactpathType: Exact    # 精确匹配
- path: /regexpathType: ImplementationSpecific  # 实现特定
多主机名配置
rules:
- host: "shop.example.com"http: {...}
- host: "blog.example.com"http: {...}

2. TLS 配置

spec:tls:- hosts:- "example.com"secretName: example-tls  # 存储证书的Secret

创建证书Secret:

kubectl create secret tls example-tls \--cert=path/to/cert.pem \--key=path/to/key.pem

3. 注解扩展功能(以Nginx为例)

annotations:# 限速设置nginx.ingress.kubernetes.io/limit-rpm: "100"# 跨域支持nginx.ingress.kubernetes.io/enable-cors: "true"# 重写规则nginx.ingress.kubernetes.io/rewrite-target: /$2# 会话保持nginx.ingress.kubernetes.io/affinity: "cookie"

四、部署实践

1. 安装Ingress Controller(以Nginx为例)

# 使用官方部署清单
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.8.1/deploy/static/provider/cloud/deploy.yaml

2. 验证安装

# 检查Controller Pod
kubectl get pods -n ingress-nginx# 获取外部IP
kubectl get svc -n ingress-nginx

3. 完整部署示例

# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: web-app
spec:replicas: 3selector:matchLabels:app: webtemplate:metadata:labels:app: webspec:containers:- name: webimage: nginx:1.21ports:- containerPort: 80
---
# service.yaml
apiVersion: v1
kind: Service
metadata:name: web-service
spec:selector:app: webports:- protocol: TCPport: 80targetPort: 80
---
# ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: web-ingress
spec:rules:- host: "demo.example.com"http:paths:- path: /pathType: Prefixbackend:service:name: web-serviceport:number: 80

五、高级功能

1. 流量切分 (Canary发布)

annotations:nginx.ingress.kubernetes.io/canary: "true"nginx.ingress.kubernetes.io/canary-weight: "20"  # 20%流量

2. 基于Header的路由

annotations:nginx.ingress.kubernetes.io/canary-by-header: "X-Canary"nginx.ingress.kubernetes.io/canary-by-header-value: "true"

3. 身份认证

# 创建认证Secret
htpasswd -c auth foo
kubectl create secret generic basic-auth --from-file=auth
annotations:nginx.ingress.kubernetes.io/auth-type: basicnginx.ingress.kubernetes.io/auth-secret: basic-auth

六、性能优化

1. 配置调优

annotations:# 连接池设置nginx.ingress.kubernetes.io/upstream-keepalive-connections: "100"nginx.ingress.kubernetes.io/upstream-keepalive-timeout: "60"# 缓冲区设置nginx.ingress.kubernetes.io/proxy-buffer-size: "16k"

2. 监控指标

# 启用Prometheus监控
annotations:prometheus.io/scrape: "true"prometheus.io/port: "10254"

七、常见问题排查

  1. Ingress Controller 未分配IP

    kubectl describe ingress <ingress-name>
    kubectl get events -n ingress-nginx
    
  2. 502 Bad Gateway

    # 检查后端服务
    kubectl get endpoints <service-name>
    kubectl logs <ingress-controller-pod>
    
  3. 证书问题

    kubectl describe secret <tls-secret-name>
    openssl s_client -connect <host>:443 -servername <host>
    

Ingress 作为 Kubernetes 的入口网关,通过灵活的配置可以满足各种生产环境需求。实际使用时需根据业务场景选择合适的 Controller 和配置策略。

相关文章:

  • Java @Transactional事物隔离级别和默认值详解
  • 【模型量化】量化基础
  • 如何禁止AutoCAD这类软件联网
  • DeepSeek-Prover-V2-671B:数学推理的大模型新力量
  • OpenCV 图形API(76)图像与通道拼接函数-----对输入图像进行归一化操作函数normalize()
  • 防止HTTPS页面通过<iframe>标签嵌入HTTP内容
  • 使用Kestrel Web 服务器部署.net6项目
  • 第四部分:赋予网页健壮的灵魂 —— TypeScript(中)
  • 高等数学-第七版-下册 选做记录 习题9-6
  • 【JavaEE】网络原理之初识(1.0)
  • PH热榜 | 2025-04-30
  • AI与无人零售:如何通过智能化技术提升消费者体验和运营效率?
  • 写劳动节前的 跨系统 文件传输
  • ArrayList的特点及应用场景
  • 【计算机视觉】图像分割:Segment Anything (SAM):通用图像分割的范式革命
  • 【Linux】Linux 系统中,定时任务(计划任务)
  • 代码随想录算法训练营第三十一天
  • 一种导弹追踪算法的MATLAB仿真实现
  • Windows 系统中安装 flash - attn
  • Dify添加ollama模型失败:NewConnectionError: Failed to establish a new connection
  • 近七成科创板公司2024年营收增长,285家营收创历史新高
  • 浙商银行一季度净赚超59亿微增0.61%,非息净收入降逾22%
  • 五一小长假,带着小狗去上海音乐厅
  • 建行一季度净利833.51亿同比下降3.99%,营收降5.4%
  • 老凤祥一季度净利减少两成,去年珠宝首饰营收下滑19%
  • 民生访谈|宝妈宝爸、毕业生、骑手……上海如何为不同人群提供就业保障