英文:
K8s Service not linking to Pod port
问题
以下是您要翻译的内容:
希望一些 Kubernetes 高手能指出我在端口映射方面做错了什么。我们有一个 NextJS 前端应用程序,调用:http://gateway-api:8000/...
- DNS 解析到了正确的服务 IP,但返回连接被拒绝。我们的 YAML 如下,端口在 Docker 层面上已经暴露。从 postman 等工具中调用 API 非常顺畅,但前端一直被拒绝。只能假设端口映射不正确,或者服务以某种其他方式没有正确“链接”到 Pod。
apiVersion: v1
kind: Pod
metadata:
name: gateway-api
spec:
selector:
name: gateway-api
template:
metadata:
labels:
run: gateway-api
containers:
- name: gateway-api
image: something/gateway-api:latest
ports:
- containerPort: 8000
和服务
apiVersion: v1
kind: Service
metadata:
name: gateway-api
labels:
run: gateway-api
spec:
ports:
- port: 8000
protocol: "TCP"
targetPort: 8000
selector:
app: gateway-api
各种信息:
kubectl describe service gateway-api
Name: gateway-api
Namespace: default
Labels: app.kubernetes.io/managed-by=tilt
run=gateway-api
Annotations: <none>
Selector: app=gateway-api
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.103.9.152
IPs: 10.103.9.152
Port: <unset> 8000/TCP
TargetPort: 8000/TCP
Endpoints: <none>
Session Affinity: None
Events: <none>
kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
gateway-api 1/1 Running 0 12m 10.1.1.161 docker-desktop <none> <none>
gateway-web 1/1 Running 0 12m 10.1.1.160 docker-desktop <none> <none>
mysql-0 1/1 Running 7 (19h ago) 75d 10.1.1.140 docker-desktop <none> <none>
kubectl get service -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
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.
apiVersion: v1
kind: Pod
metadata:
name: gateway-api
spec:
selector:
name: gateway-api
template:
metadata:
labels:
run: gateway-api
containers:
- name: gateway-api
image: something/gateway-api:latest
ports:
- containerPort: 8000
and the service
apiVersion: v1
kind: Service
metadata:
name: gateway-api
labels:
run: gateway-api
spec:
ports:
- port: 8000
protocol: "TCP"
targetPort: 8000
selector:
app: gateway-api
Various info:
kubectl describe service gateway-api
Name: gateway-api
Namespace: default
Labels: app.kubernetes.io/managed-by=tilt
run=gateway-api
Annotations: <none>
Selector: app=gateway-api
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.103.9.152
IPs: 10.103.9.152
Port: <unset> 8000/TCP
TargetPort: 8000/TCP
Endpoints: <none>
Session Affinity: None
Events: <none>
get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
gateway-api 1/1 Running 0 12m 10.1.1.161 docker-desktop <none> <none>
gateway-web 1/1 Running 0 12m 10.1.1.160 docker-desktop <none> <none>
mysql-0 1/1 Running 7 (19h ago) 75d 10.1.1.140 docker-desktop <none> <none>
kubectl get service -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
gateway-api ClusterIP 10.103.9.152 <none> 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模板中:
apiVersion: apps/v1
kind: Deployment
metadata:
name: gateway-api
labels: { ... } # 好的做法,Service不匹配
spec:
template:
metadata:
labels: # <-- Service可以匹配的部分
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:
apiVersion: apps/v1
kind: Deployment
metadata:
name: gateway-api
labels: { ... } # good practice, not matched by the Service
spec:
template:
metadata:
labels: # <-- matched by the Service
app: gateway-api
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论