简体   繁体   English

Pod 到 pod 通信不适用于 k8s 中的 Python 套接字

[英]Pod-to-pod communication doens't work with Python socket in k8s

Problem问题

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当我们沟通良好时

  • run both python scripts in the command line在命令行中运行两个 python 脚本
  • run both scripts as containers using docker build and docker run使用docker builddocker run将两个脚本作为容器运行
  • the server app container is deployed in the K8s cluster and the client app is run either on the command line or as a docker container.服务器应用程序容器部署在 K8s 集群中,客户端应用程序在命令行上运行或作为 docker 容器运行。

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.我怀疑客户端脚本在集群上部署时发出的请求存在问题,但我找不到问题所在。

Codes代码

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.

相关问题 k8s API 通过pod内部的python访问 - k8s API Access through python inside the pod 为什么我不能在 k8s pod 中杀死 python3 进程? - Why I cannot kill python3 process in k8s pod? 如何在 python k8s 客户端中以 root 身份运行“connect_get_namespaced_pod_exec” - How to run 'connect_get_namespaced_pod_exec' as root in python k8s client Python k8s 客户端:调用 list_namespaced_pod 时,是否可以在作业名称查询中使用通配符? - Python k8s client: is there a way to use wildcards on job-name query, when calling list_namespaced_pod? 有没有办法以编程方式在部署到GKE的k8s容器内检索应用程序默认API密钥? - Is there a way to retrieve the Application Default API key programmatically inside a k8s pod deployed to GKE? 在K8s pod中运行的Logstash容器的http插件要使用什么主机和端口? - What host and port to use for http plugin for a Logstash container running in K8s pod? 在 k8s 上运行 python 文件 - run python file on k8s 使用本机k8s python客户端与k8s运算符添加的API进行交互 - interact with API added by k8s operator using native k8s python client 已创建 k8s 卷快照,但它在 python k8s 客户端中返回 409 错误消息 - k8s volumesnapshot is created but it returns 409 error message in python k8s client 如何使用 k8s python 客户端删除 k8s 部署? - How to delete a k8s deployment using k8s python client?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM