codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🍃

Spring Boot

49 / 62 topics
47Kubernetes Basics for Spring Boot Applications48Creating a Kubernetes Deployment49Using Kubernetes Services50Using ConfigMaps in Kubernetes51Using Secrets in Kubernetes
Tutorials/Spring Boot/Using Kubernetes Services
🍃Spring Boot

Using Kubernetes Services

Updated 2026-04-20
3 min read

Using Kubernetes Services

Kubernetes is a powerful container orchestration platform that simplifies deploying, scaling, and managing containerized applications. In this tutorial, we will explore how to use Kubernetes services with Spring Boot applications. We'll cover the basics of Kubernetes services, different types of services, and best practices for using them in a production environment.

Introduction to Kubernetes Services

In Kubernetes, a service is an abstraction that defines a logical set of pods and a policy by which to access them. Services enable a loose coupling between dependent Pods, making it easier to manage and scale your applications.

Key Concepts

  • Pods: The smallest deployable units in Kubernetes, consisting of one or more containers.
  • Services: Provide a stable network endpoint for a set of pods.
  • Labels: Key-value pairs used to identify and select groups of resources.
  • Selectors: Rules that determine which Pods a Service targets.

Types of Kubernetes Services

Kubernetes supports several types of services, each serving different use cases:

  1. ClusterIP (Default): Exposes the service on an internal IP within the cluster. This is the default type and is suitable for internal communication between services.
  2. NodePort: Exposes the service on each Node's IP at a static port. A ClusterIP service is also created automatically. This allows external access to the service using <NodeIP>:<NodePort>.
  3. LoadBalancer: Creates an external load balancer in the cloud provider, which routes traffic to the Nodes where your Pods are running.
  4. ExternalName: Maps a Service to a DNS name, usually for external services.

Setting Up Kubernetes Services with Spring Boot

Let's walk through setting up a simple Spring Boot application and exposing it using different types of Kubernetes services.

Step 1: Create a Simple Spring Boot Application

First, create a basic Spring Boot application. You can use Spring Initializr to bootstrap your project:

curl https://start.spring.io/starter.zip -d dependencies=web -d baseDir=spring-boot-app -d bootVersion=2.7.5 -d javaVersion=11 -d packaging=jar -d name=demo -d description="Demo Project" -d type=maven-project | unzip
cd spring-boot-app

Add a simple REST controller:

package com.example.demo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String sayHello() {
        return "Hello, Kubernetes!";
    }
}

Step 2: Containerize the Application

Create a Dockerfile to containerize your Spring Boot application:

FROM openjdk:11-jre-slim
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]

Build the Docker image and push it to a registry (e.g., Docker Hub):

docker build -t your-dockerhub-username/spring-boot-app:latest .
docker push your-dockerhub-username/spring-boot-app:latest

Step 3: Deploy the Application to Kubernetes

Create a k8s directory and add a deployment manifest:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: spring-boot-app-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: spring-boot-app
  template:
    metadata:
      labels:
        app: spring-boot-app
    spec:
      containers:
      - name: spring-boot-app
        image: your-dockerhub-username/spring-boot-app:latest
        ports:
        - containerPort: 8080

Apply the deployment:

kubectl apply -f k8s/deployment.yaml

Step 4: Create a ClusterIP Service

Create a clusterip-service.yaml file:

apiVersion: v1
kind: Service
metadata:
  name: spring-boot-app-clusterip
spec:
  selector:
    app: spring-boot-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080

Apply the service:

kubectl apply -f k8s/clusterip-service.yaml

Step 5: Create a NodePort Service

Create a nodeport-service.yaml file:

apiVersion: v1
kind: Service
metadata:
  name: spring-boot-app-nodeport
spec:
  type: NodePort
  selector:
    app: spring-boot-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
      nodePort: 30007

Apply the service:

kubectl apply -f k8s/nodeport-service.yaml

Step 6: Create a LoadBalancer Service

Create a loadbalancer-service.yaml file:

apiVersion: v1
kind: Service
metadata:
  name: spring-boot-app-loadbalancer
spec:
  type: LoadBalancer
  selector:
    app: spring-boot-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080

Apply the service:

kubectl apply -f k8s/loadbalancer-service.yaml

Step 7: Accessing the Services

  • ClusterIP: Accessible only within the cluster using the service name (spring-boot-app-clusterip) and port 80.
  • NodePort: Accessible from outside the cluster using any Node's IP address and the node port (30007).
  • LoadBalancer: Accessible via a cloud provider's load balancer, typically with an external IP.

Best Practices

  1. Use Labels for Selectors: Ensure that your deployment labels match the service selectors to maintain consistency.
  2. Define Resource Limits and Requests: Specify resource limits and requests in your pod specifications to prevent resource exhaustion.
  3. Implement Health Checks: Use readiness and liveness probes to ensure your application is healthy before routing traffic.
  4. Secure Services: Use network policies to restrict access to services, enhancing security.
  5. Monitor and Log: Implement monitoring and logging solutions to keep track of service performance and troubleshoot issues.

Conclusion

Kubernetes services are essential for managing and exposing your Spring Boot applications in a production environment. By understanding the different types of services and best practices, you can effectively orchestrate your applications and ensure they are scalable, secure, and reliable.


PreviousCreating a Kubernetes DeploymentNext Using ConfigMaps in Kubernetes

Recommended Gear

Creating a Kubernetes DeploymentUsing ConfigMaps in Kubernetes