Redis5 cluster

来自linux中国网wiki
docker>Evan2019年10月10日 (四) 03:38的版本 →‎修改配置文件(三台机器6个配置)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳到导航 跳到搜索

节点信息

角色                        主机名                                                             IP1               IP2       

7000  7001               01                   192.168.10.211     
7002  7003               02                   192.168.10.212      
7004  7005               03                   192.168.10.213      

姐妹篇 Redis4 cluster

防火墙设置

第一台:
firewall-cmd --permanent --add-port=7000/tcp
firewall-cmd --permanent --add-port=17000/tcp
firewall-cmd --permanent --add-port=7001/tcp
firewall-cmd --permanent --add-port=17001/tcp
firewall-cmd --reload
firewall-cmd --list-all-zones
第二台:
firewall-cmd --permanent --add-port=7002/tcp
firewall-cmd --permanent --add-port=17002/tcp
firewall-cmd --permanent --add-port=7003/tcp
firewall-cmd --permanent --add-port=17003/tcp
firewall-cmd --reload
firewall-cmd --list-all-zones
第三台:
firewall-cmd --permanent --add-port=7004/tcp
firewall-cmd --permanent --add-port=17004/tcp
firewall-cmd --permanent --add-port=7005/tcp
firewall-cmd --permanent --add-port=17005/tcp
firewall-cmd --reload
firewall-cmd --list-all-zones

系统参数设置

vi /etc/sysctl.conf
vm.overcommit_memory = 1
net.core.somaxconn = 1024

/sbin/sysctl -p /etc/sysctl.conf

redis 5 安装

wget -c http://download.redis.io/releases/redis-5.0.5.tar.gz
redis_install_dir=/home/apps/redis
redis_ver=5.0.5

THREAD=4
tar xzf redis-${redis_ver}.tar.gz
pushd redis-${redis_ver} > /dev/null

make  -j ${THREAD}

make PREFIX=/home/apps/redis #here
make distclean
#cd src && make install #这个才行 特别是cluster ? 2019年 8月16日 星期五 23时29分41秒 CST
make install  PREFIX=/home/apps/redis  # make install #这个有时没有创建出目录来呀

#这个我没做 根本没用 哈哈 
#复制rb 或者放到安装的bin目录 然后再加path 
#cp /opt/redis-5.0.0/src/redis-trib.rb /usr/local/bin

echo 'export PATH=/home/apps/redis/bin/:$PATH' >> /etc/profile && source /etc/profile

