Analysis of k8s Easy mixing point: nodeport, Port, Targetport

Source: Internet
Author: User
1. Nodeport

One way in which external traffic accesses a service entry in a k8s cluster (the other is LoadBalancer), that is, Nodeip:nodeport is a portal that provides access to external traffic to the service in the k8s cluster.

For example, if an external user wants to access a Web application in a k8s cluster, then we can configure the type=nodeport,nodeport=30001 of the corresponding service. Other users can access the Web service through a browser http://node:30001.

and services such as databases may not need to be accessed by the outside world, only by internal service access, then we do not have to set the service Nodeport. 2. Port

k8s access to service between intra-cluster services. That is, Clusterip:port is the port on which the service is exposed on Clusterip.

The MySQL container exposes 3306 ports (refer to Dockerfile), other containers in the cluster access the MySQL service via 33306 ports, but external traffic cannot access the MySQL service because the MySQL service does not have configuration nodeport. The corresponding service.yaml are as follows:

APIVERSION:V1
kind:service
metadata:
 name:mysql-service
Spec:
 ports:
 -port:33306
   targetport:3306
 selector:
  name:mysql-pod
3. Targetport

The port of the container (the final traffic port). The Targetport is the port on the pod, and the traffic from port and Nodeport flows through the kube-proxy into the targetport of the backend pod and finally into the container.

Consistent with the ports exposed when the container was made (using expose in dockerfile), such as the official Nginx (reference Dockerfile) exposes 80 ports. The corresponding service.yaml are as follows:

APIVERSION:V1
kind:service
metadata:
 name:nginx-service
Spec:
 type:nodeport         // With configuration Nodeport, external traffic can access the services in k8s
 ports:
 -port:30080          //service access Port
   targetport:80       //container port
   nodeport : 30001      //Nodeport
 selector:
  name:nginx-pod
4. Summary

In general, both port and Nodeport are service ports that are exposed to k8s cluster internal services access, which is exposed to k8s cluster external traffic access. Data coming from the top two ports need to go through the reverse proxy kube-proxy, into the targetport of the backend pod, and finally to the container inside the pod. 5. Reference Articles

The difference and significance of Nodeport,targetport,port in Kubernetes

Kubernetes in port, target port, node port, and Kube-proxy proxy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.