An overview of the

If you want to write a small project recently, to deepen your understanding of the theoretical knowledge (such as ZK, Redis, Kafaka, etc.), I will start with the environment setup. There are three master and three slave (one for each master). (I hope you understand the principle of Redis cluster before setting up cluster!!)

  1. Redis installation
  2. In cluster mode, start six Redis-servers on ports (7000-7005).
  3. Build a cluster of six servers (using the –cluster option in redis- CLI)
  4. Configure passwords for each server

Redis download & Install

# downloadWget http://download.redis.io/releases/redis-5.0.7.tar.gz tar - XZVF redis - 5.0.7. Tar. Gz mv redis - 5.0.7 redis# build install
cd// install GCC: yum install GCCCopy the code

Start six Redis servers in cluster mode

The redis. Conf file is ready

The Redis Server cluster mode requires some special configurations, as shown below

port 7000 6 servers for 7000-7005.
# bind 127.0.0.1 // This line should be commented out otherwise you can't connect remotelyCluster-enabled yes cluster-config-file nodes.conf //node.conf file ignore. cluster-node-timeout 5000 appendonly yes //aof daemonize yes protected-mode noCopy the code

Prepare configuration files for six sever servers

 mkdir redis-cluster
 cd redis-cluster
 mkdir 7000 7001 7002 7003 7004 7005
Copy the code

Then copy the redis.conf file to the 7000 to 7005 directories (each redis.conf only has different ports).

Start the server

Start a server for each directory

redis-server redis.conf
Copy the code

Up to now, we have 6 Redis Servers (ports 7000-7005) started in cluster mode, but each server has not been assigned to slot, so it cannot provide services externally.

Cluster setup (Slot assignment)

Redis cluster management has a variety of solutions (online see useful Ruby, not deep), HERE I use redis official provide redis-cli command –cluster option. The following command will form the six servers from 7000-7005 into a cluster with a replication factor of 1, so there will be 3 masters and 3 slaves. The 16,384 slots are assigned to the three masters as evenly as possible, and the three slaves asynchronously copy from their masters.

Redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 --cluster-replicas 1Copy the code

At this point, the entire cluster can provide services externally

password

Connect to each server through redis-CLI and set the password

 config set masterauth [password]\n
 config set requirepass [password]\n
Copy the code

Write in the last

  1. The password must be set last; otherwise, an AUTH error will be reported when building a cluster
  2. If you need external network access, change the host address to the external IP address of the host in the redis-cli –cluster option abovemove toWhich machine); There is also a pit here. If the IP is changed to the external IP, 17000-17005 ports (security group and firewall should be considered) need to be opened, because cluster servers are built to communicate through these ports (port monitoring client connection + 10000).
  3. I wrote a script and posted it at the end of the article to simplify the whole startup process.
  4. To be tested: redis-cli –cluster whether to use the consistent hash algorithm?

reference

  1. Redis official website cluster building
  2. Build Redis cluster, JedisCluster with password access in Linux

Cluster startup script

#! /bin/sh
# step1: start six redis-server in cluster mode.
PREFIX=/usr/local/redis-cluster/
for((PORT=7000; PORT<=7005; PORT++));do
cd $PREFIX$PORT
pwd
(redis-server redis.conf);
done

# step2: clusterRedis-cli --cluster create 49.235.250.8:7000 49.235.250.8:7001 49.235.250.8:7002 49.235.250.8:7003 49.235.250.8:7004 49.235.250.8:7005-1 - cluster - replicas# step3: set auth
commands='
config set masterauth qwerty\n
config set requirepass qwerty\n
auth qwerty\n
config rewrite\n
exit\n'

for((PORT=7000; PORT<=7005; PORT++));do
echo -e $commands | redis-cli -p $PORT ;
done
echo 'success'
Copy the code