9.3. Networking - CoreDNS Internals, Ingress and Gateway API
Mục lục
- 1. Triển khai DNS trong Kubernetes Cluster
- 2. Lab CoreDNS
- 3. Ingress trong Kubernetes
- 4. Ingress Annotations và Rewrite-target
- 5. Lab Triển khai Ingress Controller
- 6. Lab Ingress Resources
- 7. Gateway API
- 8. Lab Gateway API
1. Triển khai DNS trong Kubernetes Cluster
1.1 CoreDNS thay thế kube-dns
Kubernetes triển khai DNS server tích hợp trong cluster. Trước phiên bản 1.13, Kubernetes dùng kube-dns làm DNS add-on mặc định. Từ Kubernetes version 1.13+, DNS server mặc định là CoreDNS.
1.2 CoreDNS Pod: Ai chạy DNS?
CoreDNS được triển khai như Deployment trong namespace kube-system. Để đảm bảo redundancy, thường có 2 replicas:
kubectl get pods -n kube-system | grep coredns
# Output (minh họa):
# coredns-5d8c9b8b7f-abc12 1/1 Running 0 5d
# coredns-5d8c9b8b7f-xyz34 1/1 Running 0 5d
Pod CoreDNS chạy executable CoreDNS giống như khi triển khai CoreDNS thủ công — nó không phải Kubernetes-specific binary.
1.3 Corefile Configuration
CoreDNS sử dụng file cấu hình gọi là Corefile, nằm tại /etc/coredns/Corefile trong container.
Trong Corefile có nhiều plugins được cấu hình:
- health: Health checking.
- ready: Readiness reporting.
- prometheus: Monitoring metrics.
- cache: Caching responses.
- kubernetes: Tích hợp với Kubernetes API.
1.4 Kubernetes Plugin trong CoreDNS
# Cấu hình kubernetes plugin trong Corefile
kubernetes cluster.local {
pods insecure
proxy . /etc/resolv.conf
}
Các options quan trọng:
| Option | Mô tả |
|---|---|
cluster.local | Tên domain root của cluster |
pods | Tạo records cho pods (mặc định disabled) |
pods insecure | Tạo records cho pods (legacy mode) |
proxy . /etc/resolv.conf | Forward unresolved queries ra upstream DNS |
1.5 Forwarding unresolved queries
Khi Pod cố gắng resolve một external name (ví dụ: www.google.com):
- DNS query đến CoreDNS.
- CoreDNS không tìm thấy record cho
www.google.com. - CoreDNS forward query đến nameserver trong
/etc/resolv.confcủa nó. /etc/resolv.confcủa CoreDNS pod trỏ đến upstream DNS (thường là--cluster-dnscủa kubelet).
1.6 Corefile như ConfigMap
Corefile được mount vào Pod như một ConfigMap object:
kubectl get configmap -n kube-system
# Output (minh họa):
# NAME DATA AGE
# coredns 1 5d
Điều này cho phép chỉnh sửa cấu hình DNS mà không cần restart Pods — kubelet sẽ detect thay đổi và reload.
1.7 DNS Service: kube-dns
Khi triển khai CoreDNS, nó tạo một Service để các components khác trong cluster truy cập DNS:
kubectl get svc -n kube-system
# Output (minh họa):
# NAME TYPE CLUSTER-IP PORT(S)
# kube-dns ClusterIP 10.96.0.10 53/UDP,53/TCP
Service có tên mặc định là kube-dns, với IP 10.96.0.10.
1.8 Cấu hình DNS trên Pods
DNS configurations trên Pods được thực hiện tự động bởi Kubernetes khi Pods được tạo. Kubelet chịu trách nhiệm cho việc này.
Trong kubelet config file (/var/lib/kubelet/config.yaml):
clusterDNS:
- 10.96.0.10
clusterDomain: cluster.local
Khi Pod được tạo, kubelet inject các giá trị này vào /etc/resolv.conf của container.
1.9 Resolving Pods: Không có search entry
Chỉ có search entries cho services, không có cho pods. Để reach một Pod, cần chỉ định đầy đủ FQDN:
# Pod DNS name format:
10-244-1-5.default.pod.cluster.local
💡 Hình dung: DNS cho Pods giống "số điện thoại người nổi tiếng" — phải nhớ đầy đủ, không có danh bạ tự động. DNS cho Services giống "gọi tên bạn bè" — chỉ cần tên, máy tự tìm số.
2. Lab CoreDNS
2.1 Xác định DNS Solution
kubectl get pods -n kube-system
# Output (minh họa):
# NAME READY STATUS RESTARTS AGE
# coredns-5d8c9b8b7f-abc12 1/1 Running 0 5d
# coredns-5d8c9b8b7f-xyz34 1/1 Running 0 5d
# ...
Kiểm tra xem DNS solution đang được sử dụng (CoreDNS pods).
2.2 Số lượng DNS Pods
kubectl get pods -n kube-system | grep -i dns
# Output (minh họa):
# coredns-5d8c9b8b7f-abc12 1/1 Running 0 5d
# coredns-5d8c9b8b7f-xyz34 1/1 Running 0 5d
Thường có 2 CoreDNS pods để đảm bảo redundancy.
2.3 Tên Service cho CoreDNS
kubectl get svc -n kube-system
# Output (minh họa):
# NAME TYPE CLUSTER-IP PORT(S)
# kube-dns ClusterIP 10.96.0.10 53/UDP,53/TCP
Service có tên là kube-dns.
2.4 IP của CoreDNS Server
kubectl get svc kube-dns -n kube-system
# Output (minh họa):
# NAME TYPE CLUSTER-IP PORT(S)
# kube-dns ClusterIP 10.96.0.10 53/UDP,53/TCP
IP mặc định thường là 10.96.0.10.
2.5 Vị trí Configuration File
Kiểm tra CoreDNS pod để tìm configuration file:
kubectl describe pod <coredns-pod-name> -n kube-system | grep -A5 "Args:"
# Output (minh họa):
# Args:
# -conf
# /etc/coredns/Corefile
Configuration file nằm tại: /etc/coredns/Corefile
2.6 Cách Corefile được Mount vào Pod
Trong pod description, tìm volumes mount:
kubectl describe pod <coredns-pod-name> -n kube-system | grep -A10 "Volumes:"
# Output (minh họa):
# Volumes:
# config-volume:
# Type: ConfigMap (a volume populated by a ConfigMap)
# Name: coredns
# ...
# Mounts:
# /etc/coredns from config-volume (ro)
Corefile được mount từ ConfigMap coredns.
3. Ingress trong Kubernetes
3.1 Vấn đề: Quản lý nhiều Services
Giả sử có 5 microservices: web-frontend, auth, catalog, cart, payment. Mỗi service cần expose ra ngoài. Cách tiếp cận truyền thống:
| Service | NodePort | URL |
|---|---|---|
| web-frontend | 30080 | http://ip:30080 |
| auth | 30081 | http://ip:30081 |
| catalog | 30082 | http://ip:30082 |
Vấn đề:
- Mỗi service cần NodePort/LoadBalancer riêng — tốn kém.
- Cần cấu hình DNS riêng cho mỗi LoadBalancer.
- Khó quản lý SSL certificate riêng lẻ.
- Người dùng phải nhớ nhiều ports.
3.2 Ingress: Layer 7 Load Balancer
Ingress giúp users truy cập ứng dụng qua một URL duy nhất, có thể cấu hình để route traffic đến các services khác nhau trong cluster.
💡 Hình dung: Ingress giống "lễ tân khách sạn" — khách đến một cửa chính, lễ tân hỏi "bạn cần gì?" rồi chuyển đến phòng phù hợp. Khách không cần nhớ từng số phòng.
Ingress là Layer 7 load balancer được tích hợp trong Kubernetes, cấu hình bằng native Kubernetes primitives.
3.3 Hai thành phần của Ingress
Ingress Controller
- Là deployment triển khai giải pháp Ingress.
- Các giải pháp được hỗ trợ: NGINX, Contour, HAProxy, Traefik, Istio, GCE.
- Có "intelligence" để theo dõi Ingress resources và tự động cấu hình.
Ingress Resources
- Là các rules và cấu hình được áp dụng trên Ingress controller.
- Được tạo bằng Kubernetes definition files (kind: Ingress).
- Có thể tạo, chỉnh sửa, xóa như resource khác.
3.4 Triển khai NGINX Ingress Controller
Deployment Configuration
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-ingress-controller
spec:
replicas: 1
selector:
matchLabels:
app: nginx-ingress
template:
metadata:
labels:
app: nginx-ingress
spec:
containers:
- name: nginx-ingress-controller
image: nginx/nginx-ingress-controller
args:
- /nginx-ingress-controller
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
ports:
- name: http
containerPort: 80
- name: https
containerPort: 443
💡 Lưu ý: NGINX program được lưu tại
/nginx-ingress-controllertrong image — đây là entry point mặc định.
Service (NodePort)
apiVersion: v1
kind: Service
metadata:
name: ingress
spec:
type: NodePort
selector:
app: nginx-ingress
ports:
- name: http
port: 80
targetPort: 80
nodePort: 30080
- name: https
port: 443
targetPort: 443
nodePort: 30443
3.5 Ingress Resource: Default Backend
Default backend xử lý traffic không khớp rules nào:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-wear
spec:
defaultBackend:
service:
name: wear-service
port:
number: 80
3.6 Ingress Rules: Route theo URL Path
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-wear-watch
spec:
rules:
- http:
paths:
- path: /wear
pathType: Prefix
backend:
service:
name: wear-service
port:
number: 80
- path: /watch
pathType: Prefix
backend:
service:
name: video-service
port:
number: 80
3.7 Ingress Rules: Route theo Hostname
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-mycompany
spec:
rules:
- host: wear.myonlinestore.com
http:
paths:
- backend:
service:
name: wear-service
port:
number: 80
- host: watch.myonlinestore.com
http:
paths:
- backend:
service:
name: video-service
port:
number: 80
3.8 Commands với Ingress
# Tạo Ingress
kubectl create -f ingress-wear.yaml
# Xem Ingress
kubectl get ingress
# Output (minh họa):
# NAME CLASS HOSTS ADDRESS PORTS
# ingress-wear-watch nginx wear.myonlinestore.com 10.0.0.5 80
# Mô tả chi tiết Ingress
kubectl describe ingress ingress-wear-watch
# Chỉnh sửa Ingress
kubectl edit ingress ingress-wear-watch
Cột CLASS trong output trên lấy giá trị từ field spec.ingressClassName của Ingress resource — field này trỏ tới tên một object IngressClass (apiVersion: networking.k8s.io/v1, kind: IngressClass) khai báo Ingress controller nào sẽ xử lý resource đó. Đây là cách làm hiện hành, thay thế annotation kubernetes.io/ingress.class đã deprecated. Nếu cluster chỉ có một Ingress controller, có thể đánh dấu IngressClass đó làm mặc định bằng annotation ingressclass.kubernetes.io/is-default-class: "true" ngay trên object IngressClass — khi đó các Ingress không cần khai báo ingressClassName vẫn tự động dùng đúng controller.
4. Ingress Annotations và Rewrite-target
4.1 Vấn đề: URL Path không khớp
Khi ứng dụng backend được truy cập tại root path (/), nhưng Ingress route yêu cầu path /watch:
http://<ingress-service>:<port>/watch
↓ (Ingress forwards)
http://<watch-service>:<port>/watch ← Backend không có /watch → 404!
Backend chỉ hiểu /, không hiểu /watch.
4.2 Giải pháp: Rewrite-target
Sử dụng annotation nginx.ingress.kubernetes.io/rewrite-target để rewrite URL:
replace("/watch", "/")
http://<ingress-service>:<port>/watch
↓ (Ingress rewrites)
http://<watch-service>:<port>/ ← OK! Backend nhận /
4.3 Ví dụ đơn giản
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: test-ingress
namespace: critical-space
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- http:
paths:
- path: /pay
pathType: Prefix
backend:
service:
name: pay-service
port:
number: 8282
Traffic /pay được rewrite thành / trước khi gửi đến pay-service.
4.4 Ví dụ với Regex (Path có variable)
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: rewrite
namespace: default
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
rules:
- host: rewrite.bar.com
http:
paths:
- path: /something(/|$)(.*)
pathType: ImplementationSpecific
backend:
service:
name: http-svc
port:
number: 80
Giải thích regex:
- Pattern
(/|$)(.*)capture:- Group
$1:(/|$)— dấu/hoặc kết thúc string. - Group
$2:(.*)— phần sau dấu/.
- Group
- Rewrite với
/$2giữ lại phần sausomething.
Ví dụ:
/something/foo→/foo./something/bar/baz→/bar/baz./something→/.
4.5 Annotations phổ biến khác
| Annotation | Mô tả |
|---|---|
nginx.ingress.kubernetes.io/ssl-redirect: "false" | Tắt chuyển hướng HTTP sang HTTPS |
nginx.ingress.kubernetes.io/force-ssl-redirect: "true" | Bắt buộc HTTPS |
nginx.ingress.kubernetes.io/proxy-body-size: "10m" | Giới hạn body size |
nginx.ingress.kubernetes.io/proxy-connect-timeout | Timeout cho upstream connection |
5. Lab Triển khai Ingress Controller
5.1 Chuẩn bị môi trường
Tạo namespace cho Ingress:
kubectl create namespace ingress-space
# Output:
# namespace/ingress-space created
5.2 Tạo ConfigMap
kubectl create configmap nginx-configuration --namespace=ingress-space
# Output:
# configmap/nginx-configuration created
5.3 Triển khai Ingress Controller
kubectl apply -f ingress-controller.yaml --namespace=ingress-space
# Output (minh họa):
# deployment.apps/nginx-ingress-controller created
Kiểm tra trạng thái:
kubectl get pods -n ingress-space
# Output (minh họa):
# NAME READY STATUS
# nginx-ingress-controller-5d9fb7c9-abc12 1/1 Running
5.4 Tạo Ingress Service
kubectl expose deployment nginx-ingress-controller \
--name=ingress \
--namespace=ingress-space \
--type=NodePort \
--port=80 \
--target-port=80
# Output:
# service/ingress exposed
5.5 Xử lý lỗi SSL Redirect
Nếu gặp lỗi "too many redirects" do SSL redirect mặc định:
kubectl edit deployment nginx-ingress-controller -n ingress-space
Thêm annotation vào Ingress resource:
metadata:
annotations:
nginx.ingress.kubernetes.io/ssl-redirect: "false"
5.6 Các lệnh hữu ích
# Xem logs của Ingress Controller
kubectl logs <ingress-controller-pod-name> -n ingress-space
# Xem logs của ứng dụng
kubectl logs <app-pod-name> -n app-space
# Theo dõi real-time logs
kubectl logs -f <ingress-controller-pod-name> -n ingress-space
6. Lab Ingress Resources
6.1 Xem môi trường hiện tại
kubectl get namespaces
# Output (minh họa):
# NAME STATUS
# default Active
# ingress-nginx Active
# app-space Active
kubectl get pods -A
# Output (minh họa):
# NAMESPACE NAME READY
# ingress-nginx nginx-ingress-controller-xxx 1/1
# app-space web-app-xxx 1/1
6.2 Kiểm tra Ingress Controller
kubectl get pods -n ingress-nginx
# Output (minh họa):
# NAME READY
# nginx-ingress-controller-5d9fb7c9-xyz34 1/1
6.3 Xem Ingress Resources
kubectl get ingress -A
# Output (minh họa):
# NAMESPACE NAME CLASS HOSTS ADDRESS
# app-space app-ingress nginx app.example.com 10.0.0.5
Xem chi tiết:
kubectl describe ingress <ingress-name> -n <namespace>
# Output (minh họa):
# Name: app-ingress
# Namespace: app-space
# Address: 10.0.0.5
# Ingress Class: nginx
# Rules:
# Host Path Backends
# ---- ---- --------
# app.example.com
# /api api-service:80 (10.244.1.5:80)
# / web-service:80 (10.244.1.10:80)
6.4 Thêm Path mới
Chỉnh sửa Ingress resource:
kubectl edit ingress <ingress-name> -n <namespace>
Thêm path mới:
spec:
rules:
- host: app.example.com
http:
paths:
- path: /food
pathType: Prefix
backend:
service:
name: food-service
port:
number: 8080
6.5 Ingress cho Namespace khác
Khi ứng dụng ở namespace khác, tạo Ingress resource trong namespace đó:
kubectl create ingress <ingress-name> \
--namespace=<namespace> \
--rule="<path>=<service>:<port>"
# Ví dụ:
kubectl create ingress food-ingress \
--namespace=app-space \
--rule="food=food-service:8080"
7. Gateway API
7.1 Hạn chế của Ingress
Vấn đề Multi-tenancy
Ingress resource là một object duy nhất, chỉ có thể được quản lý bởi một team tại một thời điểm. Trong môi trường multi-tenant, điều này gây khó khăn khi các teams khác nhau cần truy cập cùng infrastructure.
💡 Hình dung: Ingress giống "một lễ tân cho toàn bộ khách sạn" — tất cả đều phải qua một người, không thể phân quyền cho từng tầng.
Hạn chế về Routes
Ingress chỉ hỗ trợ HTTP-based rules. Các tính năng khác cần annotations controller-specific:
| Tính năng | Ingress | Gateway API |
|---|---|---|
| TCP/UDP routing | Không | Có |
| Traffic splitting | Khó | Dễ |
| Header manipulation | Annotation | Native |
| Authentication | Annotation | Native |
| Rate limiting | Annotation | Native |
Annotations Controller-Specific
Cấu hình rất khác nhau giữa các controllers:
# NGINX
annotations:
nginx.ingress.kubernetes.io/ssl-redirect: "true"
# Traefik
annotations:
traefik.ingress.kubernetes.io/ssl-permanent: "true"
7.2 Gateway API là gì?
Gateway API là official Kubernetes project tập trung vào Layer 4 và Layer 7 routing. Nó đại diện cho thế hệ tiếp theo của Kubernetes Ingress, load balancing, và service mesh APIs — service mesh là lớp hạ tầng quản lý giao tiếp giữa các service trong cluster (routing, mã hoá traffic, retry, observability...) thường triển khai dưới dạng sidecar proxy đi kèm mỗi Pod. Cũng giống Ingress, Gateway API không được đóng gói sẵn trong Kubernetes core — nó được cài đặt riêng dưới dạng bộ CRD (Custom Resource Definition) cộng với một controller triển khai các CRD đó (NGINX Gateway Fabric, Istio, Envoy Gateway...).
7.3 Ba Personas trong Gateway API
Gateway API phân chia trách nhiệm theo ba vai trò:
Infrastructure Provider
Cấu hình GatewayClass — định nghĩa network infrastructure bên dưới (NGINX, Traefik, Envoy, etc.)
Cluster Operator
Cấu hình Gateway — instances của GatewayClass, xác định cách traffic được nhận
Application Developer
Tạo HTTPRoute, TCPRoute, GRPCRoute, etc. — xác định cách traffic được route
💡 Hình dung: Gateway API giống "phân quyền khách sạn" — Infrastructure Provider setup điện/nước (GatewayClass), Operator setup lễ tân từng tầng (Gateway), Developer quyết ai ở phòng nào (HTTPRoute).
7.4 Gateway API Resources
GatewayClass
apiVersion: gateway.networking.k8s.io/v1
kind: GatewayClass
metadata:
name: example-class
spec:
controllerName: gateway-controller-name
Gateway
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: example-gateway
spec:
gatewayClassName: example-class
listeners:
- name: http
protocol: HTTP
port: 80
allowedRoutes:
namespaces:
from: All
HTTPRoute
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: example-http-route
spec:
parentRefs:
- name: example-gateway
hostnames:
- www.example.com
rules:
- matches:
- path:
type: PathPrefix
value: /login
backendRefs:
- name: example-svc
port: 8080
7.5 Các Route Options
Gateway API hỗ trợ nhiều loại routes:
| Route Type | Layer | Use Case | Channel |
|---|---|---|---|
| HTTPRoute | L7 | HTTP/HTTPS routing | Standard (GA) |
| GRPCRoute | L7 | gRPC routing | Standard (GA) |
| TLSRoute | L4 | TLS termination passthrough | Standard (GA) |
| TCPRoute | L4 | TCP routing (database, etc.) | Standard (GA) |
| UDPRoute | L4 | UDP routing (DNS, etc.) | Standard (GA) |
Gateway API phân loại resource theo hai channel: Standard (đã ổn định, an toàn để dùng production) và Experimental (đang thử nghiệm, API có thể đổi). Cả 5 route type trên đều đã graduate lên Standard channel — TCPRoute và UDPRoute là hai cái mới nhất, chỉ vừa lên Standard.
7.6 TLS Configuration (Native)
listeners:
- name: https
protocol: HTTPS
port: 443
tls:
mode: Terminate
certificateRefs:
- kind: Secret
name: tls-secret
allowedRoutes:
namespaces:
from: All
7.7 Traffic Splitting (Native, không Annotation)
rules:
- backendRefs:
- name: v1-service
port: 80
weight: 80
- name: v2-service
port: 80
weight: 20
Traffic split được định nghĩa rõ ràng trong spec — không cần annotations.
7.8 Header Modification (Native)
rules:
- filters:
- type: RequestHeaderModifier
requestHeaderModifier:
add:
x-env: staging
replace:
x-custom: new-value
remove:
- x-to-remove
backendRefs:
- name: my-app
port: 80
7.9 Implementations hỗ trợ Gateway API
Nhiều providers đã hỗ trợ Gateway API:
- Amazon EKS (AWS Gateway API Controller).
- Azure (Application Gateway for Containers).
- GKE (Google Cloud).
- Contour.
- Envoy Gateway.
- HAProxy Kubernetes Ingress Controller.
- Istio.
- Kong.
- NGINX Gateway Fabric.
- Traefik Proxy.
8. Lab Gateway API
8.1 Cài đặt Gateway API với NGINX
# Cài đặt CRDs
kubectl kustomize "https://github.com/nginx/nginx-gateway-fabric/config/crd/gateway-api/standard?ref=v1.6.2" | kubectl apply -f -
# Output (minh họa):
# customresourcedefinition.apiextensions.k8s.io/gatewayclasses.gateway.networking.k8s.io created
# customresourcedefinition.apiextensions.k8s.io/gateways.gateway.networking.k8s.io created
# customresourcedefinition.apiextensions.k8s.io/httproutes.gateway.networking.k8s.io created
# Cài đặt experimental CRDs (nếu cần)
kubectl kustomize "https://github.com/nginx/nginx-gateway-fabric/config/crd/gateway-api/experimental?ref=v1.6.2" | kubectl apply -f -
# Cài đặt NGINX Gateway Controller bằng Helm
helm install ngf oci://ghcr.io/nginx/charts/nginx-gateway-fabric --create-namespace -n nginx-gateway
# Output (minh họa):
# NAME: ngf
# NAMESPACE: nginx-gateway
# STATUS: deployed
8.2 GatewayClass Definition
apiVersion: gateway.networking.k8s.io/v1
kind: GatewayClass
metadata:
name: nginx
spec:
controllerName: nginx.org/gateway-controller
8.3 HTTP Gateway và Listener
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: nginx-gateway
namespace: default
spec:
gatewayClassName: nginx
listeners:
- name: http
protocol: HTTP
port: 80
allowedRoutes:
namespaces:
from: All
8.4 HTTP Routing
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: basic-route
namespace: default
spec:
parentRefs:
- name: nginx-gateway
rules:
- matches:
- path:
type: PathPrefix
value: /app
backendRefs:
- name: my-app
port: 80
8.5 HTTP Redirects
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: https-redirect
spec:
parentRefs:
- name: nginx-gateway
rules:
- filters:
- type: RequestRedirect
requestRedirect:
scheme: https
8.6 Path Rewrite
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: rewrite-path
spec:
parentRefs:
- name: nginx-gateway
rules:
- matches:
- path:
type: PathPrefix
value: /old
filters:
- type: URLRewrite
urlRewrite:
path:
replacePrefixMatch: /new
backendRefs:
- name: my-app
port: 80
8.7 Header Modification
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: header-mod
spec:
parentRefs:
- name: nginx-gateway
rules:
- filters:
- type: RequestHeaderModifier
requestHeaderModifier:
add:
x-env: staging
replace:
x-custom: new-value
remove:
- x-debug
backendRefs:
- name: my-app
port: 80
8.8 Traffic Splitting
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: traffic-split
spec:
parentRefs:
- name: nginx-gateway
rules:
- backendRefs:
- name: v1-service
port: 80
weight: 80
- name: v2-service
port: 80
weight: 20
8.9 Request Mirroring
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: request-mirror
spec:
parentRefs:
- name: nginx-gateway
rules:
- filters:
- type: RequestMirror
requestMirror:
backendRef:
name: mirror-service
port: 80
backendRefs:
- name: my-app
port: 80
8.10 TLS Termination
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: nginx-gateway-tls
spec:
gatewayClassName: nginx
listeners:
- name: https
protocol: HTTPS
port: 443
tls:
mode: Terminate
certificateRefs:
- kind: Secret
name: tls-secret
allowedRoutes:
namespaces:
from: All
8.11 TCP Routing
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: tcp-gateway
spec:
gatewayClassName: nginx
listeners:
- name: tcp
protocol: TCP
port: 3306
allowedRoutes:
namespaces:
from: All
8.12 UDP Routing
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: udp-gateway
spec:
gatewayClassName: nginx
listeners:
- name: udp
protocol: UDP
port: 53
allowedRoutes:
namespaces:
from: All
8.13 gRPC Routing
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: grpc-route
spec:
parentRefs:
- name: nginx-gateway
rules:
- matches:
- method:
service: my.grpc.Service
method: GetData
backendRefs:
- name: grpc-service
port: 50051
Nguồn tham khảo
Nguồn gốc: Khóa "Certified Kubernetes Administrator (CKA)" — phần "Networking" (DNS trong Cluster, CoreDNS, Ingress, Gateway API), nền tảng KodeKloud. Giảng viên: Mumshad Mannambeth.
Repo ghi chú, link tài liệu, và đáp án các practice question của toàn bộ khóa học: kodekloudhub/certified-kubernetes-administrator-course.
Fact-check:
- CoreDNS trở thành DNS add-on mặc định của kubeadm từ Kubernetes v1.13 — 2026-09-23, Kubernetes 1.13 Release Announcement.
- API
extensions/v1beta1vànetworking.k8s.io/v1beta1của Ingress/IngressClass không còn được serve từ Kubernetes v1.22 — API ổn định hiện hành lànetworking.k8s.io/v1(có từ v1.19) — 2026-09-23, Deprecated API Migration Guide. - Gateway API là dự án con riêng của Kubernetes (SIG Network), cài đặt qua bộ CRD + controller riêng — không đóng gói sẵn trong Kubernetes core. Bản mới nhất là v1.6.0 (30/06/2026); GatewayClass/Gateway/HTTPRoute đã GA từ v1.0 (2023), GRPCRoute GA từ v1.1, TLSRoute lên Standard từ v1.5 (02/2026), TCPRoute và UDPRoute lên Standard từ v1.6 (06/2026) — 2026-09-23, Gateway API v1.6: TCPRoute and UDPRoute Graduate to Standard.