Docker 中 NAT 和 HOST 的区别

Docker 中 NAT 和 HOST 的区别


料青山看我应如是
浏览 584回答 1
1回答

慕村225694

我们下面将比较原生启动 redis 实例和使用Docker启动的性能对比。测试机器:24 CPUS,48G内存的dell 420 物理服务器其中redis的配置文件如下:daemonize yesport 63791、宿主机直接启动redis实例:启动命令:redis-server /etc/redis.conf性能测试命令:redis-benchmark -h 127.0.0.1 -p 6379 -q -d 100结果如下:PING_INLINE: 70224.72 requests per secondPING_BULK: 97276.27 requests per secondSET: 99403.58 requests per secondGET: 99403.58 requests per secondINCR: 100603.62 requests per secondLPUSH: 98425.20 requests per secondLPOP: 98425.20 requests per secondSADD: 100502.52 requests per secondSPOP: 100401.61 requests per secondLPUSH (needed to benchmark LRANGE): 99700.90 requests per secondLRANGE_100 (first 100 elements): 40733.20 requests per secondLRANGE_300 (first 300 elements): 14907.57 requests per secondLRANGE_500 (first 450 elements): 5781.68 requests per secondLRANGE_600 (first 600 elements): 3832.59 requests per secondMSET (10 keys): 63816.21 requests per second2、利用taskset绑定一个CPU,在宿主机上启动redis(这也是业界优化redis的一个偏方):启动命令:taskset -c 02 redis-server /etc/redis.conf性能测试命令:redis-benchmark -h 127.0.0.1 -p 6379 -q -d 100结果如下:PING_INLINE: 73746.31 requests per secondPING_BULK: 99601.60 requests per secondSET: 99700.90 requests per secondGET: 100200.40 requests per secondINCR: 100000.00 requests per secondLPUSH: 99403.58 requests per secondLPOP: 99108.03 requests per secondSADD: 101317.12 requests per secondSPOP: 100502.52 requests per secondLPUSH (needed to benchmark LRANGE): 99304.87 requests per secondLRANGE_100 (first 100 elements): 40883.07 requests per secondLRANGE_300 (first 300 elements): 15015.02 requests per secondLRANGE_500 (first 450 elements): 5262.05 requests per secondLRANGE_600 (first 600 elements): 3892.87 requests per secondMSET (10 keys): 52938.06 requests per second3、利用Docker的Nat网络模式启动redis(我们之前介绍的方式):启动命令:docker run -v /usr/local/redis.conf:/etc/redis.conf -p 6379:6379 --name myredis -d redis:2.8.19性能测试命令:redis-benchmark -h 127.0.0.1 -p 6379 -q -d 100结果如下:PING_INLINE: 40983.61 requests per secondPING_BULK: 50276.52 requests per secondSET: 49776.01 requests per secondGET: 46533.27 requests per secondINCR: 48309.18 requests per secondLPUSH: 54406.96 requests per secondLPOP: 56369.79 requests per secondSADD: 36153.29 requests per secondSPOP: 44130.62 requests per secondLPUSH (needed to benchmark LRANGE): 49726.51 requests per secondLRANGE_100 (first 100 elements): 21459.23 requests per secondLRANGE_300 (first 300 elements): 6416.01 requests per secondLRANGE_500 (first 450 elements): 4156.97 requests per secondLRANGE_600 (first 600 elements): 3139.32 requests per secondMSET (10 keys): 50787.20 requests per second是不是结果令大家大跌眼镜,为什么官方号称只有10%的性能损耗,而实际测试下来竟然相差了50%左右,到底哪里出了问题呢?其实问题就出在了Docker的Nat网络模式上,如果我们换一种方式启动Docker容器,再看下测试结果,是不是能大幅提升性能呢?4、利用Docker的Host网络模式启动redis:启动命令:docker run -v /usr/local/redis.conf:/etc/redis.conf --net="host" --name myredis -d redis:2.8.19性能测试命令:redis-benchmark -h 127.0.0.1 -p 6379 -q -d 100结果如下:PING_INLINE: 67613.25 requests per secondPING_BULK: 100908.17 requests per secondSET: 97751.71 requests per secondGET: 98135.42 requests per secondINCR: 101936.80 requests per secondLPUSH: 100000.00 requests per secondLPOP: 99206.34 requests per secondSADD: 100704.94 requests per secondSPOP: 100806.45 requests per secondLPUSH (needed to benchmark LRANGE): 100000.00 requests per secondLRANGE_100 (first 100 elements): 40916.53 requests per secondLRANGE_300 (first 300 elements): 15008.25 requests per secondLRANGE_500 (first 450 elements): 5552.78 requests per secondLRANGE_600 (first 600 elements): 3820.29 requests per secondMSET (10 keys): 70224.72 requests per second最终第四个测试结果令我们满意,符合官方所说的Docker启动应用几乎没有性能损耗的说法,这点大家在使用Docker部署应用的时候一定要注意。另外值得一提的是,业界绑定CPU的偏方似乎并没有什么X用,可能在单机跑多个redis实例的情况下,有那么一点点性能提升。最后,在使用了host模式启动Docker之后,是无法改变container监听的端口号的,我们可以通过挂载不同的配置文件来避免端口冲突的发生。 
打开App,查看更多内容
随时随地看视频慕课网APP