docker高级应用之网络资源限制

简介:

最近我这里docker单机平台正式上线使用,使用中有很多问题都一样解决,在给一个游戏项目做测试的时候,此项目由于8080端口对公网全部开放,并且安全策略没有做好(默认的tomcat模板没有删除),导致被人进行webshell,跑了很多的流量,为了解决此问题我针对tc与openvswitch本身的qos做了深入研究,最后选择openvswitch的qos作为容器的网络资源限制方法。

docker本身仅能对容器的cpu、内存做限制,而且必须是在容器运行前做,运行过程中未发现如何动态修改,并且不提供网络资源限制,所以只能使用其他软件做了。

我的docker网络没有使用默认bridge,使用none,然后绑定openvswitch的bridge,并使用pipework提供网络,所以我可以根据容器对于openvswitch的port来进行基于port的网络资源限制,好处是可以动态的修改,坏处是容器一重启还得重新做,但也可以通过其他方法来解决。

一、下面是我做测试的结果:

wKioL1VGzcjxKeybAAFeP1FeJcw975.jpg

对于限速100m以下,其实硬盘的类型与读写速度没什么影响,但如何限速150m以上,或者无限速,那么硬盘肯定是ssd>sas>sata,所以建议进来使用sas的磁盘作为docker的挂载分区。

openvswitch默认官方文档提供的限速方法“rate limiting vm traffic using qos policing",地址是http://openvswitch.org/support/config-cookbooks/qos-rate-limiting/,此限速仅能对上传做限速,下载没有办法,所以还得通过其他的qos来做限制。

二、下面是具体限速脚本

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
36
37
38
#!/bin/bash
#filename:modify_docker_container_network_limit.sh
#author:Deng Lei
#email:dl528888@gmail.com
op =$1
container=$2
limit=$3   # Mbits/s
if  [ -z $1 ] || [ -z $2 ];  then
     echo  "Usage: operation container_name limit(default:5m)"
     echo  "Example1: I want limit 5m in the container:test"
     echo  "The command is: bash `basename $0` limit test 5"
     echo  "Example2: I want delete network limit in the container:test"
     echo  "The command is: bash `basename $0` ulimit test"
     exit  1
fi
if  [ -z $3 ]; then
     limit= '5m'
fi
if  [ `docker inspect -- format  "``.`State`.`Pid`"  $container &>> /dev/null  &&  echo  0 ||  echo  1` - eq  1 ]; then
echo  "no this container:$container"
exit  1
fi
ovs_prefix= 'veth1pl'
container_id=`docker inspect -- format  "``.`State`.`Pid`"  $container`
device_name=` echo  ${ovs_prefix}${container_id}`
if  [ $ op  ==  'limit'  ]; then
for  v  in  $device_name;  do
     ovs-vsctl  set  interface $ v  ingress_policing_rate=$((limit*1000))
     ovs-vsctl  set  interface $ v  ingress_policing_burst=$((limit*100))
     ovs-vsctl  set  port $ v  qos=@newqos -- -- id =@newqos create qos  type =linux-htb queues=0=@q0 other-config:max-rate=$((limit*1000000)) -- -- id =@q0 create queue other-config:min-rate=$((limit*1000000)) other-config:max-rate=$((limit*1000000)) &>> /dev/null  &&  echo  'modify success!'  ||  echo  'modify fail!'
done
elif  [ $ op  ==  'ulimit'  ]; then
for  v  in  $device_name;  do
     ovs-vsctl  set  interface $ v  ingress_policing_rate=0
     ovs-vsctl  set  interface $ v  ingress_policing_burst=0
     ovs-vsctl  clear  Port $ v  qos &>> /dev/null  &&  echo  'modify success!'  ||  echo  'modify fail!'
done
fi

此脚本使用的话,限速可以直接针对下载与上传,并且限制是统一生效的,比如我限制一个容器带宽为5m,那么下载与上传的限速都是5m,单位是bit不是byte。

三、下面是使用方法:

1
2
3
4
5
6
[root@docker-test3 tmp] # sh modify_docker_container_network_limit.sh
Usage: operation container_name limit(default:5m)
Example1: I want limit 5m  in  the container: test
The  command  is:  bash  modify_docker_container_network_limit.sh limit  test  5
Example2: I want delete network limit  in  the container: test
The  command  is:  bash  modify_docker_container_network_limit.sh  ulimit  test

