Setting Up Cluster IP and NodePort Services

Tutorial 2 of 5

Introduction

In this tutorial, we'll walk you through the process of setting up ClusterIP and NodePort services in a Kubernetes cluster. ClusterIP and NodePort are two of the main types of Services in Kubernetes that allow you to expose your applications to the outside world.

By the end of this tutorial, you will understand:
- What are ClusterIP and NodePort services and how they work
- How to create and configure ClusterIP and NodePort services

Prerequisites:
- Basic understanding of Kubernetes
- A running Kubernetes cluster

Step-by-Step Guide

ClusterIP

ClusterIP is the default type of Service in Kubernetes. It exposes the Service on a cluster-internal IP, making the Service only reachable from within the cluster.

Here's how to create a ClusterIP service:

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: MyApp
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9376

In this example, the Service my-service is created to represent an existing MyApp application. It serves on TCP port 80, and routes traffic to the targetPort 9376 of the Pods selected by the app: MyApp label.

NodePort

A NodePort service is the most elementary way to get external traffic directly to your service. NodePort, as the name implies, opens a specific port on all the Nodes (the VMs), and any traffic that is sent to this port is forwarded to the service.

Here's how to create a NodePort service:

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  type: NodePort
  selector:
    app: MyApp
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9376
      nodePort: 30036

In this example, the service my-service is exposed outside the cluster using the NodePort. Traffic sent to the external port (30036) on any node in the cluster is routed to the service.

Code Examples

Here is a detailed example of a NodePort service:

apiVersion: v1
kind: Service
metadata:
  name: my-nodeport-service
spec:
  type: NodePort
  selector:
    app: MyApp
  ports:
    - name: http
      protocol: TCP
      port: 80
      targetPort: 9376
      nodePort: 30036

In this example, the my-nodeport-service allows external applications to access the MyApp application inside the Kubernetes cluster. Traffic sent to the external port (30036) on any node in the cluster is routed to TCP port 9376 of the pods selected by the app: MyApp label.

Summary

In this tutorial, we've covered the basics of ClusterIP and NodePort services in Kubernetes. You've learned how to expose your applications within your Kubernetes cluster and to the outside world.

To further your understanding, you can explore the other types of services in Kubernetes, such as LoadBalancer and ExternalName.

Practice Exercises

  1. Create a ClusterIP service that routes traffic to a set of pods labeled app: MyWebApp on port 8080.

  2. Create a NodePort service that routes external traffic on port 30080 to a set of pods labeled app: MyWebApp on port 8080.

  3. Experiment with changing the type field in your Service definition between ClusterIP and NodePort, and observe the differences in behavior.

Remember to review the Kubernetes documentation and other resources to learn more about Services and their usage in Kubernetes.