Kubernetes / Kubernetes Networking

Setting Up Cluster IP and NodePort Services

In this tutorial, we'll explore how to set up Cluster IP and NodePort services in your Kubernetes cluster. These services allow you to expose your applications within your cluster…

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Explains Kubernetes networking, DNS, and service discovery.

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.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

PDF Password Protector

Add or remove passwords from PDF files.

Use tool

Color Palette Generator

Generate color palettes from images.

Use tool

Case Converter

Convert text to uppercase, lowercase, sentence case, or title case.

Use tool

Percentage Calculator

Easily calculate percentages, discounts, and more.

Use tool

Fake User Profile Generator

Generate fake user profiles with names, emails, and more.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help