Redis - HAProxy Example Redis - HAProxy Example

So, you’ve been told you need to build a Redis Server Cluster. First, if you don’t know what Redis is you are probably thinking, “What is this weird named thing and what do I need to do”. This guide will walk you through both in a way that will hopefully be easy to follow and be easy to repeat in the future.

While you can have more than three servers in a Redis cluster for the sake of simplicity, we will cover setting up a three server Redis cluster. In our three servers cluster, we will have two Redis servers with one Redis Sentinel with HAProxy to assist the Sentinel.

Much of this guide will work with a tested code stored in GitLab (https://gitlab.com/Kittell-Projects/RedHat/Redis) to make it easier to build the cluster. While this code is tested, it is suggested to build in a development environment first.

What is Redis?

In short, Redis is three or more server operational database cluster that is used for various tasks.

Retrieved June 1, 2017, from https://redis.com/company/
Redis, a database benchmarked (https://redislabs.com/cbc-2015-15-nosql-benchmark) as the world’s fastest, reduces application complexity, simplifies development, accelerates time to market and provides unprecedented flexibility to developers with its visionary data structures and modules.

Retrieved June 1, 2017, from https://redis.io/
Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, and sorted sets with range queries, bitmaps, hyperloglogs and geospatial indexes with radius queries. Redis has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.

Who Owns Redis?

While there is an enterprise option, Redis is open source. What this means is that you can use it for free in your projects or you can pay for premium service and support.

  • Redis.IO (the Open Source option) is run by Salvatore Sanfilippo with the help of sponsors.
  • RedisLabs.com (the Enterprise option) is run by Co-Founders Ofer Bengal (CEO) and Yiftach Shoolman (CTO) with Salvatore Sanfilippo as lead open source developer.

GitLab Scripts

If you are new to automation scripts and Git repositories this may seem a bit weird or uneasy to trust a script, for those that would like to see the scripts and what they do before you run them please follow these links:

Setup Redis

Some Assumptions:

  • User has root permission or is root
  • Servers are properly registered with Red Hat

Steps

  1. Get three servers setup with Red Hat 7.3 or newer. If you have not setup Red Hat before please refer to the installation guide (https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/installation_guide/index) to get started.
    1. In this guide, we will concentrate mostly in terminal shell/bash scripting of the cluster so you do not need the graphical user interface (GUI).
    2. Each server should have a static IP, it is typically best if you assign them in the installation process if you can but if not you can run this first script to get your network adapter and IP address.
      #Get Network Adapter and IP address
      ifconfig | grep inet -B1 | grep -v inet6

      Example:
      In this example, the network adapter you would choose to use is eth zero; depending on the configuration it could be eth(number) or en(number).

      eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
              inet 10.211.55.6  netmask 255.255.255.0  broadcast 10.211.55.255
      --
      lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
              inet 127.0.0.1  netmask 255.0.0.0
      --
      
    3. This script will set a static IP address, make sure you run the above script though to get the correct adapter.
      #Set Static IP address
      sudo ifconfig eth0 10.40.20.20 netmask 255.255.255.0
      sudo route add default gw 10.40.20.1
      
  2. On each server run this script.
    sh -c "$(curl -s "https://gitlab.com/Kittell-Projects/RedHat/Redis/raw/master/Redis.sh")"

    This script will:

    • First check the Red Hat server is properly registered with Red Hat; if the server is not registered, the script will assist in the registration.
    • Enable additional official Yum Repositories
    • Create a Yum Cache
    • Run a Yum Update
    • Verify the time zone is setup on the server to your desired setting, if it is not the script will assist in the setting.
      NOTE: The script looks specifically for the "tz database" format as defined on https://access.redhat.com/documentation/en-us/red_hat_virtualization/4.4
    • Install basic tools
    • Optionally set SSH banner text
    • Install needed Redis components on a node-specific configuration
    • Reboot the specific server

Once you have ran the script on all three servers your Redis Cluster is built and ready for your project.

Note: Just in case you are not familiar with Redis, your application that uses Redis should connect to node 3 as that will do the work to determine which server the work needs to go to.

HAProxy - Manage Redis

Redis - HAProxy ExampleWhen the scripts are done on all three servers, you should be able to go to the HAProxy site to get a status and run management tasks.

  • HAProxy can be reached at http://(node3IP):9000/haproxy_stats
  • Default credentials:
    • Username: admin
    • Password: passwordhere

Useful Scripts

  1. You can test the cluster on node1 and node2 with the script below.
    config="/opt/redis/node.conf"
    declare netadapter="$(grep "nodeAdapter" "$config" | awk -F'=' '{print $2}')"
    declare ip=$(/sbin/ip -o -4 addr list $netadapter | awk '{print $4}' | cut -d/ -f1)
    redis-benchmark -h $ip -q -n 1000 -c 10 -P 5 # Test Redis
    

    Example:

    redis-benchmark -h $ip -q -n 1000 -c 10 -P 5 # Test Redis
    PING_INLINE: 333333.34 requests per second
    PING_BULK: 499999.97 requests per second
    SET: 999999.94 requests per second
    GET: 499999.97 requests per second
    INCR: 499999.97 requests per second
    LPUSH: 499999.97 requests per second
    RPUSH: 499999.97 requests per second
    LPOP: 499999.97 requests per second
    RPOP: 499999.97 requests per second
    SADD: 499999.97 requests per second
    SPOP: 499999.97 requests per second
    LPUSH (needed to benchmark LRANGE): 499999.97 requests per second
    LRANGE_100 (first 100 elements): 499999.97 requests per second
    LRANGE_300 (first 300 elements): 499999.97 requests per second
    LRANGE_500 (first 450 elements): 999999.94 requests per second
    LRANGE_600 (first 600 elements): 999999.94 requests per second
    MSET (10 keys): 333333.34 requests per second
  2. Connect to Redis
    redis-cli -h 10.211.55.9 # Change IP/Node as necessary
  3. Configure/Reconfigure Slave (Once connected to Redis)
    slaveof noone # Run on the master node
    slaveof 10.211.55.9 # Slave of Node 1, change IP and node as necessary

Take advantage of your Red Hat Developers membership and download RHEL today at no cost.

Last updated: February 6, 2024