Kubernetes 网络性能测试
基于已经搭建的Kubernetes环境,来测试其网络性能。
1. 测试准备
1.1 测试环境
测试环境为VMware Workstation虚拟机搭建的一套K8S环境,版本为1.19,网络插件使用flannel。
hostname | ip | 备注 |
---|---|---|
k8s-master | 192.168.0.51 | master |
k8s-node1 | 192.168.0.52 | worker |
k8s-node2 | 192.168.0.53 | worker |
已经部署测试应用sample-webapp,三个副本:
[root@k8s-master ~]# kubectl get pod -n ingress-traefik | grep sample-webapp
sample-webapp-4wf7c 1/1 Running 0 46s
sample-webapp-jvdpv 1/1 Running 10 7d23h
sample-webapp-kdk9k 1/1 Running 0 46s
[root@k8s-master ~]# kubectl get svc -n ingress-traefik | grep sample-webapp
sample-webapp ClusterIP 10.98.210.117 <none> 8000/TCP 10d
备注:
本文测试环境,master节点取消了NoSchedule的Taints,使得master节点也可以调度业务pod。
1.2 测试场景
- Kubernetes 集群 node 节点上通过 Cluster IP 方式访问
- Kubernetes 集群内部通过 service 访问
- Kubernetes 集群外部通过 traefik ingress 暴露的地址访问
1.3 测试工具
- Locust:一个简单易用的用户负载测试工具,用来测试 web 或其他系统能够同时处理的并发用户数。
- curl
- 测试程序:sample-webapp,源码见 Github kubernetes 的分布式负载测试。
1.4 测试说明
通过向 sample-webapp 发送 curl 请求获取响应时间,直接 curl 后的结果为:
[root@k8s-master ~]# curl "http://10.98.210.117:8000"
Welcome to the "Distributed Load Testing Using Kubernetes" sample web app
2. 网络延迟测试
2.1 Kubernetes 集群 node 节点上通过 Cluster IP 访问
测试命令
curl -o /dev/null -s -w '%{time_connect} %{time_starttransfer} %{time_total}' "http://10.98.210.117:8000/"
测试10组数据,取平均结果:
[root@k8s-node1 ~]# echo "time_connect time_starttransfer time_total"; for i in {1..10}; do curl -o /dev/null -s -w '%{time_connect} %{time_starttransfer} %{time_total}\n' "http://10.98.210.117:8000/"; done
time_connect time_starttransfer time_total
0.000 0.002 0.002
0.000 0.001 0.001
0.001 0.003 0.003
0.001 0.003 0.003
0.001 0.006 0.006
0.001 0.003 0.003
0.001 0.004 0.004
0.001 0.003 0.003
0.001 0.004 0.004
0.000 0.002 0.002
平均响应时间:3.1 ms
指标说明:
-
time_connect:建立到服务器的 TCP 连接所用的时间
-
time_starttransfer:在发出请求之后,Web 服务器返回数据的第一个字节所用的时间
-
time_total:完成请求所用的时间
2.2 Kubernetes 集群内部通过 service 访问
# 进入测试的客户端
[root@k8s-node1 ~]# kubectl exec -it locust-master-vljrx -n ingress-traefik /bin/bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
# 执行测试
root@locust-master-vljrx:/# echo "time_connect time_starttransfer time_total"; for i in {1..10}; do curl -o /dev/null -s -w '%{time_connect} %{time_starttransfer} %{time_total}\n' "http://sample-webapp:8000/"; done
time_connect time_starttransfer time_total
0.001326 0.002542 0.002715
0.001444 0.003264 0.003675
0.002193 0.004262 0.004889
0.002066 0.003664 0.003876
0.001739 0.004095 0.004432
0.002339 0.004536 0.004647
0.001649 0.003288 0.003628
0.001794 0.003373 0.003911
0.001492 0.003201 0.003581
0.002036 0.003712 0.004109
平均响应时间:约4ms
2.3 在外部通过 traefik ingress 访问
本文使用了traefik作为k8s集群的网关入口,可以参考我的另外一篇文章部署traefik2.0。也可以将service改为NodePort类型进行测试。
# 添加路由规则,使得访问sample-webapp.test.com可以路由到sample-webapp服务
[root@k8s-master ~]# kubectl get ingressroute -n ingress-traefik traefik-dashboard-route -o yaml
...
resourceVersion: "975669"
selfLink: /apis/traefik.containo.us/v1alpha1/namespaces/ingress-traefik/ingressroutes/traefik-dashboard-route
uid: 4faeecab-cd87-406f-9d50-3d507a6b73ff
spec:
entryPoints:
- web
routes:
- kind: Rule
match: Host(`traefik.test.com`)
services:
- name: traefik
port: 8080
- kind: Rule
match: Host(`locust.test.com`)
services:
- name: locust-master
port: 8089
- kind: Rule
match: Host(`sample-webapp.test.com`)
services:
- name: sample-webapp
port: 8000
在外部的客户端添加域名解析后,进行访问测试:
[root@bogon ~]# echo "time_connect time_starttransfer time_total"; for i in {1..10}; do curl -o /dev/null -s -w '%{time_connect} %{time_starttransfer} %{time_total}\n' "http://sample-webapp.test.com/"; done
time_connect time_starttransfer time_total
0.048 0.056 0.062
0.030 0.108 0.115
0.029 0.036 0.046
0.048