A Kubernetes Service can expose a set of Pods through a stable endpoint. Its
port, targetPort, and nodePort describe different points along the request path.The three ports
portis the Service port, reachable through the Service's ClusterIP from within the cluster.
targetPortis the port on the selected backend Pod. It can be a number or a named container port.
nodePortis a port exposed on cluster nodes for a Service of typeNodePort(or typically as part of aLoadBalancerService). External reachability depends on network and firewall configuration.
Example
yaml_snippet
YAML · 12 lines|
| apiVersion: v1 |
| kind: Service |
| metadata: |
| name: web |
| spec: |
| type: NodePort |
| selector: |
| app: web |
| ports: |
| - port: 8080 |
| targetPort: 80 |
| nodePort: 30080 |
A client inside the cluster can connect to
web:8080. A client with network access to a node can connect to <node-ip>:30080; the Service forwards traffic to port 80 on an eligible backend Pod. The Pod application must actually listen on that port. Dockerfile EXPOSE does not itself open a port or configure a Kubernetes Service.The allowed NodePort range is cluster-configurable; the usual default is 30000–32767. Service traffic may be implemented by kube-proxy or another compatible data plane.
✦ 卷终 · 柯航存卷 ✦