无法让一个pod与另一个pod对话(Kubernetes中的ScrapyRT通信不工作)

nhaq1z21  于 4个月前  发布在  Kubernetes
关注(0)|答案(1)|浏览(55)

我正在管理Kubernetes集群,希望Pod 1Pod 2Pod 3进行API调用(但Pod 1-Pod 3失败!):
1.Pod 1:一个测试连接的笔记本环境。
1.Pod 2:一个运行在8000端口上的P2P. js应用,通过express-backend-service暴露在80端口上。
1.Pod 3:python scrapy应用,ScrapyRT监听14805端口,通过getcookie-14805-service暴露在80端口。

Pod 2服务和部署(express-backend-service):

express-deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: express-app-deployment
spec:
  #...
  containers:
  - name: express-app
    image: privaterepo/myproject-backend:latest
    ports:
    - containerPort: 8000
    #...

字符串

express-service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: express-backend-service
spec:
  selector:
    app: express-app
  ports:
  - protocol: TCP
    port: 80
    targetPort: 8000
  type: ClusterIP

Pod 3服务和部署(getcookie-14805-service):

getcookie-14805-deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: getcookie-pod
  labels:
    app: getcookie-pod
spec:
  replicas: 1
  selector:
    matchLabels:
      app: getcookie-pod
  template:
    metadata:
      labels:
        app: getcookie-pod
    spec:
      containers:
      - name: getcookie-pod
        image: privaterepo/myproject-scrapy:latest
        imagePullPolicy: Always  # Ensure the latest image is always pulled
        ports:
        - containerPort: 14805
        envFrom:
        - secretRef:
            name: scrapy-env
        env:
        - name: SCRAPYRT_PORT
          value: "14805"
      imagePullSecrets:
      - name: docker-credentials

getcookie-14805-service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: getcookie-service
spec:
  selector:
    app: getcookie-pod
  ports:
    - protocol: TCP
      port: 80
      targetPort: 14805
  type: ClusterIP

Kubernetes

kubectl get svc

$ kubectl get svc
NAME                                         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
express-backend-service                      ClusterIP   10.99.145.37    <none>        80/TCP    2d
getcookie-service                            ClusterIP   10.106.14.183   <none>        80/TCP    29m
kubernetes                                   ClusterIP   10.96.0.1       <none>        443/TCP   2d


pod kubectl get pods

$ kubectl get po
NAME                                                            READY   STATUS    RESTARTS        AGE
express-app-deployment-6896ff994c-gl4pd                         1/1     Running   3 (4h4m ago)    2d
getcookie-pod-59b8575ffc-8dcqb                                  1/1     Running   0               28m
jupyter-debug-pod                                               1/1     Running   3 (7h33m ago)   6d9h


pod 3日志(getcookie-pod-59 b8575 ffc-8dcqb):

$ kubectl logs getcookie-pod-59b8575ffc-8dcqb -f
2024-01-08 06:26:07+0000 [-] Log opened.
2024-01-08 06:26:07+0000 [-] Site starting on 14805
2024-01-08 06:26:07+0000 [-] Starting factory <twisted.web.server.Site object at 0x7fc2139a44f0>
2024-01-08 06:26:07+0000 [-] Running with reactor: AsyncioSelectorReactor.
2024-01-08 06:56:24+0000 [-] "127.0.0.1" - - [08/Jan/2024:06:56:24 +0000] "GET / HTTP/1.1" 404 167 "-" "curl/7.68.0"


只有在我对pod执行exec并运行以下命令后,上面的curl日志才会出现:

curl http://localhost:14805

2024年1月7日更新

尝试直接 curl getcookie-service不起作用:
curl http://getcookie-service输出:

curl: (7) Failed to connect to getcookie-service port 80: Connection refused

问题:

我可以使用服务名http://express-backend-service/API成功地从Pod 1发送请求到Pod 2,但是当我尝试使用类似的方法连接到Pod 3时,我会得到一个连接错误。
下面是Pod 1连接Pod 3的Python代码片段:

def getCookie(userId):
    endpoint = 'http://getcookie-14805-service.default/crawl.json?spider_name=getCookie&url=http://images.google.com/'
    post = {
        "request": {
            "url": "http://images.google.com/",
            "meta": {'userId': userId},
            "callback": "parse",
            "dont_filter": "True"
        },
        "spider_name": "getCookie"
    }
    try:
        response = requests.post(endpoint, json=post).json()
        return response['items'][0]['finalItems']
    except Exception as e:
        print('getCookie error:', e)
        return None

user = '6010dga53294c92c981ef3y576'
getCookie(user)


收到错误:

ConnectionError: HTTPConnectionPool(host='getcookie-14805-service.default', port=80): 
Max retries exceeded with url: /crawl.json?spider_name=getCookie&url=http://images.google.com/ 
(Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fde7a6cc7f0>: Failed 
to establish a new connection: [Errno 111] Connection refused'))


为什么Pod 1Pod 2通话成功,Pod 1Pod 3通话失败?
我希望Kubernetes服务能够促进pod间的通信。我需要额外的配置吗?

ssm49v7z

ssm49v7z1#

您是否看到Pod上的以下标签

app: getcookie-14805

个字符
终结点对象看起来不错。
很可能getcookie-14805 pod工作不正常。您可以使用kubectl exec进入pod并在容器内运行以下测试并验证响应吗
本地主机:14805
注意:如果服务在相同的命名空间中,则服务dns中不需要服务名称。http://getcookie-14805-service应该可以使用。

相关问题