DEV Community

Cover image for Kubernetes Services: Understanding NodePort and ClusterIP
Jensen Jose
Jensen Jose

Posted on

Kubernetes Services: Understanding NodePort and ClusterIP

Welcome back to my blog series on CK 2024! This is the ninth post in the series where we dive deep into various Kubernetes services such as ClusterIP, NodePort, ExternalName, and LoadBalancer. In this post, we'll explore how these services work and their importance in a Kubernetes environment.

Recap from Previous Posts

In our last post, we discussed ReplicaSets, ReplicationControllers, and Deployments. We learned how to create a deployment with multiple pods running an Nginx front-end application. However, we didn't expose this deployment to external users, which is what we'll focus on today using Kubernetes services.

Introduction to Kubernetes Services

Kubernetes services enable communication between various components within and outside a Kubernetes cluster. They provide a way to expose applications running on a set of pods, ensure stable IP addresses, and load balance traffic across multiple pods. The main types of services in Kubernetes are:

  1. ClusterIP
  2. NodePort
  3. ExternalName
  4. LoadBalancer

Image description

Why Use Kubernetes Services?

Services in Kubernetes make your application components loosely coupled, allowing them to communicate with each other efficiently and be accessible to external users. They ensure that your pods, which can be dynamically created and destroyed, are always reachable.

NodePort Service

A NodePort service exposes your application on a static port on each node's IP address. This service allows external users to access your application by requesting the node's IP address and the NodePort.

How NodePort Works

Let's say we have a front-end application running on a pod. We want to expose this application externally. We do this by creating a NodePort service:

  • NodePort: A static port on each node's IP (e.g., 30001).
  • Port: The port exposed by the service within the cluster (e.g., 80).
  • TargetPort: The port on which the application is running inside the pod (e.g., 80).

The traffic flow will be:
External user -> Node IP -> Service Port -> TargetPort (Application Pod)

Example NodePort YAML

apiVersion: v1
kind: Service
metadata:
  name: nodeport-service
  labels:
    env: demo
spec:
  type: NodePort
  selector:
    env: demo
  ports:
    - port: 80
      targetPort: 80
      nodePort: 30001
Enter fullscreen mode Exit fullscreen mode

Create the service using:

kubectl apply -f nodeport.yaml
Enter fullscreen mode Exit fullscreen mode

Verify the service creation:

kubectl get svc
Enter fullscreen mode Exit fullscreen mode

You should see the service listed with its ClusterIP, NodePort, and target ports.

ClusterIP Service

A ClusterIP service is the default service type in Kubernetes. It provides a stable internal IP address accessible only within the cluster. This service is used for internal communication between different pods.

How ClusterIP Works

ClusterIP allows your front-end application to communicate with back-end services or databases without exposing them to external networks. When you create a ClusterIP service, Kubernetes assigns it an internal IP address and sets up routing rules to direct traffic to the appropriate pods.

Example ClusterIP YAML

apiVersion: v1
kind: Service
metadata:
  name: clusterip-service
  labels:
    app: my-app
spec:
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
Enter fullscreen mode Exit fullscreen mode

Create the service using:

kubectl apply -f clusterip.yaml
Enter fullscreen mode Exit fullscreen mode

Verify the service creation:

kubectl get svc
Enter fullscreen mode Exit fullscreen mode

You should see the service listed with its ClusterIP and ports.

Conclusion

Understanding and correctly configuring services is crucial for managing communication within a Kubernetes cluster. NodePort and ClusterIP services serve different purposes but are both essential for building scalable and robust applications.

In the next post, we will dive deeper into Multi-container commands and namespaces. Stay tuned!

For further reference, check out the detailed YouTube video here:

Top comments (0)