Difference between targetPort and port in Kubernetes Service definition

前端 未结 7 1326
别跟我提以往
别跟我提以往 2020-12-02 04:56

A Kubernetes Service can have a targetPort and port in the service definition:

kind: Servi         


        
相关标签:
7条回答
  • 2020-12-02 05:17

    The answer given above by @Manikanta P is correct. However, the explanation of "Port" might be a little unclear at first reading. I will explain with an example:

    Consider a Web-Application with its static content (front-page, images etc) hosted by httpd and the dynamic content (eg. response to requests, etc.) hosted by tomcat. The Webserver (or the static content) is served by httpd at port 80 while Appserver (or the dynamic content) is served by tomcat at port 8080.

    What a developer wants: User should be able to access the Webserver from outside BUT not the Appserver from outside.

    Solution: The service-type of Webserver in its service.yml will be NodePort while the service-type of Appserver in its service.yml will be ClusterIP.

    Code for webserver's service.yml:

    spec:
      selector:
        app: Webserver
      type: NodePort        // written to make this service accessible from outside.
      ports:
        - nodePort: 30475   // To access from outside, type <host_IP>:30475 in browser.
          port: 5050        // (ignore for now, I will explain below).
          protocol: TCP
          targetPort: 80  // port where httpd runs inside the webserver pod.
    

    Code for Appserver's service.yml

    spec:
      selector:
        app: appserver
      type: ClusterIP        // written to make this service NOT accessible from outside.
      ports:
        - port: 5050         // port to access this container internally
          protocol: TCP
          targetPort: 8080   // port where tomcat runs inside the appserver pod.
    

    Also Note, in the httpd.conf file of the Webserver, we will write the IP that redirects a user's request to the appserver. This IP will be: host_IP:5050.

    What exactly is happening here? A user writes hostIP:30475 and sees the Webserver's page. This is because it is being served by httpd at port 80 (targetport). When a user clicks a button, a request is made. This request is redirected to the Appserver because in httpd.conf file, the port 5050 is mentioned and this is the port where Appserver's container and Webserver's conatainer communicate internally. When the appserver receives the request, it is able to serve the request because of tomcat running inside it at port 8080.

    0 讨论(0)
  • 2020-12-02 05:21

    if container listens on port 9376, then targetPort: 9376

    if a service listens on port 80, then port: 80

    Then service ports config looks like below

    ports:
     - protocol: TCP
       port: 80
       targetPort: 9376
    

    Finally, request received to the service’s port, and forwarded on the targetPort of the pod.

    0 讨论(0)
  • 2020-12-02 05:27

    It helps me to think of things from the perspective of the service.

    • nodePort: The port on the node where external traffic will come in on
    • port: The port of this service
    • targetPort The target port on the pod(s) to forward traffic to

    Traffic comes in on nodePort, forwards to port on the service which then routes to targetPort on the pod(s).

    It's worth emphasizing more that nodePort is for external traffic. Other pods in the cluster that may need to access the service will just use port, not nodePort as it's internal only access to the service.

    Also worth noting that if targetPort is not set, it will default to the same value as port. E.g. 80:80 for service port 80 targeting container port 80.

    0 讨论(0)
  • 2020-12-02 05:28

    In nutshell

    nodeport: Listens external request on all worker nodes on nodeip:port and forward the request to port.

    port: Internal cluster service port for container and listens incoming request from the nodeport and forward to targetPort.

    targetPort: Receive the request from port and forwards to container pod(port) where it's listening. even if you don't specify this will get by default assigned the same port numbers as port.

    0 讨论(0)
  • 2020-12-02 05:30

    This answer is to reference Kubernetes' documentation in addition to the other answers:

    https://kubernetes.io/docs/concepts/services-networking/connect-applications-service/:

    targetPort: is the port the container accepts traffic on,

    port: is the abstracted Service port, which can be any port other pods use to access the Service

    https://kubernetes.io/docs/concepts/services-networking/service/:

    Port definitions in Pods have names, and you can reference these names in the targetPort attribute of a Service. This works even if there is a mixture of Pods in the Service using a single configured name, with the same network protocol available via different port numbers.

    0 讨论(0)
  • 2020-12-02 05:40

    "Target port" is the port on which your container is running.

    Port : port redirects the traffic to the container from the service.

    Exposing the deployment

      master $ kubectl get deployments
    NAME         READY   UP-TO-DATE   AVAILABLE   AGE
    
    nginx        1/1     1            1           31s
    master $ kubectl expose deployment nginx --name=nginx-svc --port=8080 --target-port=80
    service/nginx-svc exposed
    
    master $ kubectl get svc
    
    NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
    
    nginx-svc    ClusterIP   10.107.209.151   <none>        8080/TCP   5s
    

    NodePort : is the port that enables the service to access the externally.

    Hope this answers.

    0 讨论(0)
提交回复
热议问题