Ingress 简介
学习目标
- 理解 Ingress 的作用和使用场景
- 掌握 Ingress 与 Service 的关系
- 了解 Ingress 的基本工作原理
核心概念
一句话解释
Ingress 是 Kubernetes 的 HTTP/HTTPS 路由规则,它将外部请求根据域名或路径转发到集群内部的 Service。
为什么需要 Ingress
问题:每个 Service 都需要一个负载均衡器
Service A (LoadBalancer) → Public IP A
Service B (LoadBalancer) → Public IP B
Service C (LoadBalancer) → Public IP C
成本高,管理复杂
解决方案:Ingress
┌─────────────────────────────────────────┐
│ Ingress Controller │
│ (一个负载均衡器) │
│ Public IP: 1.2.3.4 │
└─────────────────────────────────────────┘
↓
┌───────────────┼───────────────┐
↓ ↓ ↓
app.example.com api.example.com admin.example.com
↓ ↓ ↓
┌───────┐ ┌───────┐ ┌───────┐
│Svc A │ │Svc B │ │Svc C │
└───────┘ └───────┘ └───────┘
Ingress 组件
Ingress 包含两个部分:
1. Ingress 资源:定义路由规则
┌─────────────────────────────────────────┐
│ Ingress Resource │
│ - 域名: app.example.com │
│ - 路径: /api → Service B │
│ - 路径: / → Service A │
└─────────────────────────────────────────┘
2. Ingress Controller:执行路由规则
┌─────────────────────────────────────────┐
│ Ingress Controller │
│ - 监听 Ingress 资源变化 │
│ - 配置负载均衡器(Nginx/HAProxy等) │
│ - 处理实际流量转发 │
└─────────────────────────────────────────┘
Ingress vs Service
| 方面 | Service | Ingress |
|---|---|---|
| 层级 | L4(TCP/UDP) | L7(HTTP/HTTPS) |
| 功能 | 负载均衡 | 路由、SSL 终止、虚拟主机 |
| 域名 | 不支持 | 支持 |
| 路径路由 | 不支持 | 支持 |
| SSL 终止 | 不支持 | 支持 |
Ingress 工作流程
请求流程:
1. 用户访问 app.example.com/api
↓
2. DNS 解析到 Ingress Controller 的 IP
↓
3. Ingress Controller 接收请求
↓
4. 根据 Ingress 规则匹配
- 域名: app.example.com ✓
- 路径: /api ✓
↓
5. 转发到对应的 Service
→ Service B
↓
6. Service 负载均衡到 Pod
→ Pod B1 或 Pod B2
Ingress 示例
# 基本的 Ingress 资源
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
spec:
rules:
- host: app.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: app-service
port:
number: 80
路径类型
| 类型 | 说明 | 示例 |
|---|---|---|
| Prefix | 前缀匹配 | /api 匹配 /api、/api/users、/api/v1 |
| Exact | 精确匹配 | /api 只匹配 /api |
| ImplementationSpecific | 由控制器决定 | 取决于 Ingress Controller |
路径匹配示例:
Prefix: /api
✓ /api
✓ /api/users
✓ /api/v1/roles
✗ /apis
Exact: /api
✓ /api
✗ /api/users
✗ /api/
ImplementationSpecific
取决于具体实现
实践练习
练习 1:创建简单的 Ingress
# 部署应用
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app
spec:
replicas: 2
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
---
# 创建 Service
apiVersion: v1
kind: Service
metadata:
name: web-service
spec:
selector:
app: web
ports:
- port: 80
targetPort: 80
---
# 创建 Ingress
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: web-ingress
spec:
rules:
- host: web.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: web-service
port:
number: 80
# 应用配置
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
kubectl apply -f ingress.yaml
# 查看 Ingress
kubectl get ingress
kubectl describe ingress web-ingress
练习 2:多路径路由
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: path-ingress
spec:
rules:
- host: app.example.com
http:
paths:
- path: /api
pathType: Prefix
backend:
service:
name: api-service
port:
number: 80
- path: /web
pathType: Prefix
backend:
service:
name: web-service
port:
number: 80
练习 3:测试 Ingress
# 查看 Ingress Controller 的 IP
kubectl get ingress web-ingress
# 修改本地 hosts 文件(用于测试)
# <ingress-ip> web.example.com
# 测试访问
curl http://web.example.com
curl http://app.example.com/api
curl http://app.example.com/web
常见误解
1. Ingress 是负载均衡器
错误理解:Ingress 就是负载均衡器
正确理解:Ingress 是路由规则,Ingress Controller 才是负载均衡器
Ingress 资源:定义规则
Ingress Controller:执行规则
2. Ingress 可以处理所有协议
错误理解:Ingress 可以处理 TCP/UDP
正确理解:Ingress 只处理 HTTP/HTTPS
TCP/UDP 需要使用 Service(NodePort/LoadBalancer)
小结
| 要点 | 说明 |
|---|---|
| Ingress | HTTP/HTTPS 路由规则 |
| 组件 | Ingress 资源 + Ingress Controller |
| 功能 | 域名路由、路径路由、SSL 终止 |
| 路径类型 | Prefix、Exact、ImplementationSpecific |
| 对比 Service | L7 vs L4,支持域名和路径路由 |
练习编辑器
bash
Loading...