Kubernetes Nodeport Example

Kubernetes Nodeport Example

Kubernetes Nodeport

NodePort, as the name implies, opens a specific port on all the Nodes (the VMs). Nodeport Exposes the Service on each Node’s IP at a static port or A NodePort is an open port on every node of your cluster. any traffic that is sent to this port is forwarded to the service.

the Kubernetes controller allocates a port from a range specified by (typically 30000–32767).

We can access the application or service from outside the cluster, by requesting <any NodeIP>:<NodePort>

If you don’t specify this port, it will pick a random port range from (typically 30000–32767). If you want a specific port number, you can specify a value in the nodePort field.

NodePort Services are easy to create but hard to secure since its open the same port in all the nodes to public. and standard ports such as 80, 443 or 8443 are cannot be used. We can use ports only in a range between 3000-32767 and  if the ip of the vm/nodes changes then we have to deal this issue, so we don’t recommend this node port service in production environments.

Creating NodePort service

In any type of service we will expose the application running pods to outside world. So to create/expose the service we need pods. so lets create pods.

Deploy Pod:

using the kubectl commands we an create pods or we can use yml file. here i am creating pods with kubectl commands

kubectl run mypod --generator=run-pod/v1 --image=nginx --port=80 --labels="myapp=mynginxapp"


master $ kubectl run mypod  --generator=run-pod/v1 --image=nginx --port=80 --labels="myapp=mynginxapp"
pod/mypod created

Here ‘mypod’ is the pod name and ‘myapp=mynginxapp’ is the label attached to this pod.

Deploy Service:

here using yml file i am creating or exposing the service. save this code in one yml file and using kubectl create -f <ymlfile> we can create the service.

apiVersion: v1
kind: Service
metadata:
  name: mynginxsvc
spec:
  type: NodePort
  ports:
    - port: 80
      nodePort: 30180
      name: http
    - port: 443
      nodePort: 31443
      name: https
  selector:
    myapp: mynginxapp

Here port 80 is the port of this service. And service port 80 is mapped to nodeport 30180. And here using selector we are selecting the pods that labelled with ‘myapp: mynginxapp’. So this service will handle the pods which are labelled with ‘myapp: mynginxapp’.

Note: We can create the pods using service manifest file itself. If you want to create pods using service manifest file, add template section in above service manifest file. So no need to create pods using command line/above deploy pod method.

master $ kubectl create -f abcd.yml
service/mynginxsvc created

List the services:

using the kubectl get command we can list the services.

master $ kubectl get svc

NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                      AGE         
mynginxsvc   NodePort    10.106.119.102   <none>        80:30180/TCP,443:31443/TCP   54s

Here you can see service port is mapped to 30180 port of node.

Find the Node on which Pod is running

master $ kubectl get pods -o wide

NAME                     READY   STATUS    RESTARTS   AGE   IP          NODE     NOMINATED NODE   READINESS GATES
mypod                    1/1     Running   0          12m   10.40.0.2   node01   <none>           <none>

Here you can see the pod is running node01. To access the application, in browser enter <public ip of node 01>:30180, you can access nginx webpage.

The ip which you are seeing in above section is private ip of node01 not public ip.

 

Leave a Reply

Your email address will not be published. Required fields are marked *