liveBook · Manning

Node port

As explained in Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform

A Kubernetes Service type that exposes a service on a port of each cluster node for access from outside the cluster.

NodePort is the simplest Kubernetes Service type for making a locally deployed microservice reachable from outside the cluster. Without an explicit type, a Service defaults to ClusterIP and remains accessible only inside the cluster. section 6.8.3

Configure and use NodePort by:

  • Setting the Service type to NodePort in the deployment configuration. section 6.8.3
  • Choosing a port from Kubernetes’ 30000–32767 range, such as 30000, or omitting nodePort to receive an available port. section 6.8.3
  • Using kubectl get services to verify the assigned port when access fails. section 6.8.6
Listing 6.1 Deploying our microservice to Kubernetes
apiVersion: apps/v1
kind: Deployment                          #1
metadata:
  name: video-streaming                   #2
spec:
  replicas: 1                             #3
  selector:
    matchLabels:
      app: video-streaming                #4
  template:                               #5
    metadata:
      labels:
        app: video-streaming              #4
    spec:
      containers: 
      - name: video-streaming
        image: video-streaming:1          #6
        imagePullPolicy: Never            #7
        env:                              #8
        - name: PORT
          value: "4000"                   #9
---                                       #10
apiVersion: v1
kind: Service                             #11
metadata:
  name: video-streaming                   #12
spec:
  selector:
    app: video-streaming                  #4
  type: NodePort                          #13
  ports:
    - protocol: TCP
      port: 80
      targetPort: 4000                    #14
      nodePort: 30000                     #14

The deployment manifest shows a NodePort Service connected to the microservice through matching app labels and selectors. section 6.8.3

Testing a local service

After deploying the video-streaming microservice with nodePort 30000, open http://localhost:30000/video. For a gateway deployment, find the assigned NodePort and open http://localhost:<the-port-number>. section 6.8.6 section 10.8.3

NodePort is convenient for local development, but exposing services on a cloud-hosted cluster requires more care because outside access creates opportunities for attacks and abuse. section 6.8.3

How Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform covers it

How deep it goes
Explains Service selection, port-range configuration, local testing, and troubleshooting, but defers deeper debugging.
Examples
Demonstrates video streaming at localhost:30000/video and gateway testing through an assigned local NodePort.
What it assumes
Assumes a local Kubernetes instance and deployment manifest, while demonstrating Service type and port configuration.
What you can do after
Operate/configure a NodePort Service, test it from a browser, and diagnose port mismatches with kubectl.
How it connects
Connects Kubernetes Services with ClusterIP, Deployments, Pods, labels, selectors, and kubectl.

Also explained in

Compare how 6 books explain node port →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform. The section links point to the parts of the book it draws on.