redis-5.0.14集群部署

一、集群规格

IP 主机名 端口
10.0.0.108 master 7001
10.0.0.108 master 7002
10.0.0.109 slave1 7001
10.0.0.109 slave1 7002
10.0.0.110 slave2 7001
10.0.0.110 slave2 7002

二、安装redis-5.0.14集群

1.下载地址redis-5.0.14

2.上传包到每台服务器解压编译安装

tar -xvf redis-5.0.14.tar.gz
cd redis-5.0.14
make PREFIX=/usr/local/redis install

3.创建相关目录,编辑配置文件(在每台服务器上创建,也可以在一台上创建后拷贝到其他节点,我这里选择创建好一台后拷贝到其他节点)

mkdir -p /data/redis_cluster/{7001,7002}/{data,logs}
vi /data/redis_cluster/7001/redis_7001.conf

bind 10.0.0.108
daemonize yes
pidfile "/var/run/redis_7001.pid"
port 7001
timeout 0
tcp-keepalive 0
loglevel notice
logfile "/data/redis_cluster/7001/logs/redis.log"
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/data/redis_cluster/7001/data"
#如果做故障切换,不论主从节点都要填写密码且要保持一致
masterauth "123456"
slave-serve-stale-data yes
slave-read-only yes
repl-disable-tcp-nodelay no
slave-priority 98
#当前redis密码
requirepass "123456"
appendonly yes
# appendfsync always
appendfsync everysec
# appendfsync no
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
# Generated by CONFIG REWRITE
cluster-enabled yes
vi /data/redis_cluster/7002/redis_7002.conf

bind 10.0.0.108
daemonize yes
pidfile "/var/run/redis_7002.pid"
port 7002
timeout 0
tcp-keepalive 0
loglevel notice
logfile "/data/redis_cluster/7002/logs/redis.log"
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/data/redis_cluster/7002/data"
#如果做故障切换,不论主从节点都要填写密码且要保持一致
masterauth "123456"
slave-serve-stale-data yes
slave-read-only yes
repl-disable-tcp-nodelay no
slave-priority 98
#当前redis密码
requirepass "123456"
appendonly yes
# appendfsync always
appendfsync everysec
# appendfsync no
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
# Generated by CONFIG REWRITE
cluster-enabled yes
vi /usr/lib/systemd/system/redis_7001.service

[Unit]
Description=Redis
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/redis/bin/redis-server /data/redis_cluster/7001/redis_7001.conf
PrivateTmp=true
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
vi /usr/lib/systemd/system/redis_7002.service

[Unit]
Description=Redis
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/redis/bin/redis-server /data/redis_cluster/7002/redis_7002.conf
PrivateTmp=true
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

4.拷贝文件到其他节点

在10.0.0。108上执行
scp -r /data/redis_cluster/ root@10.0.0.109:/data/
scp -r /data/redis_cluster/ root@10.0.0.110:/data/
注意修改redis_7001.conf和redis_7002.conf中bind的IP

scp /usr/lib/systemd/system/redis_7001.service root@10.0.0.109:/usr/lib/systemd/system/redis_7001.service
scp /usr/lib/systemd/system/redis_7002.service root@10.0.0.109:/usr/lib/systemd/system/redis_7002.service
scp /usr/lib/systemd/system/redis_7001.service root@10.0.0.110:/usr/lib/systemd/system/redis_7001.service
scp /usr/lib/systemd/system/redis_7002.service root@10.0.0.110:/usr/lib/systemd/system/redis_7002.service

三、启动所有节点的redis服务

systemctl start redis_7001.service
systemctl start redis_7002.service
systemctl enable redis_7001.service
systemctl enable redis_7002.service

四、创建redis集群

cp /usr/local/redis/bin/redis-cli /usr/bin/
redis-cli -a 123456 --cluster create --cluster-replicas 1 10.0.0.108:7001 10.0.0.108:7002 10.0.0.109:7001 10.0.0.109:7002 10.0.0.110:7001 10.0.0.110:7002
执行命令后会让你确认配置,输入yes确认

五、连接集群验证

redis-cli -a 123456 -c -h 10.0.0.108 -p 7001
查看集群信息
> CLUSTER INFO
查看节点信息
> CLUSTER NODES
posted @ 2024-07-31 17:18  村尚chun叔  阅读(93)  评论(0编辑  收藏  举报