Install redis-cluster three hosts and six instances

1. Prepare the environment: three clean virtual machines and turn off the firewall and selinux

192.168.46.129 redis-1 7001 7002

192.168.46.132 redis-2 7003 7004

192.168.46.135 redis-3 7005 7006

2. Install redis

Use the script installation given above or other installation methods. Please change the script installation to the available yum source.

3. Configure different nodes and listening ports. Directories and ports are distinguished and mapped

Take 7001/7002/ of 192.168.46.129 as an example. The other two machines operate in the same way.

[root@redis-1 ~]# sh install_redis.sh
[root@redis-1 ~]# systemctl status redis
[root@redis-1 ~]# ss -lnatup | grep redis
tcp LISTEN 0 128 127.0.0.1:6379 *:* users:(("redis-server",pid=15831,fd=6))
[root@redis-1 ~]# kill -9 15831
[root@redis-1 ~]# mkdir -p /data/application/{7001,7002}
[root@redis-1 ~]# mkdir /data/application/7001/data
[root@redis-1 ~]# mkdir /data/application/7002/data
[root@redis-1 ~]# cd /data/application/
[root@redis-1 application]# vim 7001/redis.conf

Add the following configuration to redis.conf. The operations on the other two machines are the same. Be careful to modify the ports.

ort 7001
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
bind 0.0.0.0
pidfile /data/application/7001/redis_7001.pid
dir /data/application/7001/data

7002/Similar to 192.168.46.129

[root@redis-1 application]# vim 7001/redis.conf

Add the following configuration to the redis.conf file

ort 7002
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
bind 0.0.0.0
pidfile /data/application/7002/redis_7002.pid
dir /data/application/7002/data

Complete the configuration of 192.168.46.132 7003/ 7004/ and 192.168.46.135 7005/ 7006/ with the same operations as above. Pay attention to modifying the created directory and the port, path, and file numbers in the configuration file.

4. After adding the configuration, start the service and check whether the port is in the listening state

[root@redis-1 application]# ./redis-5.0.10/src/redis-server 7001/redis.conf
15892:C 02 Nov 2023 13:05:03.143 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
15892:C 02 Nov 2023 13:05:03.143 # Redis version=5.0.10, bits=64, commit=00000000, modified=0, pid=15892, just started
15892:C 02 Nov 2023 13:05:03.143 # Configuration loaded
[root@redis-1 application]# ss -lnatup | grep redis
tcp LISTEN 0 128 *:17001 *:* users:(("redis-server",pid=15893,fd=9))
tcp LISTEN 0 128 *:7001 *:* users:(("redis-server",pid=15893,fd=6))
[root@redis-1 application]# ./redis-5.0.10/src/redis-server 7002/redis.conf
15899:C 02 Nov 2023 13:05:17.784 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
15899:C 02 Nov 2023 13:05:17.784 # Redis version=5.0.10, bits=64, commit=00000000, modified=0, pid=15899, just started
15899:C 02 Nov 2023 13:05:17.784 # Configuration loaded
[root@redis-1 application]# ss -lnatup | grep redis
tcp LISTEN 0 128 *:17001 *:* users:(("redis-server",pid=15893,fd=9))
tcp LISTEN 0 128 *:17002 *:* users:(("redis-server",pid=15900,fd=9))
tcp LISTEN 0 128 *:7001 *:* users:(("redis-server",pid=15893,fd=6))
tcp LISTEN 0 128 *:7002 *:* users:(("redis-server",pid=15900,fd=6))

Start 192.168.46.132 7003/ 7004/ and 192.168.46.135 7005/ 7006/ in the same way and check whether the port is in the listening state normally

5. Create a cluster

After the three machines are configured and all services are started successfully, select any machine to create a cluster.

[root@redis-1 application]# cd redis-5.0.10/src/
[root@redis-1 src]# pwd
/data/application/redis-5.0.10/src
[root@redis-1 src]# ./redis-cli --cluster create --cluster-replicas 1 192.168.46.129:7001 192.168.46.129:7002 192.168.46.132:7003 192.168.46.132:7004 192.168.4 6.135:7005 192.168 .46.135:7006

5. Log in to the cluster and test

a. Remember to add -c when logging in, otherwise the following error will be reported

[root@redis-1 src]# ./redis-cli -h 192.168.46.129 -p 7001
192.168.46.129:7001> set name xiaoming
(error) MOVED 12182 192.168.46.135:7005

b.Correct login method

[root@redis-1 src]# ./redis-cli -c -h 192.168.46.129 -p 7001
192.168.46.129:7001> set name xiaoming
-> Redirected to slot [12182] located at 192.168.46.135:7005
OK

1. Write data and see how it is distributed

[root@redis-1 src]# ./redis-cli -c -h 192.168.46.129 -p 7001
192.168.46.129:7001> set name wanglin
-> Redirected to slot [5798] located at 192.168.46.135:7006
OK
192.168.46.135:7006> set age 1800
-> Redirected to slot [741] located at 192.168.46.132:7004
OK
192.168.46.132:7004> set sex man
OK

