Các ứng dụng hiện đại ngày càng được phân phối rộng rãi, trải rộng trên nhiều dịch vụ, khu vực và thậm chí cả nhà cung cấp đám mây. Các khái niệm mạng nâng cao giúp bạn quản lý sự phức tạp này trong khi vẫn duy trì tính bảo mật, khả năng quan sát và hiệu suất.
Điều kiện tiên quyết
- Hiểu biết sâu sắc về các khái niệm mạng và đám mây container
- Có kinh nghiệm với Kubernetes hoặc các nền tảng điều phối tương tự
- Làm quen với các mẫu kiến trúc microservice
Kiến trúc lưới dịch vụ
Lưới dịch vụ cung cấp lớp cơ sở hạ tầng chuyên dụng để liên lạc giữa các dịch vụ, xử lý bảo mật, khả năng quan sát và quản lý lưu lượng mà không thay đổi mã ứng dụng.
Service Mesh Architecture (Istio)
Control Plane Data Plane
┌─────────────────────────────────┐ ┌────────────────────────────────┐
│ │ │ │
│ ┌─────────────────────────┐ │ │ ┌─────────────────────┐ │
│ │ Pilot │ │ │ │ Service A │ │
│ │ (Traffic Management) │────┼─────┼────┤ ┌─────────────────┐│ │
│ └─────────────────────────┘ │ │ │ │ App Container ││ │
│ │ │ │ └─────────────────┘│ │
│ ┌─────────────────────────┐ │ │ │ ┌─────────────────┐│ │
│ │ Citadel │ │ │ │ │ Envoy Proxy ││ │
│ │ (Security/mTLS) │────┼─────┼────┤ │ (Sidecar) ││ │
│ └─────────────────────────┘ │ │ │ └─────────────────┘│ │
│ │ │ └─────────────────────┘ │
│ ┌─────────────────────────┐ │ │ │ │
│ │ Galley │ │ │ │ mTLS │
│ │ (Configuration API) │ │ │ ▼ │
│ └─────────────────────────┘ │ │ ┌─────────────────────┐ │
│ │ │ │ Service B │ │
│ ┌─────────────────────────┐ │ │ │ ┌─────────────────┐│ │
│ │ Mixer │ │ │ │ │ App Container ││ │
│ │ (Policy & Telemetry) │◄───┼─────┼────┤ └─────────────────┘│ │
│ └─────────────────────────┘ │ │ │ ┌─────────────────┐│ │
│ │ │ │ │ Envoy Proxy ││ │
└─────────────────────────────────┘ │ │ │ (Sidecar) ││ │
│ │ └─────────────────┘│ │
┌─────────────────────────────────┐ │ └─────────────────────┘ │
│ Observability │ │ │ │
│ │ │ │ mTLS │
│ ┌─────────────────────────┐ │ │ ▼ │
│ │ Grafana │ │ │ ┌─────────────────────┐ │
│ │ (Dashboards) │ │ │ │ Service C │ │
│ └─────────────────────────┘ │ │ │ ┌─────────────────┐│ │
│ │ │ │ │ App Container ││ │
│ ┌─────────────────────────┐ │ │ │ └─────────────────┘│ │
│ │ Jaeger │ │ │ │ ┌─────────────────┐│ │
│ │ (Distributed │◄───┼─────┼────┤ │ Envoy Proxy ││ │
│ │ Tracing) │ │ │ │ │ (Sidecar) ││ │
│ └─────────────────────────┘ │ │ │ └─────────────────┘│ │
│ │ │ └─────────────────────┘ │
│ ┌─────────────────────────┐ │ │ │
│ │ Prometheus │ │ └────────────────────────────────┘
│ │ (Metrics) │ │
│ └─────────────────────────┘ │ Benefits:
└─────────────────────────────────┘ • Automatic mTLS between services
• Traffic routing and load balancing
Service Communication Flow: • Circuit breaking and retries
Service A → Envoy Proxy A → • Rich telemetry and tracing
mTLS encrypted connection → • Policy enforcement
Envoy Proxy B → Service B • Zero application code changes
Một lưới dịch vụ thường bao gồm:
Data Plane: Proxy nhẹ được triển khai cùng với mỗi dịch vụ Control Plane: Quản lý và định cấu hình proxy
Hãy cùng khám phá Istio, một trong những triển khai lưới dịch vụ phổ biến nhất:
# istio-installation.yml
apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
metadata:
name: control-plane
spec:
values:
global:
meshID: mesh1
multiCluster:
clusterName: cluster1
network: network1
components:
pilot:
k8s:
env:
- name: PILOT_TRACE_SAMPLING
value: '100'
ingressGateways:
- name: istio-ingressgateway
enabled: true
k8s:
service:
type: LoadBalancer
Cài đặt Istio:
# Download and install Istio
curl -L https://istio.io/downloadIstio | sh -
cd istio-*
export PATH=$PWD/bin:$PATH
# Install Istio on Kubernetes
istioctl install --set values.demo=true -y
# Enable automatic sidecar injection
kubectl label namespace default istio-injection=enabled
MTLS tự động
Các lưới dịch vụ có thể tự động mã hóa tất cả thông tin liên lạc giữa các dịch vụ:
# mtls-policy.yml
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
name: require-mtls
namespace: production
spec:
mtls:
mode: STRICT
---
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
name: allow-authenticated
namespace: production
spec:
rules:
- from:
- source:
principals: ['cluster.local/ns/production/sa/web-service']
to:
- operation:
methods: ['GET', 'POST']
Áp dụng các chính sách:
# Apply mTLS requirements
kubectl apply -f mtls-policy.yml
# Verify mTLS is working
kubectl exec -it web-pod -- curl api-service:8080/health
# This should work with automatic certificate management
# Check certificate details
istioctl proxy-config secret web-pod
Quản lý giao thông
Kiểm soát cách lưu lượng truy cập giữa các dịch vụ:
# traffic-routing.yml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: user-service
spec:
http:
- match:
- headers:
canary:
exact: 'true'
route:
- destination:
host: user-service
subset: v2
weight: 100
- route:
- destination:
host: user-service
subset: v1
weight: 90
- destination:
host: user-service
subset: v2
weight: 10
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: user-service
spec:
host: user-service
trafficPolicy:
connectionPool:
tcp:
maxConnections: 10
http:
http1MaxPendingRequests: 10
maxRequestsPerConnection: 2
circuitBreaker:
consecutiveErrors: 3
interval: 30s
baseEjectionTime: 30s
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2
Khả năng quan sát với lưới dịch vụ
Các lưới dịch vụ tự động cung cấp khả năng đo từ xa phong phú:
# Install observability addons
kubectl apply -f https://raw.githubusercontent.com/istio/istio/release-1.17/samples/addons/grafana.yaml
kubectl apply -f https://raw.githubusercontent.com/istio/istio/release-1.17/samples/addons/jaeger.yaml
kubectl apply -f https://raw.githubusercontent.com/istio/istio/release-1.17/samples/addons/kiali.yaml
kubectl apply -f https://raw.githubusercontent.com/istio/istio/release-1.17/samples/addons/prometheus.yaml
# Access dashboards
kubectl port-forward -n istio-system svc/kiali 20001:20001
kubectl port-forward -n istio-system svc/grafana 3000:3000
kubectl port-forward -n istio-system svc/jaeger 16686:16686
Tạo một số lưu lượng truy cập và khám phá các công cụ có thể quan sát:
# Generate sample traffic
for i in {1..100}; do
kubectl exec -it web-pod -- curl api-service:8080/users
sleep 0.1
done
# Check service graph in Kiali (http://localhost:20001)
# View metrics in Grafana (http://localhost:3000)
# Trace requests in Jaeger (http://localhost:16686)
Mạng Zero Trust
Zero Trust cho rằng không có vị trí mạng nào đáng tin cậy. Mọi kết nối đều yêu cầu xác minh.
Zero Trust Architecture
Traditional Perimeter Security Zero Trust Security
┌─────────────────────────────┐ ┌────────────────────────────┐
│ "Castle Wall" │ │ "Never Trust, │
│ │ │ Always Verify" │
│ ┌─────────────────────┐ │ │ │
│ │ Firewall │ │ │ ┌─────────────────────┐ │
│ │ "Hard Shell" │ │ │ │ Identity-Based │ │
│ └─────────────────────┘ │ │ │ Access Control │ │
│ │ │ │ └─────────────────────┘ │
│ ▼ │ │ │ │
│ ┌─────────────────────┐ │ │ ▼ │
│ │ Internal │ │ │ │
│ │ Network │ │ │ ┌─────┐ ──verify─► ┌─────┐ │
│ │ "Soft Inside" │ │ │ │App A│ │App B│ │
│ │ │ │ │ └─────┘ ◄─verify── └─────┘ │
│ │ Trust everything │ │ VS │ │
│ │ inside │ │ │ ┌─────┐ ──verify─► ┌─────┐ │
│ │ │ │ │ │App C│ │App D│ │
│ └─────────────────────┘ │ │ └─────┘ ◄─verify── └─────┘ │
└─────────────────────────────┘ │ │
│ Every connection verified │
Problem: Once inside, │ • Encrypt everything │
attackers can move freely │ • Authenticate every user │
│ • Authorize every request │
│ • Monitor all traffic │
└────────────────────────────┘
Zero Trust Implementation Components:
┌───────────────────────────────────────────────────────────────────┐
│ Identity Provider │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ User ID │ │ Service ID │ │ Device ID │ │
│ │ (OAuth) │ │(Service Acc)│ │ (Cert/TPM) │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
└─────────────────────┬─────────────────────────────────────────────┘
│ Issues JWT tokens
▼
┌─────────────────────────────────────────────────────────────────────┐
│ Policy Engine │
│ │
│ IF user = "engineer" AND │
│ device = "managed" AND │
│ time = "business_hours" AND │
│ location = "office_ip" AND │
│ mfa = "verified" │
│ THEN allow access to "development_api" │
│ │
└─────────────────────┬───────────────────────────────────────────────┘
│ Makes decisions
▼
┌────────────────────────────────────────────────────────────────────┐
│ Enforcement Points │
│ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ API Gateway│ │ Service Mesh│ │ Firewall │ │
│ │ (External) │ │ (Internal) │ │ (Network) │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
└────────────────────────────────────────────────────────────────────┘
Every request flow:
Request → Identity Check → Policy Evaluation → Enforcement → Allow/Deny
Thực hiện nguyên tắc Zero Trust
Identity-Based Access: Xác thực mọi kết nối
# zero-trust-policy.yml
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
name: api-access-policy
namespace: production
spec:
selector:
matchLabels:
app: api-server
rules:
- from:
- source:
principals:
- 'cluster.local/ns/production/sa/web-service'
- 'cluster.local/ns/production/sa/mobile-service'
to:
- operation:
methods: ['GET', 'POST']
paths: ['/api/v1/*']
when:
- key: source.ip
notValues: ['192.168.0.0/16'] # Block internal network access
Microsegmentation: Cô lập khối lượng công việc bằng chính sách mạng
# microsegmentation.yml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: api-microsegmentation
namespace: production
spec:
podSelector:
matchLabels:
app: api-server
policyTypes:
- Ingress
- Egress
ingress:
- from:
- podSelector:
matchLabels:
app: web-server
- podSelector:
matchLabels:
app: mobile-gateway
ports:
- protocol: TCP
port: 8080
egress:
- to:
- podSelector:
matchLabels:
app: database
ports:
- protocol: TCP
port: 5432
- to: [] # Allow DNS
ports:
- protocol: UDP
port: 53
Xác minh liên tục
Thực hiện xác minh liên tục quyền truy cập mạng:
# continuous-verification.py
import time
import jwt
import requests
from datetime import datetime, timedelta
class NetworkAccessVerifier:
def __init__(self, secret_key, token_lifetime=300):
self.secret_key = secret_key
self.token_lifetime = token_lifetime
def generate_access_token(self, service_identity, allowed_destinations):
"""Generate short-lived access token for service"""
payload = {
'service': service_identity,
'destinations': allowed_destinations,
'iat': datetime.utcnow(),
'exp': datetime.utcnow() + timedelta(seconds=self.token_lifetime)
}
return jwt.encode(payload, self.secret_key, algorithm='HS256')
def verify_access(self, token, destination):
"""Verify if service can access destination"""
try:
payload = jwt.decode(token, self.secret_key, algorithms=['HS256'])
return destination in payload.get('destinations', [])
except jwt.ExpiredSignatureError:
return False
except jwt.InvalidTokenError:
return False
def make_authenticated_request(self, service_id, destination, url):
"""Make request with continuous verification"""
# Generate fresh token for each request
token = self.generate_access_token(service_id, [destination])
headers = {
'Authorization': f'Bearer {token}',
'X-Service-Identity': service_id
}
response = requests.get(url, headers=headers)
return response
# Usage example
verifier = NetworkAccessVerifier('your-secret-key')
response = verifier.make_authenticated_request(
'web-service',
'api-service',
'http://api-service:8080/data'
)
Chính sách mạng nâng cao
Ngoài các quy tắc cho phép/từ chối cơ bản, các chính sách mạng hiện đại còn hỗ trợ kiểm soát lưu lượng phức tạp.
Kiểm soát truy cập dựa trên thời gian
# time-based-policy.yml
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: maintenance-window-policy
spec:
endpointSelector:
matchLabels:
app: database
ingress:
- fromEndpoints:
- matchLabels:
app: backup-service
toPorts:
- ports:
- port: '5432'
protocol: TCP
# Only allow backup during maintenance window
# This requires custom admission controller or policy engine
- fromEndpoints:
- matchLabels:
app: api-service
toPorts:
- ports:
- port: '5432'
protocol: TCP
Chính sách nhận biết ứng dụng
# l7-policy.yml
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: api-l7-policy
spec:
endpointSelector:
matchLabels:
app: user-api
ingress:
- fromEndpoints:
- matchLabels:
app: frontend
toPorts:
- ports:
- port: '8080'
protocol: TCP
rules:
http:
- method: 'GET'
path: '/users/[0-9]+'
- method: 'POST'
path: '/users'
headers:
- 'Content-Type: application/json'
- fromEndpoints:
- matchLabels:
app: admin-panel
toPorts:
- ports:
- port: '8080'
protocol: TCP
rules:
http:
- method: 'DELETE'
path: '/users/[0-9]+'
Chính sách giới hạn tỷ giá
# rate-limiting.yml
apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
name: rate-limit-filter
spec:
workloadSelector:
labels:
app: api-gateway
configPatches:
- applyTo: HTTP_FILTER
match:
context: SIDECAR_INBOUND
listener:
filterChain:
filter:
name: envoy.filters.network.http_connection_manager
patch:
operation: INSERT_BEFORE
value:
name: envoy.filters.http.local_ratelimit
typed_config:
'@type': type.googleapis.com/udpa.type.v1.TypedStruct
type_url: type.googleapis.com/envoy.extensions.filters.http.local_ratelimit.v3.LocalRateLimit
value:
stat_prefix: rate_limiter
token_bucket:
max_tokens: 100
tokens_per_fill: 10
fill_interval: 60s
filter_enabled:
runtime_key: rate_limit_enabled
default_value:
numerator: 100
denominator: HUNDRED
filter_enforced:
runtime_key: rate_limit_enforced
default_value:
numerator: 100
denominator: HUNDRED
Mạng nhiều đám mây
Các tổ chức ngày càng sử dụng nhiều nhà cung cấp đám mây, đòi hỏi các chiến lược kết nối mạng phức tạp.
Thiết lập VPN trên nhiều đám mây
Kết nối AWS và Google Cloud:
# aws-gcp-vpn.tf
# AWS side
resource "aws_vpn_gateway" "aws_vgw" {
vpc_id = aws_vpc.main.id
tags = {
Name = "aws-to-gcp-vgw"
}
}
resource "aws_customer_gateway" "gcp_gateway" {
bgp_asn = 65000
ip_address = google_compute_vpn_gateway.gcp_gateway.ip_address
type = "ipsec.1"
tags = {
Name = "gcp-customer-gateway"
}
}
resource "aws_vpn_connection" "aws_gcp" {
vpn_gateway_id = aws_vpn_gateway.aws_vgw.id
customer_gateway_id = aws_customer_gateway.gcp_gateway.id
type = "ipsec.1"
static_routes_only = true
tags = {
Name = "aws-gcp-vpn"
}
}
# GCP side
resource "google_compute_vpn_gateway" "gcp_gateway" {
name = "gcp-to-aws-gateway"
network = google_compute_network.main.id
}
resource "google_compute_vpn_tunnel" "gcp_aws" {
name = "gcp-aws-tunnel"
peer_ip = aws_vpn_connection.aws_gcp.tunnel1_address
shared_secret = aws_vpn_connection.aws_gcp.tunnel1_preshared_key
target_vpn_gateway = google_compute_vpn_gateway.gcp_gateway.id
local_traffic_selector = ["10.1.0.0/16"]
remote_traffic_selector = ["10.0.0.0/16"]
}
Cân bằng tải toàn cầu
Phân phối lưu lượng truy cập trên nhiều vùng đám mây:
# global-lb-config.yml
apiVersion: v1
kind: ConfigMap
metadata:
name: global-lb-config
data:
nginx.conf: |
upstream us_east_servers {
server aws-us-east-lb.example.com:443;
server azure-us-east-lb.example.com:443 backup;
}
upstream eu_west_servers {
server aws-eu-west-lb.example.com:443;
server gcp-eu-west-lb.example.com:443 backup;
}
upstream asia_servers {
server gcp-asia-lb.example.com:443;
server aws-asia-lb.example.com:443 backup;
}
map $geoip2_data_continent_code $backend_pool {
default us_east_servers;
NA us_east_servers;
EU eu_west_servers;
AS asia_servers;
}
server {
listen 443 ssl http2;
server_name api.example.com;
location / {
proxy_pass https://$backend_pool;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
# Health checks and failover
proxy_next_upstream error timeout http_502 http_503 http_504;
proxy_connect_timeout 2s;
proxy_send_timeout 5s;
proxy_read_timeout 5s;
}
}
Ảo hóa mạng hàm (NFV)
Thay thế các thiết bị mạng phần cứng bằng các giải pháp dựa trên phần mềm chạy trên máy chủ tiêu chuẩn.
Mạng được xác định bằng phần mềm (SDN)
Thực hiện điều khiển mạng có thể lập trình:
# sdn-controller.py - Simple SDN controller example
from ryu.base import app_manager
from ryu.controller import ofp_event
from ryu.controller.handler import CONFIG_DISPATCHER, MAIN_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.ofproto import ofproto_v1_3
from ryu.lib.packet import packet, ethernet, ether_types
class SimpleFirewall(app_manager.RyuApp):
OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
def __init__(self, *args, **kwargs):
super(SimpleFirewall, self).__init__(*args, **kwargs)
self.mac_to_port = {}
# Firewall rules
self.blocked_ips = [
'192.168.1.100', # Blocked attacker IP
'10.0.0.50' # Compromised internal IP
]
@set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
def switch_features_handler(self, ev):
datapath = ev.msg.datapath
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
# Install default flow
match = parser.OFPMatch()
actions = [parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,
ofproto.OFPCML_NO_BUFFER)]
self.add_flow(datapath, 0, match, actions)
def add_flow(self, datapath, priority, match, actions, buffer_id=None):
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
actions)]
if buffer_id:
mod = parser.OFPFlowMod(datapath=datapath, buffer_id=buffer_id,
priority=priority, match=match,
instructions=inst)
else:
mod = parser.OFPFlowMod(datapath=datapath, priority=priority,
match=match, instructions=inst)
datapath.send_msg(mod)
@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
def packet_in_handler(self, ev):
msg = ev.msg
datapath = msg.datapath
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
in_port = msg.match['in_port']
pkt = packet.Packet(msg.data)
eth = pkt.get_protocols(ethernet.ethernet)[0]
# Drop packets from blocked IPs
if self.is_blocked_packet(pkt):
self.logger.info("Dropping blocked packet")
return
# Normal switching logic continues...
dst = eth.dst
src = eth.src
dpid = datapath.id
self.mac_to_port.setdefault(dpid, {})
self.mac_to_port[dpid][src] = in_port
if dst in self.mac_to_port[dpid]:
out_port = self.mac_to_port[dpid][dst]
else:
out_port = ofproto.OFPP_FLOOD
actions = [parser.OFPActionOutput(out_port)]
if out_port != ofproto.OFPP_FLOOD:
match = parser.OFPMatch(in_port=in_port, eth_dst=dst, eth_src=src)
self.add_flow(datapath, 1, match, actions, msg.buffer_id)
return
data = None
if msg.buffer_id == ofproto.OFP_NO_BUFFER:
data = msg.data
out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id,
in_port=in_port, actions=actions, data=data)
datapath.send_msg(out)
def is_blocked_packet(self, pkt):
# Implement packet inspection logic
# Check source IP against blocked list
return False # Simplified for example
Mạng ảo hàm
Triển khai mạng hàm dưới dạng container:
# vnf-deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: virtual-firewall
spec:
replicas: 2
selector:
matchLabels:
app: virtual-firewall
template:
metadata:
labels:
app: virtual-firewall
spec:
containers:
- name: firewall
image: custom/virtual-firewall:latest
securityContext:
capabilities:
add: ['NET_ADMIN']
env:
- name: FIREWALL_RULES
valueFrom:
configMapKeyRef:
name: firewall-config
key: rules.json
resources:
requests:
memory: '256Mi'
cpu: '200m'
limits:
memory: '512Mi'
cpu: '500m'
---
apiVersion: v1
kind: ConfigMap
metadata:
name: firewall-config
data:
rules.json: |
{
"rules": [
{
"action": "allow",
"protocol": "tcp",
"port": 80,
"source": "0.0.0.0/0"
},
{
"action": "allow",
"protocol": "tcp",
"port": 443,
"source": "0.0.0.0/0"
},
{
"action": "deny",
"protocol": "tcp",
"port": 22,
"source": "0.0.0.0/0"
}
]
}
Điện toán biên và tích hợp CDN
Đưa tính toán và kết nối mạng đến gần hơn với người dùng để có hiệu suất tốt hơn.
Kiến trúc mạng biên
# edge-deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: edge-api
namespace: edge-us-east
spec:
replicas: 3
selector:
matchLabels:
app: edge-api
location: us-east
template:
metadata:
labels:
app: edge-api
location: us-east
spec:
containers:
- name: api
image: api-server:latest
env:
- name: REGION
value: 'us-east-1'
- name: CACHE_BACKEND
value: 'redis.edge-us-east.svc.cluster.local:6379'
- name: DATABASE_REPLICA
value: 'postgres-replica.us-east.rds.amazonaws.com:5432'
resources:
requests:
memory: '128Mi'
cpu: '100m'
---
apiVersion: v1
kind: Service
metadata:
name: edge-api-service
namespace: edge-us-east
annotations:
cloud.google.com/neg: '{"ingress": true}'
spec:
selector:
app: edge-api
location: us-east
ports:
- port: 80
targetPort: 8080
type: LoadBalancer
Định tuyến giao thông thông minh
// cloudflare-worker.js - Edge computing example
addEventListener('fetch', (event) => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
const url = new URL(request.url);
const userCountry = request.cf.country;
const userRegion = getRegionFromCountry(userCountry);
// Route to nearest edge location
const edgeEndpoint = getEdgeEndpoint(userRegion);
// Add custom headers for edge processing
const modifiedRequest = new Request(request, {
headers: {
...request.headers,
'X-User-Country': userCountry,
'X-User-Region': userRegion,
'X-Edge-Location': edgeEndpoint,
},
});
// Forward to appropriate edge server
const response = await fetch(`${edgeEndpoint}${url.pathname}${url.search}`, modifiedRequest);
// Add edge caching headers
const modifiedResponse = new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: {
...response.headers,
'Cache-Control': 'public, max-age=300',
'X-Edge-Cache': 'MISS',
'X-Served-By': edgeEndpoint,
},
});
return modifiedResponse;
}
function getRegionFromCountry(country) {
const regionMap = {
US: 'us-east',
CA: 'us-east',
GB: 'eu-west',
DE: 'eu-west',
JP: 'asia-east',
AU: 'asia-southeast',
};
return regionMap[country] || 'us-east';
}
function getEdgeEndpoint(region) {
const endpoints = {
'us-east': 'https://api-us-east.example.com',
'eu-west': 'https://api-eu-west.example.com',
'asia-east': 'https://api-asia-east.example.com',
'asia-southeast': 'https://api-asia-se.example.com',
};
return endpoints[region] || endpoints['us-east'];
}
Xu hướng mạng mới nổi
Đi trước các công nghệ và mô hình mạng đang phát triển.
Mạng eBPF
Bộ lọc gói Berkeley mở rộng (eBPF) cho phép lập trình mạng ở cấp hạt nhân:
// ebpf-firewall.c - Simple eBPF firewall program
#include <linux/bpf.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <bpf/bpf_helpers.h>
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, __u32);
__type(value, __u32);
__uint(max_entries, 1000);
} blocked_ips SEC(".maps");
SEC("xdp_firewall")
int firewall_prog(struct xdp_md *ctx) {
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
struct ethhdr *eth = data;
if ((void *)(eth + 1) > data_end)
return XDP_PASS;
if (eth->h_proto != __constant_htons(ETH_P_IP))
return XDP_PASS;
struct iphdr *ip = (void *)(eth + 1);
if ((void *)(ip + 1) > data_end)
return XDP_PASS;
__u32 src_ip = ip->saddr;
__u32 *blocked = bpf_map_lookup_elem(&blocked_ips, &src_ip);
if (blocked) {
// Drop packets from blocked IPs
return XDP_DROP;
}
return XDP_PASS;
}
char _license[] SEC("license") = "GPL";
Mạng không có máy chủ
Kết nối mạng trong môi trường không có máy chủ yêu cầu các cách tiếp cận khác nhau:
# serverless-network.yml
apiVersion: networking.knative.dev/v1alpha1
kind: Ingress
metadata:
name: serverless-api-ingress
namespace: default
spec:
rules:
- hosts:
- api.example.com
http:
paths:
- splits:
- serviceName: user-service
serviceNamespace: default
percent: 90
- serviceName: user-service-canary
serviceNamespace: default
percent: 10
visibility: ExternalIP
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: serverless-routing
spec:
hosts:
- api.example.com
http:
- match:
- uri:
prefix: '/v1/'
route:
- destination:
host: knative-local-gateway.istio-system.svc.cluster.local
headers:
request:
set:
Host: api.example.com
Tối ưu hóa hiệu suất ở quy mô
Tối ưu hóa hiệu suất mạng cho các hệ thống phân tán quy mô lớn.
Kết nối tổng hợp và ghép kênh
// connection-pool.go - HTTP/2 connection pooling example
package main
import (
"context"
"crypto/tls"
"net/http"
"time"
"golang.org/x/net/http2"
)
type OptimizedClient struct {
client *http.Client
}
func NewOptimizedClient() *OptimizedClient {
// Configure HTTP/2 transport
transport := &http2.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: false,
},
// Connection pooling settings
MaxReadFrameSize: 1048576,
AllowHTTP: false,
ReadIdleTimeout: time.Second * 30,
PingTimeout: time.Second * 15,
WriteByteTimeout: time.Second * 10,
}
client := &http.Client{
Transport: transport,
Timeout: time.Second * 30,
}
return &OptimizedClient{client: client}
}
func (c *OptimizedClient) MakeRequest(ctx context.Context, url string) (*http.Response, error) {
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, err
}
// Add performance headers
req.Header.Set("User-Agent", "OptimizedClient/1.0")
req.Header.Set("Accept-Encoding", "gzip, deflate, br")
req.Header.Set("Connection", "keep-alive")
return c.client.Do(req)
}
Lập kế hoạch nhận biết mạng
# network-aware-scheduling.yml
apiVersion: v1
kind: Pod
metadata:
name: latency-sensitive-app
spec:
nodeSelector:
topology.kubernetes.io/zone: us-east-1a
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values: ['database']
topologyKey: kubernetes.io/hostname
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
preference:
matchExpressions:
- key: network-performance
operator: In
values: ['high']
tolerations:
- key: 'high-network-performance'
operator: 'Equal'
value: 'true'
effect: 'NoSchedule'
containers:
- name: app
image: latency-sensitive-app:latest
resources:
requests:
memory: '1Gi'
cpu: '500m'
limits:
memory: '2Gi'
cpu: '1000m'
Bây giờ bạn đã khám phá các khái niệm mạng nâng cao hỗ trợ các hệ thống phân tán hiện đại. Những mẫu này giúp bạn xây dựng các ứng dụng linh hoạt, an toàn và hiệu suất cao, có thể mở rộng quy mô trên toàn cầu trong khi vẫn duy trì khả năng quan sát và kiểm soát.
Bối cảnh mạng tiếp tục phát triển nhanh chóng. Các công nghệ như lưới dịch vụ, eBPF và điện toán biên đang định hình lại cách chúng ta nghĩ về kiến trúc mạng. Luôn tò mò, thử nghiệm các công cụ mới và luôn xem xét ý nghĩa mạng trong các quyết định kiến trúc của bạn.
Những kiến thức cơ bản mà bạn đã học được trong suốt hướng dẫn này cung cấp nền tảng vững chắc để hiểu và triển khai các khái niệm nâng cao này. Sử dụng chúng một cách khôn ngoan để xây dựng hệ thống tốt hơn.
Chúc bạn kết nối mạng vui vẻ!