Skip to content

Expose pods: Creating Nodeport service

Networking in Kubernetes it´s not a simple matter but it is possible to advance a little beginning for a simple step: expose the pods, and make it accessible from a web browser.

Pods with nginx servers created in the last article are in a subnet different not the 192.168.0.X used in the previous examples ...Why??

So we have pods running nginx in a flat, cluster-wide, address space. In theory, you could talk to these pods directly, but what happens when a node dies? The pods die with it, and the ReplicaSet inside the Deployment will create new ones, with different IPs. This is the problem a Service solves.

A Kubernetes Service is an abstraction that defines a logical set of Pods running somewhere in your cluster, that all provide the same functionality. When created, each Service is assigned a unique IP address (also called clusterIP). This address is tied to the lifespan of the Service, and will not change while the Service is alive. Pods can be configured to talk to the Service, and know that communication to the Service will be automatically load-balanced out to some pod that is a member of the Service.

Steps to access the POD from outside the cluster using Nodeport

Create a Kubernetes pod

For instance, we create an Nginx pod and try to access it outside the world.

  1. create a YAML file using your favorite text editor

  2. nginx sample yaml file.

    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx
      labels:
        name: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
    

nginx.yaml

*We tied pods with services using Labels.

  1. create a pod using below comment

    kubectl create -f <filename.yaml>
    
  2. To check pod is created or not.

    kubectl get pod
    

Create a service yaml file for ngnix using nodeport

To create a service.yaml file.

    apiVersion: v1
    kind: Service
    metadata:
      name: nginx
      labels:
        name: nginx
    spec:
      type: NodePort
      ports:
        - port: 80
          nodePort: 30080
          name: http
        - port: 443
          nodePort: 30443
          name: https
      selector:
        name: nginx

The service.yaml file we used Nodeport as a 30080.

*Nodeport range: 30,000 TO 32767

The selector work here is to choose a specific pod among many pods, so here we give the Nginx pod labels name so selector only chooses Nginx pod.

  1. Create a service file.

    kubectl create -f service.yaml
    
  2. To check service is created or not use below cmd.

     kubectl get svc
    
     NAME            TYPE           CLUSTER-IP     EXTERNAL-IP                                           PORT(S)                      AGE
     kubernetes      ClusterIP      10.43.0.1      <none>                                                443/TCP                      29h
     nginx           NodePort       10.43.58.151   <none>                                                80:30080/TCP,443:30443/TCP   7m49s
     nginx-project   LoadBalancer   10.43.83.4     192.168.0.80,192.168.0.81,192.168.0.82,192.168.0.83   80:30751/TCP                 54m
    
  3. To cross-check the Nodeport you must describe the svc using below cmd.

    kubectl describe svc <servicename>
    
    Name:                     nginx
    Namespace:                default
    Labels:                   name=nginx
    Annotations:              <none>
    Selector:                 name=nginx
    Type:                     NodePort
    IP Family Policy:         SingleStack
    IP Families:              IPv4
    IP:                       10.43.58.151
    IPs:                      10.43.58.151
    Port:                     http  80/TCP
    TargetPort:               80/TCP
    NodePort:                 http  30080/TCP
    Endpoints:                10.42.2.7:80
    Port:                     https  443/TCP
    TargetPort:               443/TCP
    NodePort:                 https  30443/TCP
    Endpoints:                10.42.2.7:443
    Session Affinity:         None
    External Traffic Policy:  Cluster
    Events:                   <none>
    

Verification:

Open the web browser from the local machine and access type the Kubernetes node IP (one of the external ip provided by load balancer) along with the Node port (30080 in this example).

We are able to access the Nginx homepage successfully. It’s a containerized web server listening in the port 80 and we mapped it to 30080 in every node in the cluster.

browser