目录

Linux操作系统-iperf3网络测试

iperf3 是一个 TCP、UDP 和 SCTP 网络带宽测量工具.

1. 前言

我们测试网络速度通常使用 speedtest、fast.com 等测速节点,一方面很多时候不能真实反映本地连接速度,另一方面不能直接用来测试局域网内机器之间的网速。有个 iperf3 工具可以从本地测试局域网内机器可达的最大带宽速度,支持 TCP /UDP 多线程并发测速。

2. 安装

二进制和源码可以官网下载:iperf 官网

也可以使用docker镜像部署:Dockerfile

我们本次使用 centos7 系统做测试,通过 yum 安装:

yum install iperf3 iperf3-devel -y

测试

1. 主机之间使用

主机和主机之间测试,直接使用2台机器,一台作为服务器端,一台作为客户端使用即可,没什么特别需要注意的地方。

服务器端

技巧

服务端常用参数:

-s 以服务器模式运行

-p 指定端口

-D 后台运行服务器模式

iperf3 -s -p 5201 -D

客户端

技巧

客户端常用参数:

-A 设置cpu亲和

-c 以客户端模式运行,连接到服务端

-t 传输时间,默认10秒

-n 传输内容大小,不能与-t同时使用

-b 目标比特率(0表示无限)(UDP默认1Mbit/sec,TCP不受限制)

-l 要读取或写入的缓冲区长度(TCP默认128 KB,UDP默认1460)

-O 忽略前几秒

-p 指定服务器端口

-i 带宽报告的时间间隔

-R 反向模式运行,即服务端发送,客户端接收

-u 使用UDP协议,默认使用TCP协议

iperf3 -u -l 1400 16 -b 500m -t 120 -c 192.168.22.6 -i 1 -p 5201 -A 1

2. 在容器内使用

测试容器之间的速度跟主机之间测试并没太多的区别,这里也会介绍怎么在 kubernetes 集群内测试网络速度。

2.1 docker 使用

从 docker hub下载一个 iperf3 的镜像

networkstatic/iperf3

启动server端
1
2
3
4
docker run  -it --rm --name=iperf3-server -p 5201:5201 networkstatic/iperf3 -s
-----------------------------------------------------------
Server listening on 5201
-----------------------------------------------------------
获取server端的ip
1
2
docker inspect --format "{{ .NetworkSettings.IPAddress }}" iperf3-server
(Returned) 172.17.0.163
启动client端
1
docker run  -it --rm networkstatic/iperf3 -c 172.17.0.163language-bash复制代码
结果输出
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
Connecting to host 172.17.0.163, port 5201
[  4] local 172.17.0.191 port 51148 connected to 172.17.0.163 port 5201
[ ID] Interval           Transfer     Bandwidth       Retr  Cwnd
[  4]   0.00-1.00   sec  4.16 GBytes  35.7 Gbits/sec    0    468 KBytes
[  4]   1.00-2.00   sec  4.10 GBytes  35.2 Gbits/sec    0    632 KBytes
[  4]   2.00-3.00   sec  4.28 GBytes  36.8 Gbits/sec    0   1.02 MBytes
[  4]   3.00-4.00   sec  4.25 GBytes  36.5 Gbits/sec    0   1.28 MBytes
[  4]   4.00-5.00   sec  4.20 GBytes  36.0 Gbits/sec    0   1.37 MBytes
[  4]   5.00-6.00   sec  4.23 GBytes  36.3 Gbits/sec    0   1.40 MBytes
[  4]   6.00-7.00   sec  4.17 GBytes  35.8 Gbits/sec    0   1.40 MBytes
[  4]   7.00-8.00   sec  4.14 GBytes  35.6 Gbits/sec    0   1.40 MBytes
[  4]   8.00-9.00   sec  4.29 GBytes  36.8 Gbits/sec    0   1.64 MBytes
[  4]   9.00-10.00  sec  4.15 GBytes  35.7 Gbits/sec    0   1.68 MBytes
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval           Transfer     Bandwidth       Retr
[  4]   0.00-10.00  sec  42.0 GBytes  36.1 Gbits/sec    0             sender
[  4]   0.00-10.00  sec  42.0 GBytes  36.0 Gbits/sec                  receiver

