最近接到一个需求,对日志上传的pod进行流量限制。

# 前期准备k8s一份

calico装好

# k8s配置

由于默认情况下calico并没有启用流量限制的功能,所以需要在calico的配置文件里面启用一下。在每个节点的/etc/cni/net.d/10-calico.conflist 文件中加入bandwidth的支持。这一步最好在安装calico的时候做了,就不用每个节点都配置一遍。期待calico把这个选项也默认打开的一天(https://github.com/projectcalico/calico/issues/2815)。因为是实验环境,我只有一个节点,就手动配置了。

vagrant@ubuntu:~$ cat /etc/cni/net.d/10-calico.conflist

{

"name": "k8s-pod-network",

"cniVersion": "0.3.1",

"plugins": [

{

"type": "calico",

"datastore_type": "kubernetes",

"mtu": 1410,

"nodename_file_optional": false,

"log_file_path": "/var/log/calico/cni/cni.log",

"ipam": {

"type": "calico-ipam",

"assign_ipv4" : "true",

"assign_ipv6" : "false"

},

"container_settings": {

"allow_ip_forwarding": false

},

"policy": {

"type": "k8s"

},

"kubernetes": {

"kubeconfig": "/etc/cni/net.d/calico-kubeconfig"

}

},

{"type": "portmap", "snat": true, "capabilities": {"portMappings": true}},

{

"type": "bandwidth",

"capabilities": {"bandwidth": true}

}

]

}

#流量限制的原理

其实原理很简单,就是在pod启动的时候,在对应的虚拟网络设备加上相应的tc规则,通过tc实现的流量限制。不懂TC的可以参阅https://cloud.tencent.com/developer/article/1409664 等资料。

#实验一

没有流量限制的情况下两个pod之间。

通过下面的yaml启动两个pod。

---apiVersion:v1kind:Podmetadata:name:perf1labels:app:perf1# annotations:# kubernetes.io/ingress-bandwidth: 10M# kubernetes.io/egress-bandwidth: 10Mspec:containers:- name:perf-serverimage:elynn/pperf:latestimagePullPolicy:Alwayscommand:- "/opt/runserver.sh"ports:- containerPort:5201- containerPort:5203---apiVersion:v1kind:Podmetadata:name:perf2labels:app:perf2# annotations:# kubernetes.io/ingress-bandwidth: 1M# kubernetes.io/egress-bandwidth: 1Mspec:containers:- name:perf-serverimage:elynn/pperf:latestimagePullPolicy:Alwayscommand:- "/opt/runserver.sh"ports:- containerPort:5201- containerPort:5203

pod启动完之后,我们先看看对应节点的tc规则,并没有找到任何的流量限制。

$ tc qdisc show

qdisc noqueue 0: dev lo root refcnt 2

qdisc pfifo_fast 0: dev ens33 root refcnt 2 bands 3 priomap 1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1

qdisc noqueue 0: dev docker0 root refcnt 2

qdisc noqueue 0: dev cali51d8b092aa9 root refcnt 2

qdisc noqueue 0: dev cali40347405ff0 root refcnt 2

qdisc noqueue 0: dev vxlan.calico root refcnt 2

qdisc noqueue 0: dev cali3fe07939e27 root refcnt 2

qdisc noqueue 0: dev califde8991e611 root refcnt 2

qdisc noqueue 0: dev calieec63a8d445 root refcnt 2

到pod里面跑个iperf试试,单机转发30Gbits的流量。

$ kubectl exec -it perf1 bash

kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.

root@perf1:/opt# iperf3 -c 192.168.243.203

Connecting to host 192.168.243.203, port 5201

[ 4] local 192.168.243.202 port 57728 connected to 192.168.243.203 port 5201

[ ID] Interval Transfer Bandwidth Retr Cwnd

[ 4] 0.00-1.00 sec 3.62 GBytes 31.1 Gbits/sec 283 893 KBytes

[ 4] 1.00-2.00 sec 3.64 GBytes 31.2 Gbits/sec 0 896 KBytes

[ 4] 2.00-3.00 sec 3.57 GBytes 30.7 Gbits/sec 0 1.38 MBytes

[ 4] 3.00-4.00 sec 3.61 GBytes 31.0 Gbits/sec 0 1.38 MBytes

[ 4] 4.00-5.00 sec 3.55 GBytes 30.5 Gbits/sec 0 1.38 MBytes

[ 4] 5.00-6.00 sec 3.64 GBytes 31.2 Gbits/sec 0 1.39 MBytes

[ 4] 6.00-7.00 sec 3.55 GBytes 30.5 Gbits/sec 0 1.39 MBytes

[ 4] 7.00-8.00 sec 3.59 GBytes 30.8 Gbits/sec 0 1.39 MBytes

[ 4] 8.00-9.00 sec 3.50 GBytes 30.1 Gbits/sec 0 1.49 MBytes

^C[ 4] 9.00-9.46 sec 1.67 GBytes 31.4 Gbits/sec 0 1.52 MBytes

- - - - - - - - - - - - - - - - - - - - - - - - -

[ ID] Interval Transfer Bandwidth Retr

[ 4] 0.00-9.46 sec 33.9 GBytes 30.8 Gbits/sec 283 sender

[ 4] 0.00-9.46 sec 0.00 Bytes 0.00 bits/sec receiver

#实验二

试试把流量控制加上。把实验一中的yaml文件的下列内容加上,就表示限制这个pod的进出流量都是10M。

annotations:kubernetes.io/ingress-bandwidth:10Mkubernetes.io/egress-bandwidth:10M

把之前的pod删了,再创建一下。创建成功之后就能看到tc规则加上了。

```

vagrant@ubuntu:~$ tc qdisc show

qdisc noqueue 0: dev lo root refcnt 2

qdisc pfifo_fast 0: dev ens33 root refcnt 2 bands 3 priomap 1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1

qdisc noqueue 0: dev docker0 root refcnt 2

qdisc noqueue 0: dev cali51d8b092aa9 root refcnt 2

qdisc noqueue 0: dev cali40347405ff0 root refcnt 2

qdisc noqueue 0: dev vxlan.calico root refcnt 2

qdisc noqueue 0: dev cali3fe07939e27 root refcnt 2

qdisc noqueue 0: dev calieec63a8d445 root refcnt 2

qdisc tbf 1: dev califde8991e611 root refcnt 2 rate 10Mbit burst 256Mb lat 25.0ms

qdisc ingress ffff: dev califde8991e611 parent ffff:fff1 ----------------

qdisc tbf 1: dev bwp9524c730aa56 root refcnt 2 rate 10Mbit burst 256Mb lat 25.0ms

```

跑一把流量测试看看,就能看到流量被限制到了10Mbits/s。

vagrant@ubuntu:~$ kubectl exec -it perf1 bash

kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.

root@perf1:/opt# iperf3 -c 192.168.243.204

Connecting to host 192.168.243.204, port 5201

[ 4] local 192.168.243.205 port 35668 connected to 192.168.243.204 port 5201

[ ID] Interval Transfer Bandwidth Retr Cwnd

[ 4] 0.00-1.00 sec 246 MBytes 2.06 Gbits/sec 4 263 KBytes

[ 4] 1.00-2.00 sec 1.12 MBytes 9.39 Mbits/sec 0 263 KBytes

[ 4] 2.00-3.00 sec 1.12 MBytes 9.39 Mbits/sec 0 263 KBytes

[ 4] 3.00-4.00 sec 1.18 MBytes 9.91 Mbits/sec 0 263 KBytes

[ 4] 4.00-5.00 sec 1.12 MBytes 9.39 Mbits/sec 0 263 KBytes

[ 4] 5.00-6.00 sec 1.12 MBytes 9.39 Mbits/sec 0 263 KBytes

[ 4] 6.00-7.00 sec 1.18 MBytes 9.91 Mbits/sec 0 263 KBytes

[ 4] 7.00-8.00 sec 1.12 MBytes 9.39 Mbits/sec 0 263 KBytes

[ 4] 8.00-9.00 sec 1.12 MBytes 9.39 Mbits/sec 0 263 KBytes

^C[ 4] 9.00-10.00 sec 955 KBytes 7.86 Mbits/sec 0 264 KBytes

- - - - - - - - - - - - - - - - - - - - - - - - -

[ ID] Interval Transfer Bandwidth Retr

[ 4] 0.00-10.00 sec 256 MBytes 215 Mbits/sec 4 sender

[ 4] 0.00-10.00 sec 0.00 Bytes 0.00 bits/sec receiver

iperf3: interrupt - the client has terminated

#使用限制

目前发现有一下的一些限制:如果不是docker而是使用containerd作为runtime,需要containerd 1.4版本才能支持。

不能动态更新annotation里面的流量限制大小,更新之后必须删除pod重建。

k8s 查看pod流量_K8s中对pod进行流量限制相关推荐

  1. k8s 查看ip地址属于哪个pod_Kubernetes Pod 如何获取 IP 地址

    [编者的话]在学习 Kubernetes 网络模型的过程中,了解各种网络组件的作用以及如何交互非常重要.本文就介绍了各种网络组件在 Kubernetes 集群中是如何交互的,以及如何帮助每个 Pod ...

  2. k8s 查看mysql 日志_k8s 使用 Init Container 确保依赖的服务已经启动

    k8s 使用 Init Container 确保依赖的服务已经启动 Intro 最近 helm 3 正式发布了,dotnetcore 3.1 也正式发布了,最近打算把我的活动室预约项目做一个升级,项目 ...

  3. k8s 查看pod流量_Kubernetes K8S之Pod生命周期与探针检测

    K8S中Pod的生命周期与ExecAction.TCPSocketAction和HTTPGetAction探针检测 主机配置规划 Pod容器生命周期 Pause容器说明 每个Pod里运行着一个特殊的被 ...

  4. k8s查看pod的yaml文件_K8S系列学习,Pod实战那些事儿,有必要知道知道

    Kubernetes学习路上的那些事儿,很有必要分享出来 什么是Pod 要弄清楚这个概念,我们还是先上官网瞧瞧,https://kubernetes.io/zh/docs/concepts/workl ...

  5. k8s查看pod的yaml文件_每天5分钟|轻松掌握开发工作中必会的k8s-yaml配置说明和常用命令...

    在 K8S 中,创建资源有两种方式: 方式一:使用kubectl 命令行,在输入kubectl命令的时候,填上对应的参数 优点:方便快捷,一行命令搞定 缺点:一行命令太多参数,可读性差,不利于维护 方 ...

  6. K8S中的pod自动扩容与缩容

    k8s应用自动扩容缩容方案 原因: 在实际的业务场景中,我们经常会遇到某个服务需要扩容的场景(例如:测试对服务压测.电商平台秒杀.大促活动.或由于资源紧张.工作负载降低等都需要对服务实例数进行扩缩容操 ...

  7. k8s 查看 pod 网段

    文章目录 k8s 查看 pod 网段 k8s 查看 pod 网段 查看当前k8s集群中 pod 和 service 网段信息 [root@master ~]# kubectl get configma ...

  8. K8S中的pod、services、容器的概念和区别

    K8S中的pod.services.容器的概念和区别 k8s的部署架构 kubernetes中有两类资源,分别是master和nodes,master和nodes上跑的服务如下图: 1 kube-ap ...

  9. k8s查看pod的yaml文件_K8s-yaml的使用及命令

    YAML配置文件管理对象 对象管理: # 创建deployment资源 kubectl create-f nginx-deployment.yaml # 查看deployment kubectl ge ...

最新文章

  1. 代码规范+设计模式落地之路
  2. 阿里飞猪搜索技术的应用与创新
  3. 全民自动驾驶5年内真的会来吗?这是Lyft的自动驾驶2.0
  4. mvn 打包项目到eclipse
  5. 1.1操作系统的概念或功能
  6. java 抽象工厂工厂_java之抽象工厂
  7. nmap入门之主机发现
  8. intellij idea 简单使用教程
  9. wcf中如何Host多个WCF服务?
  10. 海思3518C 4G模块移植及运用
  11. Vue学习--黑马电商管理系统
  12. 使用Python在Windows环境下切换输入法
  13. 杭州银行2018信息科技部面试
  14. 个人项目-数独(Python实现)——从解数独到写游戏
  15. 揭秘交通大脑——BATJ技术争夺战,滴滴与高德的AI新版图
  16. party_bid_core三种数据结构分析
  17. Filezilla使用教程
  18. 生成二维码的三种方式
  19. 阅读开源Python脚本学习笔记1,之python异常
  20. 游戏行业的发展前景有什么看法

热门文章

  1. 一个男人最高的品味是他选择的女人
  2. ae2020不支持的视频驱动程序_AE 2020版本安装提示系统兼容性报告怎么解决?
  3. 初识node.js(1)
  4. Java程序员常用开发工具
  5. 数字图像处理篇(7)角点检测
  6. 多易教育KAFKA实战(1)-KAFKA集群安装和shell客户端
  7. css 文字溢出...显示,hover时显示隐藏文字
  8. 一.僵死进程(僵尸进程)
  9. ErrMsg:server is DOWN now, please try again later!
  10. ubuntu下常用软件下载安装