Learn how to deploy Redis Sentinel with HAProxy on Microk8s to build highly available, fault-tolerant Redis clusters with automatic failover and seamless traffic routing.
This blog builds on our previous MicroK8s guides. We began with MicroK8s installation on Windows, followed by a single-node Linux setup, and then a multi-node cluster deployment.
In this post, we focus on setting up Redis Sentinel with HAProxy on MicroK8s to enable high availability and dynamic failover — a critical step for running stateful services in production-grade Kubernetes clusters.
Redis is widely used for data storage and caching becauseof its high performance and flexibility. In Kubernetes environments, RedisSentinel provides automatic monitoring and failover of Redis nodes by promotingreplicas when a master fails.
However, Redis Sentinel alone does not handle clienttraffic routing after a failover. Applications must still connect to thecurrent Redis master consistently without manual reconfiguration.
HAProxy addresses this gap as a TCP loadbalancer, providing a stable connection endpoint that automatically routestraffic to the active Redis master. This setup simplifies applicationarchitecture and ensures seamless failover handling at the network layer.
This guide explains setting up HAProxy with RedisSentinel on Microk8s to build a highly available, fault-tolerant Redis cluster.
By combining Redis Sentinel's automatic failover withHAProxy's dynamic load balancing, teams can build resilient, production-readyRedis infrastructures without adding complexity to the application layer.
Setting up Redis Sentinel with HAProxy inKubernetes ensures high availability and automatic failover between Redismaster/primary and replica instances.
Redis Sentinel manages Redis monitoring andpromotion processes, while HAProxy provides a stable endpoint thatdynamically routes client traffic to the active Redis master — withoutrequiring application-side reconfiguration.
Redis Sentinel provides four core functions:
By combining Redis Sentinel's failover capabilitieswith HAProxy's dynamic load balancing, Redis deployments can handle nodefailures transparently — minimizing service disruption and improving overallcluster resilience.
Before starting, ensure the following prerequisites arein place:
· Basic knowledge of Kubernetes, Redis, andcontainerized deployments.
· A running Kubernetes cluster (Microk8s orequivalent).
· Redis Sentinel is deployed and operationalwithin your Kubernetes cluster.
· Familiarity with HAProxy and basic TCP loadbalancing concepts.
· Clone or download Deployment files from GitHub: https://github.com/arysdev/microk8s.git
To set up HAProxy, you must have a running Redis Sentinel cluster.
The deployment involves
· Redis master/primary and replica instances
· Redis Sentinel pods monitor the health of Redisnodes and handle failover
You can manually create the Redis and Sentinel deployments,use official Kubernetes examples, or leverage Helm charts if preferred.
Redis and Sentinel Deployment Files: https://github.com/arysdev/microk8s/tree/7ff13698e6e91378336bcb84dcaef75df1c60412/redis
Deploy HAProxy as a TCP load balancer to route traffic tothe current Redis master. HAProxy ensures applications always connect to theactive master node, even during Redis Sentinel failover events.
Use the provided Kubernetes manifests to deploy:
· A HAProxy Deployment to run the pods
· A HAProxy Service to expose HAProxy within thecluster
Deployment Files: https://github.com/arysdev/microk8s/tree/7ff13698e6e91378336bcb84dcaef75df1c60412/haproxy
After deploying HAProxy, configure it to route trafficdynamically to the active Redis master.
This is achieved by creating a Kubernetes ConfigMap containing the haproxy.cfgconfiguration.
3a. Key Components of the HAProxy Configuration
The HAProxy configuration does the following:
· Accepts TCP connections on port 6379 fromclients.
· Uses health checks to detect the current Redismaster.
· Leverages Kubernetes DNS to discover Redis podsdynamically.
· Forwards traffic only to the active primary/masternode.
Example configuration:
frontend redis-write
bind *:6379
mode tcp
default_backend redis-primary
backend redis-primary
mode tcp
option tcp-check
tcp-check send PING\r\n
tcp-check expect string +PONG
tcp-check send info replication\r\n
tcp-check expect string role:master
default-server inter 1s fall 3 rise 2 on-marked-down shutdown-sessions
server-template redis 3 redis-0.redis.svc.cluster.local:6379 check resolvers kube-dns init-addr none maxconn 30000
Explanation:
Create a Kubernetes ConfigMap to store the HAProxyconfiguration:
kubectl create configmap haproxy-config --from-file=haproxy.cfg=haproxy.cfg -n default
server redis-0 redis-0.redis.svc.cluster.local:6379 check inter 1000ms resolvers kube-dns init-addr none on-marked-down shutdown-sessions maxconn 30000
HAProxy Config Files: https://github.com/arysdev/microk8s/tree/7ff13698e6e91378336bcb84dcaef75df1c60412/haproxy
By configuring HAProxy with dynamic backend discovery,TCP-based health checks, and failover session handling:
· Client applications always connect to a healthyRedis master.
· Redis master failovers are transparent toclients.
· Scaling Redis replicas up or down does notrequire HAProxy reloads or configuration change
To expose HAProxy externally for Redis client connections,you must configure Kubernetes Ingress resources that handle TCP trafficforwarding.
In Microk8s, the built-in NGINX Ingress Controller supportsTCP services by patching its configuration.
Ingress manifests for HAProxy: https://github.com/arysdev/microk8s/tree/7ff13698e6e91378336bcb84dcaef75df1c60412/haproxy
After creating the Ingress resources, update the Microk8sNGINX Ingress Controller to forward external TCP traffic on Redis port 6379.
Patch the nginx-ingress-tcp-microk8s-confConfigMap to map port 6379 to the HAProxy service:
microk8s kubectl patch configmap nginx-ingress-tcp-microk8s-conf -n ingress --patch '{"data":{"6379":"default/haproxy-service:6379"}}'
· This command forwards external traffic on port 6379to haproxy-service in the default namespace.
· Adjust the namespace if your HAProxy deploymentuses a different one.
Download the patch file from the GitHublink below: https://github.com/arysdev/microk8s/tree/7ff13698e6e91378336bcb84dcaef75df1c60412/ingress
microk8s kubectl patch daemonset nginx-ingress-microk8s-controller --patch "$(cat patch-redis-port.yaml)" -n ingress
Important Notes:
· Ensure the Ingress Controller Service isconfigured as a LoadBalancer.
· Verify that the namespace for the IngressController matches your environment.
· Once patched, TCP port 6379 will be accessibleexternally via your Kubernetes node IP or LoadBalancer IP.
Configure and deploy redis master/primary and replica in microk8s.
Redis-config, script,redis statefulset, and service files: https://github.com/arysdev/microk8s/tree/7ff13698e6e91378336bcb84dcaef75df1c60412/redis
· Manages Redis master and replica pods withstable network identities (redis-0, redis-1, redis-2, etc.).
· Ensures that replicas are consistent andcorrectly replicated from the master.
· A headless Kubernetes service is used todiscover DNS-based services within the cluster.
· Allows pods to reach each other directly withouta load balancer.
· Configuration files are injected via ConfigMapsor mounted volumes.
· Scripts are provided to configure replicationsettings automatically (e.g., setting the replicaof directive).
protected-mode yes
is enabled for basic security.Once the Redis master and replicas are running and healthy,deploy the Redis Sentinel StatefulSet.
Sentinel deployment files are available here: https://github.com/arysdev/microk8s/tree/7ff13698e6e91378336bcb84dcaef75df1c60412/sentinel
o Deploysmultiple Sentinel pods to monitor the Redis master and replicas.
o Sentinelscommunicate among themselves and with Redis nodes to detect failures andcoordinate failovers.
o Sentinelconfiguration files are mapped through the redis-scripts-config-map
.
o EachSentinel pod shares the same base configuration but uses Kubernetes DNS todiscover Redis endpoints dynamically.
1. Deploy HAProxy and Redis pods first.
2. Wait until all Redis pods (redis-0, redis-1, etc.) are readyand healthy.
3. Deploy Sentinel pods only after the Redis pods are running.
DeployingHAProxy with Redis Sentinel on Microk8s provides a scalable andresilient solution for managing Redis failovers and minimizing servicedisruptions.
By integrating Redis Sentinel for monitoring andautomated master elections with HAProxy for dynamic traffic routing, you canensure that client connections are always directed to a healthy Redis master, without application-side reconfiguration.
In this blog, we covered:
Following this architecture, you can build Redisclusters that:
As Redis deployments grow, it's crucial to continuouslymonitor, optimize, and adjust your HAProxy and Sentinel configurations based onworkload demands and cluster changes.
Combining Redis Sentinel's failover capabilities withHAProxy's dynamic load balancing establishes a strong foundation for highlyavailable, production-grade Redis systems in Kubernetes environments.
Whether you need advice or are ready to get started, we're here to help. We go the extra mile to empower your digital transformation.