Conclusion: Randomly assigned to different nodes

2. Check whether the data can be found at any node

a. Log in to different nodes to query the data entered in 1

[root@redis-1 src]# ./redis-cli -c -h 192.168.46.129 -p 7002
192.168.46.129:7002> get name
-> Redirected to slot [5798] located at 192.168.46.135:7006
"wanglin"
[root@redis-1 src]# ./redis-cli -c -h 192.168.46.132 -p 7003
192.168.46.132:7003> get age
-> Redirected to slot [741] located at 192.168.46.132:7004
"1800"
[root@redis-1 src]# ./redis-cli -c -h 192.168.46.135 -p 7006
192.168.46.135:7006> get sex
-> Redirected to slot [2584] located at 192.168.46.132:7004
"man"

Conclusion: Data can be queried no matter which node it is

3. Downtime test, stop a certain master and see the status changes of the cluster. Start the stopped master and see the status changes of the cluster.

a. Log in to any redis to view cluster information

[root@redis-1 src]# ./redis-cli -c -h 192.168.46.129 -p 7001
192.168.46.129:7001> cluster nodes
3f25e206a5df9ed60c1de89b6956e891370ba312 192.168.46.135:7006@17006 master - 0 1698916073722 7 connected 5461-10922
D5A799A486EFE90C6AA20E45E3D17171D83B45108B 192.168.46.132:7003@17003 SLAVE 3F25E206A5DF9C1DE89B691370ba312 0 1698916074727 7 Conn ECTED
8c17c775d53d49843378ca9c5adf61b641046e22 192.168.46.129:7002@17002 slave bbf159991cb2a5ca5debfbaf828dd3d411a86bb3 0 1698916072716 5 connected
bbf159991cb2a5ca5debfbaf828dd3d411a86bb3 192.168.46.135:7005@17005 master - 0 1698916073220 5 connected 10923-16383
63cf5411d594fb606584991f032b1e08d59258c8 0 1698916071 000 1 connected
863b12c51f8366998e4652869d5f21342fa50c88 192.168.46.132:7004@17004 master - 0 1698916074224 8 connected 0-5460

b. According to the query results, 92.168.46.135:7005 (master) and 192.168.46.129:7002 (slave) are a pair of master and slave. Stop the 7005 port of the virtual machine IP 192.168.46.135.

[root@redis-3 ~]# ss -lnatup | grep redis | grep LISTEN
tcp LISTEN 0 128 *:17006 *:* users:(("redis-server",pid=16217,fd=9))
tcp LISTEN 0 128 *:7005 *:* users:(("redis-server",pid=16212,fd=6))
tcp LISTEN 0 128 *:7006 *:* users:(("redis-server",pid=16217,fd=6))
tcp LISTEN 0 128 *:17005 *:* users:(("redis-server",pid=16212,fd=9))
[root@redis-3 ~]# kill -9 16212

c. Check the cluster information again and find that 192.168.46.135:7005 has reported an error and 192.168.46.129:7002 has become master.

[root@redis-1 src]# ./redis-cli -c -h 192.168.46.129 -p 7001
192.168.46.129:7001> cluster nodes
3f25e206a5df9ed60c1de89b6956e891370ba312 192.168.46.135:7006@17006 master - 0 1698916402982 7 connected 5461-10922
D5A799A486EFe90C6AA20E45E3D171D83B45108B 192.168.46.132@17003 SLAVE 3F25E206A5DF9ED60C1DE89B691370ba312 0 16989164034887 7 Conn ECTED
8c17c775d53d49843378ca9c5adf61b641046e22 192.168.46.129:7002@17002 master - 0 1698916402982 9 connected 10923-16383
bbf159991cb2a5ca5debfbaf828dd3d411a86bb3 192.168.46.135:7005@17005 master,fail - 1698916310990 1698916309884 5 disconnected
63cf5411d594fb606584991f032b1e08d59258c8 0 1698916403 000 1 connected
863b12c51f8366998e4652869d5f21342fa50c88 192.168.46.132:7004@17004 master - 0 1698916404492 8 connected 0-5460

d. Restart 192.168.46.135:7005

[root@redis-3 ~]# /data/application/redis-5.0.10/src/redis-server /data/application/7005/redis.conf
16798:C 02 Nov 2023 17:15:08.465 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
16798:C 02 Nov 2023 17:15:08.465 # Redis version=5.0.10, bits=64, commit=00000000, modified=0, pid=16798, just started
16798:C 02 Nov 2023 17:15:08.465 # Configuration loaded

e. Check the cluster information again and find that the master and slave of 192.168.46.135:7005 and 192.168.46.129:7002 have been exchanged.

