一、tcpcopy工具介绍
tcpcopy 是一个分布式在线压力测试工具,可以将线上流量拷贝到测试机器,实时的模拟线上环境,达到在程序不上线的情况下实时承担线上流量的效果,尽早发现 bug,增加上线信心。
tcpcopy 的优势在于其实时性及真实性,除了少量的丢包,完全拷贝线上流量到测试机器,真实的模拟线上流量的变化规律。
二、tcpcopy原理
tcpcopy新版架构图
tcpcopy包含三部分:online server、assistant server、target server
tcpcopy拷贝一次流量访问的步骤如下
1、一个访问请求到达线上内核后端机;
2、socket 包在 IP 层被拷贝了一份传给tcpcopy 进程;
3、tcpcopy 修改包的目的及源地址,发给测试内核后端机;
4、拷贝的包到达测试内核后端机;
5、测试内核后端机的推荐内核处理访问,并返回结果;
6、返回结果在 IP 层被截获、丢弃,由 intercept 拷贝返回结果的 IP header 返回;
7、IP header 被发送给线上内核后端机的 tcpcopy 进程。
三、tcpcopy搭建
3.1 服务器规划
角色 | 主机名 | mysql端口 |
online server | test00 | 3308(5.6) |
assistant server | offline01 | |
target server | offline02 | 3318(5.7) |
3.2 tcpcopy安装及配置
安装依赖
Id yum -y install libpcap-devel
intercept安装
Id git clone git://github.com/session-replay-tools/intercept.git
Id cd intercept
Id ./configure
Id make && make install
tcpcopy安装
Id git clone git://github.com/session-replay-tools/tcpcopy.git
Id cd tcpcopy
Id ./configure
Id make && make install
四、开启tcpcopy流量复制
4.1 target server上操作:
Id route add -net 10.0.0.8 netmask 255.255.252.0 gw 10.0.0.0
成功后可以看到
Id ss -an |head
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 0 127.0.0.1:199 *:*
LISTEN 512 0 *:80 *:*
ESTAB 0 0 192.168.0.230:80 10.10.10.1:62602
ESTAB 0 0 192.168.0.230:80 10.10.10.4:54595
ESTAB 0 0 192.168.0.230:80 10.10.10.3:53566
ESTAB 0 0 192.168.0.230:80 10.10.10.6:49260
ESTAB 0 0 192.168.0.230:80 10.10.10.8:57598
ESTAB 0 0 192.168.0.230:80 10.10.10.7:64454
4.2 assistant server上操作(root privilege):
Id cd /usr/local/intercept/sbin/
Id ./intercept -i bond1 -F 'tcp and src port 3318' -d
成功后可以看到
Id ss -an |grep 36524
LISTEN 0 5 *:36524 *:*
ESTAB 0 66 192.168.0.219:36524 192.168.0.8:49034
ESTAB 0 0 192.168.0.219:36524 192.168.0.8:49035
ESTAB 0 66 192.168.0.219:36524 192.168.0.8:49032
ESTAB 0 0 192.168.0.219:36524 192.168.0.8:49033
4.3 online source server 上操作(root privilege):
Id cd /usr/local/tcpcopy/sbin/
Id ./tcpcopy -x 3308-10.0.0.1:3318 -s 10.0.0.2 -c 10.0.0.8 -d
成功后可以看到
Idss -an|grep 192.168.0.219
ESTAB 0 0 192.168.0.8:49034 192.168.0.219:36524
ESTAB 0 0 192.168.0.8:49035 192.168.0.219:36524
ESTAB 0 0 192.168.0.8:49032 192.168.0.219:36524
ESTAB 0 0 192.168.0.8:49033 192.168.0.219:36524
五、tcpcopy遇到的坑
1、tcpcopy log日志提示many connections cant established
把tcpcopy指定的-c 伪装客户端ip换成和线上一致的,可以ping通的网段就可以
2、大多数云环境设置了安全限制,一个网卡无法绑定2个IP,所以online server则会拒绝发送复制包,在线上抓包就会显示
Id tcpdump -i eth0 -nn port 3306 and host 1.1.1.4
11:53:58.573735 IP 10.0.0.1.https > 10.0.0.2.37121: Flags [R.], seq 0, ack 2906947315, win 0, length 0
11:53:58.573774 IP 10.0.0.2.37121 > 10.0.0.1.http: Flags [.], ack 2906947314, win 1024, length 0
11:53:58.573812 IP 10.0.0.1.http > 10.0.0.2.37121: Flags [R], seq 2906947314, win 0, length 0
参考:
https://mfcheer.github.io/20170228-index.html
http://ju.outofmemory.cn/entry/106907
http://www.itts-union.com/2814.html
http://www.361way.com/tcpcopy-online-offline/3510.html
https://github.com/session-replay-tools/tcpcopy
为了方便大家交流,本人开通了微信公众号和QQ群,QQ群:291519319,喜欢技术的一起来交流吧