容器服务里限制应用的带宽

容器场景下,一个标准的需求就是限制容器能够使用的最大资源,在Kubernetes里,我们可以很方便的设置一个容器能使用的最大内存,也能限制容器能分配到的CPU。然而容器使用的资源不光CPU和内存,网络带宽也是很重要的资源,设想某个应用因为代码缺陷把整个节点的带宽打满,必然影响同一台机器上其他的其他服务,造成服务的不可用。本文将介绍一种方式,能够限制Kubernetes集群里一个容器能够使用的最大带宽。

前置条件

本文所有的示例都在阿里云容器服务上验证通过。要运行本文中的示例,首先,你需要一个Kubernetes集群,推荐使用阿里云容器服务,你可以通过阿里云容器服务的控制台一键创建Kubernetes集群。如果你已经有Kubernetes,也可以直接使用。

部署kube-tc

kube-tc是阿里云容器服务团队提供的Kubernetes下的流控组件,通过kubectl命令就可以一键部署到Kubernetes集群

注意:

  • 部署之前,请先在集群下所有Kubernetes节点上执行modprobe sch_htb ,确保每个节点都要执行
  • 如果您用的网络插件不是flannel,请将编排文件的 args: ["-interface", "eth0", "-network", "flannel"]改成 args: ["-interface", "eth0"]
kubectl apply -f http://acs-public.oss-cn-hangzhou.aliyuncs.com/kubernetes/network/kube-tc.yml

部署之后稍等一会,运行kubectl get pod -l 'app=kube-tc',当Pod都变成了running,就说明部署成功了。

限制应用带宽

要限制一个应用的带宽,只要简单在Pod上加一个annotation,声明分配给Pod的带宽就可以了。下面的编排代码中,限制了Pod 5M的入网带宽和10M的出网带宽。

apiVersion: v1
kind: Pod
metadata:
  name: centos
  annotations:
    kubernetes.io/ingress-bandwidth: 5M  #进入到容器的最大带宽
    kubernetes.io/egress-bandwidth: 10M   #容器向外访问的最大带宽
spec:
  nodeSelector:
    role: test
  containers:
  - name: centos
    image: registry.cn-hangzhou.aliyuncs.com/acs-sample/centos:7
    tty: true

来看看效果,把上面的编排保存的centos.yml里,执行kubectl apply -f centos.yml,等一段时间直到centos处于running状态。

# kubectl get po/centos -o wide
NAME      READY     STATUS    RESTARTS   AGE       IP            NODE
centos    1/1       Running   0          3m        172.16.3.21   cn-beijing.i-2zeggd58nxvl1w3235pl

此时,centos这个pod已经在运行了,ip地址是172.16.3.21,接下来我们登录到同一个Kubernetes集群的任意节点上,用iperf3测试下centos的带宽。我的例子中,Kubernetes节点是192.168.177.86

先测试入网流量,执行kubectl exec centos -it -- bash,进入centos中,再执行iperf3 -s,接着在192.168.177.86上执行iperf -c 172.16.3.21

kubectl exec centos -it -- bash
[root@centos /]# iperf3 -s
-----------------------------------------------------------
Server listening on 5201
-----------------------------------------------------------
Accepted connection from 192.168.177.86, port 47034
[  5] local 172.16.3.21 port 5201 connected to 192.168.177.86 port 47036
[ ID] Interval           Transfer     Bandwidth
[  5]   0.00-1.00   sec  4.40 MBytes  36.9 Mbits/sec
[  5]   1.00-2.00   sec  4.56 MBytes  38.2 Mbits/sec
[  5]   2.00-3.00   sec  4.56 MBytes  38.3 Mbits/sec
[  5]   3.00-4.00   sec  4.56 MBytes  38.3 Mbits/sec
[  5]   4.00-5.00   sec  4.56 MBytes  38.2 Mbits/sec
[  5]   5.00-6.00   sec  4.56 MBytes  38.3 Mbits/sec
[  5]   6.00-7.00   sec  4.56 MBytes  38.3 Mbits/sec
[  5]   7.00-8.00   sec  4.57 MBytes  38.4 Mbits/sec
[  5]   8.00-9.00   sec  4.55 MBytes  38.2 Mbits/sec
[  5]   9.00-10.00  sec  4.57 MBytes  38.3 Mbits/sec
[  5]  10.00-10.36  sec  1.62 MBytes  37.9 Mbits/sec
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval           Transfer     Bandwidth
[  5]   0.00-10.36  sec  0.00 Bytes  0.00 bits/sec                  sender
[  5]   0.00-10.36  sec  47.1 MBytes  38.1 Mbits/sec                  receiver

可以看到带宽的限制已经生效了,不过不是严格的5M,原因和流控策略和iperf3本身发包方式有关,这里就不深入探讨了。

再看看出口带宽。这次把centos作为iperf3的client。192.168.177.86上执行iperf3 -s,centos里执行iperf3 -c 192.168.177.86

-----------------------------------------------------------
Server listening on 5201
-----------------------------------------------------------
Accepted connection from 172.16.3.21, port 47560
[  5] local 192.168.177.86 port 5201 connected to 172.16.3.21 port 47562
[ ID] Interval           Transfer     Bandwidth
[  5]   0.00-1.00   sec  8.78 MBytes  73.7 Mbits/sec
[  5]   1.00-2.00   sec  9.08 MBytes  76.1 Mbits/sec
[  5]   2.00-3.00   sec  9.12 MBytes  76.5 Mbits/sec
[  5]   3.00-4.00   sec  9.12 MBytes  76.5 Mbits/sec
[  5]   4.00-5.00   sec  9.12 MBytes  76.5 Mbits/sec
[  5]   5.00-6.00   sec  9.12 MBytes  76.5 Mbits/sec
[  5]   6.00-7.00   sec  9.12 MBytes  76.5 Mbits/sec
[  5]   7.00-8.00   sec  9.13 MBytes  76.6 Mbits/sec
[  5]   8.00-9.00   sec  9.12 MBytes  76.5 Mbits/sec
[  5]   9.00-10.00  sec  9.12 MBytes  76.5 Mbits/sec
[  5]  10.00-10.32  sec  2.89 MBytes  76.2 Mbits/sec
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval           Transfer     Bandwidth
[  5]   0.00-10.32  sec  0.00 Bytes  0.00 bits/sec                  sender
[  5]   0.00-10.32  sec  93.7 MBytes  76.2 Mbits/sec                  receiver
-----------------------------------------------------------
Server listening on 5201
-----------------------------------------------------------

看上去很不错,带宽已经限制在10M。

上一篇:wdcp-apache配置错误导致进程淤积进而内存吃紧


下一篇:C#解析json文件的方法