nginx 无法使用服务群集IP与另一个Pod通信,使用POD IP能够通信

iqih9akk  于 2023-11-17  发布在  Nginx
关注(0)|答案(1)|浏览(228)

我有nginx pod和其他pod包含gunicorn pod API,我试图通过nginx pod连接。
当我在nginx pod中使用gunicorn pod的POD IP时,我可以连接nginx pod。输出:

  1. root@nginx:/etc/nginx# curl -v http://10.244.1.155:20000/_ems/plant
  2. * Trying 10.244.1.155:20000...
  3. * Connected to 10.244.1.155 (10.244.1.155) port 20000 (#0)

字符串
当我使用服务,而不是POD IP,其失败.错误:

  1. root@nginx:/etc/nginx# curl -v http://ems-service.default.svc.cluster.local:20000/_ems/plant;
  2. * Trying 10.106.43.2:20000...
  3. * connect to 10.106.43.2 port 20000 failed: Connection refused
  4. * Failed to connect to ems-service.default.svc.cluster.local port 20000: Connection refused
  5. * Closing connection 0
  6. curl: (7) Failed to connect to ems-service.default.svc.cluster.local port 20000: Connection refused


  1. NAME READY STATUS RESTARTS AGE
  2. pod/ems 1/1 Running 0 5m56s
  3. pod/nginx 1/1 Running 0 4h46m
  4. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  5. service/ems-service ClusterIP 10.106.43.2 <none> 20000/TCP 103m
  6. [eds@rnd-4 pod-4px]$ kubectl describe service -n default ems-service
  7. Name: ems-service
  8. Namespace: default
  9. Labels: <none>
  10. Annotations: <none>
  11. Selector: name=ems
  12. Type: ClusterIP
  13. IP Family Policy: SingleStack
  14. IP Families: IPv4
  15. IP: 10.106.43.2
  16. IPs: 10.106.43.2
  17. Port: <unset> 20000/TCP
  18. TargetPort: 80/TCP
  19. Endpoints: <none>
  20. Session Affinity: None
  21. Events: <none>


Yaml文件:

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: ems
  5. labels:
  6. app: ems
  7. spec:
  8. containers:
  9. - name: ems
  10. image:
  11. command: ["/bin/sh"]
  12. args: ["-c", "chmod +x /root/code/run.sh && /root/code/run.sh"]
  13. env:
  14. - name: LOGS_DIR
  15. value: /root/code/logs
  16. imagePullPolicy: Always
  17. ports:
  18. - containerPort: 20000
  19. ----------------------------------
  20. apiVersion: v1
  21. kind: Service
  22. metadata:
  23. name: ems-service
  24. spec:
  25. selector:
  26. name: ems
  27. ports:
  28. - protocol: TCP
  29. port: 20000
  30. targetPort: 80

ej83mcc0

ej83mcc01#

如果kubectl describe service表示Endpoints: <none>,这通常是服务的selector:与Pod的labels:不匹配的明显迹象。

  1. # pod.yaml
  2. metadata:
  3. labels:
  4. app: ems

个字符
最有可能的修复方法是在服务中将name更改为app
你通常不应该运行一个裸Pod,因为一些原因超出了这个问题的范围。我建议将Pod更改为Deployment,假设你不需要访问持久存储。在这种情况下,Service需要匹配spec: { template: { metadata: { labels: } } } per-Pod标签。

相关问题