三台机器

192.168.100.118
192.168.100.119
192.168.100.120

第一步、分别在3台机器安装redis

下载redis

wget https://download.redis.io/releases/redis-5.0.10.tar.gz?_ga=2.135031601.1446451102.1613786086-233831403.1613634374

安装依赖

yum install -y tcl rsync

安装

 cd redis-5.0.10/
  make
  make test

复制到其他两台机器

rsync -av /usr/local/bin/* 192.168.100.119:/usr/local/bin/
rsync -av /usr/local/bin/* 192.168.100.120:/usr/local/bin/

第二部、配置redis

在三台机器操作

建立两个空目录

 mkdir -p /data/redis/6300/
 mkdir -p /data/redis/6301/

配置文件

vim /data/redis/6301/redis.conf
bind 192.168.100.118
protected-mode yes
port 6301
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /data/redis/6301/redis_6379.pid
loglevel notice
logfile "/data/redis/6301/redis.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /data/redis/6301
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite yes
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
cluster-enabled yes
cluster-config-file /data/redis/6301/nodes-6379.conf
cluster-node-timeout 15000
cluster-migration-barrier 1
cluster-require-full-coverage yes
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes

三台机器启动redis

 redis-server /data/redis/6300/redis.conf
 redis-server /data/redis/6301/redis.conf

 可以看见起来了
 [root@test118 6300]# ps aux| grep redis
root      79405  0.1  0.2 156556  7956 ?        Ssl  10:30   0:02 redis-server 192.168.100.118:6300 [cluster]
root      79454  0.1  0.3 160652 12132 ?        Ssl  10:30   0:02 redis-server 192.168.100.118:6301 [cluster]

第三步、创建集群

redis-cli --cluster create 192.168.100.118:6300 192.168.100.118:6301 192.168.100.119:6300 192.168.100.119:6301 192.168.100.120:6300 192.168.100.120:6301 --cluster-replicas 1

查看集群状态

[root@test118 6300]# redis-cli  --cluster --help
Unknown --cluster subcommand
[root@test118 6300]# redis-cli  --cluster check 192.168.100.118:6300
192.168.100.118:6300 (8caf14ad...) -> 0 keys | 5461 slots | 1 slaves.
192.168.100.120:6300 (f9243c8a...) -> 0 keys | 5461 slots | 1 slaves.
192.168.100.119:6300 (7524f968...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.100.118:6300)
M: 8caf14ad916cd36420981d5630947a3e2a2e0cab 192.168.100.118:6300
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: f9243c8af6b7bc403254731fd8a182c59f216537 192.168.100.120:6300
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 589d0454d3e6472aa8b0231c056082536cdc39d7 192.168.100.120:6301
   slots: (0 slots) slave
   replicates 7524f9682cae3e1073aa02a748b43b2f0dd9f9bb
S: 17e10bbd99bfe79909cc19cee31c81740b9c41fc 192.168.100.118:6301
   slots: (0 slots) slave
   replicates f9243c8af6b7bc403254731fd8a182c59f216537
S: 2242e524f9b4535e397f7b145e78878fb79b73d7 192.168.100.119:6301
   slots: (0 slots) slave
   replicates 8caf14ad916cd36420981d5630947a3e2a2e0cab
M: 7524f9682cae3e1073aa02a748b43b2f0dd9f9bb 192.168.100.119:6300
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

第四步 检查状态

插入测试

[root@test118 6300]# redis-cli -c -h 192.168.100.118 -p 6300
192.168.100.118:6300> set hello world
OK
192.168.100.118:6300> KEYS *
1) "hello"

到其他节点查看

[root@test120 6300]# redis-cli -c -h 192.168.100.120 -p 6300
192.168.100.120:6300> KEYS *
(empty list or set)
192.168.100.120:6300> exit
[root@test120 6300]# redis-cli -c -h 192.168.100.120 -p 6301
192.168.100.120:6301> keys *
(empty list or set)
192.168.100.120:6301> exit
[root@test120 6300]# redis-cli -c -h 192.168.100.118 -p 6301
192.168.100.118:6301> KEYS *
(empty list or set)
192.168.100.118:6301> exit
[root@test120 6300]# redis-cli -c -h 192.168.100.119 -p 6301
192.168.100.119:6301> KEYS *
1) "hello"

最后在119节点看到了在118插入的key,hello