Shahzad Bhatti Welcome to my ramblings and rants!

March 24, 2025

K8 Highlander: Managing Stateful and Singleton Processes in Kubernetes

Filed under: Technology — Tags: , — admin @ 2:22 pm

Introduction

Kubernetes has revolutionized how we deploy, scale, and manage applications in the cloud. I’ve been using Kubernetes for many years to build scalable, resilient, and maintainable services. However, Kubernetes was primarily designed for stateless applications – services that can scale horizontally. While such shared-nothing architecture is must-have for most modern microservices but it presents challenges for use-cases such as:

  1. Stateful/Singleton Processes: Applications that must run as a single instance across a cluster to avoid conflicts, race conditions, or data corruption. Examples include:
    • Legacy applications not designed for distributed operation
    • Batch processors that need exclusive access to resources
    • Job schedulers that must ensure jobs run exactly once
    • Applications with sequential ID generators
  2. Active/Passive Disaster Recovery: High-availability setups where you need a primary instance running with hot standbys ready to take over instantly if the primary fails.

Traditional Kubernetes primitives like StatefulSets provide stable network identities and ordered deployment but don’t solve the “exactly-one-active” problem. DaemonSets ensure one pod per node, but don’t address the need for a single instance across the entire cluster. This gap led me to develop K8 Highlander – a solution that ensures “there can be only one” active instance of your workloads while maintaining high availability through automatic failover.

Architecture

K8 Highlander implements distributed leader election to ensure only one controller instance is active at any time, with others ready to take over if the leader fails. The name “Highlander” refers to the tagline from the 1980s movie & show: “There can be only one.”

Core Components

K8 Highlander Architecture

The system consists of several key components:

  1. Leader Election: Uses distributed locking (via Redis or a database) to ensure only one controller is active at a time. The leader periodically renews its lock, and if it fails, another controller can acquire the lock and take over.
  2. Workload Manager: Manages different types of workloads in Kubernetes, ensuring they’re running and healthy when this controller is the leader.
  3. Monitoring Server: Provides real-time metrics and status information about the controller and its workloads.
  4. HTTP Server: Serves a dashboard and API endpoints for monitoring and management.

How Leader Election Works

The leader election process follows these steps:

  1. Each controller instance attempts to acquire a distributed lock with a TTL (Time-To-Live)
  2. Only one instance succeeds and becomes the leader
  3. The leader periodically renews its lock to maintain leadership
  4. If the leader fails to renew (due to crash, network issues, etc.), the lock expires
  5. Another instance acquires the lock and becomes the new leader
  6. The new leader starts managing workloads

This approach ensures high availability while preventing split-brain scenarios where multiple instances might be active simultaneously.

Workload Types

K8 Highlander supports four types of workloads:

  1. Process Workloads: Single-instance processes running in pods
  2. CronJob Workloads: Scheduled tasks that run at specific intervals
  3. Service Workloads: Continuously running services using Deployments
  4. Persistent Workloads: Stateful applications with persistent storage using StatefulSets

Each workload type is managed to ensure exactly one instance is running across the cluster, with automatic recreation if terminated unexpectedly.

Deploying and Using K8 Highlander

Let me walk through how to deploy and use K8 Highlander for your singleton workloads.

Prerequisites

  • Kubernetes cluster (v1.16+)
  • Redis server or PostgreSQL database for leader state storage
  • kubectl configured to access your cluster

Installation Using Docker

The simplest way to install K8 Highlander is using the pre-built Docker image:

HTML

This deploys K8 Highlander with your configuration, ensuring high availability with multiple replicas while maintaining the singleton behavior for your workloads.

Using K8 Highlander Locally for Testing

You can also run K8 Highlander locally for testing:

HTML

Basic Configuration

K8 Highlander uses a YAML configuration file to define its behavior and workloads. Here’s a simple example:

HTML

Example Workload Configurations

Let’s look at examples for each workload type:

Process Workload

Use this for single-instance processes that need to run continuously:

HTML

CronJob Workload

For scheduled tasks that should run exactly once at specified intervals:

HTML

Service Workload

For continuously running services that need to be singleton but highly available:

HTML

Persistent Workload

For stateful applications with persistent storage:

HTML

High Availability Setup

For production environments, run multiple instances of K8 Highlander to ensure high availability:

HTML

API and Monitoring Capabilities

K8 Highlander provides comprehensive monitoring, metrics, and API endpoints for observability and management.

Dashboard

Access the built-in dashboard at http://<controller-address>:8080/ to see the status of the controller and its workloads in real-time.

Leader Dashboard Screenshot

The dashboard shows:

  • Current leader status
  • Workload health and status
  • Redis/database connectivity
  • Failover history
  • Resource usage

API Endpoints

K8 Highlander exposes several HTTP endpoints for monitoring and integration:

  • GET /status: Returns the current status of the controller
  • GET /api/workloads: Lists all managed workloads and their status
  • GET /api/workloads/{name}: Gets the status of a specific workload
  • GET /healthz: Liveness probe endpoint
  • GET /readyz: Readiness probe endpoint

Example API response:

HTML

Prometheus Metrics

K8 Highlander exposes Prometheus metrics at /metrics for monitoring and alerting:

HTML

Key metrics include:

  • Leadership status and transitions
  • Workload health and status
  • Redis/database operations
  • Failover events and duration
  • System resource usage

Grafana Dashboard

A Grafana dashboard is available for visualizing K8 Highlander metrics. Import the dashboard from the dashboards directory in the repository.

Advanced Features

Multi-Tenant Support

K8 Highlander supports multi-tenant deployments, where different teams or environments can have their own isolated leader election and workload management:

HTML
HTML

Each tenant has its own leader election process, so one controller can be the leader for tenant A while another is the leader for tenant B.

Multi-Cluster Deployment

For disaster recovery scenarios, K8 Highlander can be deployed across multiple Kubernetes clusters with a shared Redis or database:

HTML
HTML

If the primary cluster fails, a controller in the secondary cluster can become the leader and take over workload management.

Summary

K8 Highlander fills a critical gap in Kubernetes’ capabilities by providing reliable singleton workload management with automatic failover. It’s ideal for:

  • Legacy applications that don’t support horizontal scaling
  • Processes that need exclusive access to resources
  • Scheduled jobs that should run exactly once
  • Active/passive high-availability setups

The solution ensures high availability without sacrificing the “exactly one active” constraint that many applications require. By handling the complexity of leader election and workload management, K8 Highlander allows you to run stateful workloads in Kubernetes with confidence.

Where to Go from Here

K8 Highlander is an open-source project with MIT license, and contributions are welcome! Feel free to submit issues, feature requests, or pull requests to help improve the project.


No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URL

Sorry, the comment form is closed at this time.

Powered by WordPress