NOTE
redis5 用redis-cli创建整个redis集群(redis5以前的版本集群是依靠ruby脚本redis-trib.rb实现

创建节点

#第一台:192.168.10.211 #here

cd /home/apps/redis 
mkdir -p redis_cluster
cd redis_cluster
mkdir data 7000 7001

cp /root/src/redis-5.0.5/redis.conf  /home/apps/redis/redis_cluster
cd  /home/apps/redis/redis_cluster
cp redis.conf 7000/redis_7000.conf
mv redis.conf  7001/redis_7001.conf


#第2台:192.168.10.212
cd /home/apps/redis 
mkdir -p redis_cluster
cd redis_cluster
mkdir data 7002 7003

cp /root/src/redis-5.0.5/redis.conf  .

cp  redis.conf 7002/redis_7002.conf
mv redis.conf  7003/redis_7003.conf



#第3台:192.168.10.213
cd /home/apps/redis 
mkdir -p redis_cluster
cd redis_cluster
mkdir data 7004 7005
cp /root/src/redis-5.0.5/redis.conf  .
cp redis.conf 7004/redis_7004.conf
mv redis.conf 7005/redis_7005.conf


修改配置文件(三台机器6个配置)

note  日志在系统那  因为我们配置文件没指定 /var/log/redis/

ls /home/apps/redis/redis_cluster/data/
appendonly-7000.aof  dump_7000.rdb  nodes_7000.conf
appendonly-7001.aof  dump_7001.rdb  nodes_7001.conf
# 生成在这里了  /home/apps/redis/redis_cluster/data/nodes_7000.conf  nodes_7001.conf

配置建议加上这个 
# appendfsync always
appendfsync everysec
# appendfsync no



第一台:
 pushd /home/apps/redis/redis_cluste
mkdir -p /var/log/redis
: > 7000/redis_7000.conf
vi 7000/redis_7000.conf

port 7000
bind 192.168.10.211 127.0.0.1
cluster-enabled yes
appendfilename "appendonly-7000.aof"
cluster-config-file nodes_7000.conf 
cluster-node-timeout 15000
appendonly yes
daemonize yes
dbfilename dump_7000.rdb
dir /home/apps/redis/redis_cluster/data 
logfile "/var/log/redis/7000.log"
protected-mode yes
pidfile /var/run/redis_7000.pid

:>7001/redis_7001.conf
vi 7001/redis_7001.conf

port 7001
bind 192.168.10.211 127.0.0.1
cluster-enabled yes
appendfilename "appendonly-7001.aof"
cluster-config-file nodes_7001.conf
cluster-node-timeout 15000
appendonly yes
daemonize yes
dbfilename dump_7001.rdb
dir /home/apps/redis/redis_cluster/data 
logfile "/var/log/redis/7001.log"
protected-mode yes
pidfile /var/run/redis_7001.pid


第二台 
 pushd /home/apps/redis/redis_cluste
mkdir -p /var/log/redis
:>7002/redis_7002.conf
vi 7002/redis_7002.conf

port 7002
bind 192.168.10.212 127.0.0.1
cluster-enabled yes
appendfilename "appendonly-7002.aof"
cluster-config-file nodes_7002.conf
cluster-node-timeout 15000
appendonly yes
daemonize yes
dbfilename dump_7002.rdb
dir /home/apps/redis/redis_cluster/data 
logfile "/var/log/redis/7002.log"
protected-mode yes
pidfile /var/run/redis_7002.pid

:>7003/redis_7003.conf
vi 7003/redis_7003.conf

port 7003
bind 192.168.10.212 127.0.0.1
cluster-enabled yes
appendfilename "appendonly-7003.aof"
cluster-config-file nodes_7003.conf
cluster-node-timeout 15000
appendonly yes
daemonize yes
dbfilename dump_7003.rdb
dir /home/apps/redis/redis_cluster/data 
logfile "/var/log/redis/7003.log"
protected-mode yes
pidfile /var/run/redis_7003.pid


第3台
 pushd /home/apps/redis/redis_cluste
mkdir -p /var/log/redis
:>7004/redis_7004.conf
vi 7004/redis_7004.conf

port 7004
bind 192.168.10.213 127.0.0.1
cluster-enabled yes
appendfilename "appendonly-7004.aof"
cluster-config-file nodes_7004.conf
cluster-node-timeout 15000
appendonly yes
daemonize yes
dbfilename dump_7004.rdb
dir /home/apps/redis/redis_cluster/data 
logfile "/var/log/redis/7004.log"
protected-mode yes
pidfile /var/run/redis_7004.pid

:>7005/redis_7005.conf
vi 7005/redis_7005.conf

port 7005
bind 192.168.10.213 127.0.0.1
cluster-enabled yes
appendfilename "appendonly-7005.aof"
cluster-config-file nodes_7005.conf
cluster-node-timeout 15000
appendonly yes
daemonize yes
dbfilename dump_7005.rdb
dir /home/apps/redis/redis_cluster/data 
logfile "/var/log/redis/7005.log"
protected-mode yes
pidfile /var/run/redis_7005.pid


启动redis

cd /usr/local/redis/etc/redis_cluster

第一台:
mkdir /var/log/redis
#mkdir -p  /home/apps/redis/etc/redis_cluster
#cd /home/apps/redis/etc/redis_cluster # 没这个路径哦 cd /usr/local/redis/etc/redis_cluster
/home/apps/redis/bin/redis-server /home/apps/redis/redis_cluster/7000/redis_7000.conf
/home/apps/redis/bin/redis-server /home/apps/redis/redis_cluster/7001/redis_7001.conf



mkdir /var/log/redis
#这个暂时感觉没用 
#mkdir -p  /home/apps/redis/etc/redis_cluster
#cd /home/apps/redis/etc/redis_cluster # 没这个路径哦 cd /usr/local/redis/etc/redis_cluster
/home/apps/redis/bin/redis-server /home/apps/redis/redis_cluster/7002/redis_7002.conf
/home/apps/redis/bin/redis-server /home/apps/redis/redis_cluster/7003/redis_7003.conf

第三台:
mkdir /var/log/redis
#mkdir -p  /home/apps/redis/etc/redis_cluster
#cd /home/apps/redis/etc/redis_cluster # 没这个路径哦 cd /usr/local/redis/etc/redis_cluster
/home/apps/redis/bin/redis-server /home/apps/redis/redis_cluster/7004/redis_7004.conf
/home/apps/redis/bin/redis-server /home/apps/redis/redis_cluster/7005/redis_7005.conf

登陆redis

/home/apps/redis/bin/redis-cli  -c -h  192.168.10.211 -p 7000
redis-cli -h 10.3.10.141 -p 7001

redis-cli -h 10.3.10.142 -p 7002
redis-cli -h 10.3.10.142 -p 7003

redis-cli -h 10.3.10.143 -p 7004
redis-cli -h 10.3.10.143 -p 7005

创建集群

注意:在任意一台上运行 不要在每台机器上都运行,一台就够了
#要开通端口  firewalld

redis-cli --cluster create --cluster-replicas 1 192.168.10.211:7000 192.168.10.211:7001 192.168.10.212:7002 192.168.10.212:7003 192.168.10.213:7004  192.168.10.213:7005
#--replicas  1  表示 自动为每一个master节点分配一个slave节点    上面有6个节点,程序会按照一定规则生成 3个master(主)3个slave(从)

执行详情

 [root@dev-hello-1 redis_cluster]# redis-cli --cluster create --cluster-replicas 1 192.168.10.201:7000 192.168.10.201:7001 192.168.10.202:7002 192.168.10.202:7003 192.168.10.203:7004  192.168.10.203:7005
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.10.202:7003 to 192.168.10.201:7000
Adding replica 192.168.10.203:7005 to 192.168.10.202:7002
Adding replica 192.168.10.201:7001 to 192.168.10.203:7004
M: caf3a2c83f4f34498d9152161d3d62ca499094eb 192.168.10.201:7000
   slots:[0-5460] (5461 slots) master
S: ecbc0c648ff15c4ff2c176563afa580c7c8b7f3b 192.168.10.201:7001
   replicates 3792aae886d957e1a0272bcd198a06798f05c26d
M: 9c2f0b7827180d93371b493e8ca59b6eea776b0c 192.168.10.202:7002
   slots:[5461-10922] (5462 slots) master
S: b66f09b38e321ce3f8ee000b7cb40fe804f3a450 192.168.10.202:7003
   replicates caf3a2c83f4f34498d9152161d3d62ca499094eb
M: 3792aae886d957e1a0272bcd198a06798f05c26d 192.168.10.203:7004
   slots:[10923-16383] (5461 slots) master
S: a027592b56eb7477fe039786490318efdda8a0d6 192.168.10.203:7005
   replicates 9c2f0b7827180d93371b493e8ca59b6eea776b0c
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 192.168.10.201:7000)
M: caf3a2c83f4f34498d9152161d3d62ca499094eb 192.168.10.201:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: a027592b56eb7477fe039786490318efdda8a0d6 192.168.10.203:7005
   slots: (0 slots) slave
   replicates 9c2f0b7827180d93371b493e8ca59b6eea776b0c
