docker高级应用之网络资源限制
最近我这里docker单机平台正式上线使用,使用中有很多问题都一样解决,在给一个游戏项目做测试的时候,此项目由于8080端口对公网全部开放,并且安全策略没有做好(默认的tomcat模板没有删除),导致被人进行webshell,跑了很多的流量,为了解决此问题我针对tc与openvswitch本身的qos做了深入研究,最后选择openvswitch的qos作为容器的网络资源限制方法。 docker本身仅能对容器的cpu、内存做限制,而且必须是在容器运行前做,运行过程中未发现如何动态修改,并且不提供网络资源限制,所以只能使用其他软件做了。 我的docker网络没有使用默认bridge,使用none,然后绑定openvswitch的bridge,并使用pipework提供网络,所以我可以根据容器对于openvswitch的port来进行基于port的网络资源限制,好处是可以动态的修改,坏处是容器一重启还得重新做,但也可以通过其他方法来解决。 一、下面是我做测试的结果: 对于限速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:DengLei #email:dl528888@gmail.com op =$1 container=$2 limit=$3 #Mbits/s if [-z$1]||[-z$2]; then echo "Usage:operationcontainer_namelimit(default:5m)" echo "Example1:Iwantlimit5minthecontainer:test" echo "Thecommandis:bash`basename$0`limittest5" echo "Example2:Iwantdeletenetworklimitinthecontainer:test" echo "Thecommandis:bash`basename$0`ulimittest" exit 1 fi if [-z$3]; then limit= '5m' fi if [`dockerinspect-- format "``.`State`.`Pid`" $container&>> /dev/null && echo 0|| echo 1`- eq 1]; then echo "nothiscontainer:$container" exit 1 fi ovs_prefix= 'veth1pl' container_id=`dockerinspect-- 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 =@newqoscreateqos type =linux-htbqueues=0=@q0other-config:max-rate=$((limit*1000000))---- id =@q0createqueueother-config:min-rate=$((limit*1000000))other-config:max-rate=$((limit*1000000))&>> /dev/null && echo 'modifysuccess!' || echo 'modifyfail!' 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 'modifysuccess!' || echo 'modifyfail!' done fi 此脚本使用的话,限速可以直接针对下载与上传,并且限制是统一生效的,比如我限制一个容器带宽为5m,那么下载与上传的限速都是5m,单位是bit不是byte。 三、下面是使用方法: 1 2 3 4 5 6 [root@docker-test3tmp] #shmodify_docker_container_network_limit.sh Usage:operationcontainer_namelimit(default:5m) Example1:Iwantlimit5m in thecontainer: test The command is: bash modify_docker_container_network_limit.shlimit test 5 Example2:Iwantdeletenetworklimit in thecontainer: 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--progress172.16.1.126:/tmp/test_client.iso/tmp/ root@172.16.1.126'spassword: receivingincremental file list test_client.iso 104857600100%56.82MB /s 0:00:01(xfer #1,to-check=0/1) sent30bytesreceived104892888bytes1226817.75bytes /sec totalsizeis104857600speedupis1.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'spassword: sendingincremental file list test_server.iso 104857600100%51.08MB /s 0:00:01(xfer #1,to-check=0/1) sent104892883bytesreceived31bytes29969404.00bytes /sec totalsizeis104857600speedupis1.00 2、限速5m的 下载速度 1 2 3 4 5 6 7 8 14:15:27 #rsync-avz--progress172.16.1.126:/tmp/test_client.iso/tmp/ root@172.16.1.126'spassword: receivingincremental file list test_client.iso 104857600100%580.46kB /s 0:02:56(xfer #1,to-check=0/1) sent30bytesreceived104892888bytes590946.02bytes /sec totalsizeis104857600speedupis1.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'spassword: sendingincremental file list test_server.iso 104857600100%616.19kB /s 0:02:46(xfer #1,to-check=0/1) sent104892883bytesreceived31bytes571623.51bytes /sec totalsizeis104857600speedupis1.00 3、限速为10m 下载速度 1 2 3 4 5 6 7 8 9 14:28:55 #rsync-avz--progress172.16.1.126:/tmp/test_client.iso/tmp/ root@172.16.1.126'spassword: receivingincremental file list test_client.iso 104857600100%1.13MB /s 0:01:28(xfer #1,to-check=0/1) sent30bytesreceived104892888bytes1109977.97bytes /sec totalsizeis104857600speedupis1.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'spassword: sendingincremental file list test_server.iso 104857600100%1.21MB /s 0:01:22(xfer #1,to-check=0/1) sent104892883bytesreceived31bytes1133977.45bytes /sec totalsizeis104857600speedupis1.00 4、限速为20m 下载速度 1 2 3 4 5 6 7 8 14:32:57 #rsync-avz--progress172.16.1.126:/tmp/test_client.iso/tmp/ root@172.16.1.126'spassword: receivingincremental file list test_client.iso 104857600100%2.27MB /s 0:00:44(xfer #1,to-check=0/1) sent30bytesreceived104892888bytes2305338.86bytes /sec totalsizeis104857600speedupis1.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'spassword: sendingincremental file list test_server.iso 104857600100%2.45MB /s 0:00:40(xfer #1,to-check=0/1) sent104892883bytesreceived31bytes2305338.77bytes /sec totalsizeis104857600speedupis1.00 5、限速为50m 下载速度 1 2 3 4 5 6 7 8 14:35:20 #rsync-avz--progress172.16.1.126:/tmp/test_client.iso/tmp/ root@172.16.1.126'spassword: receivingincremental file list test_client.iso 104857600100%5.67MB /s 0:00:17(xfer #1,to-check=0/1) sent30bytesreceived104892888bytes5379124.00bytes /sec totalsizeis104857600speedupis1.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'spassword: sendingincremental file list test_server.iso 104857600100%6.33MB /s 0:00:15(xfer #1,to-check=0/1) sent104892883bytesreceived31bytes5116727.51bytes /sec totalsizeis104857600speedupis1.00 6、限速100m 下载速度 1 2 3 4 5 6 7 8 14:37:18 #rsync-avz--progress172.16.1.126:/tmp/test_client.iso/tmp/ root@172.16.1.126'spassword: receivingincremental file list test_client.iso 104857600100%11.35MB /s 0:00:08(xfer #1,to-check=0/1) sent30bytesreceived104892888bytes8391433.44bytes /sec totalsizeis104857600speedupis1.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'spassword: sendingincremental file list test_server.iso 104857600100%13.19MB /s 0:00:07(xfer #1,to-check=0/1) sent104892883bytesreceived31bytes11041359.37bytes /sec totalsizeis104857600speedupis1.00 7、限速150m 下载速度 1 2 3 4 5 6 7 8 14:38:39 #rsync-avz--progress172.16.1.126:/tmp/test_client.iso/tmp/ root@172.16.1.126'spassword: receivingincremental file list test_client.iso 104857600100%11.35MB /s 0:00:08(xfer #1,to-check=0/1) sent30bytesreceived104892888bytes11041359.79bytes /sec totalsizeis104857600speedupis1.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'spassword: sendingincremental file list test_server.iso 104857600100%13.14MB /s 0:00:07(xfer #1,to-check=0/1) sent104892883bytesreceived31bytes11041359.37bytes /sec totalsizeis104857600speedupis1.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'spassword: receivingincremental file list test_client.iso 2147483648100%106.42MB /s 0:00:19(xfer #1,to-check=0/1) sent30bytesreceived2148204633bytes95475762.80bytes /sec totalsizeis2147483648speedupis1.00 上传速度 1 2 3 4 5 6 7 8 17:20:06 #rsync-avz--progress/tmp/test_server.iso172.16.1.53:/tmp/ root@172.16.1.53'spassword: sendingincremental file list test_server.iso 2147483648100%112.51MB /s 0:00:18(xfer #1,to-check=0/1) sent2148204628bytesreceived31bytes95475762.62bytes /sec totalsizeis2147483648speedupis1.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'spassword: receivingincremental file list test_client.iso 104857600100%591.44kB /s 0:02:53(xfer #1,to-check=0/1) sent30bytesreceived104892888bytes587635.39bytes /sec totalsizeis104857600speedupis1.00 上传速度 1 2 3 4 5 6 7 8 17:38:57 #rsync-avz--progress/tmp/test_server.iso172.16.1.53:/tmp/ root@172.16.1.53'spassword: sendingincremental file list test_server.iso 104857600100%590.35kB /s 0:02:53(xfer #1,to-check=0/1) sent104892883bytesreceived31bytes574755.69bytes /sec totalsizeis104857600speedupis1.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'spassword: receivingincremental file list test_client.iso 104857600100%1.15MB /s 0:01:26(xfer #1,to-check=0/1) sent30bytesreceived104892888bytes1146370.69bytes /sec totalsizeis104857600speedupis1.00 上传速度 1 2 3 4 5 6 7 8 17:44:31 #rsync-avz--progress/tmp/test_server.iso172.16.1.53:/tmp/ root@172.16.1.53'spassword: sendingincremental file list test_server.iso 104857600100%1.16MB /s 0:01:26(xfer #1,to-check=0/1) sent104892883bytesreceived31bytes1146370.64bytes /sec totalsizeis104857600speedupis1.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'spassword: receivingincremental file list test_client.iso 104857600100%2.32MB /s 0:00:43(xfer #1,to-check=0/1) sent30bytesreceived104892888bytes2162740.58bytes /sec totalsizeis104857600speedupis1.00 上传速度 1 2 3 4 5 6 7 8 17:48:06 #rsync-avz--progress/tmp/test_server.iso172.16.1.53:/tmp/ root@172.16.1.53'spassword: sendingincremental file list test_server.iso 104857600100%2.38MB /s 0:00:42(xfer #1,to-check=0/1) sent104892883bytesreceived31bytes2255761.59bytes /sec totalsizeis104857600speedupis1.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'spassword: receivingincremental file list test_client.iso 104857600100%5.84MB /s 0:00:17(xfer #1,to-check=0/1) sent30bytesreceived104892888bytes5116727.71bytes /sec totalsizeis104857600speedupis1.00 上传速度 1 2 3 4 5 6 7 8 17:53:15 #rsync-avz--progress/tmp/test_server.iso172.16.1.53:/tmp/ root@172.16.1.53'spassword: sendingincremental file list test_server.iso 104857600100%6.10MB /s 0:00:16(xfer #1,to-check=0/1) sent104892883bytesreceived31bytes5993880.80bytes /sec totalsizeis104857600speedupis1.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'spassword: receivingincremental file list test_client.iso 104857600100%11.75MB /s 0:00:08(xfer #1,to-check=0/1) sent30bytesreceived104892888bytes9989801.71bytes /sec totalsizeis104857600speedupis1.00 上传速度 1 2 3 4 5 6 7 8 17:55:39 #rsync-avz--progress/tmp/test_server.iso172.16.1.53:/tmp/ root@172.16.1.53'spassword: sendingincremental file list test_server.iso 104857600100%12.72MB /s 0:00:07(xfer #1,to-check=0/1) sent104892883bytesreceived31bytes11041359.37bytes /sec totalsizeis104857600speedupis1.00 14、限速150m 下载速度 1 2 3 4 5 6 7 8 17:56:58 #rsync-avz--progress172.16.1.53:/tmp/test_client.iso/tmp/ root@172.16.1.53'spassword: receivingincremental file list test_client.iso 104857600100%17.69MB /s 0:00:05(xfer #1,to-check=0/1) sent30bytesreceived104892888bytes13985722.40bytes /sec totalsizeis104857600speedupis1.00 上传速度 1 2 3 4 5 6 7 8 17:57:18 #rsync-avz--progress/tmp/test_server.iso172.16.1.53:/tmp/ root@172.16.1.53'spassword: sendingincremental file list test_server.iso 104857600100%20.44MB /s 0:00:04(xfer #1,to-check=0/1) sent104892883bytesreceived31bytes16137371.38bytes /sec totalsizeis104857600speedupis1.00 如果大家有问题可以留言给我,我会及时回复。 本文转自 reinxu 51CTO博客,原文链接:http://blog.51cto.com/dl528888/1641569,如需转载请自行联系原作者