四、下面是测试过程:

测试的方法是:

找另外一个主机172.16.1.126,然后dd生成个100m的文件/tmp/test_client.iso,在本机下载这个文件来测试下载速度,在本机dd生成100m的文件/tmp/test_server.iso把此文件上传到172.16.1.126里测试上传速度。

sata 7.5k

1、没有限制的情况

下载速度

1
2
3
4
5
6
7
8
14:12:18  # rsync -avz --progress 172.16.1.126:/tmp/test_client.iso/tmp/
root@172.16.1.126's password:
receiving incremental  file  list
test_client.iso
    104857600 100%   56.82MB /s    0:00:01 (xfer #1, to-check=0/1)
  
sent 30 bytes  received104892888 bytes  1226817.75 bytes /sec
total size is 104857600 speedup is 1.00

上传速度

1
2
3
4
5
6
7
8
14:14:27  # rsync -avz --progress /tmp/test_server.iso172.16.1.126:/tmp/
root@172.16.1.126's password:
sending incremental  file  list
test_server.iso
    104857600 100%   51.08MB /s    0:00:01 (xfer #1, to-check=0/1)
  
sent 104892883 bytes  received31 bytes  29969404.00 bytes /sec
total size is 104857600 speedup is 1.00

2、限速5m的

下载速度

1
2
3
4
5
6
7
8
14:15:27  # rsync -avz --progress 172.16.1.126:/tmp/test_client.iso/tmp/
root@172.16.1.126's password:
receiving incremental  file  list
test_client.iso
    104857600 100%  580.46kB /s    0:02:56 (xfer #1, to-check=0/1)
  
sent 30 bytes  received104892888 bytes  590946.02 bytes /sec
total size is 104857600 speedup is 1.00

上传速度

1
2
3
4
5
6
7
8
14:22:10  # rsync -avz --progress /tmp/test_server.iso172.16.1.126:/tmp/
root@172.16.1.126's password:
sending incremental  file  list
test_server.iso
    104857600 100%  616.19kB /s    0:02:46 (xfer #1, to-check=0/1)
  
sent 104892883 bytes  received31 bytes  571623.51 bytes /sec
total size is 104857600 speedup is 1.00

3、限速为10m

下载速度

1
2
3
4
5
6
7
8
9
14:28:55  # rsync -avz --progress 172.16.1.126:/tmp/test_client.iso/tmp/
root@172.16.1.126's password:
receiving incremental  file  list
test_client.iso
    104857600 100%    1.13MB /s    0:01:28 (xfer #1, to-check=0/1)
  
sent 30 bytes  received 104892888bytes  1109977.97 bytes /sec
total size is 104857600 speedup is 1.00
root@fdc81b0d2508: /tmp

上传速度

1
2
3
4
5
6
7
8
14:30:33  # rsync -avz --progress /tmp/test_server.iso172.16.1.126:/tmp/
root@172.16.1.126's password:
sending incremental  file  list
test_server.iso
    104857600 100%    1.21MB /s    0:01:22 (xfer #1, to-check=0/1)
  
sent 104892883 bytes  received31 bytes  1133977.45 bytes /sec
total size is 104857600 speedup is 1.00

4、限速为20m

下载速度

1
2
3
4
5
6
7
8
14:32:57  # rsync -avz --progress 172.16.1.126:/tmp/test_client.iso/tmp/
root@172.16.1.126's password:
receiving incremental  file  list
test_client.iso
    104857600 100%    2.27MB /s    0:00:44 (xfer #1, to-check=0/1)
  
sent 30 bytes  received104892888 bytes  2305338.86 bytes /sec
total size is 104857600 speedup is 1.00

上传速度

1
2
3
4
5
6
7
8
14:33:59  # rsync -avz --progress /tmp/test_server.iso172.16.1.126:/tmp/
root@172.16.1.126's password:
sending incremental  file  list
test_server.iso
    104857600 100%    2.45MB /s    0:00:40 (xfer #1, to-check=0/1)
  
sent 104892883 bytes  received31 bytes  2305338.77 bytes /sec
total size is 104857600 speedup is 1.00

5、限速为50m

下载速度

1
2
3
4
5
6
7
8
14:35:20  # rsync -avz --progress 172.16.1.126:/tmp/test_client.iso/tmp/
root@172.16.1.126's password:
receiving incremental  file  list
test_client.iso
    104857600 100%    5.67MB /s    0:00:17 (xfer #1, to-check=0/1)
  
sent 30 bytes  received 104892888bytes  5379124.00 bytes /sec
total size is 104857600 speedup is 1.00

上传速度

1
2
3
4
5
6
7
8
14:35:54  # rsync -avz --progress /tmp/test_server.iso172.16.1.126:/tmp/
root@172.16.1.126's password:
sending incremental  file  list
test_server.iso
    104857600 100%    6.33MB /s    0:00:15 (xfer #1, to-check=0/1)
  
sent 104892883 bytes  received31 bytes  5116727.51 bytes /sec
total size is 104857600 speedup is 1.00

6、限速100m

下载速度

1
2
3
4
5
6
7
8
14:37:18  # rsync -avz --progress 172.16.1.126:/tmp/test_client.iso/tmp/
root@172.16.1.126's password:
receiving incremental  file  list
test_client.iso
    104857600 100%   11.35MB /s    0:00:08 (xfer #1, to-check=0/1)
  
sent 30 bytes  received104892888 bytes  8391433.44 bytes /sec
total size is 104857600 speedup is 1.00

上传速度

1
2
3
4
5
6
7
8
14:37:39  # rsync -avz --progress /tmp/test_server.iso172.16.1.126:/tmp/
root@172.16.1.126's password:
sending incremental  file  list
test_server.iso
    104857600 100%   13.19MB /s    0:00:07 (xfer #1, to-check=0/1)
  
sent 104892883 bytes  received31 bytes  11041359.37 bytes /sec
total size is 104857600 speedup is 1.00

7、限速150m

下载速度

1
2
3
4
5
6
7
8
14:38:39  # rsync -avz --progress 172.16.1.126:/tmp/test_client.iso/tmp/
root@172.16.1.126's password:
receiving incremental  file  list
test_client.iso
    104857600 100%   11.35MB /s    0:00:08 (xfer #1, to-check=0/1)
  
sent 30 bytes  received 104892888bytes  11041359.79 bytes /sec
total size is 104857600  speedup is 1.00

上传速度

1
2
3
4
5
6
7
8
14:38:56  # rsync -avz --progress /tmp/test_server.iso172.16.1.126:/tmp/
root@172.16.1.126's password:
sending incremental  file  list
test_server.iso
    104857600 100%   13.14MB /s     0:00:07 (xfer #1, to-check=0/1)
  
sent 104892883 bytes  received31 bytes  11041359.37 bytes /sec
total size is 104857600 speedup is 1.00

下面是使用sas 7.5k硬盘的测试结果

8、无限速的

下载速度

1
2
3
4
5
6
7
8
17:11:07  # rsync -avz --progress172.16.1.53:/tmp/test_client.iso /tmp/
root@172.16.1.53's password:
receiving incremental  file  list
test_client.iso
  2147483648 100%  106.42MB /s     0:00:19 (xfer #1, to-check=0/1)
  
sent 30 bytes  received 2148204633 bytes  95475762.80 bytes /sec
total size is 2147483648  speedup is 1.00

上传速度

1
2
3
4
5
6
7
8
17:20:06  # rsync -avz --progress/tmp/test_server.iso 172.16.1.53:/tmp/
root@172.16.1.53's password:
sending incremental  file  list
test_server.iso
  2147483648 100%  112.51MB /s     0:00:18 (xfer #1, to-check=0/1)
  
sent 2148204628 bytes  received 31 bytes  95475762.62 bytes /sec
total size is 2147483648  speedup is 1.00

9、5m限速

下载速度

1
2
3
4
5
6
7
8
17:30:30  # rsync -avz --progress172.16.1.53:/tmp/test_client.iso /tmp/
root@172.16.1.53's password:
receiving incremental  file  list
test_client.iso
   104857600 100%  591.44kB /s     0:02:53 (xfer #1, to-check=0/1)
  
sent 30 bytes  received 104892888 bytes  587635.39 bytes /sec
total size is 104857600  speedup is 1.00

上传速度

1
2
3
4
5
6
7
8
17:38:57  # rsync -avz --progress/tmp/test_server.iso 172.16.1.53:/tmp/
root@172.16.1.53's password:
sending incremental  file  list
test_server.iso
    104857600 100%  590.35kB /s    0:02:53 (xfer #1, to-check=0/1)
  
sent 104892883 bytes  received 31 bytes  574755.69 bytes /sec
total size is 104857600  speedup is 1.00

10、限速10m

下载速度

1
2
3
4
5
6
7
8
17:42:54  # rsync -avz --progress172.16.1.53:/tmp/test_client.iso /tmp/
root@172.16.1.53's password:
receiving incremental  file  list
test_client.iso
   104857600 100%    1.15MB /s     0:01:26 (xfer #1, to-check=0/1)
  
sent 30 bytes  received 104892888 bytes  1146370.69 bytes /sec
total size is 104857600  speedup is 1.00

上传速度

1
2
3
4
5
6
7
8
17:44:31  # rsync -avz --progress/tmp/test_server.iso 172.16.1.53:/tmp/
root@172.16.1.53's password:
sending incremental  file  list
test_server.iso
   104857600 100%    1.16MB /s     0:01:26 (xfer #1, to-check=0/1)
  
sent 104892883 bytes  received 31 bytes  1146370.64 bytes /sec
total size is 104857600  speedup is 1.00

11、限速20m

下载速度

1
2
3
4
5
6
7
8
17:47:02  # rsync -avz --progress172.16.1.53:/tmp/test_client.iso /tmp/
root@172.16.1.53's password:
receiving incremental  file  list
test_client.iso
   104857600 100%    2.32MB /s     0:00:43 (xfer #1, to-check=0/1)
  
sent 30 bytes  received 104892888 bytes  2162740.58 bytes /sec
total size is 104857600  speedup is 1.00

上传速度

1
2
3
4
5
6
7
8
17:48:06  # rsync -avz --progress/tmp/test_server.iso 172.16.1.53:/tmp/
root@172.16.1.53's password:
sending incremental  file  list
test_server.iso
   104857600 100%    2.38MB /s     0:00:42 (xfer #1, to-check=0/1)
  
sent 104892883 bytes  received 31 bytes  2255761.59 bytes /sec
total size is 104857600  speedup is 1.00

12、限速50m

下载速度

1
2
3
4
5
6
7
8
17:52:52  # rsync -avz --progress172.16.1.53:/tmp/test_client.iso /tmp/
root@172.16.1.53's password:
receiving incremental  file  list
test_client.iso
   104857600 100%    5.84MB /s     0:00:17 (xfer #1, to-check=0/1)
  
sent 30 bytes  received 104892888 bytes  5116727.71 bytes /sec
total size is 104857600  speedup is 1.00

上传速度

1
2
3
4
5
6
7
8
17:53:15  # rsync -avz --progress/tmp/test_server.iso 172.16.1.53:/tmp/
root@172.16.1.53's password:
sending incremental  file  list
test_server.iso
   104857600 100%    6.10MB /s     0:00:16 (xfer #1, to-check=0/1)
  
sent 104892883 bytes  received 31 bytes  5993880.80 bytes /sec
total size is 104857600  speedup is 1.00

13、限速为100m

下载速度

1
2
3
4
5
6
7
8
17:55:16  # rsync -avz --progress172.16.1.53:/tmp/test_client.iso /tmp/
root@172.16.1.53's password:
receiving incremental  file  list
test_client.iso
   104857600 100%   11.75MB /s     0:00:08 (xfer #1, to-check=0/1)
  
sent 30 bytes  received 104892888 bytes  9989801.71 bytes /sec
total size is 104857600  speedup is 1.00

上传速度

1
2
3
4
5
6
7
8
17:55:39  # rsync -avz --progress/tmp/test_server.iso 172.16.1.53:/tmp/
root@172.16.1.53's password:
sending incremental  file  list
test_server.iso
   104857600 100%   12.72MB /s     0:00:07 (xfer #1, to-check=0/1)
  
sent 104892883 bytes  received 31 bytes  11041359.37 bytes /sec
total size is 104857600  speedup is 1.00

14、限速150m

下载速度

1
2
3
4
5
6
7
8
17:56:58  # rsync -avz --progress 172.16.1.53:/tmp/test_client.iso/tmp/
root@172.16.1.53's password:
receiving incremental  file  list
test_client.iso
   104857600 100%   17.69MB /s     0:00:05 (xfer #1, to-check=0/1)
  
sent 30 bytes  received 104892888 bytes  13985722.40 bytes /sec
total size is 104857600  speedup is 1.00

上传速度

1
2
3
4
5
6
7
8
17:57:18  # rsync -avz --progress/tmp/test_server.iso 172.16.1.53:/tmp/
root@172.16.1.53's password:
sending incremental  file  list
test_server.iso
   104857600 100%   20.44MB /s     0:00:04 (xfer #1, to-check=0/1)
  
sent 104892883 bytes  received 31 bytes  16137371.38 bytes /sec
total size is 104857600  speedup is 1.00

如果大家有问题可以留言给我,我会及时回复。





 本文转自 reinxu 51CTO博客,原文链接:http://blog.51cto.com/dl528888/1641569,如需转载请自行联系原作者

相关文章
|
3月前
|
Kubernetes Docker Python
Docker 与 Kubernetes 容器化部署核心技术及企业级应用实践全方案解析
本文详解Docker与Kubernetes容器化技术,涵盖概念原理、环境搭建、镜像构建、应用部署及监控扩展,助你掌握企业级容器化方案,提升应用开发与运维效率。
700 108
|
1月前
|
监控 Kubernetes 安全
还没搞懂Docker? Docker容器技术实战指南 ! 从入门到企业级应用 !
蒋星熠Jaxonic,技术探索者,以代码为笔,在二进制星河中书写极客诗篇。专注Docker与容器化实践,分享从入门到企业级应用的深度经验,助力开发者乘风破浪,驶向云原生新世界。
还没搞懂Docker? Docker容器技术实战指南 ! 从入门到企业级应用 !
|
2月前
|
前端开发 JavaScript 应用服务中间件
在Docker部署的前端应用中使用动态环境变量
以上步骤展示了如何在 Docker 配置过程中处理并注入环墨遁形成可执行操作流程,并确保最终用户能够无缝地与之交互而无须关心背后复杂性。
160 13
|
4月前
|
存储 监控 Java
如何对迁移到Docker容器中的应用进行性能优化?
如何对迁移到Docker容器中的应用进行性能优化?
309 59
|
6月前
|
Prometheus 监控 Cloud Native
除了Prometheus,还有哪些工具可以监控Docker Swarm集群的资源使用情况?
除了Prometheus,还有哪些工具可以监控Docker Swarm集群的资源使用情况?
514 79
|
7月前
|
监控 Java Go
无感改造,完美监控:Docker 多阶段构建 Go 应用无侵入观测
本文将介绍一种基于 Docker 多阶段构建的无侵入 Golang 应用观测方法,通过此方法用户无需对 Golang 应用源代码或者编译指令做任何改造,即可零成本为 Golang 应用注入可观测能力。
381 85
|
5月前
|
Docker 容器
Docker网关冲突导致容器启动网络异常解决方案
当执行`docker-compose up`命令时,服务器网络可能因Docker创建新网桥导致IP段冲突而中断。原因是Docker默认的docker0网卡(172.17.0.1/16)与宿主机网络地址段重叠,引发路由异常。解决方法为修改docker0地址段,通过配置`/etc/docker/daemon.json`调整为非冲突段(如192.168.200.1/24),并重启服务。同时,在`docker-compose.yml`中指定网络模式为`bridge`,最后通过检查docker0地址、网络接口列表及测试容器启动验证修复效果。
|
4月前
|
缓存 Java Docker
如何对应用代码进行优化以提高在Docker容器中的性能?
如何对应用代码进行优化以提高在Docker容器中的性能?
271 1
|
3月前
|
运维 Cloud Native Docker
Docker:重塑现代应用交付的基石
Docker:重塑现代应用交付的基石
|
5月前
|
安全 Java Docker
Docker 部署 Java 应用实战指南与长尾优化方案
本文详细介绍了Docker容器化部署Java应用的最佳实践。首先阐述了采用多阶段构建和精简JRE的镜像优化技术,可将镜像体积减少60%。其次讲解了资源配置、健康检查、启动优化等容器化关键配置,并演示了Spring Boot微服务的多模块构建与Docker Compose编排方案。最后深入探讨了Kubernetes生产部署、监控日志集成、灰度发布策略以及性能调优和安全加固措施,为Java应用的容器化部署提供了完整的解决方案指南。文章还包含大量可落地的代码示例,涵盖从基础到高级的生产环境实践。
252 3

热门文章

最新文章