Published on

Microservices with Java, Kubernetes, Microk8s, and Kong Gateway with a simple User Management Application

Authors

Alright, my friend, fasten your seatbelt and prepare for an epic adventure through the realms of Java, Kubernetes, microk8s, and Kong Gateway. Together, we'll conquer the world of microservices and user management! Let's dive right in.

First, let's meet our three trusty services: employee management, employee promotion, and employee resignation. We'll implement them using the powerful Spring Boot framework.

  1. Employee Management Service:
@RestController
@RequestMapping("/employees")
public class EmployeeManagementController {

    @GetMapping("/{id}")
    public ResponseEntity<Employee> getEmployeeById(@PathVariable String id) {
        // Logic to fetch employee details from the database
        Employee employee = employeeService.getEmployeeById(id);
        
        if (employee == null) {
            return ResponseEntity.notFound().build();
        }
        
        return ResponseEntity.ok(employee);
    }

    @PostMapping
    public ResponseEntity<Employee> createEmployee(@RequestBody Employee employee) {
        // Logic to create a new employee in the database
        Employee createdEmployee = employeeService.createEmployee(employee);
        
        return ResponseEntity.status(HttpStatus.CREATED).body(createdEmployee);
    }

    // Additional methods for updating and deleting employees
}
  1. Employee Promotion Service:
@RestController
@RequestMapping("/promotions")
public class EmployeePromotionController {

    @PostMapping("/{id}")
    public ResponseEntity<Employee> promoteEmployee(@PathVariable String id) {
        // Logic to promote an employee in the database
        Employee promotedEmployee = employeeService.promoteEmployee(id);
        
        if (promotedEmployee == null) {
            return ResponseEntity.notFound().build();
        }
        
        return ResponseEntity.ok(promotedEmployee);
    }

    // Additional methods for managing promotions
}
  1. Employee Resignation Service:
@RestController
@RequestMapping("/resignations")
public class EmployeeResignationController {

    @PostMapping("/{id}")
    public ResponseEntity<String> handleResignation(@PathVariable String id) {
        // Logic to handle employee resignation
        // Send resignation notification, update employee status, etc.
        
        return ResponseEntity.ok("Resignation handled successfully");
    }

    // Additional methods for managing resignations
}

These delightful code snippets demonstrate the implementation of our three services using Spring Boot's magic. Each service has its own controller with carefully crafted endpoints and business logic.

Now, let's move on to the deployment stage. We'll utilize the lightweight microk8s for our local Kubernetes cluster. Here's what you need to do:

  1. Install microk8s: Follow the official microk8s documentation to install and configure it on your machine.

  2. Create YAML manifests for the services: Declare the deployment and service configurations for each service in separate YAML files. Here's an example for the Employee Management Service:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: employee-management
spec:
  replicas: 1
  selector:
    matchLabels:
      app: employee-management
  template:
    metadata:
      labels:
        app: employee-management
    spec:
      containers:
        - name: employee

-management
          image: your-employee-management-image:tag
          ports:
            - containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: employee-management
spec:
  selector:
    app: employee-management
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  1. Apply the YAML files: Use the kubectl apply -f <filename> command to apply the YAML files and deploy the services to microk8s. Repeat this step for each service.
kubectl apply -f employee-management.yaml
kubectl apply -f employee-promotion.yaml
kubectl apply -f employee-resignation.yaml

Fantastic! Our microservices are now happily running in the microk8s cluster.

But wait, there's more! Let's take our API management to the next level by integrating Kong Gateway. Follow these steps:

  1. Install Kong Gateway: Refer to the Kong Gateway documentation for installation instructions tailored to your environment.

  2. Declare Kong as the Ingress Controller: In your Kubernetes manifest file (e.g., kong.yaml), define Kong as the ingress controller.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: kong-proxy
spec:
  replicas: 1
  selector:
    matchLabels:
      app: kong-proxy
  template:
    metadata:
      labels:
        app: kong-proxy
    spec:
      containers:
        - name: kong-proxy
          image: kong:latest
          ports:
            - containerPort: 8000
            - containerPort: 8443
            - containerPort: 8001
            - containerPort: 8444
  1. Create Kong Services and Routes: Define Kong services and routes for each of our user management services. These configurations tell Kong how to map incoming requests to the corresponding services.
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
  name: employee-management-plugin
config:
  service_id: <employee-management-service-id>
plugin: key-auth

---

apiVersion: configuration.konghq.com/v1
kind: KongIngress
metadata:
  name: employee-management-ingress
spec:
  rules:
    - http:
        paths:
          - path: /employees
            method: GET
            backend:
              serviceName: employee-management
              servicePort: 80
          - path: /employees
            method: POST
            backend:
              serviceName: employee-management
              servicePort: 80
          # Add more paths for other operations

---

# Repeat the above sections for employee-promotion and employee-resignation
  1. Apply Kong configurations: Deploy the Kong configurations to your microk8s cluster using the kubectl apply -f <filename> command.
kubectl apply -f kong.yaml
kubectl apply -f kong-services.yaml

Marvelous! Our services are now integrated with Kong Gateway, providing advanced API management capabilities.

To access your services through Kong, utilize Kong's proxy URL. For example, if Kong is exposed at http://localhost:8000, you can access the employee management service at http://localhost:8000/employees.

Congratulations, my Java-savvy friend! You've successfully mastered microservices with Java, Kubernetes, microk8s, and Kong Gateway. Now, go forth, explore the endless possibilities of microservice architectures, and may your laughter be as infinite as your coding prowess!