M: 3792aae886d957e1a0272bcd198a06798f05c26d 192.168.10.203:7004
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 9c2f0b7827180d93371b493e8ca59b6eea776b0c 192.168.10.202:7002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: ecbc0c648ff15c4ff2c176563afa580c7c8b7f3b 192.168.10.201:7001
   slots: (0 slots) slave
   replicates 3792aae886d957e1a0272bcd198a06798f05c26d
S: b66f09b38e321ce3f8ee000b7cb40fe804f3a450 192.168.10.202:7003
   slots: (0 slots) slave
   replicates caf3a2c83f4f34498d9152161d3d62ca499094eb
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

查看redis集群情况

redis-cli -h 192.168.10.211 -p 7000 cluster nodes

a027592b56eb7477fe039786490318efdda8a0d6 192.168.10.203:7005@17005 slave 9c2f0b7827180d93371b493e8ca59b6eea776b0c 0 1567502665000 6 connected
3792aae886d957e1a0272bcd198a06798f05c26d 192.168.10.203:7004@17004 master - 0 1567502666171 5 connected 10923-16383
9c2f0b7827180d93371b493e8ca59b6eea776b0c 192.168.10.202:7002@17002 master - 0 1567502664166 3 connected 5461-10922
ecbc0c648ff15c4ff2c176563afa580c7c8b7f3b 192.168.10.201:7001@17001 slave 3792aae886d957e1a0272bcd198a06798f05c26d 0 1567502664000 5 connected
caf3a2c83f4f34498d9152161d3d62ca499094eb 192.168.10.201:7000@17000 myself,master - 0 1567502663000 1 connected 0-5460
b66f09b38e321ce3f8ee000b7cb40fe804f3a450 192.168.10.202:7003@17003 slave caf3a2c83f4f34498d9152161d3d62ca499094eb 0 1567502665169 4 connected

