0 Votes

Redis

Last modified by Jeff McDonald on 2022/02/22 08:03

Notes on creating a Redis cluster on Oracle Linux 8

Adapted from:
https://www.dltlabs.com/blog/how-to-setup-configure-a-redis-cluster-easily-573120

Disable SELinux

Temporarily disable SELinux

sudo setenforce 0

Permanently disable SELinux by editing the SELinux config file:

sudo vi /etc/selinux/config

Set

SELINUX=disabled

Disable Firewall

Disable the firewall:

sudo systemctl stop firewalld
sudo systemctl disable firewalld

Install Docker

Install docker / podman:

sudo yum -y install docker

Pull the latest version of Redis:

sudo docker pull redis

Choose: docker.io/library/redis:latest

Create custom config files

Get a copy of the standard redis.config:

sudo yum -y install redis
sudo cp /etc/redis.conf .
sudo chown jeff:jeff redis.conf

Enable the following parameters:

port 7000
cluster-config-file nodes-7000.conf
requirepass foobared
cluster-enabled yes

Disable the following parameters;

#'logfile /var/log/redis/redis.log'
#dir /var/lib/redis
#bind 127.0.0.1

Create 6 different config files in 6 different directories, copy the redis.conf file, changing the port numbers:

mkdir 7000 7001 7002 7003 7004 7005

sed 's/7000/7000/' redis.conf > 7000/redis.conf
sed 's/7000/7001/' redis.conf > 7001/redis.conf
sed 's/7000/7002/' redis.conf > 7002/redis.conf
sed 's/7000/7003/' redis.conf > 7003/redis.conf
sed 's/7000/7004/' redis.conf > 7004/redis.conf
sed 's/7000/7005/' redis.conf > 7005/redis.conf

Start the Redis servers

Start each server using a command similar to:

docker run -v /home/jeff/redis-conf/7000/redis.conf:/usr/local/etc/redis/redis.conf -d --restart unless-stopped --net=host --name myredis-7000 redis redis-server /usr/local/etc/redis/redis.conf

docker run -v /home/jeff/redis-conf/7001/redis.conf:/usr/local/etc/redis/redis.conf -d --restart unless-stopped --net=host --name myredis-7001 redis redis-server /usr/local/etc/redis/redis.conf

docker run -v /home/jeff/redis-conf/7002/redis.conf:/usr/local/etc/redis/redis.conf -d --restart unless-stopped --net=host --name myredis-7002 redis redis-server /usr/local/etc/redis/redis.conf

docker run -v /home/jeff/redis-conf/7003/redis.conf:/usr/local/etc/redis/redis.conf -d --restart unless-stopped --net=host --name myredis-7003 redis redis-server /usr/local/etc/redis/redis.conf

docker run -v /home/jeff/redis-conf/7004/redis.conf:/usr/local/etc/redis/redis.conf -d --restart unless-stopped --net=host --name myredis-7004 redis redis-server /usr/local/etc/redis/redis.conf

docker run -v /home/jeff/redis-conf/7005/redis.conf:/usr/local/etc/redis/redis.conf -d --restart unless-stopped --net=host --name myredis-7005 redis redis-server /usr/local/etc/redis/redis.conf

Configure the Cluster

Attach to one of the docker images:

docker ps

Emulate Docker CLI using podman. Create /etc/containers/nodocker to quiet msg.
CONTAINER ID  IMAGE                           COMMAND               CREATED         STATUS             PORTS       NAMES
2772a1252d0f  docker.io/library/redis:latest  redis-server /usr...  14 minutes ago  Up 14 minutes ago              myredis-0
8fd226455ae9  docker.io/library/redis:latest  redis-server /usr...  14 minutes ago  Up 14 minutes ago              myredis-1
3dc8235a318f  docker.io/library/redis:latest  redis-server /usr...  14 minutes ago  Up 14 minutes ago              myredis-2
abea92c460a5  docker.io/library/redis:latest  redis-server /usr...  14 minutes ago  Up 14 minutes ago              myredis-3
0bed8a823812  docker.io/library/redis:latest  redis-server /usr...  14 minutes ago  Up 14 minutes ago              myredis-4
c62c20bb7039  docker.io/library/redis:latest  redis-server /usr...  14 minutes ago  Up 14 minutes ago              myredis-5

docker exec -it 2772a1252d0f sh

Create the cluster:

redis-cli --cluster create 192.168.1.145:7000 192.168.1.145:7001 192.168.1.145:7002 192.168.1.145:7003 192.168.1.145:7004 192.168.1.145:7005 --cluster-replicas 1 -a 'foobared'

Programming in Java

Here's an example of using the Jedis Java client to connect to a Redis cluster:

 @Override
 public void contextInitialized(ServletContextEvent event) {
  logger.info("Initializing Redis connection pool...");

  String password = "foobared";
  String host = "192.168.1.145";
  int port = 7000;

  int connectionTimeout = 5000;
  int soTimeout = 100;
  int maxAttempts = 10;

  Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
  jedisClusterNodes.add(new HostAndPort(host, port));

  JedisPoolConfig poolConfig = new JedisPoolConfig();
  poolConfig.setMaxTotal(128);
  poolConfig.setMaxIdle(128);
  poolConfig.setMinIdle(16);
  poolConfig.setTestOnBorrow(true);
  poolConfig.setTestOnReturn(true);
  poolConfig.setTestWhileIdle(true);
  poolConfig.setNumTestsPerEvictionRun(3);
  poolConfig.setBlockWhenExhausted(true);

  jedis = new JedisCluster(jedisClusterNodes, connectionTimeout, soTimeout, maxAttempts, password, poolConfig);

  // Testing
  jedis.hset("Test1", "name1", "success!");
  logger.info("Testing connection... {}", jedis.hget("Test1", "name1"));
 }

 @Override
 public void contextDestroyed(ServletContextEvent event) {
  logger.info("Destroying Redis connection pool...");
  jedis.close();
 }