Schedule an appointment

In house vs outsourced experience

Setting Up HAProxy with Redis Sentinel for High Availability on Microk8s (Kubernetes)

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.


Introduction:

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.

Common Use Cases

Deploying HAProxy with Redis Sentinel is valuable inscenarios such as:

  • Application caching that requires a stable     connection endpoint during Redis master failovers.
  • Session management systems that must handle automatic     failover without client-side reconnect logic.
  • Real-time data pipelines using Redis for pub/sub or     queuing workloads where downtime is unacceptable.
  • Microservices architectures that rely on Redis for     coordination tasks like distributed locks or counters.
  • High-traffic e-commerce, gaming, or SaaS platforms where Redis availability is critical under node failures or surges.

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.

Purpose

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:

  • Monitoring: Continuously checks the health of Redis     master/primary and replica nodes.
  • Notification: Alerts administrators or automation systems when     node failures are detected.
  • Automatic Failover: Promotes a replica to master if     the current master fails and reconfigures the cluster relationships.
  • Configuration Provider: Supplies updated master     addresses to clients during and after failover events.

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.


Prerequisites:

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


Deployment Steps:

Step 1: Deploy Redis Sentinel in Kubernetes


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

 

Step 2: Deploy HAProxy in Kubernetes

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

Step 3: Configure 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:

  • The frontend redis-write listens for incoming Redis traffic on port 6379.
  • The backend redis-primary performs health checks:
       
    • Sends a PING command to validate the connection.
    •  
    • Sends INFO replication to confirm the node's role as master
  •  
  • Dynamic discovery using server-template and Kubernetes DNS enables HAProxy to track Redis pod changes automatically.

3b. Create the HAProxy ConfigMap

Create a Kubernetes ConfigMap to store the HAProxyconfiguration:

kubectl create configmap haproxy-config --from-file=haproxy.cfg=haproxy.cfg -n default
  • Replace the default with your target namespace if it is different.
  • This ConfigMap should be mounted into the HAProxy Deployment.
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

Step 4: Ingress file for HAproxy.

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

 

Step 5: Configure Ingress Controller to Expose Redis TCP Traffic

After creating the Ingress resources, update the Microk8sNGINX Ingress Controller to forward external TCP traffic on Redis port 6379.

5a. Patch the TCP Services ConfigMap

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.

5b. Patch the Ingress Controller DaemonSet

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.

Step 6: Deploy Redis Master and Replicas


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

Key Components:

StatefulSet:

·        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.

Service:

·        A headless Kubernetes service is used todiscover DNS-based services within the cluster.

·        Allows pods to reach each other directly withouta load balancer.

Redis Configuration:

·        Configuration files are injected via ConfigMapsor mounted volumes.

·        Scripts are provided to configure replicationsettings automatically (e.g., setting the replicaof directive).

Important Considerations:

  • Redis replicas automatically follow the primary/master nstance upon deployment.
  • Ensure that the protected-mode yes is enabled for basic security.
  • Persistent volumes (PVCs) should be used to store Redis data to survive pod restarts or rescheduling.
  • You can customize the StatefulSet and Service specifications depending on your replication factors, memory sizing, persistence needs, and security policies.

Step 7: Deploy Redis Sentinel

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

Key Components:

·        Sentinel StatefulSet:

o    Deploysmultiple Sentinel pods to monitor the Redis master and replicas.

o    Sentinelscommunicate among themselves and with Redis nodes to detect failures andcoordinate failovers.

·        Sentinel Configuration:

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.

Deployment sequence:

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.

Conclusion:

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:

  • Deploying Redis Sentinel for high availability monitoring.
  • Configuring HAProxy to load-balance Redis traffic based on master     status.
  • Setting up Kubernetes services and ingress to expose Redis     externally.
  • Ensuring dynamic pod discovery and TCP health checks through     Kubernetes DNS integration.

Following this architecture, you can build Redisclusters that:

  • Survive primary/master node failures with automatic failover.
  • Maintain stable client endpoints across failovers.
  • Scale cleanly with Kubernetes-native service discovery.

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.

 

Tags

Arystech Logo

We Love to Talk Tech

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.

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

accreditations

AWS - Lambda Delivery PartnerAWS - Well-Architected PartnerMicrosoft Partner