一个计算机技术爱好者与学习者

0%

好好学K8S:Pod网络限速

1. 前言

为避免Pod打满带宽,有时需要对Pod进行限速。
本文中,我们就来学习一下Pod中的网络限速方法。

相关文档:

2. Pod网络限速思路

1、CNI插件需要支持带宽限速
2、Pod中配置注解

注意:这种限速方式,对内网和外网都有效。

3. CNI插件启用带宽限速

3.1. Flannel

1、编辑flannel插件的配置

1
kubectl edit cm -n kube-system kube-flannel-cfg

plugins 配置中,增加 bandwidth 配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
apiVersion: v1
data:
cni-conf.json: |
{
"name": "cb0",
"cniVersion":"0.3.1",
"plugins": [
{
"type": "bandwidth",
"capabilities": {"bandwidth": true}
}
]
}

2、重建flannel pod

1
kubectl -n kube-system delete pod -l app=flannel

3.2. Calico

1、编辑calico插件的配置

1
vim /etc/cni/net.d/10-calico.conflist

plugins 配置中,增加 bandwidth 配置:

1
2
3
4
5
6
7
8
9
10
{
"name": "k8s-pod-network",
"cniVersion": "0.3.1",
"plugins": [
{
"type": "bandwidth",
"capabilities": {"bandwidth": true}
}
]
}

2、重建calico pod

1
kubectl -n calico-system delete pod -l k8s-app=calico-node

4. Pod中配置注解

1、pod中添加限速注解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
annotations:
kubernetes.io/ingress-bandwidth: "1M" # 限制入站带宽为 1 MBps,单位Byte
kubernetes.io/egress-bandwidth: "3M" # 限制出站带宽为 3 MBps,单位Byte
labels:
run: testpod
name: testpod
spec:
containers:
- image: debian:bookworm-slim
name: testpod
resources: {}
command: ["/bin/sh", "-c"]
args:
- tail -f /dev/null
status: {}

2、登录进入pod中

1
kubectl exec -it testpod -- /bin/bash

3、测试外网速度

1
2
3
4
5
apt update
apt install -y wget python3

wget https://raw.github.com/sivel/speedtest-cli/master/speedtest.py
python3 speedtest.py

4、测试内网速度
创建两个pod,其中一个启动 iperf server,另外一个启动 iperf client
(1)pod1启动iperf server:

1
2
apt install -y iperf
iperf -s

(2)pod2启动iperf client:

1
2
apt install -y iperf
iperf -c $POD1_IP -i 3

5. 后记

实际测试发现,Pod中配置注解的方式确实可以实现限制带宽,测速时的 带宽量级 会随着 限速量级 的变化而变化。
但是,限速效果不太好,测出的 带宽数值 与设置的 限速数值 有较大差距,并且多次测试结果波动很大。
整体来说,Pod中配置注解的方式可以限速,不可以精确限速。