程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了为什么我无法从 pod 中使用 python 连接到 redis?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决为什么我无法从 pod 中使用 python 连接到 redis??

开发过程中遇到为什么我无法从 pod 中使用 python 连接到 redis?的问题如何解决?下面主要结合日常开发的经验,给出你关于为什么我无法从 pod 中使用 python 连接到 redis?的解决方法建议,希望对你解决为什么我无法从 pod 中使用 python 连接到 redis?有所启发或帮助;

我启动了 redis pod 并创建了 NodePort 类型的服务。 我进入flask pod,使用Nslookup找到redis.default如下图。

/ # nslookup redis.default
Server:  10.96.0.10
Address: 10.96.0.10#53
name: redis.default.svc.cluster.local
Address: 10.98.129.45
/ # python3
Python 3.8.7 (default,Dec 26 2020,08:45:55) 
[GCC 10.2.1 20201203] on linux
Type "Help","copyright","credits" or "license" for more information.
>>> import redis
>>> redis_db = redis.Strictredis(host="redis.default",port=6379,db=0)
>>> redis_db.Ping()

稍后的结果:

TraceBACk (most recent call last):
file "/usr/lib/python3.8/site-packages/redis/connection.py",line 559,in connect
sock = self._connect()
file "/usr/lib/python3.8/site-packages/redis/connection.py",line 615,in _connect
raise err
file "/usr/lib/python3.8/site-packages/redis/connection.py",line 603,in _connect
sock.connect(socket_address)
TimeoutError: [Errno 110] Operation timed out
During handling of the above exception,another exception occurred:
TraceBACk (most recent call last):
file "<stdin>",line 1,in <module>
file "/usr/lib/python3.8/site-packages/redis/clIEnt.py",line 1378,in Ping
return self.execute_command('Ping')
file "/usr/lib/python3.8/site-packages/redis/clIEnt.py",line 898,in execute_command
conn = self.connection or pool.get_connection(command_name,**options)
file "/usr/lib/python3.8/site-packages/redis/connection.py",line 1192,in get_connection
connection.connect()
file "/usr/lib/python3.8/site-packages/redis/connection.py",line 563,in connect
raise ConnectionError(self._error_message(E))
redis.exceptions.ConnectionError: Error 110 connecTing to redis.default:6379. Operation timed out.

但是当我尝试从 Flask pod 内的 python 终端 Ping 时,我没有得到任何结果。 但是从 pod 外部我可以使用 redis-cli 进入正在运行的 redis

# kubectl get services
name  TYPE CLUSTER-IP EXTERNAL-IP  PORT(S) AGE
flask NodePort 10.107.57.184  <none> 5000:32071/TCP  37m
kubernetes  ClusterIP  10.96.0.1  <none> 443/TCP 100m
redis NodePort 10.98.129.45 <none> 6379:30667/TCP  8m14s
root@rockpix:/home/skumar/kfd-flask# redis-cli -h 192.168.5.165 -p 30667
192.168.5.165:30667> Ping
PONG
192.168.5.165:30667> 

其中 192.168.5.165 是 Pod 正在运行的位置。

我也检查了 redis pod :

# kubectl exec -it redis-85df5dd845-28xvk -- /bin/sh
/data # netstat
ActivE internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address  Foreign Address  State 
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags  Type  State  I-Node Path
/data # netstat -ltnp | grep redis
/data # netstat -ltnp | grep 6379
tcp 0 0 0.0.0.0:6379 0.0.0.0:*  ListEN -
tcp 0 0 :::6379  :::* ListEN -
the port number is correct.

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

@H_673_36@

大佬总结

以上是大佬教程为你收集整理的为什么我无法从 pod 中使用 python 连接到 redis?全部内容,希望文章能够帮你解决为什么我无法从 pod 中使用 python 连接到 redis?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。