千家信息网

Redis5.x 集群部署实战

发表于:2024-11-18 作者:千家信息网编辑
千家信息网最后更新 2024年11月18日,实验环境主机名IP地址Redis端口划分备注node171172.20.20.17116001,16002node172172.20.20.17216001,16002node173172.20.20
千家信息网最后更新 2024年11月18日Redis5.x 集群部署实战

实验环境

主机名

IP地址

Redis端口划分

备注

node171

172.20.20.171

16001,16002


node172

172.20.20.172

16001,16002


node173

172.20.20.173

16001,16002


软件版本

操作系统:CentOS X64 7.3

Redis版本:5.0.5

公共基础配置

3台均操作,这里无特别说明,均是root操作

1. 关闭防火墙

systemctl stop firewalld.service

setenforce 0

sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config

2. 配置主机名

cat >> /etc/hosts << EOF

172.20.20.171 node171

172.20.20.172 node172

172.20.20.173 node173

EOF

3. 新建目录

mkdir -p /opt/redis;mkdir -p /opt/redis/{bin,conf,logs}

mkdir -p /data/redis/data

安装Redis

3台均操作

PS:若是购买云ECS进行自建,可以做成一台,打包成基础或可以使用SaltAnsible进行批量部署

yum install -y gcc gcc-c++

wget http://download.redis.io/releases/redis-5.0.5.tar.gz

cd /root

tar xf redis-5.0.5.tar.gz

cd redis-5.0.5

make MALLOC=libc

make PREFIX=/opt/redis/ install

cp /root/redis-5.0.5/redis.conf /opt/redis/conf/redis-16001.conf

cd /opt/redis/conf/

sed -i 's#bind 127.0.0.1#bind 0.0.0.0#g' redis-16001.conf

sed -i "s#port 6379#port 16001#g" redis-16001.conf

sed -i "s#daemonize no#daemonize yes#g" redis-16001.conf

sed -i 's#pidfile /var/run/redis_6379.pid#pidfile /opt/redis/logs/redis_16001.pid#g' redis-16001.conf

sed -i 's#logfile ""#logfile "/opt/redis/logs/redis_16001.log"#g' redis-16001.conf

sed -i 's#dbfilename dump.rdb#dbfilename redis_16001.rdb#g' redis-16001.conf

sed -i 's#dir ./#dir /data/redis/data/#g' redis-16001.conf

sed -i "s#appendonly no#appendonly yes#g" redis-16001.conf

sed -i 's#appendfilename "appendonly.aof"#appendfilename "redis_16001.aof"#g' redis-16001.conf

sed -i 's#\# cluster-enabled yes# cluster-enabled yes#g' redis-16001.conf

sed -i 's#\# cluster-config-file nodes-6379.conf#cluster-config-file /opt/redis/conf/nodes-16001.conf#g' redis-16001.conf

sed -i 's#\# masterauth #masterauth zjkj#g' /opt/redis/conf/redis-16001.conf


cp redis-16001.conf redis-16002.conf

sed -i 's#16001#16002#g' redis-16002.conf

启动Redis

3台均操作

/opt/redis/bin/redis-server /opt/redis/conf/redis-16001.conf

/opt/redis/bin/redis-server /opt/redis/conf/redis-16002.conf

启动Redis集群

在三台任意一台上执行

/opt/redis/bin/redis-cli --cluster create 172.20.20.171:16001 172.20.20.171:16002 172.20.20.172:16001 172.20.20.172:16002 172.20.20.173:16001 172.20.20.173:16002 --cluster-replicas 1

>>> Performing hash slots allocation on 6 nodes...

Master[0] -> Slots 0 - 5460

Master[1] -> Slots 5461 - 10922

Master[2] -> Slots 10923 - 16383

Adding replica 172.20.20.172:16002 to 172.20.20.171:16001

Adding replica 172.20.20.173:16002 to 172.20.20.172:16001

Adding replica 172.20.20.171:16002 to 172.20.20.173:16001

M: a9ab7a12884d505efcf066fcc3aae74c2b3f101d 172.20.20.171:16001

slots:[0-5460] (5461 slots) master

S: 81d1b25ae1ea85421bd4abb2be094c258026c505 172.20.20.171:16002

replicates 04d9c29ef2569b1fc8abd9594d64fca33e4ad4f2

M: 761348a0107f5b009cabc22c214e39578d0aa707 172.20.20.172:16001

