---
# System prepended metadata

title: K8s Iperf 叢集網路效能測速
tags: [K8S]

---

# K8s Iperf 叢集網路效能測速

* 建立 iperf DaemonSet，透過 iperf3 指令測試 pod 之間網路效能。
```
$ kubectl create ns iperf

$ echo 'apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: ds-iperf
  namespace: iperf
spec:
  selector:
    matchLabels:
      app: iperf3
  template:
    metadata:
      labels:
        app: iperf3
    spec:
      containers:
      - name: iperf3
        image: leodotcloud/swiss-army-knife
        command: ["iperf3"]
        args: ["-s", "-p 12345"]
        ports:
        - containerPort: 12345
          hostPort: 12345' | kubectl apply -f -
```


```
$ kubectl -n iperf get po -owide
NAME             READY   STATUS    RESTARTS   AGE   IP            NODE        NOMINATED NODE   READINESS GATES
ds-iperf-4sfcg   1/1     Running   0          10s   10.42.0.225   cilium-m1   <none>           <none>
ds-iperf-lktlh   1/1     Running   0          10s   10.42.1.187   cilium-m3   <none>           <none>
ds-iperf-pn89n   1/1     Running   0          10s   10.42.2.60    cilium-m2   <none>           <none>
ds-iperf-rq54c   1/1     Running   0          10s   10.42.3.224   cilium-w1   <none>           <none>
```


## 測速

```
$ kubectl -n iperf get po -owide
NAME             READY   STATUS    RESTARTS   AGE     IP            NODE        NOMINATED NODE   READINESS GATES
ds-iperf-74qrk   1/1     Running   0          3m40s   10.42.1.179   cilium-w1   <none>           <none>
ds-iperf-bh575   1/1     Running   0          3m40s   10.42.2.94    cilium-w2   <none>           <none>
ds-iperf-cr4nj   1/1     Running   0          3m40s   10.42.0.166   cilium-m1   <none>           <none>
ds-iperf-grndx   1/1     Running   0          3m40s   10.42.4.249   cilium-m2   <none>           <none>
ds-iperf-vgb2g   1/1     Running   0          3m40s   10.42.5.177   cilium-m3   <none>           <none>
```
* 測試 pod 對 pod 網速。
```
$ kubectl -n iperf exec ds-iperf-74qrk -- iperf3 -c 10.42.2.94 -p 12345
Connecting to host 10.42.2.94, port 12345
[  4] local 10.42.1.179 port 42436 connected to 10.42.2.94 port 12345
[ ID] Interval           Transfer     Bandwidth       Retr  Cwnd
[  4]   0.00-1.00   sec   107 MBytes   899 Mbits/sec  521    399 KBytes
[  4]   1.00-2.00   sec   102 MBytes   856 Mbits/sec  888    513 KBytes
[  4]   2.00-3.00   sec   107 MBytes   894 Mbits/sec  571    610 KBytes
[  4]   3.00-4.00   sec   104 MBytes   873 Mbits/sec  173    709 KBytes
[  4]   4.00-5.00   sec   105 MBytes   879 Mbits/sec  860    789 KBytes
[  4]   5.00-6.00   sec   103 MBytes   868 Mbits/sec  1040    842 KBytes
[  4]   6.00-7.00   sec   106 MBytes   888 Mbits/sec  1249    907 KBytes
[  4]   7.00-8.00   sec   101 MBytes   849 Mbits/sec  1700    960 KBytes
[  4]   8.00-9.00   sec  96.5 MBytes   809 Mbits/sec  779    580 KBytes
[  4]   9.00-10.00  sec   105 MBytes   881 Mbits/sec  638    498 KBytes
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval           Transfer     Bandwidth       Retr
[  4]   0.00-10.00  sec  1.01 GBytes   870 Mbits/sec  8419             sender
[  4]   0.00-10.00  sec  1.01 GBytes   867 Mbits/sec                  receiver

iperf Done.
```

* 測試叢集外連到 pod 網速，因為有開 `hostPort: 12345` 所以可以直接從 node 12345 port 連到 pod。

```
$ iperf3 -c 192.168.11.110 -p 12345
Connecting to host 192.168.11.110, port 12345
[  5] local 192.168.11.65 port 58700 connected to 192.168.11.110 port 12345
[ ID] Interval           Transfer     Bitrate         Retr  Cwnd
[  5]   0.00-1.00   sec   106 MBytes   889 Mbits/sec   39    374 KBytes
[  5]   1.00-2.00   sec   111 MBytes   927 Mbits/sec  566    497 KBytes
[  5]   2.00-3.00   sec   107 MBytes   902 Mbits/sec  732    612 KBytes
[  5]   3.00-4.00   sec   109 MBytes   912 Mbits/sec  112    448 KBytes
[  5]   4.00-5.00   sec   108 MBytes   902 Mbits/sec    4    594 KBytes
[  5]   5.00-6.00   sec   111 MBytes   933 Mbits/sec    7    711 KBytes
[  5]   6.00-7.00   sec   109 MBytes   912 Mbits/sec   23    805 KBytes
[  5]   7.00-8.00   sec   108 MBytes   902 Mbits/sec    0    898 KBytes
[  5]   8.00-9.00   sec   108 MBytes   902 Mbits/sec  199    969 KBytes
[  5]   9.00-10.00  sec   111 MBytes   933 Mbits/sec    0   1.03 MBytes
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval           Transfer     Bitrate         Retr
[  5]   0.00-10.00  sec  1.06 GBytes   911 Mbits/sec  1682             sender
[  5]   0.00-10.00  sec  1.06 GBytes   909 Mbits/sec                  receiver

iperf Done.
```

## 網路壓測
* 壓測 pod 網路速度
```
# 建立一個 nginx pod
$ echo 'apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 80' | kubectl apply -f -

# 確認 pod ip
$ kubectl get po -owide
NAME        READY   STATUS    RESTARTS   AGE   IP           NODE        NOMINATED NODE   READINESS GATES
nginx-pod   1/1     Running   0          20s   10.42.0.36   cilium-m1   <none>           <none>

# 需修改自己的 nginx pod ip
$ echo 'apiVersion: v1
kind: Pod
metadata:
  labels:
    run: test
  name: test
spec:
  containers:
  - name: test
    image: jordi/ab
    args:
      - "-k"
      - "-c"
      - "1000"
      - "-n"
      - "1000000"
      - "http://10.42.0.36/"     # 修改此行
  dnsPolicy: ClusterFirst
  restartPolicy: Never' | kubectl apply -f -
```
* 壓測完後查看 log，可以觀察 "Requests per second" (每秒請求數)這個數值，數值越高表示請求越多，代表網路效能越好。
```
$ kubectl logs test
This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
.......
Concurrency Level:      1000
Time taken for tests:   41.483 seconds
Complete requests:      1000000
Failed requests:        0
Keep-Alive requests:    999500
Total transferred:      852997500 bytes
HTML transferred:       615000000 bytes
Requests per second:    24106.07 [#/sec] (mean)
Time per request:       41.483 [ms] (mean)
Time per request:       0.041 [ms] (mean, across all concurrent requests)
Transfer rate:          20080.49 [Kbytes/sec] received
```
* 有關 ab 壓測工具可以參考 https://httpd.apache.org/docs/2.4/programs/ab.html

* 在 sles 上安裝 ab 指令
```
$ zypper in -y apache2-utils
```
* 壓力測試，注意 url 最後要加 '/'
```
$ ab -k -c 1000 -n 1000000 http://192.168.11.103:31705/
```
## 參考
https://www.suse.com/support/kb/doc/?id=00002095