Ingress & Service
請求進入 Pod 的三層架構
外部使用者
│
▼
┌──────────┐
│ Ingress │ L7 路由規則(/api/* → Service A, /dashboard/* → Service B)
└──────────┘
│
▼
┌──────────┐
│ Service │ 穩定的內部入口,用 label selector 找到 Pod 做 load balancing
└──────────┘
│
▼
┌──────────┐
│ Pod │ 實際跑程式的容器
└──────────┘
Ingress
- 本質:L7 路由規則表,定義「什麼 host/path → 哪個 Service」
- 不是 endpoint,真正開出 endpoint 的是 Ingress Controller
- 需要搭配 Ingress Controller 才能運作(AWS ALB Controller, NGINX 等)
pathType 三種
| pathType | 說明 |
|---|---|
Exact | 完全一致(/foo 只匹配 /foo) |
Prefix | 前綴匹配,以 / 為邊界 |
ImplementationSpecific | 交給 Ingress Controller 決定(可支援 wildcard *) |
Ingress NGINX 退役(2026/03)
- 退役的是 Ingress NGINX Controller,不是 Ingress API 本身
- Ingress API 是 GA,frozen 但不會 deprecate
- 官方推薦遷移到 Gateway API(v1.5, 2026/02 發布)
- AWS ALB Controller 不受影響(AWS 自己維護)
Service
三種 type
| type | 對外暴露方式 | 用途 |
|---|---|---|
| ClusterIP | 只有 cluster 內部可連(預設) | 內部服務互相呼叫 |
| NodePort | 每個 Node 開固定 port(30000-32767) | 搭配 ALB 等外部 LB |
| LoadBalancer | 自動建雲端 LB | 正式對外服務 |
port vs targetPort
ports:
- port: 80 # Service 對外暴露的 port(cluster 內用這個)
targetPort: 8081 # Pod 實際 listen 的 port(= containerPort)
內部 DNS 解析
K8s 自動為每個 Service 建立 DNS record,Pod 之間可以直接用名稱互連:
# 同 namespace → 直接用 Service 名稱
curl http://my-service:80
# 跨 namespace → 加 namespace 名稱
curl http://my-service.other-namespace:80
# 完整 FQDN(較少用,但最明確)
curl http://my-service.other-namespace.svc.cluster.local:80
AWS ALB Target Type: Instance vs IP Mode
When using AWS ALB Controller with EKS, the ALB needs to know where to send traffic. There are two modes:
Instance Mode (default)
Client → ALB → Node EC2:NodePort → kube-proxy (iptables NAT) → Pod:8427
↑ ↑
2 hops Extra NAT layer
- ALB registers Node EC2 instances as targets
- Traffic hits Node's NodePort → kube-proxy forwards to correct Pod
- Requires: Service type must be
NodePort(otherwise no NodePort → port=0 → error) - No annotation needed (this is the default)
ALB 把流量送到 Node 的 NodePort,Node 上的 kube-proxy 再轉發到 Pod。Service 必須是 NodePort type 。
IP Mode
Client → ALB → Pod:8427
↑
1 hop, direct
- ALB registers Pod IPs directly as targets
- Traffic goes straight to Pod, bypasses Node and kube-proxy
- Works with any Service type (ClusterIP or NodePort)
- Requires annotation:
alb.ingress.kubernetes.io/target-type: ip
ALB 直接送流量到 Pod IP,不經過 Node、不經過 kube-proxy。ClusterIP service 就可以用。
Setting in Ingress YAML
# Instance mode (default, no annotation needed)
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
alb.ingress.kubernetes.io/scheme: internal
# No target-type annotation → defaults to instance mode
# Service MUST be NodePort type
---
# IP mode (explicit annotation)
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
alb.ingress.kubernetes.io/scheme: internal
alb.ingress.kubernetes.io/target-type: ip # ← This line
# Service can be ClusterIP or NodePort
Comparison / 比較
| Instance Mode (NodePort) | IP Mode | |
|---|---|---|
| Service type required / 要求 | Must be NodePort | ClusterIP or NodePort |
| ALB target / 註冊目標 | Node EC2 instance | Pod IP |
| Traffic hops / 流量跳數 | 2 (ALB→Node→Pod) | 1 (ALB→Pod) |
| Goes through kube-proxy / 經過 kube-proxy | Yes (iptables NAT) | No |
| Latency / 延遲 | Slightly higher | Lower |
| Opens port on Node / Node 開 port | Yes (30000-32767) | No |
| Pod scaling response / 擴縮反應 | Slower (update Node targets) | Faster (register/remove Pod IPs directly) |
| Health check accuracy / 健康檢查準確度 | Hits Node, may not reflect Pod state | Hits Pod directly |
| EKS support / EKS 支援 | All versions | Requires VPC CNI (installed by default on EKS) |