alt

#所有节点安装redis

root@C17 ~]# yum -y install gcc jemalloc-devel
[root@C17 ~]# wget http://download.redis.io/releases/redis-5.0.7.tar.gz
[root@C17 ~]#  tar xvf redis-5.0.7.tar.gz
[root@C17 ~]# cd redis-5.0.7/
[root@C17 redis-5.0.7]# make -j && make install
[root@localhost redis-5.0.7]# ./utils/install_server.sh  #初始化配置,一直回车或者输入其他路径
[root@localhost redis-5.0.7]# pkill redis-server

#修改配置文件,开启群集模式
[root@redis-node1 ~]vim /etc/redis/6379.conf
bind 0.0.0.0   #修改监听地址
masterauth 123456  #建议配置,否则后期的master和slave主从复制无法成功,还需再配置
requirepass 123456
cluster-enabled yes #取消此行注释,必须开启集群,开启后redis 进程会有cluster显示
cluster-config-file nodes-6379.conf #取消此行注释,此为集群状态文件,记录主从关系及slot范围信息,由redis cluster 集群自动创建和维护
cluster-require-full-coverage no  #默认值为yes,设为no可以防止一个节点不可用导致整个cluster不可能

#所有节点启动redis
systemctl start redis_6379.service

#监听端口  6379  16379
[root@C67 redis-5.0.7]# ss -lnt
State       Recv-Q Send-Q                         Local Address:Port                                        Peer Address:Port
LISTEN      0      128                                        *:6379                                                   *:*
LISTEN      0      128                                        *:22                                                     *:*
LISTEN      0      100                                127.0.0.1:25                                                     *:*
LISTEN      0      128                                        *:16379                                                  *:*
LISTEN      0      128                                       :::22                                                    :::*
LISTEN      0      100                                      ::1:25                                                    :::*


#创建群集,任意一台群集节点上操作都可以
[root@C67 redis-5.0.7]# redis-cli -a 123456 --cluster create 172.16.1.17:6379 172.16.1.27:6379 172.16.1.37:6379 172.16.1.47:6379 172.16.1.57:6379 172.16.1.67:6379 --cluster-replicas 1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 172.16.1.57:6379 to 172.16.1.17:6379
Adding replica 172.16.1.67:6379 to 172.16.1.27:6379
Adding replica 172.16.1.47:6379 to 172.16.1.37:6379
M: 8ee05aca90d60502e6dd0b8bb2d11ddca43601b7 172.16.1.17:6379
   slots:[0-5460] (5461 slots) master
M: 561315520545ee63e91dc45b947c976aebf96cf9 172.16.1.27:6379
   slots:[5461-10922] (5462 slots) master
M: 8fd3ca285a152ed06f0cfe6fd489035219197801 172.16.1.37:6379
   slots:[10923-16383] (5461 slots) master
S: 13e2b9ae9ce39923d4210b254adbea06dc35397c 172.16.1.47:6379
   replicates 8fd3ca285a152ed06f0cfe6fd489035219197801
S: ce7b2d2aa9c64e01427e559283e9caf61f82d6ba 172.16.1.57:6379
   replicates 8ee05aca90d60502e6dd0b8bb2d11ddca43601b7
S: a7f3aa4b313f1262069dab639ab1f9bf218144c4 172.16.1.67:6379
   replicates 561315520545ee63e91dc45b947c976aebf96cf9
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.......
>>> Performing Cluster Check (using node 172.16.1.17:6379)
M: 8ee05aca90d60502e6dd0b8bb2d11ddca43601b7 172.16.1.17:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 561315520545ee63e91dc45b947c976aebf96cf9 172.16.1.27:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: a7f3aa4b313f1262069dab639ab1f9bf218144c4 172.16.1.67:6379
   slots: (0 slots) slave
   replicates 561315520545ee63e91dc45b947c976aebf96cf9
S: 13e2b9ae9ce39923d4210b254adbea06dc35397c 172.16.1.47:6379
   slots: (0 slots) slave
   replicates 8fd3ca285a152ed06f0cfe6fd489035219197801
M: 8fd3ca285a152ed06f0cfe6fd489035219197801 172.16.1.37:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: ce7b2d2aa9c64e01427e559283e9caf61f82d6ba 172.16.1.57:6379
   slots: (0 slots) slave
   replicates 8ee05aca90d60502e6dd0b8bb2d11ddca43601b7
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

#查看群集节点
[root@C67 redis-5.0.7]# redis-cli -h 172.16.1.17 -c cluster nodes
NOAUTH Authentication required.
[root@C67 redis-5.0.7]# redis-cli -h 172.16.1.17 -a 123456 -c cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
561315520545ee63e91dc45b947c976aebf96cf9 172.16.1.27:6379@16379 master - 0 1640355716000 2 connected 5461-10922
a7f3aa4b313f1262069dab639ab1f9bf218144c4 172.16.1.67:6379@16379 slave 561315520545ee63e91dc45b947c976aebf96cf9 0 1640355718188 6 connected
13e2b9ae9ce39923d4210b254adbea06dc35397c 172.16.1.47:6379@16379 slave 8fd3ca285a152ed06f0cfe6fd489035219197801 0 1640355719205 4 connected
8fd3ca285a152ed06f0cfe6fd489035219197801 172.16.1.37:6379@16379 master - 0 1640355717000 3 connected 10923-16383
ce7b2d2aa9c64e01427e559283e9caf61f82d6ba 172.16.1.57:6379@16379 slave 8ee05aca90d60502e6dd0b8bb2d11ddca43601b7 0 1640355719000 5 connected
8ee05aca90d60502e6dd0b8bb2d11ddca43601b7 172.16.1.17:6379@16379 myself,master - 0 1640355719000 1 connected 0-5460

#测试
[root@C67 redis-5.0.7]# redis-cli -h 172.16.1.17 -a 123456 -c
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.16.1.17:6379> set name wswj
-> Redirected to slot [5798] located at 172.16.1.27:6379
OK
172.16.1.27:6379> set age 20
-> Redirected to slot [741] located at 172.16.1.17:6379
OK
172.16.1.17:6379> get name
-> Redirected to slot [5798] located at 172.16.1.27:6379
"wswj"
172.16.1.37:6379> get age
-> Redirected to slot [741] located at 172.16.1.17:6379
"20"