slots:[5461-10922] (5462 slots) master

S: 6dec89e63a48a9a9f393011a698a0bda21b70f1e 172.20.20.172:16002

replicates a9ab7a12884d505efcf066fcc3aae74c2b3f101d

M: 04d9c29ef2569b1fc8abd9594d64fca33e4ad4f2 172.20.20.173:16001

slots:[10923-16383] (5461 slots) master

S: 14e79155f78065e4518e00cd5bd057336b17e3a7 172.20.20.173:16002

replicates 761348a0107f5b009cabc22c214e39578d0aa707

Can I set the above configuration? (type 'yes' to accept): yes #输入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 172.20.20.171:16001)

M: a9ab7a12884d505efcf066fcc3aae74c2b3f101d 172.20.20.171:16001

slots:[0-5460] (5461 slots) master

1 additional replica(s)

S: 81d1b25ae1ea85421bd4abb2be094c258026c505 172.20.20.171:16002

slots: (0 slots) slave

replicates 04d9c29ef2569b1fc8abd9594d64fca33e4ad4f2

M: 761348a0107f5b009cabc22c214e39578d0aa707 172.20.20.172:16001

slots:[5461-10922] (5462 slots) master

1 additional replica(s)

S: 14e79155f78065e4518e00cd5bd057336b17e3a7 172.20.20.173:16002

slots: (0 slots) slave

replicates 761348a0107f5b009cabc22c214e39578d0aa707

S: 6dec89e63a48a9a9f393011a698a0bda21b70f1e 172.20.20.172:16002

slots: (0 slots) slave

replicates a9ab7a12884d505efcf066fcc3aae74c2b3f101d

M: 04d9c29ef2569b1fc8abd9594d64fca33e4ad4f2 172.20.20.173:16001

slots:[10923-16383] (5461 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@node172 conf]# /opt/redis/bin/redis-cli -h 172.20.20.172 -p 16001

172.20.20.172:16001> cluster nodes

04d9c29ef2569b1fc8abd9594d64fca33e4ad4f2 172.20.20.173:16001@26001 master - 0 1563967987854 5 connected 10923-16383

a9ab7a12884d505efcf066fcc3aae74c2b3f101d 172.20.20.171:16001@26001 master - 0 1563967985839 1 connected 0-5460

81d1b25ae1ea85421bd4abb2be094c258026c505 172.20.20.171:16002@26002 slave 04d9c29ef2569b1fc8abd9594d64fca33e4ad4f2 0 1563967983822 5 connected

6dec89e63a48a9a9f393011a698a0bda21b70f1e 172.20.20.172:16002@26002 slave a9ab7a12884d505efcf066fcc3aae74c2b3f101d 0 1563967988858 4 connected

14e79155f78065e4518e00cd5bd057336b17e3a7 172.20.20.173:16002@26002 slave 761348a0107f5b009cabc22c214e39578d0aa707 0 1563967986847 6 connected

761348a0107f5b009cabc22c214e39578d0aa707 172.20.20.172:16001@26001 myself,master - 0 1563967986000 3 connected 5461-10922

172.20.20.172:16001> set name "Mr.Zhang"

OK

172.20.20.172:16001> get name

"Mr.Zhang"

Redis集群启动脚本

3台均配置

cat >> /opt/redis/bin/start-all.sh << EOF

/opt/redis/bin/redis-server /opt/redis/conf/redis-16001.conf

/opt/redis/bin/redis-server /opt/redis/conf/redis-16002.conf

EOF

chmod +x /opt/redis/bin/start-all.sh

Redis集群停止脚本

任意一台即可

cat /opt/redis/bin/stop-all.sh

#!/bin/bash

# Settings

PORT=16000

TIMEOUT=2000

NODES=2

REPLICAS=1

HOSTS=(172.20.20.171 172.20.20.172 172.20.20.173)

# You may want to put the above config parameters into config.sh in order to

# override the defaults without modifying this script.

if [ -a config.sh ]

then

source "config.sh"

fi

# Computed vars

ENDPORT=$((PORT+NODES))

if [ "$1" == "stop" ]

then

while [ $((PORT < ENDPORT)) != "0" ]; do

PORT=$((PORT+1))

for host in ${HOSTS[@]}

do

/opt/redis/bin/redis-cli -h $host -p $PORT shutdown nosave

done

done

exit 0

fi

echo "Usage: $0 [stop]"

echo "stop -- Stop Redis Cluster instances."


0