K8s服务未链接到Pod端口。

huangapple go评论91阅读模式
英文:

K8s Service not linking to Pod port

问题

以下是您要翻译的内容:

希望一些 Kubernetes 高手能指出我在端口映射方面做错了什么。我们有一个 NextJS 前端应用程序,调用:http://gateway-api:8000/... - DNS 解析到了正确的服务 IP,但返回连接被拒绝。我们的 YAML 如下,端口在 Docker 层面上已经暴露。从 postman 等工具中调用 API 非常顺畅,但前端一直被拒绝。只能假设端口映射不正确,或者服务以某种其他方式没有正确“链接”到 Pod。

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: gateway-api
  5. spec:
  6. selector:
  7. name: gateway-api
  8. template:
  9. metadata:
  10. labels:
  11. run: gateway-api
  12. containers:
  13. - name: gateway-api
  14. image: something/gateway-api:latest
  15. ports:
  16. - containerPort: 8000

和服务

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: gateway-api
  5. labels:
  6. run: gateway-api
  7. spec:
  8. ports:
  9. - port: 8000
  10. protocol: "TCP"
  11. targetPort: 8000
  12. selector:
  13. app: gateway-api

各种信息:

  1. kubectl describe service gateway-api
  2. Name: gateway-api
  3. Namespace: default
  4. Labels: app.kubernetes.io/managed-by=tilt
  5. run=gateway-api
  6. Annotations: <none>
  7. Selector: app=gateway-api
  8. Type: ClusterIP
  9. IP Family Policy: SingleStack
  10. IP Families: IPv4
  11. IP: 10.103.9.152
  12. IPs: 10.103.9.152
  13. Port: <unset> 8000/TCP
  14. TargetPort: 8000/TCP
  15. Endpoints: <none>
  16. Session Affinity: None
  17. Events: <none>
  1. kubectl get pod -o wide
  2. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  3. gateway-api 1/1 Running 0 12m 10.1.1.161 docker-desktop <none> <none>
  4. gateway-web 1/1 Running 0 12m 10.1.1.160 docker-desktop <none> <none>
  5. mysql-0 1/1 Running 7 (19h ago) 75d 10.1.1.140 docker-desktop <none> <none>
  1. kubectl get service -o wide
  2. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
  3. gateway-api ClusterIP 10.103.9.152 <none> 8000/TCP 11m app=gateway-api

还应该说明,由于我们使用了 NextJS 服务器,我们无法使用 localhost:xxxx,因此采用了命名的 Pod/服务路由。

英文:

Hopefully some k8s genius can point out what I'm doing wrong with port mappings. We have a NextJS FE app, which calls: http://gateway-api:8000/... - The dns resolves to the correct service IP, but it returns connection refused. Our YAML are below, and the port is exposed at the docker level. Calling the API works flawlessly from the likes of postman et al, but the FE consistently gets refused. One can only presume that the ports aren't mapped correctly, or the Service isn't 'linking' to the Pod correctly in some other fashion.

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: gateway-api
  5. spec:
  6. selector:
  7. name: gateway-api
  8. template:
  9. metadata:
  10. labels:
  11. run: gateway-api
  12. containers:
  13. - name: gateway-api
  14. image: something/gateway-api:latest
  15. ports:
  16. - containerPort: 8000

and the service

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: gateway-api
  5. labels:
  6. run: gateway-api
  7. spec:
  8. ports:
  9. - port: 8000
  10. protocol: &quot;TCP&quot;
  11. targetPort: 8000
  12. selector:
  13. app: gateway-api

Various info:

  1. kubectl describe service gateway-api
  2. Name: gateway-api
  3. Namespace: default
  4. Labels: app.kubernetes.io/managed-by=tilt
  5. run=gateway-api
  6. Annotations: &lt;none&gt;
  7. Selector: app=gateway-api
  8. Type: ClusterIP
  9. IP Family Policy: SingleStack
  10. IP Families: IPv4
  11. IP: 10.103.9.152
  12. IPs: 10.103.9.152
  13. Port: &lt;unset&gt; 8000/TCP
  14. TargetPort: 8000/TCP
  15. Endpoints: &lt;none&gt;
  16. Session Affinity: None
  17. Events: &lt;none&gt;
  1. get pod -o wide
  2. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  3. gateway-api 1/1 Running 0 12m 10.1.1.161 docker-desktop &lt;none&gt; &lt;none&gt;
  4. gateway-web 1/1 Running 0 12m 10.1.1.160 docker-desktop &lt;none&gt; &lt;none&gt;
  5. mysql-0 1/1 Running 7 (19h ago) 75d 10.1.1.140 docker-desktop &lt;none&gt; &lt;none&gt;
  1. kubectl get service -o wide
  2. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
  3. gateway-api ClusterIP 10.103.9.152 &lt;none&gt; 8000/TCP 11m app=gateway-api

Should also state, that we're unable to use localhost:xxxx owing to the fact we utilise NextJS server. Hence the named pod/service route.

答案1

得分: 1

The Service的selector:与Pod的labels:匹配。 您的Pod没有metadata: { labels: },因此没有Service可以匹配它。

在实际操作中,直接创建Pod是不常见的;您几乎总是会使用高级对象,例如Deployment。 标签需要在Deployment规范中的Pod模板中:

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: gateway-api
  5. labels: { ... } # 好的做法,Service不匹配
  6. spec:
  7. template:
  8. metadata:
  9. labels: # <-- Service可以匹配的部分
  10. app: gateway-api
英文:

The Service's selector: matches the Pod's labels:. Your Pod has no metadata: { labels: } and so no Service can ever match it.

In practice, it's unusual to directly create Pods; you'd almost always use a higher-level object like a Deployment. The labels need to be in the Pod template in the Deployment spec:

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: gateway-api
  5. labels: { ... } # good practice, not matched by the Service
  6. spec:
  7. template:
  8. metadata:
  9. labels: # &lt;-- matched by the Service
  10. app: gateway-api
  11. </details>

huangapple
  • 本文由 发表于 2023年6月27日 17:55:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76563686.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定