文档课题:redis Cluster集群环境搭建.
操作系统:rhel 7.9 64位
数据库:redis 6.2.6
环境说明:
第1台机器:主机名leo-redis626-a 192.168.133.100 8001端口 8002端口
第2台机器:主机名leo-redis626-b 192.168.133.101 8001端口 8002端口
第3台机器:主机名leo-redis626-c 192.168.133.102 8001端口 8002端口
前提条件:三台服务器均已安装好redis 6.2.6.
1、创建文件夹
[root@leo-redis626-a ~]# mkdir -p /usr/local/redis/redis-cluster/{8001,8002}
[root@leo-redis626-b ~]# mkdir -p /usr/local/redis/redis-cluster/{8001,8002}
[root@leo-redis626-c ~]# mkdir -p /usr/local/redis/redis-cluster/{8001,8002}
2、配置文件处理
2.1、拷贝文件
--将redis安装目录下的redis.conf文件拷贝到8001目录.
[root@leo-redis626-a redis-6.2.6]# cp /usr/local/src/redis-6.2.6/redis.conf /usr/local/redis/redis-cluster/8001/
2.2、修改文件
--修改redis.conf文件.
[root@leo-redis626-a redis-6.2.6]# vi /usr/local/redis/redis-cluster/8001/redis.conf
将文件替换为以下内容:
port 8001
daemonize yes
pidfile "/var/run/redis_8001.pid"
#指定数据文件存放位置,必须要指定不同的目录位置,不然会丢失数据
dir /usr/local/redis/redis-cluster/8001/
#启动集群模式
cluster-enabled yes
#集群节点信息文件,这里800x最好和port对应上
cluster-config-file nodes-8001.conf
# 节点离线的超时时间
cluster-node-timeout 5000
#去掉bind绑定访问ip信息
#bind 127.0.0.1
#关闭保护模式
protected-mode no
#启动AOF文件
appendonly yes
#如果要设置密码需要增加如下配置:
#设置redis访问密码
#requirepass root123
#设置集群节点间访问密码,跟上面一致
2.3、复制到8002目录
--将文件拷贝到8002文件夹.
[root@leo-redis626-a redis-6.2.6]# cp /usr/local/redis/redis-cluster/8001/redis.conf /usr/local/redis/redis-cluster/8002/
[root@leo-redis626-a redis-6.2.6]# cd /usr/local/redis/redis-cluster/8002/
[root@leo-redis626-a 8002]# ll
total 4
-rw-r--r--. 1 root root 654 Apr 10 16:06 redis.conf
2.4、修改8002下文件
--批量修改字符串.
[root@leo-redis626-a 8002]# vi redis.conf
:%s/8001/8002/g
2.5、传递配置文件
将leo-redis626-a机器上的文件传递到另外两台机器的相应目录中.
[root@leo-redis626-a 8001]# scp /usr/local/redis/redis-cluster/8001/redis.conf [email protected]:/usr/local/redis/redis-cluster/8001/
[root@leo-redis626-a 8001]# scp /usr/local/redis/redis-cluster/8001/redis.conf [email protected]:/usr/local/redis/redis-cluster/8001/
[root@leo-redis626-a 8002]# scp /usr/local/redis/redis-cluster/8002/redis.conf [email protected]:/usr/local/redis/redis-cluster/8002/
[root@leo-redis626-a 8002]# scp /usr/local/redis/redis-cluster/8002/redis.conf [email protected]:/usr/local/redis/redis-cluster/8002/
3、启动实例
--分别启动6个redis实例。
[root@leo-redis626-a 8001]# redis-server /usr/local/redis/redis-cluster/8001/redis.conf
[root@leo-redis626-a 8001]# redis-server /usr/local/redis/redis-cluster/8002/redis.conf
[root@leo-redis626-a 8001]# ps -ef|grep redis
avahi 870 1 0 12:50 ? 00:00:00 avahi-daemon: running [leo-redis626-a.local]
root 58863 1 0 16:23 ? 00:00:00 redis-server *:8001 [cluster]
root 58869 1 0 16:24 ? 00:00:00 redis-server *:8002 [cluster]
root 58875 56861 0 16:24 pts/0 00:00:00 grep --color=auto redis
[root@leo-redis626-b 8001]# redis-server /usr/local/redis/redis-cluster/8001/redis.conf
[root@leo-redis626-b 8001]# redis-server /usr/local/redis/redis-cluster/8002/redis.conf
[root@leo-redis626-b 8001]# ps -ef|grep redis
avahi 854 1 0 12:47 ? 00:00:00 avahi-daemon: running [leo-redis626-b.local]
root 58746 1 0 16:24 ? 00:00:00 redis-server *:8001 [cluster]
root 58752 1 0 16:24 ? 00:00:00 redis-server *:8002 [cluster]
root 58767 56755 0 16:25 pts/0 00:00:00 grep --color=auto redis
[root@leo-redis626-c 8001]# redis-server /usr/local/redis/redis-cluster/8001/redis.conf
[root@leo-redis626-c 8001]# redis-server /usr/local/redis/redis-cluster/8002/redis.conf
[root@leo-redis626-c 8001]# ps -ef|grep redis
avahi 871 1 0 12:37 ? 00:00:00 avahi-daemon: running [leo-redis626-c.local]
root 58057 1 0 16:24 ? 00:00:00 redis-server *:8001 [cluster]
root 58063 1 0 16:24 ? 00:00:00 redis-server *:8002 [cluster]
root 58069 57625 0 16:24 pts/3 00:00:00 grep --color=auto redis
4、关闭防火墙
--关闭三台服务器防火墙。
[root@leo-redis626-a src]# systemctl stop firewalld
[root@leo-redis626-a src]# systemctl disable firewalld
[root@leo-redis626-b src]# systemctl stop firewalld
[root@leo-redis626-b src]# systemctl disable firewalld
[root@leo-redis626-c src]# systemctl stop firewalld
[root@leo-redis626-c src]# systemctl disable firewalld
5、创建集群
--使用redis-cli创建整个redis集群。
[root@leo-redis626-a src]# /usr/local/src/redis-6.2.6/src/redis-cli --cluster create --cluster-replicas 1 \
> 192.168.133.100:8001 192.168.133.100:8002 \
> 192.168.133.101:8001 192.168.133.101:8002 \
> 192.168.133.102:8001 192.168.133.102:8002
>>> 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.133.101:8002 to 192.168.133.100:8001
Adding replica 192.168.133.102:8002 to 192.168.133.101:8001
Adding replica 192.168.133.100:8002 to 192.168.133.102:8001
M: c036f0322c2f8bdd04b1f156f8ff7c29804eb28b 192.168.133.100:8001
slots:[0-5460] (5461 slots) master
S: 6bf0eff5c1874da48cb64df57accac8eaf7aa57d 192.168.133.100:8002
replicates 81c45df99418144728954cda61dfa9fb3e1c5b3f
M: 6d9ca761fc09c83b6a3c2273a36b565c4c525ac3 192.168.133.101:8001
slots:[5461-10922] (5462 slots) master
S: 9f2b2480fecb0a193fdb2feed8c507c7c664cd74 192.168.133.101:8002
replicates c036f0322c2f8bdd04b1f156f8ff7c29804eb28b
M: 81c45df99418144728954cda61dfa9fb3e1c5b3f 192.168.133.102:8001
slots:[10923-16383] (5461 slots) master
S: 5d9ff4989ef75877c6683ece2cc39f22fa4160e6 192.168.133.102:8002
replicates 6d9ca761fc09c83b6a3c2273a36b565c4c525ac3
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.133.100:8001)
M: c036f0322c2f8bdd04b1f156f8ff7c29804eb28b 192.168.133.100:8001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 5d9ff4989ef75877c6683ece2cc39f22fa4160e6 192.168.133.102:8002
slots: (0 slots) slave
replicates 6d9ca761fc09c83b6a3c2273a36b565c4c525ac3
S: 6bf0eff5c1874da48cb64df57accac8eaf7aa57d 192.168.133.100:8002
slots: (0 slots) slave
replicates 81c45df99418144728954cda61dfa9fb3e1c5b3f
M: 6d9ca761fc09c83b6a3c2273a36b565c4c525ac3 192.168.133.101:8001
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 81c45df99418144728954cda61dfa9fb3e1c5b3f 192.168.133.102:8001
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 9f2b2480fecb0a193fdb2feed8c507c7c664cd74 192.168.133.101:8002
slots: (0 slots) slave
replicates c036f0322c2f8bdd04b1f156f8ff7c29804eb28b
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
参数说明:-a:密码 --cluster-replicas 1:表示1个master下挂1个slave;--cluster-replicas 2:表示1个 master下挂2个slave。
查看帮助命令
redis-cli --cluster help
参数:
create:创建一个集群环境host1:port1... hostN:portN
call:可以执行redis命令
add-node:将一个节点添加到集群里,第一个参数为新节点的ip:port,第二个参数为集群中任意一个已经存在的节点的ip:port
del-node:移除一个节点
reshard:重新分片
check:检查集群状态
6、验证集群
--连接任意一个客户端
[root@leo-redis626-b 8001]# redis-cli -c -h 192.168.133.101 -p 8001
--查看集群信息
192.168.133.101:8001> 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:6
cluster_my_epoch:3
cluster_stats_messages_ping_sent:1553
cluster_stats_messages_pong_sent:1592
cluster_stats_messages_meet_sent:1
cluster_stats_messages_sent:3146
cluster_stats_messages_ping_received:1592
cluster_stats_messages_pong_received:1554
cluster_stats_messages_received:3146
--查看节点
192.168.133.101:8001> cluster nodes
81c45df99418144728954cda61dfa9fb3e1c5b3f 192.168.133.102:8001@18001 master - 0 1681117111641 5 connected 10923-16383
6bf0eff5c1874da48cb64df57accac8eaf7aa57d 192.168.133.100:8002@18002 slave 81c45df99418144728954cda61dfa9fb3e1c5b3f 0 1681117112523 5 connected
c036f0322c2f8bdd04b1f156f8ff7c29804eb28b 192.168.133.100:8001@18001 master - 0 1681117112745 1 connected 0-5460
5d9ff4989ef75877c6683ece2cc39f22fa4160e6 192.168.133.102:8002@18002 slave 6d9ca761fc09c83b6a3c2273a36b565c4c525ac3 0 1681117112000 3 connected
6d9ca761fc09c83b6a3c2273a36b565c4c525ac3 192.168.133.101:8001@18001 myself,master - 0 1681117111000 3 connected 5461-10922
9f2b2480fecb0a193fdb2feed8c507c7c664cd74 192.168.133.101:8002@18002 slave c036f0322c2f8bdd04b1f156f8ff7c29804eb28b 0 1681117111102 1 connected
参考网址:
https://bg-webapi-1307080946.cos-website.ap-nanjing.myqcloud.com/%E8%BD%AF%E4%BB%B6%E5%B7%A5%E5%85%B7/%E7%8E%AF%E5%A2%83%E5%AE%89%E8%A3%85/10007-Redis%E9%9B%86%E7%BE%A4%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA.html#_1-%E6%96%87%E6%A1%A3%E4%B8%8B%E8%BD%BD
标签:cluster,redis,192.168,Cluster,集群,root,8001,8002
From: https://blog.51cto.com/u_12991611/6181363