[root@redis-2 src]# ./redis-cli -c -h 192.168.46.129 -p 7001
192.168.46.129:7001> cluster nodes
3f25e206a5df9ed60c1de89b6956e891370ba312 192.168.46.135:7006@17006 master - 0 1698916555102 7 connected 5461-10922
D5A799A486EFe90C6AA20E45E3D171D83B45108B 192.168.46.132:7003@17003 SLAVE 3F25E206A5DF9C1DE89B691370ba312 0 1698916554398 7 Conn ECTED
8c17c775d53d49843378ca9c5adf61b641046e22 192.168.46.129:7002@17002 master - 0 1698916554900 9 connected 10923-16383
bbf159991cb2a5ca5debfbaf828dd3d411a86bb3 192.168.46.135:7005@17005 slave 8c17c775d53d49843378ca9c5adf61b641046e22 0 1698916555906 9 connected
63cf5411d594fb606584991f032b1e08d59258c8 192.168.46.129:7001@17001 myself,slave 863b12c51f8366998e4652869d5f21342fa50c88 0 1698916553 000 1 connected
863b12c51f8366998e4652869d5f21342fa50c88 192.168.46.132:7004@17004 master - 0 1698916555404 8 connected 0-5460

Conclusion: Master-slave swap

4. Stop a master-slave pair to see if the cluster is normal.

[root@redis-2 src]# ./redis-cli -c -h 192.168.46.129 -p 7001
192.168.46.129:7001> cluster nodes
3f25e206a5df9ed60c1de89b6956e891370ba312 192.168.46.135:7006@17006 master - 0 1698916744337 7 connected 5461-10922
D5A799A486EFE3AA20E45E3D171D171D83B45108B 192.168.46.132@17003 SLAVE 3F25E206A5DF9C1DE89B691370ba312 0 169898916745000 7 ConneNe cTED
8c17c775d53d49843378ca9c5adf61b641046e22 192.168.46.129:7002@17002 master - 0 1698916745343 9 connected 10923-16383
bbf159991cb2a5ca5debfbaf828dd3d411a86bb3 192.168.46.135:7005@17005 slave 8c17c775d53d49843378ca9c5adf61b641046e22 0 1698916745042 9 connected
63cf5411d594fb606584991f032b1e08d59258c8 192.168.46.129:7001@17001 myself,slave 863b12c51f8366998e4652869d5f21342fa50c88 0 1698916742 000 1 connected
863b12c51f8366998e4652869d5f21342fa50c88 192.168.46.132:7004@17004 master - 0 1698916743333 8 connected 0-5460
192.168.46.129:7001> 

a. According to the query results, we know that 192.168.46.135:7006 and 192.168.46.132:7003 are a pair of master and slave. Stop them respectively.

[root@redis-2 src]# ss -lnatup | grep redis | grep LISTEN
tcp LISTEN 0 128 *:17003 *:* users:(("redis-server",pid=16320,fd=9))
tcp LISTEN 0 128 *:17004 *:* users:(("redis-server",pid=16256,fd=9))
tcp LISTEN 0 128 *:7003 *:* users:(("redis-server",pid=16320,fd=6))
tcp LISTEN 0 128 *:7004 *:* users:(("redis-server",pid=16256,fd=6))
[root@redis-2 src]# kill -9 16320
[root@redis-3 ~]# ss -lnatup | grep redis | grep LISTEN
tcp LISTEN 0 128 *:17006 *:* users:(("redis-server",pid=16217,fd=9))
tcp LISTEN 0 128 *:7005 *:* users:(("redis-server",pid=16799,fd=6))
tcp LISTEN 0 128 *:7006 *:* users:(("redis-server",pid=16217,fd=6))
tcp LISTEN 0 128 *:17005 *:* users:(("redis-server",pid=16799,fd=9))
[root@redis-3 ~]# kill -9 16217

b. Log in to the redis cluster and define variables for testing

[root@redis-1 src]# ./redis-cli -c -h 192.168.46.129 -p 7001
192.168.46.129:7001> cluster nodes
3f25e206a5df9ed60c1de89b6956e891370ba312 192.168.46.135:7006@17006 master,fail - 1698917022330 1698917020722 7 disconnected 5461-10922
d5a799a486efe90c6aa20e45e3d171d83b45108b 192.168.46.132:7003@17003 slave,fail 3f25e206a5df9ed60c1de89b6956e891370ba312 1698917026260 1 698917024749 7 disconnected
8c17c775d53d49843378ca9c5adf61b641046e22 192.168.46.129:7002@17002 master - 0 1698917101342 9 connected 10923-16383
bbf159991cb2a5ca5debfbaf828dd3d411a86bb3 192.168.46.135:7005@17005 slave 8c17c775d53d49843378ca9c5adf61b641046e22 0 1698917102853 9 connected
63cf5411d594fb606584991f032b1e08d59258c8 192.168.46.129:7001@17001 myself,slave 863b12c51f8366998e4652869d5f21342fa50c88 0 1698917099 000 1 connected
863b12c51f8366998e4652869d5f21342fa50c88 192.168.46.132:7004@17004 master - 0 1698917103359 8 connected 0-5460
192.168.46.129:7001> set name xiaoming
(error) CLUSTERDOWN The cluster is down

Conclusion: An error occurs when defining variables, and the cluster is no longer normal.