当前最新稳定版本是redis 6.0.6,所以就用这个版本搭建redis集群
使用3台机器,每台机器2个节点
系统centos7
每个机器之间互通
192.168.10.1 代表第一台机器ip
192.168.10.2 代表第二台机器ip
192.168.10.3 代表第三台机器ip
升级gcc [3台都执行]
# 安装ruby
yum install ruby rubygems -y
# 升级gcc(数字7表示7.x版本,8表示8.x版本)
yum install centos-release-scl devtoolset-7-gcc* -y
# 临时生效gcc版本
scl enable devtoolset-7 bash
# 永久生效gcc版本
echo 'source /opt/rh/devtoolset-7/enable' >>/etc/profile && source /etc/profile
下载安装redis [3台都执行]
wget http://download.redis.io/releases/redis-6.0.6.tar.gz
tar xf redis-6.0.6.tar.gz
cd redis-6.0.6
make
make install PREFIX=/usr/local/redis
echo 'export PATH=$PATH:/usr/local/redis/bin' >>/etc/profile && source /etc/profile
创建目录和配置文件
# 第一台
mkdir -p /opt/redis-cluster/{7001,7002}
# 第二台
mkdir -p /opt/redis-cluster/{7003,7004}
# 第三台
mkdir -p /opt/redis-cluster/{7005,7006}
# 配置文件模板以7001节点为例(这里绑定本机ip和127.0.0.1),复制到其他节点目录下并改下ip和端口还有文件名称
cd /opt/redis-cluster/7001/
vim redis7001.conf
# 配置内容
bind 192.168.10.1 127.0.0.1
port 7001
daemonize yes
cluster-enabled yes
cluster-config-file nodes7001.conf
cluster-node-timeout 5000
cluster-require-full-coverage no
#aof
appendonly yes
appendfsync everysec
appendfilename "appendonly_7001.aof"
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
no-appendfsync-on-rewrite yes
#rdb
save 900 1
save 300 10
save 60 10000
dbfilename node7001.rdb
rdbcompression yes
rdbchecksum yes
stop-writes-on-bgsave-error no
dir /opt/redis-cluster/7001/
pidfile /var/run/redis_7001.pid
logfile /var/log/redis_7001.log
启动各个节点
# 启动redis各节点
# 第一台
/usr/local/redis/bin/redis-server /opt/redis-cluster/7001/redis7001.conf
/usr/local/redis/bin/redis-server /opt/redis-cluster/7002/redis7002.conf
# 第一台
/usr/local/redis/bin/redis-server /opt/redis-cluster/7003/redis7003.conf
/usr/local/redis/bin/redis-server /opt/redis-cluster/7004/redis7004.conf
# 第一台
/usr/local/redis/bin/redis-server /opt/redis-cluster/7005/redis7005.conf
/usr/local/redis/bin/redis-server /opt/redis-cluster/7006/redis7006.conf
创建集群,任意一台执行即可
redis-cli --cluster create 192.168.10.1:7001 192.168.10.1:7002 192.168.10.2:7003 192.168.10.2:7004 192.168.10.3:7005 192.168.10.3:7006 --cluster-replicas 1
查看集群状态信息
#例如当前节点端口为7001和7002的
redis-cli -c -p 7001
#连接其他节点用
redis-cli -c -h ip地址 -p 端口号
# 查看集群信息
127.0.0.1:7001> 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:1328
cluster_stats_messages_pong_sent:1318
cluster_stats_messages_sent:2646
cluster_stats_messages_ping_received:1313
cluster_stats_messages_pong_received:1328
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:2646
# 查看节点信息
127.0.0.1:7001> CLUSTER nodes
4cc528ff35eadeb795af874fefc0009e555774d6 192.168.10.3:7006@17006 slave db2ae554b712331f12e728fc7d246c89fa79a78d 0 1618554198477 3 connected
f202aff7ae33e68a6d331b34b5c32ca71d91e8f3 192.168.10.1:7001@17001 myself,master - 0 1618554197000 1 connected 0-5460
c528e42056573406479b0af5f5b089ec990836a5 192.168.10.1:7002@17002 slave 2e3f01f01627f0249c9bc5a5e6808c8805ffd40b 0 1618554197473 5 connected
2e3f01f01627f0249c9bc5a5e6808c8805ffd40b 192.168.10.3:7005@17005 master - 0 1618554198576 5 connected 10923-16383
dbecff7305a5ad51f42052548253f73419b714dc 192.168.10.2:7004@17004 slave f202aff7ae33e68a6d331b34b5c32ca71d91e8f3 0 1618554199480 1 connected
db2ae554b712331f12e728fc7d246c89fa79a78d 192.168.10.2:7003@17003 master - 0 1618554198978 3 connected 5461-10922
127.0.0.1:7001>
存取key测试
本文最后记录时间 2024-03-30
文章链接地址:https://wojc.cn/archives/1104.html
本站文章除注明[转载|引用|来源],均为本站原创内容,转载前请注明出处
文章链接地址:https://wojc.cn/archives/1104.html
本站文章除注明[转载|引用|来源],均为本站原创内容,转载前请注明出处