NodePort is a basic Service type in Kubernetes. It adds node port mapping to intra-cluster access. This means the Service is exposed on each node's IP address at a static port. When you create a NodePort Service, Kubernetes automatically allocates a cluster-scoped IP address (ClusterIP). When clients outside the cluster access <node-IP>:<node-port>, the traffic will be forwarded to the target pod through the ClusterIP of the NodePort Service.
If pods require temporary access or the traffic is low, you can create a NodePort Service. For example, in a testing environment, you can use a NodePort Service when deploying and debugging a web application.
Figure 1 NodePort Service access

Parameter | Description |
|---|---|
Service Name | Enter a name, which can be the same as the workload name. |
Service Type | Select NodePort. |
Namespace | Select the namespace that the workload belongs to. |
Service Affinity | Whether to route external traffic to a local node or a cluster-wide endpoint. For details, see Service Affinity (externalTrafficPolicy).
|
Selector | The Service will be associated with the workload pods based on the label and direct traffic to the pods with this label. You can add a key and value for the pod label and click Confirm. You can also click Reference Workload Label to use the label of an existing workload. In the dialog box displayed, select a workload and click OK. |
IPv6 | This function is disabled by default. After this function is enabled, the cluster IP address of the Service changes to an IPv6 address. This function is available only in clusters of v1.15 or later with IPv6 enabled (set during cluster creation). |
Port |
|
You can configure Service access using kubectl. This section uses an Nginx workload as an example to describe how to configure a NodePort Service using kubectl.
vi nginx-deployment.yaml
File content:
apiVersion: apps/v1kind: Deploymentmetadata:name: nginxspec:replicas: 1selector:matchLabels:app: nginxtemplate:metadata:labels:app: nginxspec:containers:- image: nginx:latestname: nginximagePullSecrets:- name: default-secret
vi nginx-nodeport-svc.yaml
File content:
apiVersion: v1kind: Servicemetadata:labels:app: nginxname: nginx-nodeportspec:ports:- name: servicenodePort: 30000 # Node port. The value ranges from 30000 to 32767.port: 8080 # Port for accessing a Serviceprotocol: TCP # Protocol used for accessing a Service. The value can be TCP or UDP.targetPort: 80 # Port used by a Service to access the target container. This port is closely related to the applications running in a container. In this example, the Nginx image uses port 80 by default.selector: # Label selector. A Service selects a pod based on the label and forwards the requests for accessing the Service to the pod. In this example, select the pod with the app:nginx label.app: nginxtype: NodePort # Service type. NodePort indicates that the Service is accessed through a node port.
kubectl create -f nginx-deployment.yaml
If information similar to the following is displayed, the workload has been created:
deployment/nginx created
Check the created workload.
kubectl get pod
If information similar to the following is displayed, the workload is running:
NAME READY STATUS RESTARTS AGEnginx-2601814895-znhbr 1/1 Running 0 15s
kubectl create -f nginx-nodeport-svc.yaml
If information similar to the following is displayed, the Service is being created:
service/nginx-nodeport created
Check the created Service.
kubectl get svc
If information similar to the following is displayed, the Service has been created:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEkubernetes ClusterIP 10.247.0.1 <none> 443/TCP 4d8hnginx-nodeport NodePort 10.247.30.40 <none> 8080:30000/TCP 18s
Create a container in the cluster and access it using IP-address-of-the-node:node-port.
kubectl get node -owide
Command output:
NAME STATUS ROLES AGE INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME10.100.0.136 Ready <none> 152m 10.100.0.136 <none> CentOS Linux 7 (Core) 3.10.0-1160.25.1.el7.x86_64 docker://18.9.010.100.0.5 Ready <none> 152m 10.100.0.5 <none> CentOS Linux 7 (Core) 3.10.0-1160.25.1.el7.x86_64 docker://18.9.0
kubectl run -i --tty --image nginx:alpine test --rm /bin/sh
curl 10.100.0.136:30000
Command output:
/ # <!DOCTYPE html><html><head><title>Welcome to nginx!</title><style>body {width: 35em;margin: 0 auto;font-family: Tahoma, Verdana, Arial, sans-serif;}</style></head><body><h1>Welcome to nginx!</h1><p>If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.</p><p>For online documentation and support please refer to<a href="http://nginx.org/">nginx.org</a>.<br/>Commercial support is available at<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p></body></html>/ #