无法使用服务集群IP与其他Pod通信,使用POD IP可以通信

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

我有 nginx pod,其他 pod 包含 Gunicorn pod api,我正在尝试通过 nginx pod 连接。

当我在nginx pod中使用gunicorn pod的POD IP时,我能够与nginx pod连接。 输出:

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

当我使用Service而不是POD IP时,它失败了。 错误:

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

日志:

NAME                                                         READY   STATUS             RESTARTS         AGE
pod/ems                                                      1/1     Running            0                5m56s
pod/nginx                                                    1/1     Running            0                4h46m
NAME                       TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
service/ems-service        ClusterIP   10.106.43.2    <none>        20000/TCP        103m
[eds@rnd-4 pod-4px]$ kubectl describe service -n default ems-service
Name:              ems-service
Namespace:         default
Labels:            <none>
Annotations:       <none>
Selector:          name=ems
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.106.43.2
IPs:               10.106.43.2
Port:              <unset>  20000/TCP
TargetPort:        80/TCP
Endpoints:         <none>
Session Affinity:  None
Events:            <none>

Yaml 文件:

apiVersion: v1
kind: Pod
metadata:
  name: ems
  labels:
    app: ems
spec:
  containers:
    - name: ems
      image: 
      command: ["/bin/sh"]
      args: ["-c", "chmod +x /root/code/run.sh && /root/code/run.sh"]
      env:
      - name: LOGS_DIR
        value: /root/code/logs
      imagePullPolicy: Always
      ports:
      - containerPort: 20000
----------------------------------
apiVersion: v1
kind: Service
metadata:
  name: ems-service
spec:
  selector:
    name: ems
  ports:
    - protocol: TCP
      port: 20000
      targetPort: 80
kubernetes nginx kubernetes-ingress
1个回答
1
投票

如果

kubectl describe service
显示
Endpoints: <none>
,这通常是一个明显的迹象,表明服务的
selector:
与 Pod 的
labels:
不匹配。在您的设置中,您有

# pod.yaml
metadata:
  labels:
    app: ems
# service.yaml
spec:
  selector:
    name: ems

这些需要匹配。最可能的解决方法是在服务中将

name
更改为
app

您通常不应该运行裸 Pod,原因有几个,超出了本问题的范围。假设您不需要访问持久存储,我建议将 Pod 更改为 Deployment。在这种情况下,服务需要匹配每个 Pod 的

spec: { template: { metadata: { labels: } } }
标签。

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