
Ingress Controller คืออะไร? ทำไมถึงสำคัญ?
ใน Kubernetes เมื่อต้องการให้ Traffic จากภายนอก (Internet) เข้าถึง Service ภายใน Cluster ได้ มี 3 วิธีหลัก คือ NodePort, LoadBalancer และ Ingress ซึ่ง Ingress เป็นวิธีที่ยืดหยุ่นและทรงพลังที่สุด
Ingress คือ Kubernetes Resource ที่กำหนดกฎ (Rules) สำหรับ Route HTTP/HTTPS Traffic จากภายนอกไปยัง Service ภายใน Cluster โดยอาศัย Ingress Controller เป็นตัวทำงานจริง เปรียบเสมือน Reverse Proxy ที่อ่าน Ingress Rules แล้วตั้งค่า Routing ให้อัตโนมัติ
Ingress vs LoadBalancer vs NodePort
| วิธี | ข้อดี | ข้อเสีย | ใช้เมื่อ |
|---|---|---|---|
| NodePort | ง่าย ไม่ต้องตั้งค่าเพิ่ม | Port จำกัด (30000-32767) ไม่มี TLS/Path routing | Dev/Test เท่านั้น |
| LoadBalancer | ได้ External IP อัตโนมัติ | 1 Service = 1 LB (แพงมาก!) ไม่มี Path routing | Service เดียวที่ต้องการ External IP |
| Ingress | หลาย Service ใช้ LB เดียว, TLS, Path/Host routing, Rate limiting | ต้องติดตั้ง Ingress Controller | Production ทุกกรณี (แนะนำ) |
# ตัวอย่าง Ingress Resource
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-app-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx # ระบุ Ingress Controller ที่จะใช้
tls:
- hosts:
- myapp.example.com
secretName: myapp-tls
rules:
- host: myapp.example.com
http:
paths:
- path: /api
pathType: Prefix
backend:
service:
name: api-service
port:
number: 8080
- path: /
pathType: Prefix
backend:
service:
name: frontend-service
port:
number: 80
IngressClass — เลือก Controller ที่จะใช้
ตั้งแต่ Kubernetes 1.18+ มี IngressClass เพื่อระบุว่า Ingress Resource จะใช้ Controller ตัวไหน ทำให้สามารถรัน Controller หลายตัวพร้อมกันใน Cluster เดียวได้
# IngressClass สำหรับ Nginx
apiVersion: networking.k8s.io/v1
kind: IngressClass
metadata:
name: nginx
annotations:
ingressclass.kubernetes.io/is-default-class: "true" # Default Ingress Controller
spec:
controller: k8s.io/ingress-nginx
# IngressClass สำหรับ Traefik
apiVersion: networking.k8s.io/v1
kind: IngressClass
metadata:
name: traefik
spec:
controller: traefik.io/ingress-controller
# ใช้ใน Ingress Resource:
spec:
ingressClassName: nginx # หรือ traefik
# ตรวจสอบ IngressClass ที่มี:
kubectl get ingressclass
# NAME CONTROLLER PARAMETERS AGE
# nginx k8s.io/ingress-nginx <none> 30d
# traefik traefik.io/ingress-controller <none> 15d
Nginx Ingress Controller — ยอดนิยมอันดับ 1
Nginx Ingress Controller (โดย Kubernetes community) เป็น Controller ที่ได้รับความนิยมสูงสุด ใช้ Nginx เป็น Reverse Proxy ภายใน มีฟีเจอร์ครบถ้วน รองรับ Traffic สูง และมี Community ขนาดใหญ่
ติดตั้ง Nginx Ingress Controller
# วิธีที่ 1: Helm (แนะนำ)
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update
helm install ingress-nginx ingress-nginx/ingress-nginx --namespace ingress-nginx --create-namespace --set controller.replicaCount=2 --set controller.resources.requests.cpu=100m --set controller.resources.requests.memory=128Mi --set controller.metrics.enabled=true --set controller.metrics.serviceMonitor.enabled=true
# วิธีที่ 2: kubectl apply
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.10.0/deploy/static/provider/cloud/deploy.yaml
# ตรวจสอบ:
kubectl get pods -n ingress-nginx
kubectl get svc -n ingress-nginx
Nginx Annotations ที่ใช้บ่อย
# Rate Limiting
nginx.ingress.kubernetes.io/limit-rps: "10" # จำกัด 10 req/sec ต่อ IP
nginx.ingress.kubernetes.io/limit-connections: "5" # จำกัด 5 connections พร้อมกัน
# TLS/SSL
nginx.ingress.kubernetes.io/ssl-redirect: "true" # Force HTTPS
nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
# Proxy Settings
nginx.ingress.kubernetes.io/proxy-body-size: "50m" # Upload size limit
nginx.ingress.kubernetes.io/proxy-read-timeout: "300" # Read timeout (seconds)
nginx.ingress.kubernetes.io/proxy-send-timeout: "300" # Send timeout
# CORS
nginx.ingress.kubernetes.io/enable-cors: "true"
nginx.ingress.kubernetes.io/cors-allow-origin: "https://myapp.com"
nginx.ingress.kubernetes.io/cors-allow-methods: "GET, POST, PUT, DELETE"
# WebSocket Support
nginx.ingress.kubernetes.io/proxy-read-timeout: "3600"
nginx.ingress.kubernetes.io/proxy-send-timeout: "3600"
nginx.ingress.kubernetes.io/websocket-services: "ws-service"
# Authentication (Basic Auth)
nginx.ingress.kubernetes.io/auth-type: basic
nginx.ingress.kubernetes.io/auth-secret: basic-auth-secret
nginx.ingress.kubernetes.io/auth-realm: "Authentication Required"
# Rewrite
nginx.ingress.kubernetes.io/rewrite-target: /$2
nginx.ingress.kubernetes.io/use-regex: "true"
# Custom Headers
nginx.ingress.kubernetes.io/configuration-snippet: |
more_set_headers "X-Frame-Options: DENY";
more_set_headers "X-Content-Type-Options: nosniff";
Nginx Ingress ตัวอย่างเต็ม
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: production-ingress
namespace: production
annotations:
nginx.ingress.kubernetes.io/ssl-redirect: "true"
nginx.ingress.kubernetes.io/limit-rps: "20"
nginx.ingress.kubernetes.io/proxy-body-size: "10m"
nginx.ingress.kubernetes.io/enable-cors: "true"
cert-manager.io/cluster-issuer: letsencrypt-prod
spec:
ingressClassName: nginx
tls:
- hosts:
- api.example.com
- www.example.com
secretName: example-tls
rules:
- host: api.example.com
http:
paths:
- path: /v1
pathType: Prefix
backend:
service:
name: api-v1
port:
number: 8080
- path: /v2
pathType: Prefix
backend:
service:
name: api-v2
port:
number: 8080
- host: www.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: frontend
port:
number: 80
Traefik — Cloud-Native Reverse Proxy
Traefik ถูกออกแบบมาสำหรับ Cloud-Native ตั้งแต่แรก มี Dashboard ในตัว รองรับ Auto-discovery ของ Services และตั้งค่าผ่าน Annotations ได้ง่าย เป็น Default Ingress Controller ใน K3s
ติดตั้ง Traefik
# Helm Install
helm repo add traefik https://traefik.github.io/charts
helm repo update
helm install traefik traefik/traefik --namespace traefik --create-namespace --set deployment.replicas=2 --set dashboard.enabled=true --set dashboard.ingressRoute.enabled=true --set ports.websecure.tls.enabled=true --set providers.kubernetesIngress.enabled=true --set providers.kubernetesCRD.enabled=true
# ตรวจสอบ:
kubectl get pods -n traefik
kubectl port-forward -n traefik svc/traefik-dashboard 9000:9000
Traefik IngressRoute (CRD)
# Traefik มี CRD เป็นของตัวเอง (IngressRoute)
# ยืดหยุ่นกว่า Standard Ingress Resource
apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
name: my-app
namespace: production
spec:
entryPoints:
- websecure
routes:
- match: Host(`api.example.com`) && PathPrefix(`/v1`)
kind: Rule
services:
- name: api-v1
port: 8080
middlewares:
- name: rate-limit
- name: auth-headers
- match: Host(`api.example.com`) && PathPrefix(`/v2`)
kind: Rule
services:
- name: api-v2
port: 8080
tls:
certResolver: letsencrypt
---
# Middleware: Rate Limiting
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: rate-limit
spec:
rateLimit:
average: 100
burst: 50
period: 1m
---
# Middleware: Custom Headers
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: auth-headers
spec:
headers:
customRequestHeaders:
X-Forwarded-Proto: https
customResponseHeaders:
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
Traefik Auto TLS (Let’s Encrypt)
# Traefik สามารถขอ TLS Certificate จาก Let's Encrypt อัตโนมัติ
# ไม่ต้องใช้ cert-manager แยก!
# Helm values สำหรับ ACME:
# values.yaml
additionalArguments:
- "[email protected]"
- "--certificatesresolvers.letsencrypt.acme.storage=/data/acme.json"
- "--certificatesresolvers.letsencrypt.acme.httpchallenge.entrypoint=web"
# IngressRoute ใช้ certResolver:
spec:
tls:
certResolver: letsencrypt
# → Traefik ขอ Cert อัตโนมัติ + Auto-renew!
HAProxy Ingress Controller
HAProxy Ingress Controller ใช้ HAProxy ซึ่งเป็น Load Balancer ที่ทรงพลังและมีประสิทธิภาพสูงมาก เหมาะกับงานที่ต้องการ Performance สูงสุด และ Connection handling ที่ดีเยี่ยม
ติดตั้ง HAProxy Ingress
# Helm Install
helm repo add haproxytech https://haproxytech.github.io/helm-charts
helm repo update
helm install haproxy-ingress haproxytech/kubernetes-ingress --namespace haproxy-ingress --create-namespace --set controller.replicaCount=2 --set controller.resources.requests.cpu=200m --set controller.resources.requests.memory=256Mi --set controller.service.type=LoadBalancer
# ตรวจสอบ:
kubectl get pods -n haproxy-ingress
HAProxy Annotations
# Rate Limiting
haproxy.org/rate-limit-requests: "50"
haproxy.org/rate-limit-period: "1m"
# Timeouts
haproxy.org/timeout-connect: "5s"
haproxy.org/timeout-server: "300s"
haproxy.org/timeout-client: "300s"
haproxy.org/timeout-tunnel: "3600s" # WebSocket
# SSL
haproxy.org/ssl-redirect: "true"
haproxy.org/ssl-redirect-code: "301"
# Load Balancing Algorithm
haproxy.org/load-balance: "leastconn" # roundrobin, leastconn, source
# Health Check
haproxy.org/check: "true"
haproxy.org/check-http: "/healthz"
haproxy.org/check-interval: "5s"
# CORS
haproxy.org/cors-enable: "true"
haproxy.org/cors-allow-origin: "*"
haproxy.org/cors-allow-methods: "GET, POST, PUT, DELETE"
# IP Whitelist
haproxy.org/whitelist: "10.0.0.0/8, 192.168.0.0/16"
# Custom Config
haproxy.org/config-snippet: |
http-response set-header X-Frame-Options DENY
http-response set-header X-Content-Type-Options nosniff
AWS ALB Ingress Controller
AWS Load Balancer Controller (เดิมชื่อ ALB Ingress Controller) สร้าง Application Load Balancer (ALB) ของ AWS โดยตรง เหมาะกับ EKS (Elastic Kubernetes Service)
# ติดตั้ง AWS Load Balancer Controller
helm repo add eks https://aws.github.io/eks-charts
helm repo update
helm install aws-load-balancer-controller eks/aws-load-balancer-controller --namespace kube-system --set clusterName=my-cluster --set serviceAccount.create=true --set serviceAccount.annotations."eks\.amazonaws\.com/role-arn"=arn:aws:iam::123456:role/AWSLoadBalancerControllerRole
# Ingress ที่ใช้ ALB:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: alb-ingress
annotations:
kubernetes.io/ingress.class: alb
alb.ingress.kubernetes.io/scheme: internet-facing
alb.ingress.kubernetes.io/target-type: ip
alb.ingress.kubernetes.io/certificate-arn: arn:aws:acm:ap-southeast-1:123456:certificate/xxx
alb.ingress.kubernetes.io/listen-ports: '[{"HTTPS":443}]'
alb.ingress.kubernetes.io/healthcheck-path: /healthz
alb.ingress.kubernetes.io/wafv2-acl-arn: arn:aws:wafv2:...
spec:
rules:
- host: api.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: api-service
port:
number: 8080
Istio Gateway — Service Mesh Ingress
Istio Gateway เป็นส่วนหนึ่งของ Istio Service Mesh ที่ทำหน้าที่เป็น Ingress ด้วย รองรับ mTLS, Traffic management, Observability และ Security policies ขั้นสูง
# Istio Gateway
apiVersion: networking.istio.io/v1beta1
kind: Gateway
metadata:
name: my-gateway
spec:
selector:
istio: ingressgateway
servers:
- port:
number: 443
name: https
protocol: HTTPS
tls:
mode: SIMPLE
credentialName: example-tls
hosts:
- api.example.com
---
# VirtualService (เหมือน Ingress Rules)
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: api-routes
spec:
hosts:
- api.example.com
gateways:
- my-gateway
http:
- match:
- uri:
prefix: /v1
route:
- destination:
host: api-v1
port:
number: 8080
timeout: 30s
retries:
attempts: 3
perTryTimeout: 10s
- match:
- uri:
prefix: /v2
route:
- destination:
host: api-v2
port:
number: 8080
weight: 90
- destination:
host: api-v2-canary
port:
number: 8080
weight: 10 # Canary deployment 10%
ตารางเปรียบเทียบ Ingress Controllers ทั้งหมด
| Feature | Nginx | Traefik | HAProxy | AWS ALB | Istio |
|---|---|---|---|---|---|
| TLS Termination | Yes | Yes + Auto ACME | Yes | Yes (ACM) | Yes + mTLS |
| Path-based Routing | Yes | Yes | Yes | Yes | Yes |
| Host-based Routing | Yes | Yes | Yes | Yes | Yes |
| Rate Limiting | Annotation | Middleware | Annotation | WAF | Policy |
| Authentication | Basic/External Auth | BasicAuth/ForwardAuth | Basic/OAuth | Cognito/OIDC | JWT/mTLS |
| WebSocket | Yes | Yes | Yes | Yes | Yes |
| gRPC | Yes | Yes | Yes | Yes (gRPC target) | Yes |
| Dashboard | No (ใช้ Prometheus) | Yes (built-in) | Yes (stats page) | AWS Console | Kiali |
| Auto TLS (ACME) | ต้องใช้ cert-manager | Built-in | ต้องใช้ cert-manager | ACM (ฟรี) | cert-manager |
| Canary Deploy | Annotation (weight) | TraefikService | ไม่มี built-in | ไม่มี built-in | VirtualService weight |
| Config Reload | Reload Nginx | Hot reload | Hot reload | AWS API | Hot reload |
| Performance | สูงมาก | สูง | สูงมาก | AWS managed | ปานกลาง (sidecar) |
| Community | ใหญ่ที่สุด | ใหญ่ | ปานกลาง | AWS only | ใหญ่ |
Gateway API — อนาคตของ Ingress
Gateway API เป็น Standard ใหม่ที่จะมาแทน Ingress Resource ในอนาคต ออกแบบมาให้ยืดหยุ่นกว่า Ingress และรองรับ Use case ที่ซับซ้อนกว่า เช่น TCP/UDP routing, Traffic splitting, Header-based routing
# Gateway API Resources:
# 1. GatewayClass — เหมือน IngressClass (ระบุ Controller)
# 2. Gateway — เหมือน Ingress (ระบุ Listener/Port/TLS)
# 3. HTTPRoute — กฎ Routing (ยืดหยุ่นกว่า Ingress rules)
# GatewayClass
apiVersion: gateway.networking.k8s.io/v1
kind: GatewayClass
metadata:
name: nginx
spec:
controllerName: k8s.io/ingress-nginx
# Gateway
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: production-gateway
spec:
gatewayClassName: nginx
listeners:
- name: https
protocol: HTTPS
port: 443
tls:
mode: Terminate
certificateRefs:
- name: example-tls
allowedRoutes:
namespaces:
from: All
# HTTPRoute
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: api-route
spec:
parentRefs:
- name: production-gateway
hostnames:
- api.example.com
rules:
- matches:
- path:
type: PathPrefix
value: /v1
- headers:
- name: X-Version
value: v1
backendRefs:
- name: api-v1
port: 8080
weight: 90
- name: api-v1-canary
port: 8080
weight: 10
filters:
- type: RequestHeaderModifier
requestHeaderModifier:
add:
- name: X-Gateway
value: production
Gateway API vs Ingress
| Feature | Ingress | Gateway API |
|---|---|---|
| HTTP Routing | Host + Path เท่านั้น | Host + Path + Header + Query + Method |
| Traffic Splitting | Annotation (ขึ้นอยู่กับ Controller) | Built-in (weight) |
| TCP/UDP | ไม่รองรับ | TCPRoute, UDPRoute |
| gRPC | Annotation | GRPCRoute |
| Header Manipulation | Annotation | Built-in filter |
| Role-based | ไม่มี | Infra team → Gateway, Dev team → Route |
| Maturity | Stable (GA) | v1 GA (2023+) |
| Controller Support | ทุกตัว | Nginx, Traefik, Istio, Contour, HAProxy |
Performance Comparison
จากการ Benchmark ด้วย wrk และ vegeta บน Kubernetes 1.30 Cluster ขนาด 3 nodes (8 vCPU, 16GB RAM) ผลลัพธ์โดยประมาณ:
| Metric | Nginx | Traefik | HAProxy |
|---|---|---|---|
| Requests/sec (RPS) | ~35,000 | ~28,000 | ~40,000 |
| Latency p50 | 1.2ms | 1.8ms | 1.0ms |
| Latency p99 | 8ms | 12ms | 6ms |
| Memory Usage | ~200MB | ~150MB | ~180MB |
| CPU Usage (at 10K RPS) | ~300m | ~400m | ~250m |
| TLS Overhead | ~15% | ~20% | ~12% |
| Config Reload Time | ~2s (reload) | ~0s (hot) | ~0s (hot) |
สรุป Performance: HAProxy เร็วที่สุด > Nginx > Traefik แต่ Traefik ใช้ Memory น้อยที่สุดและ Config reload ทันทีไม่มี downtime
เลือก Ingress Controller ตาม Use Case
| Use Case | แนะนำ | เหตุผล |
|---|---|---|
| Production ทั่วไป | Nginx | Community ใหญ่ ตัวอย่างเยอะ Stable ที่สุด |
| K3s / Edge / IoT | Traefik | Default ใน K3s, Dashboard, Auto TLS, lightweight |
| High Performance / Banking | HAProxy | RPS สูงสุด Latency ต่ำสุด Connection handling ดีเยี่ยม |
| AWS EKS | AWS ALB Controller | Integrate กับ AWS services (ACM, WAF, Cognito) |
| Service Mesh | Istio Gateway | mTLS, Traffic management, Observability ครบ |
| Multi-cloud / Startup | Traefik | Cloud-agnostic, Dashboard, Auto TLS, ง่ายในการ Setup |
| Microservices ขนาดใหญ่ | Istio หรือ Nginx | Istio ถ้าต้องการ Service Mesh, Nginx ถ้าแค่ Ingress |
Monitoring Ingress Controllers
# Nginx Ingress — Prometheus Metrics
# Enable metrics:
helm upgrade ingress-nginx ingress-nginx/ingress-nginx --set controller.metrics.enabled=true --set controller.metrics.serviceMonitor.enabled=true
# Key Prometheus Metrics:
# nginx_ingress_controller_requests — จำนวน requests
# nginx_ingress_controller_request_duration_seconds — latency
# nginx_ingress_controller_response_size — response size
# nginx_ingress_controller_ssl_expire_time_seconds — SSL cert expiry
# Grafana Dashboard:
# Import Dashboard ID: 14314 (Nginx Ingress Controller)
# Traefik — Built-in Dashboard
# Port-forward:
kubectl port-forward -n traefik svc/traefik-dashboard 9000:9000
# หรือตั้ง IngressRoute ไปที่ Dashboard
# HAProxy — Stats Page
# Enable via ConfigMap:
kubectl create configmap haproxy-config --from-literal=stats-enabled=true --from-literal=stats-port=1024
Best Practices สำหรับ Ingress Controller
| ข้อ | Best Practice | เหตุผล |
|---|---|---|
| 1 | รัน Controller อย่างน้อย 2 replicas | High Availability ถ้า pod ตัวหนึ่งตาย ยังมีอีกตัว |
| 2 | ตั้ง Resource limits (CPU/Memory) | ป้องกัน Controller กิน Resource ทั้ง Node |
| 3 | ใช้ cert-manager (หรือ Traefik ACME) สำหรับ TLS | Auto-renew ไม่ต้อง Manual จัดการ Cert |
| 4 | เปิด Rate Limiting | ป้องกัน DDoS และ Abuse |
| 5 | เปิด Metrics + Grafana Dashboard | Monitor Traffic, Latency, Error rate |
| 6 | ตั้ง Timeout ให้เหมาะสม | ไม่ให้ Connection ค้างนานเกินไป |
| 7 | ใช้ IngressClass เสมอ | ระบุชัดเจนว่าใช้ Controller ตัวไหน |
| 8 | เตรียม Migrate ไป Gateway API | Standard ใหม่ที่จะมาแทน Ingress ในอนาคต |
| 9 | ทดสอบ Load ก่อน Production | ใช้ wrk หรือ vegeta ทดสอบว่า Controller รับ Traffic ได้เท่าไร |
| 10 | Update Controller สม่ำเสมอ | Security patches และ Bug fixes |
สรุป — เลือก Ingress Controller ที่ใช่
Ingress Controller เป็นหัวใจของ Kubernetes Networking ที่ทำให้ Traffic จากภายนอกเข้าถึง Service ได้ การเลือก Controller ที่เหมาะสมขึ้นอยู่กับ Use case: Nginx สำหรับ Production ทั่วไป (Community ใหญ่ Stable ที่สุด), Traefik สำหรับ K3s/Edge/Startup (Dashboard, Auto TLS, ง่าย), HAProxy สำหรับ High Performance (RPS สูง Latency ต่ำ), AWS ALB สำหรับ EKS และ Istio สำหรับ Service Mesh
อนาคตของ Ingress คือ Gateway API ซึ่ง GA แล้วและรองรับ Use case ที่ซับซ้อนกว่า Ingress มาก ควรเริ่มศึกษาและเตรียม Migrate ไปเมื่อ Controller ที่ใช้อยู่รองรับ Gateway API อย่างเต็มที่