Skip to content

Service port mapping

Service port mapping defines the relationship and translation rules between the port exposed by a Kubernetes Service and the port opened on the container running inside a Pod^[400-devops__06-Kubernetes__k8s-ithelp__Day7__README.md].

Core Ports

In a Service configuration, specifically under the spec.ports field, there are three key port definitions that establish this mapping^[400-devops__06-Kubernetes__k8s-ithelp__Day7__README.md]:

  • port: The port number exposed on the Service itself (specifically the Cluster IP). This is the port other Pods or internal clients use to connect to the service^[400-devops__06-Kubernetes__k8s-ithelp__Day7__README.md].
  • targetPort: The port number on the Pod (or container) to which traffic is forwarded^[400-devops__06-Kubernetes__k8s-ithelp__Day7__README.md]. This allows the Service to abstract the container's actual listening port.
  • nodePort: A port number on the Node itself. This is used when the Service type is NodePort or LoadBalancer to allow external traffic to enter the cluster^[400-devops__06-Kubernetes__k8s-ithelp__Day7__README.md]. If not specified in the configuration, Kubernetes will automatically assign a valid port number^[400-devops__06-Kubernetes__k8s-ithelp__Day7__README.md].

Usage and Behavior

When defining a Service, you typically map a public or internal-facing port (port) to a specific container port (targetPort)^[400-devops__06-Kubernetes__k8s-ithelp__Day7__README.md]. For example, you might map Service port 8000 to targetPort 8080 where the application container is listening^[400-devops__06-Kubernetes__k8s-ithelp__Day7__README.md].

Kubernetes Services support multiple protocols, including TCP, SCTP, and UDP, with TCP being the default^[400-devops__06-Kubernetes__k8s-ithelp__Day7__README.md].

Sources

  • 400-devops__06-Kubernetes__k8s-ithelp__Day7__README.md