iperf Done.

2.2 kubernetes 使用

主要看使用场景,如果是压测,可以使用 github 上的一个开源的项目,测试 Kubernetes Service 性能的:kubernetes-iperf3

如果是测试 Pod 之间在集群内的网络速度和性能,可以参考这个项目。

目录结构
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# cd kubernetes-iperf3/
# tree
.
├── LICENSE
├── README.md
├── iperf3.sh
├── iperf3.yaml
├── network-policy.yaml
└── steps
    ├── cleanup.sh
    ├── run.sh
    └── setup.sh
技巧
iper3.yaml 定义了一个 deployment ,一个 server pod 在 master 节点,一个 deployment 对应的 service ,一组 DaemonSet(分布在每个 node 节点的 client pod)
如何使用

确保机器上有 .kube/config ,执行脚本:

./iperf3.sh

脚本支持任何 iperf3 支持的参数, e.g.:

./iperf3.sh -t 2

如果需要网络策略放开:

kubectl apply -f network-policy.yaml

清理网络策略:

kubectl delete -f network-policy.yaml

所需资源会通过脚本自动创建并且在测试完后自动清除。

结果输出
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
deployment.apps "iperf3-server-deployment" created
service "iperf3-server" created
daemonset.apps "iperf3-clients" created
Waiting for iperf3 server to start...
Waiting for iperf3 server to start...
Waiting for iperf3 server to start...
Server is running

Client on 172.20.47.197:  Connecting to host iperf3-server, port 5201
Client on 172.20.47.197:  [  4] local 100.96.0.28 port 37580 connected to 100.65.13.40 port 5201
Client on 172.20.47.197:  [ ID] Interval           Transfer     Bandwidth       Retr  Cwnd
Client on 172.20.47.197:  [  4]   0.00-1.00   sec  3.59 GBytes  30.8 Gbits/sec    0   4.00 MBytes
Client on 172.20.47.197:  [  4]   1.00-2.00   sec  3.66 GBytes  31.5 Gbits/sec    0   4.00 MBytes
Client on 172.20.47.197:  - - - - - - - - - - - - - - - - - - - - - - - - -
Client on 172.20.47.197:  [ ID] Interval           Transfer     Bandwidth       Retr
Client on 172.20.47.197:  [  4]   0.00-2.00   sec  7.25 GBytes  31.1 Gbits/sec    0             sender
Client on 172.20.47.197:  [  4]   0.00-2.00   sec  7.25 GBytes  31.1 Gbits/sec                  receiver
Client on 172.20.47.197:
Client on 172.20.47.197:  iperf Done.

Client on 172.20.57.129:  Connecting to host iperf3-server, port 5201
Client on 172.20.57.129:  [  4] local 100.96.1.31 port 55166 connected to 100.65.13.40 port 5201
Client on 172.20.57.129:  [ ID] Interval           Transfer     Bandwidth       Retr  Cwnd
Client on 172.20.57.129:  [  4]   0.00-1.00   sec   954 MBytes  8.00 Gbits/sec    5   3.10 MBytes
Client on 172.20.57.129:  [  4]   1.00-2.00   sec   948 MBytes  7.95 Gbits/sec    1   4.22 MBytes
Client on 172.20.57.129:  - - - - - - - - - - - - - - - - - - - - - - - - -
Client on 172.20.57.129:  [ ID] Interval           Transfer     Bandwidth       Retr
Client on 172.20.57.129:  [  4]   0.00-2.00   sec  1.86 GBytes  7.97 Gbits/sec    6             sender
Client on 172.20.57.129:  [  4]   0.00-2.00   sec  1.85 GBytes  7.93 Gbits/sec                  receiver
Client on 172.20.57.129:
Client on 172.20.57.129:  iperf Done.

deployment.apps "iperf3-server-deployment" deleted
service "iperf3-server" deleted
daemonset.apps "iperf3-clients" deleted