cluster 完整性检查


redis-trib.rb check 10.3.10.140:7000
redis-trib.rb check 10.3.10.140:7001
redis-trib.rb check 10.3.10.140:7002
redis-trib.rb check 10.3.10.140:7003
redis-trib.rb check 10.3.10.140:7004
redis-trib.rb check 10.3.10.140:7005

结果如下  16384个槽都分配到了 节点 

[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

测试

  
redis-cli -c -h  192.168.10.201 -p 7000

[root@dev-hello-1 redis_cluster]# redis-cli -c -h  192.168.10.201 -p 7000
192.168.10.201:7000> set evan 36
-> Redirected to slot [14924] located at 192.168.10.203:7004
OK
192.168.10.203:7004> 

[root@dev-hello-2 redis_cluster]# redis-cli -c -h  192.168.10.202 -p 7002
192.168.10.202:7002> get evan 
-> Redirected to slot [14924] located at 192.168.10.203:7004
"36"
192.168.10.203:7004> 

dev-hello-3 redis_cluster]#  redis-cli -c -h  192.168.10.203  -p 7005
192.168.10.203:7005> get evan
-> Redirected to slot [14924] located at 192.168.10.203:7004
"36"
192.168.10.203:7004>

测试通过了 

添加redis集群密码

方式一:修改所有Redis集群中的redis.conf文件加入

 
# NOTE   还是这个办法好  不然可能有些 没加载到配置文件去呢 
masterauth passwd123 
requirepass passwd123 
说明:这种方式需要重新启动各节点

方式二:进入各个实例进行设置 就是所有节点:

 
redis-cli -c -h 10.3.10.141 -p 7001
#好像不能一起执行
config set masterauth 36DbRGvv 
config set requirepass 36DbRGvv 
auth  36DbRGvv
config rewrite 

添加redis集群密码 一开始忘记加-c 导致 143 两个都加不上了  redis本身的密码这样就有了

-a 后 再执行命令就再 auth , 而如果是进入后 auth password 要每次 都再验证一下密码 麻烦 


开机自己启动

vi /apps/redis/redis_cluster/run

/apps/redis/bin/redis-server /home/apps/redis/redis_cluster/7004/redis_7004.conf
/apps/redis/bin/redis-server /home/apps/redis/redis_cluster/7005/redis_7005.conf

chmod  +x  /apps/redis/redis_cluster/run
chmod +x /etc/rc.d/rc.local

 vi  /etc/rc.d/rc.local
/apps/redis/redis_cluster/run


开启关闭

redis-cli -a xxx -c -h 192.168.0.60 -p 8001 shutdown  #集群cluster_state变成了fail状态?



troubleshooting

ISCONF Errors writing to the AOF file: No space left on device; nested exception is redis.clients.jedis.exceptions.JedisDataException: MISCONF Errors writing to the AOF file: No space left on device

org.springframework.dao.InvalidDataAccessApiUsageException: MISCONF Errors writing to the AOF file: No space left on device; nested exception is redis.clients.jedis.exceptions.JedisDataException: MISCONF Errors writing to the AOF file: No space left on device

原因 有一个台redis的机器 空间100% 

see also

redis-5.0 cluster带认证及客户端连接

Redis 5 之后版本的高可用集群搭建