kubernetes K8s服务未链接到Pod端口

pu82cl6c  于 2023-06-28  发布在  Kubernetes
关注(0)|答案(1)|浏览(216)

希望一些k8s天才可以指出我在端口Map上做错了什么。我们有一个NextJS FE应用程序,它调用:http://gateway-api:8000/...-dns解析到正确的服务IP,但返回连接拒绝。我们的YAML在下面,端口在docker级别公开。调用API可以完美地从 Postman 等人那里工作,但是FE总是被拒绝。我们只能假设端口没有正确Map,或者服务没有以其他方式正确“链接”到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>
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/service路由。

aiqt4smr

aiqt4smr1#

Service的selector:匹配Pod的labels:。您的Pod没有metadata: { labels: },因此没有服务可以匹配它。
在实践中,直接创建Pod是不常见的;你几乎总是使用更高级别的对象,比如Deployment。标签需要在Deployment规范中的Pod模板中:

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

相关问题