Drupal 8 on Redis Sentinel in Docker containers

How to run Drupal 8 on Redis Sentinels containers

Posted on 27 January 2020

Installing and running “redis” seem to be enough to run Drupal with it - but sometimes you want to scale Redis for HA.

Tool named “redis-sentinel” provides “redis” services such as:

  • Monitoring: It can check master or slave services whether is working correctly and healthy or not.
  • Notification: It can notify when a service goes down.
  • Automatic Failover: It starts a failover scenario and assigns one of slaves as a master when master service goes down.
  • Configuration Provider: It can configure and share master service information when a new service is added to cluster.

Redis Sentinel Drupal Docker

Setup for the Redis Sentinel POC

Following services are created using Docker:

  • Redis master: Master Redis Server
  • Redis slave: Slave Redis Server
  • Redis sentinel: Sentinel Server
  • Nginx: For Drupal
  • PHP: PHP FPM for Drupal
  • MySQL: For Drupal DB

See the functional POC code & config here https://github.com/dakkusingh/docker_drupal_redis_sentinel

Get this repo

git clone https://github.com/dakkusingh/docker_drupal_redis_sentinel

Setup Drupal 8 + Redis + Sentinels

Setup Drupal 8 using Composer

Composer install Drupal into subdir called drupal.

composer create-project drupal-composer/drupal-project:8.x-dev drupal --no-interaction

Install Pecl Redis

pecl install redis

This setup is already done in the provided PHP container. See: https://github.com/phpredis/phpredis/blob/develop/INSTALL.markdown

Download Drupal Redis Module

composer require 'drupal/redis:^1.2'

See: https://www.drupal.org/project/redis/releases

Enable Redis module

drush en redis

Configure Redis Module for Drupal

## REDIS ##
$settings['redis.connection']['interface'] = 'PhpRedis';

// Sentinels instances list with hostname:port format.
$settings['redis.connection']['host'] = ['sentinel:26000'];

// Redis instance name.
$settings['redis.connection']['instance']  = 'mymaster';

// Set the redis backend.
$settings['cache']['default'] = 'cache.backend.redis';

// Connect via UNIX socket
$conf['redis_cache_socket'] = '/tmp/redis.sock';

Setup Redis Sentinel in Docker

Sentinel Conf

The sentinels are configured with mymaster instance and following properties see https://github.com/dakkusingh/docker_drupal_redis_sentinel/blob/master/docker-config/sentinel/Dockerfile

ENV SENTINEL_QUORUM 2
ENV SENTINEL_DOWN_AFTER 5000
ENV SENTINEL_FAILOVER 10000
ENV SENTINEL_PORT 26000
ENV SENTINEL_PAR_SYNC 1

Run all services locally

Get it up

docker-compose up --build -d
....
....
....

Scale up Redis slaves & Sentinels

docker-compose scale slave=2 sentinel=3
....
....
....
Creating docker_redis_sentinel_slave_2 ... done
Starting docker_redis_sentinel_sentinel_1 ... done
Creating docker_redis_sentinel_sentinel_2 ... done
Creating docker_redis_sentinel_sentinel_3 ... done

Confirm all services are running

docker-compose ps
              Name                            Command               State           Ports
--------------------------------------------------------------------------------------------------
docker_redis_sentinel_db_1         docker-entrypoint.sh mysqld      Up      0.0.0.0:3306->3306/tcp
docker_redis_sentinel_master_1     docker-entrypoint.sh redis ...   Up      6379/tcp
docker_redis_sentinel_php_1        docker-php-entrypoint php-fpm    Up      9000/tcp
docker_redis_sentinel_sentinel_1   entrypoint.sh                    Up      6379/tcp
docker_redis_sentinel_sentinel_2   entrypoint.sh                    Up      6379/tcp
docker_redis_sentinel_sentinel_3   entrypoint.sh                    Up      6379/tcp
docker_redis_sentinel_slave_1      docker-entrypoint.sh redis ...   Up      6379/tcp
docker_redis_sentinel_slave_2      docker-entrypoint.sh redis ...   Up      6379/tcp
docker_redis_sentinel_web_1        nginx -g daemon off;             Up      0.0.0.0:80->80/tcp

Check Redis Sentinel config & Test Failover

Check Sentinel Config

docker exec -it docker_redis_sentinel_sentinel_1 /bin/sh

cat /etc/redis/sentinel.conf

Check Sentinel Info

docker-compose exec sentinel redis-cli -p 26000 SENTINEL get-master-addr-by-name mymaster

1) "172.17.0.2"
2) "6379"

Test Failover

docker-compose pause master

Pausing docker_redis_sentinel_master_1 ... done

Check info again to confirm failover.

docker-compose exec sentinel redis-cli -p 26000 SENTINEL get-master-addr-by-name mymaster

1) "172.17.0.3"
2) "6379"

References

See: