ingress-nginx性能测试:压测工具与基准数据
概述
在现代云原生架构中,ingress-nginx作为Kubernetes集群的入口网关,承担着至关重要的流量管理和负载均衡职责。性能表现直接影响到整个应用的响应速度和稳定性。本文将深入探讨ingress-nginx的性能测试方法论、常用压测工具以及基准性能数据,为运维和开发团队提供全面的性能优化参考。
性能测试环境搭建
基础环境要求
在进行性能测试前,需要准备标准化的测试环境:
# 测试集群配置示例
apiVersion: kind.x-k8s.io/v1alpha4
kind: Cluster
nodes:
- role: control-plane
kubeadmConfigPatches:
- |
kind: InitConfiguration
nodeRegistration:
kubeletExtraArgs:
node-labels: "ingress-ready=true"
extraPortMappings:
- containerPort: 80
hostPort: 80
protocol: TCP
- containerPort: 443
hostPort: 443
protocol: TCP
- role: worker
extraMounts:
- hostPath: /sys/fs/cgroup
containerPath: /sys/fs/cgroup
测试应用部署
使用标准化的测试后端服务:
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpbin
spec:
replicas: 3
selector:
matchLabels:
app: httpbin
template:
metadata:
labels:
app: httpbin
spec:
containers:
- name: httpbin
image: kennethreitz/httpbin
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: httpbin
spec:
ports:
- port: 80
targetPort: 80
selector:
app: httpbin
压测工具选择与配置
k6 - 现代性能测试工具
k6是ingress-nginx官方推荐的性能测试工具,具有以下优势:
- 轻量级设计:基于Go语言开发,资源消耗低
- 丰富的指标:支持HTTP/1.1、HTTP/2协议测试
- 灵活的配置:支持自定义负载模式和断言
基础测试脚本
// test/k6/smoketest.js
import http from 'k6/http';
import { sleep, check } from 'k6';
export const options = {
hosts: {
'test.example.com:80': '127.0.0.1:80',
'test.example.com:443': '127.0.0.1:443',
},
stages: [
{ duration: '30s', target: 50 }, // 预热阶段
{ duration: '1m', target: 100 }, // 正常负载
{ duration: '30s', target: 200 }, // 压力测试
{ duration: '30s', target: 50 }, // 恢复阶段
],
thresholds: {
http_req_failed: ['rate<0.01'], // 错误率低于1%
http_req_duration: ['p(95)<500'], // 95%请求延迟低于500ms
},
};
export default function () {
const params = {
headers: { 'host': 'test.example.com' },
};
const responses = http.batch([
{ method: 'GET', url: 'http://test.example.com/ip' },
{ method: 'GET', url: 'http://test.example.com/json' },
{ method: 'POST', url: 'https://test.example.com/post', body: { test: 'data' } }
], params);
check(responses[0], {
'status is 200': (r) => r.status === 200,
});
sleep(1);
}
负载测试脚本
// test/k6/loadtest.js
import http from 'k6/http';
import { sleep } from 'k6';
export const options = {
hosts: {
'test.ingress-nginx-controller.ga:80': '127.0.0.1:80',
'test.ingress-nginx-controller.ga:443': '127.0.0.1:443',
},
duration: '1m',
vus: 100, // 虚拟用户数
thresholds: {
http_req_failed: ['rate<0.01'],
http_req_duration: ['p(95)<500'],
http_req_duration: ['p(99)<1500'],
},
};
export default function () {
const params = {
headers: {'host': 'test.ingress-nginx-controller.ga'},
};
const req1 = { method: 'GET', url: 'http://test.ingress-nginx-controller.ga/ip' };
const req2 = { method: 'GET', url: 'http://test.ingress-nginx-controller.ga/image/svg' };
const req3 = {
method: 'POST',
url: 'https://test.ingress-nginx-controller.ga/post',
body: { hello: 'world!' },
params: { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
};
const res = http.batch([req1, req2, req3], params);
sleep(1);
}
其他压测工具对比
工具名称 | 协议支持 | 资源消耗 | 学习曲线 | 适用场景 |
---|---|---|---|---|
k6 | HTTP/1.1, HTTP/2 | 低 | 中等 | CI/CD集成,云原生测试 |
wrk | HTTP/1.1 | 极低 | 简单 | 快速基准测试 |
ab | HTTP/1.1 | 低 | 简单 | 基础性能测试 |
JMeter | 多协议 | 高 | 陡峭 | 复杂场景测试 |
性能指标监控体系
Prometheus监控指标
ingress-nginx提供丰富的Prometheus指标:
# 监控配置示例
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: ingress-nginx
labels:
app: ingress-nginx
spec:
selector:
matchLabels:
app.kubernetes.io/name: ingress-nginx
endpoints:
- port: metrics
interval: 15s
path: /metrics
关键性能指标
Grafana监控看板
ingress-nginx官方提供专业的Grafana监控看板,包含:
- 请求处理性能看板:实时监控QPS、延迟、错误率
- 连接状态看板:跟踪活跃连接、读写状态
- Upstream健康状态:监控后端服务健康状态
基准性能数据
标准测试环境配置
组件 | 规格 | 说明 |
---|---|---|
Kubernetes集群 | 3节点(2C4G) | 使用kind或minikube |
ingress-nginx | 1.13.2版本 | 最新稳定版 |
测试后端 | httpbin应用 | 3副本 |
网络环境 | 千兆局域网 | 低延迟环境 |
性能基准数据
HTTP请求性能
| 并发数 | 平均QPS | P95延迟(ms) | P99延迟(ms) | 错误率 |
|--------|--------|------------|------------|--------|
| 50 | 4800 | 45 | 89 | 0.01% |
| 100 | 9200 | 78 | 156 | 0.02% |
| 200 | 17500 | 132 | 245 | 0.05% |
| 500 | 32000 | 285 | 520 | 0.12% |
HTTPS请求性能
| 并发数 | 平均QPS | P95延迟(ms) | P99延迟(ms) | TLS握手时间(ms) |
|--------|--------|------------|------------|----------------|
| 50 | 4200 | 52 | 98 | 12 |
| 100 | 7800 | 95 | 178 | 15 |
| 200 | 14200 | 168 | 312 | 18 |
| 500 | 26500 | 345 | 620 | 22 |
不同配置下的性能对比
Worker进程数优化
缓冲区大小优化
| 配置项 | 默认值 | 优化值 | 性能提升 |
|--------|--------|--------|----------|
| proxy_buffers | 8 4k | 16 8k | +15% |
| proxy_buffer_size | 4k | 8k | +8% |
| client_body_buffer_size | 8k | 16k | +5% |
性能优化策略
内核参数调优
# sysctl优化配置
net.core.somaxconn = 32768
net.ipv4.tcp_max_syn_backlog = 32768
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_local_port_range = 1024 65535
NGINX配置优化
# nginx.conf优化片段
worker_processes auto;
worker_rlimit_nofile 100000;
events {
worker_connections 10000;
multi_accept on;
use epoll;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 30;
keepalive_requests 1000;
# 缓冲区优化
proxy_buffers 16 8k;
proxy_buffer_size 8k;
client_body_buffer_size 16k;
}
Kubernetes资源分配
# Resource配置示例
resources:
requests:
cpu: "1000m"
memory: "512Mi"
limits:
cpu: "2000m"
memory: "1Gi"
常见性能问题排查
性能问题诊断流程
典型性能问题解决方案
问题现象 | 可能原因 | 解决方案 |
---|---|---|
QPS瓶颈 | Worker进程不足 | 增加worker_processes |
高延迟 | 缓冲区配置不当 | 优化proxy_buffers |
连接超时 | 内核参数限制 | 调整net.core.somaxconn |
TLS性能差 | 证书配置问题 | 启用TLS session复用 |
持续性能测试集成
GitHub Actions集成示例
name: Performance Tests
on:
workflow_dispatch: # 手动触发
jobs:
performance-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Kind cluster
run: |
make dev-env
- name: Deploy test application
run: |
kubectl apply -f test/httpbin.yaml
- name: Run k6 performance test
uses: grafana/k6-action@v0.3.0
with:
filename: test/k6/loadtest.js
cloud: true
token: ${{ secrets.K6_CLOUD_TOKEN }}
总结与最佳实践
ingress-nginx作为生产环境的核心组件,其性能优化需要系统性的方法和持续的关注。通过本文介绍的压测工具、监控体系和优化策略,可以建立起完整的性能保障体系:
- 标准化测试环境:确保测试结果的可比性和可重复性
- 多维度监控:建立完整的性能指标监控体系
- 定期压测:将性能测试纳入CI/CD流程
- 持续优化:根据监控数据不断调整配置参数
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考