参考回答
Ingress controllers can become bottlenecks due to several factors:
Internet Traffic → Load Balancer → Ingress Controller → Backend Services
↓
Performance Bottleneck
(CPU, Memory, Network, Configuration)
Common bottleneck sources:
- Insufficient ingress controller resources
- Poor load balancing algorithms
- Inefficient SSL/TLS termination
- Configuration overhead and rule complexity
- Backend service capacity limitations
Diagnostic Methodology
1. Performance Metrics Analysis
Key Ingress Controller Metrics:
# NGINX Ingress Controller metrics
kubectl get --raw /api/v1/namespaces/ingress-nginx/services/ingress-nginx-controller-metrics:http-metrics/proxy/metrics | grep -E "nginx_ingress_controller_requests_total|nginx_ingress_controller_request_duration_seconds|nginx_ingress_controller_response_size"
Critical metrics to monitor:
nginx_ingress_controller_requests_total: Request rate and volume
nginx_ingress_controller_request_duration_seconds: Response latency percentiles
nginx_ingress_controller_response_size: Response payload analysis
nginx_ingress_controller_ssl_expire_time_seconds: Certificate health
nginx_ingress_controller_nginx_process_*: Process-level resource usage
2. Resource Utilization Assessment
# Analyze current resource consumption
kubectl top pod -n ingress-nginx --containers
kubectl describe pod -n ingress-nginx
# Check node resource availability
kubectl describe node | grep -A 10 "Allocated resources"
3. Configuration Analysis
Review ingress configuration complexity:
- Number of ingress rules and backends
- SSL certificate configuration overhead
- Routing complexity and regex patterns
- Middleware and annotation usage
Horizontal Scaling Strategies
1. Ingress Controller Replica Scaling
apiVersion: apps/v1
kind: Deployment
metadata:
name: ingress-nginx-controller
namespace: ingress-nginx
spec:
replicas: 5 # Scale up from default
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1 # Ensure availability during updates
maxSurge: 1
template:
spec:
containers:
- name: controller
image: k8s.gcr.io/ingress-nginx/controller:v1.1.1
resources:
requests:
cpu: 500m # Increased from default 100m
memory: 512Mi # Increased from default 90Mi
limits:
cpu: 1000m
memory: 1Gi
2. Horizontal Pod Autoscaling (HPA)
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: ingress-nginx-hpa
namespace: ingress-nginx
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: ingress-nginx-controller
minReplicas: 3 # Minimum for high availability
maxReplicas: 20 # Scale based on demand
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70 # Scale up at 70% CPU
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 80 # Scale up at 80% memory
behavior:
scaleUp:
stabilizationWindowSeconds: 60
policies:
- type: Percent
value: 100 # Double replicas quickly under load
periodSeconds: 15
scaleDown:
stabilizationWindowSeconds: 300
policies:
- type: Percent
value: 10 # Conservative scale-down
periodSeconds: 60
Key HPA considerations:
- Conservative scale-down to avoid thrashing
- Aggressive scale-up for traffic spikes
- Stabilization windows to prevent rapid scaling
- Multiple metrics for comprehensive scaling decisions
Performance Optimization
1. NGINX Configuration Tuning
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-configuration
namespace: ingress-nginx
data:
# Worker process optimization
worker-processes: "auto" # Match CPU cores
worker-connections: "16384" # Connections per worker
max-worker-open-files: "65536" # File descriptor limit
# Connection handling
upstream-keepalive-connections: "320" # Backend keepalive
upstream-keepalive-timeout: "60" # Keepalive timeout
upstream-keepalive-requests: "10000" # Requests per connection
keep-alive: "75" # Client keepalive
keep-alive-requests: "1000" # Client keepalive requests
# Buffer optimization
large-client-header-buffers: "4 16k" # Header buffer size
client-body-buffer-size: "64k" # Body buffer size
proxy-buffer-size: "16k" # Proxy buffer size
proxy-buffers: "8 16k" # Number of proxy buffers
# Compression and caching
enable-brotli: "true" # Enable Brotli compression
gzip-level: "6" # Gzip compression level
proxy-cache-valid: "200 302 1h" # Cache valid responses
# Rate limiting
rate-limit: "100" # Requests per second
rate-limit-window: "1m" # Rate limit window
Performance tuning rationale:
- Worker processes match available CPU cores
- Increased connection limits for high concurrency
- Optimized buffer sizes for typical workloads
- Compression and caching for response optimization
2. SSL/TLS Optimization
# SSL configuration optimization
data:
ssl-protocols: "TLSv1.2 TLSv1.3" # Modern protocols only
ssl-ciphers: "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384"
ssl-session-cache-size: "10m" # SSL session cache
ssl-session-timeout: "1h" # Session timeout
ssl-buffer-size: "4k" # SSL buffer optimization
Advanced Scaling Patterns
1. Multi-Tier Ingress Architecture
Public Internet → External Load Balancer → Public Ingress Controllers
↓
Internal Network → Internal Load Balancer → Internal Ingress Controllers
↓
Backend Services
Public Ingress Configuration:
# Public traffic ingress
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: public-api-ingress
annotations:
kubernetes.io/ingress.class: "nginx-public"
nginx.ingress.kubernetes.io/rate-limit: "1000"
nginx.ingress.kubernetes.io/rate-limit-window: "1m"
spec:
tls:
- hosts:
- api.company.com
secretName: api-tls-secret
rules:
- host: api.company.com
http:
paths:
- path: /api/v1
pathType: Prefix
backend:
service:
name: public-api-service
port:
number: 80
Internal Ingress Configuration:
# Internal traffic ingress
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: internal-api-ingress
annotations:
kubernetes.io/ingress.class: "nginx-internal"
nginx.ingress.kubernetes.io/whitelist-source-range: "10.0.0.0/8,172.16.0.0/12"
spec:
rules:
- host: internal-api.company.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: internal-api-service
port:
number: 80
2. Geographic Load Distribution
# Regional ingress controllers
apiVersion: apps/v1
kind: Deployment
metadata:
name: ingress-nginx-us-east
namespace: ingress-nginx
spec:
replicas: 5
template:
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: topology.kubernetes.io/region
operator: In
values: ["us-east-1"]
containers:
- name: controller
image: k8s.gcr.io/ingress-nginx/controller:v1.1.1
env:
- name: POD_REGION
value: "us-east-1"
Load Balancing Strategies
1. Advanced Load Balancing Algorithms
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: app-ingress
annotations:
nginx.ingress.kubernetes.io/upstream-hash-by: "$binary_remote_addr" # IP hash
nginx.ingress.kubernetes.io/load-balance: "ip_hash" # Sticky sessions
nginx.ingress.kubernetes.io/session-cookie-name: "ingress-session" # Session affinity
nginx.ingress.kubernetes.io/session-cookie-expires: "86400" # 24 hours
spec:
rules:
- host: app.company.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: app-service
port:
number: 80
Load balancing method selection:
- Round Robin: Default, good for stateless applications
- IP Hash: Session affinity for stateful applications
- Least Connections: Best for long-running connections
- Weighted: Different capacity backend services
2. Circuit Breaker Integration
# Circuit breaker configuration
annotations:
nginx.ingress.kubernetes.io/server-snippet: |
location /health {
access_log off;
return 200 "healthy\n";
}
nginx.ingress.kubernetes.io/configuration-snippet: |
if ($request_uri = /health) {
return 200 "healthy\n";
}
error_page 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
Monitoring and Alerting
1. Performance Monitoring Dashboard
Key performance indicators (KPIs):
- Request rate (RPS) and volume trends
- Response latency percentiles (P50, P95, P99)
- Error rate and status code distribution
- SSL certificate expiration monitoring
- Backend service health and availability
2. Automated Alerting
# Prometheus alert rules for ingress performance
groups:
- name: ingress-performance.rules
rules:
- alert: IngressHighLatency
expr: histogram_quantile(0.95, increase(nginx_ingress_controller_request_duration_seconds_bucket[5m])) > 1.0
for: 2m
labels:
severity: warning
annotations:
summary: "Ingress latency is high"
description: "95th percentile latency is {{ $value }} seconds"
- alert: IngressHighErrorRate
expr: rate(nginx_ingress_controller_requests_total{status=~"5.."}[5m]) / rate(nginx_ingress_controller_requests_total[5m]) > 0.05
for: 3m
labels:
severity: critical
annotations:
summary: "High error rate on ingress controller"
description: "Error rate is {{ $value | humanizePercentage }}"
- alert: IngressControllerDown
expr: up{job="ingress-nginx-controller-metrics"} == 0
for: 1m
labels:
severity: critical
annotations:
summary: "Ingress controller is down"
Capacity Planning
1. Traffic Pattern Analysis
- Historical traffic analysis and trending
- Peak usage identification and planning
- Seasonal and business cycle considerations
- Growth projection and capacity modeling
2. Load Testing Strategy
# Load testing with realistic traffic patterns
# Use tools like Artillery, k6, or JMeter
# Example k6 load test script
import http from 'k6/http';
import { check, sleep } from 'k6';
export let options = {
stages: [
{ duration: '2m', target: 100 }, // Ramp up
{ duration: '5m', target: 100 }, // Stay at 100 users
{ duration: '2m', target: 200 }, // Ramp up to 200 users
{ duration: '5m', target: 200 }, // Stay at 200 users
{ duration: '2m', target: 0 }, // Ramp down
],
thresholds: {
http_req_duration: ['p(95)<500'], // 95% of requests under 500ms
http_req_failed: ['rate<0.05'], # Error rate under 5%
},
};
export default function() {
let response = http.get('https://api.company.com/health');
check(response, {
'status is 200': (r) => r.status === 200,
});
sleep(1);
}
Best Practices for Production
1. High Availability Design
- Multi-zone ingress controller deployment
- Health check configuration and monitoring
- Graceful shutdown and connection draining
- Backup ingress controllers for disaster recovery
2. Security Considerations
- Rate limiting and DDoS protection
- Web Application Firewall (WAF) integration
- SSL/TLS configuration hardening
- Security headers and policy enforcement
3. Operational Excellence
- Blue-green deployment for ingress updates
- Canary releases for configuration changes
- Automated rollback procedures
- Regular performance testing and optimization