今天我遇到了一个关于servicePort的误解。
我以为服务可以与ingress链接,只指定servicePort: 80,但是servicePort: 80和servicePort: 8080 也都可以。
谁能帮助我理解为什么服务同时暴露port和targetPort,而不是只有port?
Service (app)
spec:
  type: ClusterIP
  ports:
    - port: 80
      targetPort: 8080
Ingress (ingress-nginx)
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /
        backend:
          serviceName: app
          servicePort: 8080
 
                             
        
集群中的每个 Pod 都有一个 endpoint,即 Pod 的 IP 和 targetPort。
service是将流量发送给endpoint。
可以通过以下命令查看:
kubectl get endpointsingress 将 service 的 endpoint 配置给内置的 nginx。
最终,port 和 targetPort 都会转换成容器端口,也就是
targetPort,所以都可以。你的答案