无法让一个 pod 与另一个 pod 通信(Kubernetes 中的 ScrapyRT 通信不起作用)

问题描述 投票:0回答:1

我正在管理 Kubernetes 集群,并希望 Pod1Pod2Pod3 进行 API 调用(但是 Pod1 - Pod3 失败!):

  1. Pod1:用于测试连接的 Jupyter Notebook 环境。
  2. Pod2:在端口 8000 上运行的 Express.js 应用程序,通过
    express-backend-service
    在端口 80 上公开。
  3. Pod3:一个 python scrapy 应用程序,其中 ScrapyRT 监听端口 14805,通过
    getcookie-14805-service
    在端口 80 上公开。

Pod2 服务和部署(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

Pod3 服务和部署(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

豆荚

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

pod3 日志(getcookie-pod-59b8575ffc-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 http://localhost:14805

更新 1/7/2024

尝试直接curl getcookie-service 不起作用:

curl http://getcookie-service
输出:

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

问题:

我可以使用服务名称 http://express-backend-service/api 成功从 Pod1Pod2 发送请求。但是,当尝试使用类似方法连接到 Pod3 时,我收到连接错误。

这是 Pod1 中用于连接到 Pod3 的 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'))

为什么我可以成功地从Pod1拨打Pod2,但无法从Pod1拨打Pod3

我希望 Kubernetes 服务能够促进 Pod 间的通信。我需要额外配置吗?

python node.js docker kubernetes scrapy
1个回答
0
投票

您看到烟弹上的下面标签了吗

app: getcookie-14805
k get ep
NAME ENDPOINTS AGE 
express-backend-service 10.244.0.230:8000 2d8h 
getcookie-14805-service 10.244.0.12:14805 20h 
kubernetes 100.65.21.11:443 2d23h

端点对象看起来不错。

很可能 getcookie-14805 pod 无法正常工作。您可以使用 kubectl exec 进入 pod 并在容器内运行以下测试并验证响应吗

卷曲http://localhost:14805

注意:如果服务位于同一命名空间中,则服务 dns 中不需要服务名称。 http://getcookie-14805-service 应该可以工作。

© www.soinside.com 2019 - 2024. All rights reserved.