娃哈哈好喝-真的!
技术够用就行,吃好喝好睡好!

redis集群搭建

redis集群

1. 实验环境:
serverip
redis1172.20.20.231
redis2172.20.20.232
redis3172.20.20.233
redis4172.20.20.234
redis5172.20.20.235
redis6172.20.20.236

系统:centos7.6,redis:6.2.6,集群模式:3个master,3个slave,master和slave随机选举

2. 配置

redis路径:

[root@centos redis]# pwd
/data/redis
[root@centos redis]# ls
bin conf data log pid  restart.sh
[root@centos redis]#

配置文件,所有节点都相同:

[root@centos conf]# pwd
/data/redis/conf
[root@centos conf]# ls
redis.conf sentinel.conf
[root@centos conf]# cat redis.conf
bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile "/data/redis/pid/redis.pid"
loglevel notice
logfile "/data/redis/log/redis.log"
databases 16
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/data/redis/data/redis"
replica-serve-stale-data yes
replica-read-only no
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
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
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
aof-rewrite-incremental-fsync yes
# 修改redis同一时间最大客户端连接数,如果设置为 maxclients 0,表示不作限制
maxclients 10000
# 配置密码
requirepass 1234@abcd
# 配置主redis密码(如果主redis挂掉,需要通过密码认证连接从redis)(密码配置为从redis密码)
masterauth 1234@abcd
# Generated by CONFIG REWRITE
save 3600 1
save 300 100
save 60 10000
#集群模式一定要开启的
cluster-enabled yes
#下面这个文件名字可以自定义
cluster-config-file node01.conf
cluster-require-full-coverage no
#允许redis使用的最大内存
maxmemory 2G
[root@centos conf]#

如果是多个实例放在一台服务器,需要将cluster-config-file这里的文件名字不能相同

启动redis:

[root@centos ~]# redis-server /data/redis/conf/redis.conf 
[root@centos ~]# ps -ef| grep redis
root      30503      1  0 Nov21 ?        00:01:47 /data/redis/bin/redis-server 0.0.0.0:6379 [cluster]
root      81818  41873  0 17:07 pts/0    00:00:00 grep --color=auto redis
[root@centos ~]# netstat -tunlap| grep redis
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      30503/redis-server  
tcp        0      0 0.0.0.0:16379           0.0.0.0:*               LISTEN      30503/redis-server

创建集群:

[root@centos ~]# redis-cli --cluster create --cluster-replicas 1 172.20.20.231:6379 172.20.20.232:6379 172.20.20.233:6379 172.20.20.234:6379 172.20.20.235:6379 172.20.20.236:6379  -a 1234@abcd

创建好之后查看集群状态

[root@centos ~]# redis-cli -c -h 172.20.20.231 -a 1234@abcd
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.20.20.231:6379> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:8
cluster_my_epoch:7
cluster_stats_messages_ping_sent:62426
cluster_stats_messages_pong_sent:66796
cluster_stats_messages_meet_sent:1
cluster_stats_messages_sent:129223
cluster_stats_messages_ping_received:66796
cluster_stats_messages_pong_received:62423
cluster_stats_messages_received:129219
172.20.20.231:6379> cluster nodes
29eee6a868665ac8a201ebbc261717f3c0e3f017 172.20.20.232:6379@16379 master - 0 1669108352000 0 connected 8393 10439
1d1731591dcb0818c312787f3f6cf60a8f0eeb93 172.20.20.235:6379@16379 master - 0 1669108351000 7 connected 0-5461
ad4cc6600fd0cdef45f82674db440f4b8586210c 172.20.20.231:6379@16379 myself,slave 1d1731591dcb0818c312787f3f6cf60a8f0eeb93 0 1669108354000 7 connected
bd110bc4ee40dc387c4c4e80d7dbeb5663886ffd 172.20.20.233:6379@16379 master - 0 1669108354157 8 connected 10440-16383
8fefbff6233465acad82627cd4ae52632903f91d 172.20.20.236:6379@16379 slave - 0 1669108353152 2 connected
568a869ae9c1712c944a0811923e198a7534395d 172.20.20.234:6379@16379 slave bd110bc4ee40dc387c4c4e80d7dbeb5663886ffd 0 1669108353000 8 connected
172.20.20.231:6379>

redis-cli参数说明

-c 连接集群

-a 密码

-h 主机ip

-p 端口

4. 测试

模拟故障

[root@centos ~]# redis-cli -h 172.20.20.235 -p 6379 -a 1234@abcd shutdown

将235这台关闭,查看集群状态

[root@centos ~]# redis-cli --cluster check 172.20.20.231:6379 -a 1234@abcd
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Could not connect to Redis at 172.20.20.235:6379: Connection refused
*** WARNING: 172.20.20.236:6379 claims to be slave of unknown node ID 10c6dda404afaf32cf13c0df1e262ba68ab69807.
172.20.20.231:6379 (ad4cc660...) -> 2 keys | 6194 slots | 0 slaves.
172.20.20.232:6379 (29eee6a8...) -> 2 keys | 2 slots | 0 slaves.
172.20.20.233:6379 (bd110bc4...) -> 0 keys | 10188 slots | 1 slaves.
[OK] 4 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 172.20.20.231:6379)
M: ad4cc6600fd0cdef45f82674db440f4b8586210c 172.20.20.231:6379
  slots:(6194 slots) master
M: 29eee6a868665ac8a201ebbc261717f3c0e3f017 172.20.20.232:6379
  slots:[8393],[10439] (2 slots) master
M: bd110bc4ee40dc387c4c4e80d7dbeb5663886ffd 172.20.20.233:6379
  slots: (10188 slots) master
  1 additional replica(s)
S: 8fefbff6233465acad82627cd4ae52632903f91d 172.20.20.236:6379
  slots: (0 slots) slave
  replicates 10c6dda404afaf32cf13c0df1e262ba68ab69807
S: 568a869ae9c1712c944a0811923e198a7534395d 172.20.20.234:6379
  slots: (0 slots) slave
  replicates bd110bc4ee40dc387c4c4e80d7dbeb5663886ffd
[ERR] Nodes don't agree about configuration!
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@centos data]#

之前235是master,模拟关闭后233提升了master

手动故障转移,172.20.20.234和172.20.20.233互为主从,172.20.20.234是从,手工故障转移可以将主从身份互换

[root@centos ~]# redis-cli -c -h 172.20.20.234 -p 6379 -a 1234@abcd cluster failover
赞(0)
未经允许不得转载:娃哈哈好喝 » redis集群搭建
分享到: 更多 (0)