Software Development

Building a Resilient Kubernetes Operator for Managed Valkey and Redis Services

The architecture of modern managed database services is increasingly defined by the underlying orchestration layer, particularly when operating within the constraints of Kubernetes. For KVNest, a service provider specializing in managed Redis and Valkey instances, the technical journey to building a custom Kubernetes operator was driven by a fundamental operational constraint: the "Starter" plan’s reliance on non-persistent storage. Because these instances run with appendonly disabled and save set to an empty string, any pod restart results in the immediate, total loss of customer data. This design reality necessitated an operator architecture that rigorously separates configuration changes that require a pod restart from those that can be applied live.

The Architectural Foundation: Managing State Without Disruption

When KVNest began developing its infrastructure, existing third-party operators proved insufficient for a commercial, multi-tenant, and externally exposed product. The engineering team opted for a bespoke solution using the Kubebuilder framework. The resulting controller is remarkably lean, comprising fewer than 1,400 lines of Go code. This simplicity is a strategic choice, reducing the surface area for bugs and simplifying maintenance.

The system utilizes a single-object API model. By defining a custom resource—the ValkeyInstance—the team ensures that the Kubernetes API server serves as the "single source of truth." There is no secondary database to drift out of sync with the cluster state. The CRD (Custom Resource Definition) includes extensive validation logic via Common Expression Language (CEL) rules. This removes the need for a dedicated admission webhook, which can become a single point of failure. If the webhook goes down, the entire operator becomes paralyzed; by embedding validation directly into the CRD, the system remains robust even under heavy API server load.

Chronology and Component Lifecycle

The creation of a new instance is a strictly orchestrated process. Upon the creation of a ValkeyInstance object, the operator executes a nine-step deployment sequence:

Rotating a database password without a restart: how our Redis and Valkey Kubernetes operator works
  1. Namespace initialization
  2. ResourceQuota enforcement
  3. NetworkPolicy implementation
  4. Credentials Secret generation
  5. Configuration Secret generation
  6. Service definition
  7. StatefulSet deployment
  8. MiddlewareTCP creation
  9. IngressRouteTCP publication

This order is not incidental. By enforcing network policies and quotas before the workload is even scheduled, the operator prevents potential resource leakage or security vulnerabilities. Furthermore, the operator employs label-filtering for its informer cache. By only tracking objects labeled with "managed-by: kvnest," the operator avoids the performance overhead of monitoring every secret or service within a multi-tenant cluster.

The Reconcile Loop: Ensuring Stability and Uptime

The core of the operator is its reconciliation loop, which ensures the actual state of the cluster matches the desired state defined in the CRD. A critical challenge identified by the team was the ambiguity of the "Ready" status. In Kubernetes, a StatefulSet might report that a replica is ready while it is in the middle of a rolling update or a shutdown sequence. To prevent the premature synchronization of Access Control Lists (ACLs) to a departing pod, the operator implements a sophisticated "rolledOut" check. This function verifies the observed generation against the current revision to ensure the pod is fully functional before any management commands are issued.

Failed states are handled with a focus on self-healing. If an instance fails to transition to a ready state within ten minutes, it is marked as "Failed." However, this is not a terminal condition. The operator continues to attempt reconciliation, allowing the system to recover autonomously if the underlying issue—such as a transient network error or resource contention—is resolved.

Two-Tiered Configuration Management

Perhaps the most significant innovation in the KVNest operator is the dual-class approach to configuration. The configuration file (valkey.conf) is split into static and dynamic components. When a user modifies a setting that requires a restart—such as changing an eviction policy or enabling a module—the operator updates a hash annotation. This triggers a rolling restart of the StatefulSet.

Conversely, for routine security tasks like rotating a password or updating user permissions, the operator performs a live synchronization. It connects to the instance over the network, executes the ACL SETUSER command, and cleans up old credentials without ever touching the pod’s lifecycle. This ensures that a routine security rotation—which could occur frequently—does not result in the accidental purging of a customer’s volatile cache.

Rotating a database password without a restart: how our Redis and Valkey Kubernetes operator works

Security and Isolation in a Multi-Tenant Environment

Security in a shared environment is paramount. The operator enforces a "restricted" Pod Security Standard across all instances. Every pod runs as a non-root user (uid 999), with a read-only root filesystem and minimal Linux capabilities. Egress traffic is blocked entirely via NetworkPolicy, as a database instance should have no reason to initiate outbound connections.

The operator also manages an internal "kvnest" user for its own health checks and metrics collection. By isolating this internal user’s permissions from those granted to the customer, the operator ensures that a customer cannot inadvertently break the monitoring or readiness probes. Even the CLIENT KILL command is restricted, preventing a customer from intentionally causing a "Denial of Service" on the monitoring sidecar.

Data-Driven Performance and Future Outlook

The operator provides granular observability by exposing a time_to_ready histogram. This metric tracks the duration from initial object creation to the first Ready signal. By persisting this data in an annotation on the object, the operator ensures that statistics remain accurate even if the operator itself restarts or if the pod is recreated.

The current implementation has specific limitations that the engineering team acknowledges. As of November 2026, the operator does not handle automated replication, failover, or high-availability clusters. These features, while complex, represent the next frontier for the project. The decision to omit them initially was intentional, allowing the team to perfect the "single-node" lifecycle before tackling the significant distributed systems challenges inherent in multi-node Valkey replication.

Implications for the Industry

The KVNest operator serves as a case study in the efficacy of "opinionated" software engineering within the Kubernetes ecosystem. By prioritizing a lean codebase, declarative security, and a strict separation of configuration classes, the team has created a system that is both predictable and maintainable.

Rotating a database password without a restart: how our Redis and Valkey Kubernetes operator works

For other operators in the database space, the implications are clear: complexity is the enemy of reliability. By moving logic into the API server and treating configuration as a two-tiered system, operators can significantly reduce the "blast radius" of management operations. As managed services move further toward automated, self-healing architectures, the patterns pioneered here—particularly the careful management of readiness probes and the avoidance of unnecessary pod restarts—will likely become standard practice.

The success of this approach is evidenced by the "Starter" plan itself, which remains viable despite the extreme constraints of volatile storage. Through rigorous validation, careful state management, and a focus on the lifecycle of child objects, the operator provides a template for how to scale managed database services on Kubernetes while maintaining strict performance and security standards. Whether or not this architecture will scale to handle complex, multi-node clusters remains a question for future development, but for the current, single-instance use case, it represents a mature and disciplined engineering effort.

Related Articles

Back to top button
PlanMon
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.