🔊博主介绍
🥤本文内容
使用宝塔面板搭建
集群规划
使用CentOS7进行操作,Linux系统,配置为2个处理器核心,4个线程,8GB内存和100GB磁盘存储,所有计算机都首先进行YUM更新,修改每台计算机的固定IP地址,然后安装宝塔面板,在宝塔面板的软件商店中安装Redis,并开放端口6379(Redis默认端口)。
IP地址布局:
主节点1(Master1):192.168.80.101
主节点2(Master2):192.168.80.102
主节点3(Master3):192.168.80.103
从节点1(Slave1):192.168.80.111
从节点2(Slave2):192.168.80.112
从节点3(Slave3):192.168.80.113
配置
在Redis 6.0中,你可以使用server_cpulist、bio_cpulist、aof_rewrite_cpulist和bgsave_cpulist等配置项来指定Redis的不同进程或线程应该绑定到哪些CPU内核上运行。你需要根据你的服务器硬件和性能需求来设置这些参数。例如,如果你的服务器有8个CPU内核,并且你希望Redis进程和I/O线程在不同的内核上运行,你可以像下面示例那样配置它们。
- server_cpulist:RedisServer和IO线程绑定到CPU内核
- bio_cpulist:后台子线程绑定到CPU内核
- aof_rewrite_cpulist:后台AOF rewrite进程绑定到CPU内核
- bgsave_cpulist:后台RDB进程绑定到CPU内核
这可以帮助你优化Redis的性能,特别是当你有多个CPU内核,并且希望将不同类型的操作分散到不同的内核上时。以下是一个redis.conf配置文件的示例,展示了如何设置这些参数:
# Redis配置文件示例
# 保护模式设置为no,这样Redis就可以接受来自任何主机的连接
protected-mode no
# 端口号
port 6379
# 开启集群模式
cluster-enabled yes
# 每一个节点需要有一个配置文件,需要6份。每个节点处于集群的角色都需要告知其他所有节点,彼此知道,这个文件用于存储集群模式下的集群状态等信息,这个文件是由redis自己维护,我们不用管。如果你要重新创建集群,那么把这个文件删了就行
cluster-config-file nodes-6379.conf
# 超时时间,超时则认为master宕机,随后主备切换
cluster-node-timeout 5000
# TCP 后台线程和I/O线程:如果启用了 TCP 后台线程(io-threads-do-reads)或 I/O 线程(io-threads),确保为这些线程配置了正确的 CPU 内核列表(server_cpulist、bio_cpulist 等)。
io-threads-do-reads yes
io-threads 4
# Redis Server绑定到的CPU内核列表,这里绑定到CPU 0和1
server_cpulist 0-1
# 后台I/O线程绑定到的CPU内核列表,这里绑定到CPU 2和3
bio_cpulist 2-3
# AOF重写进程绑定到的CPU内核列表,这里绑定到CPU 4
aof_rewrite_cpulist 4
# RDB持久化进程绑定到的CPU内核列表,这里绑定到CPU 5
bgsave_cpulist 5
# 启用AOF持久化
appendonly yes
# AOF文件名称
appendfilename "appendonly.aof"
# 密码设置
requirepass admin
# Redis集群启用了密码验证,那么除了在每个节点的配置文件中设置requirepass之外,还需要设置masterauth
masterauth admin
# 其他配置...
这些参数的具体使用可能会因Redis版本的不同而有所变化,所以请确保查阅你正在使用的Redis版本的官方文档以获取最准确的信息。
在配置完成后,你需要重启Redis服务来使新的配置生效。
验证
验证是否启动成功
ps -ef | grep redis
查看版本
redis-cli --version
redis5.0以上集群创建方式改为了C编写的redis-cli创建,创建集群,–cluster-replicas 1指定从库数量1,创建顺序三主-三从。即主-主-主-从-从-从。如果redis设置了密码,则创建集群时需要添加密码信息 -a 密码。在任意节点上执行以下命令:
redis-cli -a admin --cluster create 192.168.80.101:6371 192.168.80.102:6372 192.168.80.103:6373 192.168.80.111:6381 192.168.80.112:6382 192.168.80.113:6383 --cluster-replicas 1
确认集群配置信息,确认无误则输入 yes 并按回车。
[root@master1 redis]# redis-cli -a admin --cluster create 192.168.80.101:6371 192.168.80.102:6372 192.168.80.103:6373 192.168.80.111:6381 192.168.80.112:6382 192.168.80.113:6383 --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 192.168.80.112:6382 to 192.168.80.101:6371
Adding replica 192.168.80.113:6383 to 192.168.80.102:6372
Adding replica 192.168.80.111:6381 to 192.168.80.103:6373
M: e6e0dd6a4e109660df421b4bef93f4f72991619c 192.168.80.101:6371
slots:[0-5460] (5461 slots) master
M: 5520b535a6e9adc9424c6bd35ccff689a7f9c18a 192.168.80.102:6372
slots:[5461-10922] (5462 slots) master
M: ea32e9db2b4a9b16e509ecb6a2235abb7a13cd0c 192.168.80.103:6373
slots:[10923-16383] (5461 slots) master
S: bb78f4547103f6e1bed1482a5ee8204c4c257f81 192.168.80.111:6381
replicates ea32e9db2b4a9b16e509ecb6a2235abb7a13cd0c
S: 975f23baca9ccc541b16cab916d66efa7d1b6fc2 192.168.80.112:6382
replicates e6e0dd6a4e109660df421b4bef93f4f72991619c
S: 559528309e92929364623c58208e268c73933d9c 192.168.80.113:6383
replicates 5520b535a6e9adc9424c6bd35ccff689a7f9c18a
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
检查集群:
redis-cli -a admin --cluster check 192.168.80.101:6379
查看集群信息:
redis-cli -a admin -h 192.168.80.101 -p 6379 -c
-a Redis密码
-c表示集群模式,指定IP和端口
cluster info
查看集群节点列表
cluster nodes
使用docker搭建
在安装好docker之后,直接在不同机器上执行以下命令(节点1):
docker run -d --name redis-node-1 --net host --privileged=true -v /data/redis/share/redis-node-1:/data redis:7.2.4 --cluster-enabled yes --appendonly yes --port 6379
使用脚本搭建
或者执行编写脚本docker_redis6.sh:
vim docker_redis6.sh
#!/bin/bash
for i in $(seq 6)
do
docker run -d --name redis-node-$i --net host --privileged=true -v /data/redis/share/redis-node-$i:/data redis:7.2.4 --cluster-enabled yes --protected-mode no --cluster-config-file nodes-637$i.conf --cluster-node-timeout 5000 --requirepass admin --masterauth admin --appendonly yes --port 637$i
done
然后执行以下命令:
sh docker_redis6.sh
redis-cli -a admin --cluster create 192.168.80.111:6371 192.168.80.111:6372 192.168.80.111:6373 192.168.80.111:6374 192.168.80.111:6375 192.168.80.111:6376 --cluster-replicas 1
操作日志如下:
[root@slave1 ~]# vim docker_redis6.sh
[root@slave1 ~]# sh docker_redis6.sh
Unable to find image 'redis:7.2.4' locally
7.2.4: Pulling from library/redis
e1caac4eb9d2: Pull complete
7469c6c5b625: Pull complete
a3d1b68c4a62: Pull complete
152cbe749752: Pull complete
7218480dfba1: Pull complete
e61c48a0d344: Pull complete
4f4fb700ef54: Pull complete
82adb0efabd8: Pull complete
Digest: sha256:e647cfe134bf5e8e74e620f66346f93418acfc240b71dd85640325cb7cd01402
Status: Downloaded newer image for redis:7.2.4
98f2f7e129a7546ce00837f4e0c64bffee1f2849a2933e6afba12f9bc2e3eb8a
e64a8c103e66aa3dde0fbd23ecb6a3ebdce798e84758557a4cdce4420239227f
f303e860123621c973651c5fa4a632e48836d8b1d713dd55d083c94535a08967
bc7cb1fbe943daf0cf12d367e48eb74e70c1399b33b7ad7795d13bbc32146272
227ec0dcb91615d303185c8891da3cc5da8aa562fd5967dc79800a57c9366898
0ae333fac21b81e996ac37dda03d4267564027448a7ab50f07c514a223ebf8c9
[root@slave1 ~]# cat docker_redis6.sh
#!/bin/bash
for i in $(seq 6)
do
docker run -d --name redis-node-$i --net host --privileged=true -v /data/redis/share/redis-node-$i:/data redis:7.2.4 --cluster-enabled yes --protected-mode no --cluster-config-file nodes-637$i.conf --cluster-node-timeout 5000 --requirepass admin --masterauth admin --appendonly yes --port 637$i
done
[root@slave1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0ae333fac21b redis:7.2.4 "docker-entrypoint.s…" 33 seconds ago Up 31 seconds redis-node-6
227ec0dcb916 redis:7.2.4 "docker-entrypoint.s…" 35 seconds ago Up 33 seconds redis-node-5
bc7cb1fbe943 redis:7.2.4 "docker-entrypoint.s…" 37 seconds ago Up 35 seconds redis-node-4
f303e8601236 redis:7.2.4 "docker-entrypoint.s…" 39 seconds ago Up 36 seconds redis-node-3
e64a8c103e66 redis:7.2.4 "docker-entrypoint.s…" 41 seconds ago Up 38 seconds redis-node-2
98f2f7e129a7 redis:7.2.4 "docker-entrypoint.s…" 43 seconds ago Up 41 seconds redis-node-1
[root@slave1 ~]# docker exec -it redis-node-1 bash
root@slave1:/data# redis-cli -a admin --cluster create 192.168.80.111:6371 192.168.80.111:6372 192.168.80.111:6373 192.168.80.111:6374 192.168.80.111:6375 192.168.80.111:6376 --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 192.168.80.111:6375 to 192.168.80.111:6371
Adding replica 192.168.80.111:6376 to 192.168.80.111:6372
Adding replica 192.168.80.111:6374 to 192.168.80.111:6373
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 91f832b617cdd004b37a0d096d6ff64cb4a1d155 192.168.80.111:6371
slots:[0-5460] (5461 slots) master
M: 2e404745b1a823893fbe342ed107e46f479abc67 192.168.80.111:6372
slots:[5461-10922] (5462 slots) master
M: fc2ff40aed0043e187340a52f4eeac3d02173e3e 192.168.80.111:6373
slots:[10923-16383] (5461 slots) master
S: 3e1612dbbd52cbf2c96fb62583eec0583df1d42b 192.168.80.111:6374
replicates fc2ff40aed0043e187340a52f4eeac3d02173e3e
S: 60cfe4ead7d6b32a9293f953b527488cd6d04976 192.168.80.111:6375
replicates 91f832b617cdd004b37a0d096d6ff64cb4a1d155
S: 37d1e82aff6ac1cee20fcdcb97c49ffb60f28fbf 192.168.80.111:6376
replicates 2e404745b1a823893fbe342ed107e46f479abc67
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.80.111:6371)
M: 91f832b617cdd004b37a0d096d6ff64cb4a1d155 192.168.80.111:6371
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 3e1612dbbd52cbf2c96fb62583eec0583df1d42b 192.168.80.111:6374
slots: (0 slots) slave
replicates fc2ff40aed0043e187340a52f4eeac3d02173e3e
M: fc2ff40aed0043e187340a52f4eeac3d02173e3e 192.168.80.111:6373
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 60cfe4ead7d6b32a9293f953b527488cd6d04976 192.168.80.111:6375
slots: (0 slots) slave
replicates 91f832b617cdd004b37a0d096d6ff64cb4a1d155
S: 37d1e82aff6ac1cee20fcdcb97c49ffb60f28fbf 192.168.80.111:6376
slots: (0 slots) slave
replicates 2e404745b1a823893fbe342ed107e46f479abc67
M: 2e404745b1a823893fbe342ed107e46f479abc67 192.168.80.111:6372
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@slave1:/data# redis-cli -p 6371 -a admin
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6371> 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:1
cluster_stats_messages_ping_sent:104
cluster_stats_messages_pong_sent:118
cluster_stats_messages_sent:222
cluster_stats_messages_ping_received:113
cluster_stats_messages_pong_received:104
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:222
total_cluster_links_buffer_limit_exceeded:0
127.0.0.1:6371> cluster nodes
3e1612dbbd52cbf2c96fb62583eec0583df1d42b 192.168.80.111:6374@16374 slave fc2ff40aed0043e187340a52f4eeac3d02173e3e 0 1708959944000 3 connected
fc2ff40aed0043e187340a52f4eeac3d02173e3e 192.168.80.111:6373@16373 master - 0 1708959943000 3 connected 10923-16383
60cfe4ead7d6b32a9293f953b527488cd6d04976 192.168.80.111:6375@16375 slave 91f832b617cdd004b37a0d096d6ff64cb4a1d155 0 1708959943000 1 connected
91f832b617cdd004b37a0d096d6ff64cb4a1d155 192.168.80.111:6371@16371 myself,master - 0 1708959943000 1 connected 0-5460
37d1e82aff6ac1cee20fcdcb97c49ffb60f28fbf 192.168.80.111:6376@16376 slave 2e404745b1a823893fbe342ed107e46f479abc67 0 1708959943547 2 connected
2e404745b1a823893fbe342ed107e46f479abc67 192.168.80.111:6372@16372 master - 0 1708959943748 2 connected 5461-10922
127.0.0.1:6371>
📢文章总结
对本篇文章进行总结:
🔔以上就是今天要讲的内容,阅读结束后,反思和总结所学内容,并尝试应用到现实中,有助于深化理解和应用知识。与朋友或同事分享所读内容,讨论细节并获得反馈,也有助于加深对知识的理解和吸收。
🔔如果您需要转载或者搬运这篇文章的话,非常欢迎您私信我哦~
📥博主目标
- 🍋程序开发这条路不能停,停下来容易被淘汰掉,吃不了自律的苦,就要受平庸的罪,持续的能力才能带来持续的自信。我本是一个很普通的程序员,放在人堆里,除了与生俱来的盛世美颜,就剩180的大高个了,就是我这样的一个人,默默写博文也有好多年了。
- 📺有句老话说的好,牛逼之前都是傻逼式的坚持,希望自己可以通过大量的作品、时间的积累、个人魅力、运气、时机,可以打造属于自己的技术影响力。
- 💥内心起伏不定,我时而激动,时而沉思。我希望自己能成为一个综合性人才,具备技术、业务和管理方面的精湛技能。我想成为产品架构路线的总设计师,团队的指挥者,技术团队的中流砥柱,企业战略和资本规划的实战专家。
- 🎉这个目标的实现需要不懈的努力和持续的成长,但我必须努力追求。因为我知道,只有成为这样的人才,我才能在职业生涯中不断前进并为企业的发展带来真正的价值。在这个不断变化的时代,我们必须随时准备好迎接挑战,不断学习和探索新的领域,才能不断地向前推进。我坚信,只要我不断努力,我一定会达到自己的目标。