[英]Pod-to-pod communication doens't work with Python socket in k8s
In our project, we want two pods working as server-client that communicate via the Python socket
library.在我们的项目中,我们希望两个 pod 作为服务器-客户端,通过 Python
socket
库进行通信。 Both containers are built locally with docker build
, pulled locally via imagePullPolicy: IfNotPresent
on the yaml files and run on the same node of the k8s cluster (I'm running kubernetes vanilla, if that's important).两个容器都是使用
docker build
本地构建的,通过imagePullPolicy: IfNotPresent
在 yaml 文件上本地拉取,并在 k8s 集群的同一节点上运行(我正在运行 ZB76E98,AF9AAA680979BFA5 重要的)
The communication works well when we当我们沟通良好时
docker build
and docker run
docker build
和docker run
将两个脚本作为容器运行 The communication fails when both server and client are deployed in K8s.当服务器和客户端都部署在 K8s 中时,通信失败。
kubectl logs client -f
returns: kubectl logs client -f
返回:
Traceback (most recent call last):
File "client.py", line 7, in <module>
client_socket.connect((IP_Server,PORT_Server))
TimeoutError: [Errno 110] Connection timed out
I suspect there's a problem with the outgoing request from the client script when it's deployed on the cluster, but I can't find where the problem lies.我怀疑客户端脚本在集群上部署时发出的请求存在问题,但我找不到问题所在。
server.py服务器.py
import socket
IP = "0.0.0.0"
PORT = 1234
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((IP, PORT))
server_socket.listen()
...
server.yaml服务器.yaml
apiVersion: v1
kind: Service
metadata:
name: server
labels:
app: server
spec:
ports:
- port: 1234
targetPort: 1234
protocol: TCP
selector:
app: server
---
apiVersion: v1
kind: Pod
metadata:
name: server
labels:
app: server
spec:
containers:
- name: server
image: server:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 1234
client.py客户端.py
import socket
IP_Server = # the IP of the server service, obtained from "kubectl get svc"
PORT_Server = 1234
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((IP_Server,PORT_Server)) # fails here
...
client.yaml客户端.yaml
apiVersion: v1
kind: Pod
metadata:
name: client
labels:
app: client
spec:
containers:
- name: client
image: client:latest
imagePullPolicy: IfNotPresent
In a default setup - there shouldn't be anything preventing you to connect between 2 pods.在默认设置中 - 不应该有任何东西阻止您在 2 个 pod 之间进行连接。 However, you shouldn't be relying on the IP addresses for communication inside your cluster.
但是,您不应依赖 IP 地址在集群内部进行通信。 Try using the service name:
尝试使用服务名称:
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 10m
server ClusterIP 10.98.48.92 <none> 80/TCP 9m39s
server
should be typically available to all pods running inside the same namespace. server
通常应该对在同一命名空间内运行的所有 pod 可用。
There may be something wrong with the way you resolve the IP address of the server, but the "service" created by the server should implicitly be accessible over DNS (eg as server:1234), as explained here , so maybe you can use that instead?您解析服务器的 IP 地址的方式可能有问题,但是服务器创建的“服务”应该可以通过 DNS 隐式访问(例如服务器:1234),如此处所述,所以也许您可以使用它反而?
In case anyone gets here in the future, I found the answer that worked for me here: https://github.com/kubernetes/kubernetes/issues/86762#issuecomment-836338017万一将来有人来到这里,我在这里找到了对我有用的答案: https://github.com/kubernetes/kubernetes/issues/86762#issuecomment-836338017
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.