微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法连接到在不同网络命名空间中运行的 redis 服务器

如何解决无法连接到在不同网络命名空间中运行的 redis 服务器

设置:

  • 创建了一个网络命名空间。

  • 创建了一个 vethvethavethb,以 vethb 结尾 主机中的命名空间和其他。

  • 192.168.122.50添加一个 ip vethb

  • 在该网络命名空间的容器中启动 redis 服务器

直接从在名为 redis 的同一主机上运行的客户端向 secondaryvm 服务器发送请求。但是,它抛出:Failed to connect to Redis: Connection timed out 错误。无法理解为什么会发生这种情况,因此通过嗅探 tcpdump 来查看 vetha 流,下面发布了相同的内容

21:48:33.764967 IP (tos 0x0,ttl 64,id 31361,offset 0,flags [DF],proto TCP (6),length 60)
    secondaryvm.34540 > 192.168.122.50.6379: Flags [S],cksum 0x75d6 (incorrect -> 0xe169),seq 3863373484,win 64240,options [mss 1460,sackOK,TS val 3417390465 ecr 0,nop,wscale 7],length 0
21:48:38.884975 ARP,Ethernet (len 6),IPv4 (len 4),Request who-has 192.168.122.50 tell secondaryvm,length 28
21:48:38.885101 ARP,Reply 192.168.122.50 is-at 1a:8c:7f:8b:4d:e0 (oui UnkNown),length 28
21:49:16.138726 IP (tos 0x0,id 29195,length 60)
    secondaryvm.34544 > 192.168.122.50.6379: Flags [S],cksum 0x75d6 (incorrect -> 0xe563),seq 550142628,TS val 3417432839 ecr 0,length 0
21:49:17.156952 IP (tos 0x0,id 29196,TS val 3417433857 ecr 0,length 0
21:49:19.173031 IP (tos 0x0,id 29197,cksum 0x75d6 (incorrect -> 0xd989),TS val 3417435873 ecr 0,length 0
21:49:22.405182 ARP,length 28
21:49:22.405262 ARP,length 28
21:49:23.429054 IP (tos 0x0,id 29198,cksum 0x75d6 (incorrect -> 0xc8e9),TS val 3417440129 ecr 0,length 0
21:49:31.621030 IP (tos 0x0,id 29199,cksum 0x75d6 (incorrect -> 0xa8e9),TS val 3417448321 ecr 0,length 0
21:49:47.749024 IP (tos 0x0,id 29200,cksum 0x75d6 (incorrect -> 0x69e9),TS val 3417464449 ecr 0,length 0
21:50:20.261084 IP (tos 0x0,id 29201,cksum 0x75d6 (incorrect -> 0xeae8),TS val 3417496961 ecr 0,length 0
21:50:25.381045 ARP,length 28
21:50:25.381176 ARP,length 28

Redis 服务器已启动,客户端根据 terminal 输出连接到它,但未根据 tcpdump 输出连接。根据{{​​1}}的输出,客户端正在尝试连接,但没有成功建立连接。

客户端终端输出

tcpdump

客户端代码(用redis-plus-plus编写):

========================100==================================================
terminate called after throwing an instance of 'sw::redis::TimeoutError'
  what():  Failed to connect to Redis: Connection timed out
Aborted (core dumped)

根据终端的输出,成功建立连接。

无法理解这里发生了什么。

  1. 是不是像 int main(){ auto redis = Redis("tcp://192.168.122.50:6379); sleep(5); int ep = 100; while(true){ cout<<"===================="<<ep<<"======================\n"; auto pipe = redis.pipeline(false); for(int i=1; i<=500; i++){ string s = to_string(i); if(i%2 == 1){ pipe.set(s,s); } else { string st = to_string(i-1); pipe.get(st); } } auto pipe_replies = pipe.exec(); pipe.discard(); } 行没有建立连接?

  2. redis-server 肯定已经启动了,那么为什么在查看 tcp 流时没有建立连接?

Redis 版本 auto redis = Redis("tcp://192.168.122.50:6379"); 并且它在端口 6379 上运行。

如何防止这个错误

编辑:

我已按照以下步骤/命令设置网络命名空间:

6.0.9

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。