Docker 中 NAT 和 HOST 的区别
官方说明使用Dokcer容器启动应用可以媲美直接在宿主机上启动的性能,宣称使用Docker启动应用大约可以达到原来性能的90%,如此低的性能损耗应当归功于Docker容器虚拟化的轻量级。
但是事实真的如官方所说,仅有10%的损耗吗?我们下面将比较原生启动 redis 实例和使用Docker启动的性能对比。
测试机器:
24 CPUS,48G内存的dell 420 物理服务器
其中redis的配置文件如下:
daemonize yes
port 6379
1、宿主机直接启动redis实例:
启动命令:
redis-server /etc/redis.conf
性能测试命令:
redis-benchmark -h 127.0.0.1 -p 6379 -q -d 100
结果如下:
PING_INLINE: 70224.72 requests per second
PING_BULK: 97276.27 requests per second
SET: 99403.58 requests per second
GET: 99403.58 requests per second
INCR: 100603.62 requests per second
LPUSH: 98425.20 requests per second
LPOP: 98425.20 requests per second
SADD: 100502.52 requests per second
SPOP: 100401.61 requests per second
LPUSH (needed to benchmark LRANGE): 99700.90 requests per second
LRANGE_100 (first 100 elements): 40733.20 requests per second
LRANGE_300 (first 300 elements): 14907.57 requests per second
LRANGE_500 (first 450 elements): 5781.68 requests per second
LRANGE_600 (first 600 elements): 3832.59 requests per second
MSET (10 keys): 63816.21 requests per second
2、利用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 second
PING_BULK: 99601.60 requests per second
SET: 99700.90 requests per second
GET: 100200.40 requests per second
INCR: 100000.00 requests per second
LPUSH: 99403.58 requests per second
LPOP: 99108.03 requests per second
SADD: 101317.12 requests per second
SPOP: 100502.52 requests per second
LPUSH (needed to benchmark LRANGE): 99304.87 requests per second
LRANGE_100 (first 100 elements): 40883.07 requests per second
LRANGE_300 (first 300 elements): 15015.02 requests per second
LRANGE_500 (first 450 elements): 5262.05 requests per second
LRANGE_600 (first 600 elements): 3892.87 requests per second
MSET (10 keys): 52938.06 requests per second
3、利用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 second
PING_BULK: 50276.52 requests per second
SET: 49776.01 requests per second
GET: 46533.27 requests per second
INCR: 48309.18 requests per second
LPUSH: 54406.96 requests per second
LPOP: 56369.79 requests per second
SADD: 36153.29 requests per second
SPOP: 44130.62 requests per second
LPUSH (needed to benchmark LRANGE): 49726.51 requests per second
LRANGE_100 (first 100 elements): 21459.23 requests per second
LRANGE_300 (first 300 elements): 6416.01 requests per second
LRANGE_500 (first 450 elements): 4156.97 requests per second
LRANGE_600 (first 600 elements): 3139.32 requests per second
MSET (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 second
PING_BULK: 100908.17 requests per second
SET: 97751.71 requests per second
GET: 98135.42 requests per second
INCR: 101936.80 requests per second
LPUSH: 100000.00 requests per second
LPOP: 99206.34 requests per second
SADD: 100704.94 requests per second
SPOP: 100806.45 requests per second
LPUSH (needed to benchmark LRANGE): 100000.00 requests per second
LRANGE_100 (first 100 elements): 40916.53 requests per second
LRANGE_300 (first 300 elements): 15008.25 requests per second
LRANGE_500 (first 450 elements): 5552.78 requests per second
LRANGE_600 (first 600 elements): 3820.29 requests per second
MSET (10 keys): 70224.72 requests per second
最终第四个测试结果令我们满意,符合官方所说的Docker启动应用几乎没有性能损耗的说法,这点大家在使用Docker部署应用的时候一定要注意。另外值得一提的是,业界绑定CPU的偏方似乎并没有什么X用,可能在单机跑多个redis实例的情况下,有那么一点点性能提升。
最后,在使用了host模式启动Docker之后,是无法改变container监听的端口号的,我们可以通过挂载不同的配置文件来避免端口冲突的发生。