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