NodePort


Table of contents

  1. What is NodePort
  2. NodePort Service creating

What is NodePort

NodePort - Service type wich expose a static port on each Node’s IP. The Kubernetes provide port from a range 30000 - 32627 by default. Using a NodePort gives you the freedom to set up your own load balancing solution, to configure environments that are not fully supported by Kubernetes, or even to just expose one or more nodes’ IPs directly.

NodePort Service creating

We can create NodePort Service with two ways:

1) Create a yaml file called for example kube-svc.yaml with the following listing’s contents:

apiVersion: v1
kind: Service
metadata:
  name: kube-svc
spec:
  type: NodePort  
  ports:
    - port: 80
      targetPort: 8080
  selector:
    app: kube

Use kubectl create -f kube-svc.yaml command to create NodePort Service.

2) If you already created Service you can edit it with command:

  • kubectl edit svc kube-svc.

Make nessesary changes and saved. It will be automatically applyed to your cluster.

Now you can list all Service resources in your namespace and see that an internal cluster IP has been assigned to your service. To see it use the command:

  • kubectl get scv

Now we can use command: kubectl describe svc kube-svc to see NodePort type in our Service’s details.