redis分布式存储

redis分布式存储


一、架构图
1687823152672
二、安装redis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/bin/bash
#
#************************************
#Author: 白展躺
#Date: 2023-06-24
#FileName: installredis.sh
#Description: 说明
#Copyright: 2023
#************************************
DIR=/apps/redis
REDIS_VERSION=7.0.11
PORT=$1
function install(){
tar xf /usr/local/src/redis-${REDIS_VERSION}.tar.gz -C /usr/src/
cd /usr/src/redis-${REDIS_VERSION}/
make PREFIX=$DIR install
useradd -M -s /bin/nologin redis
mkdir $DIR/{etc,log,data} -p
chown redis:redis ${DIR} -R
mkdir /data/redis
chown redis:redis /data/redis -R
cat > /etc/profile.d/redis.sh <<EOF
PATH=/apps/redis/bin:$PATH
EOF
source /etc/profile.d/redis.sh
}


install

安装redis实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/bash
#
#************************************
#Author: 白展躺
#Date: 2023-06-24
#FileName: installredis.sh
#Description: 说明
#Copyright: 2023
#************************************
DIR=/apps/redis
REDIS_VERSION=7.0.11
PORT=$1
IP=$2
function install(){
tar xf /usr/local/src/redis-${REDIS_VERSION}.tar.gz -C /usr/src/
cd /usr/src/${REDIS_VERSION}/
make PREFIX=$DIR install
useradd -M -s /bin/nologin redis
mkdir $DIR/{etc,log,data} -p
chown redis:redis ${DIR} -R


}
function installdb(){
if [ -z $PORT ];then
echo '端口不能为空'
exit 1
fi
cp /usr/src/redis-${REDIS_VERSION}/redis.conf ${DIR}/etc/redis_${PORT}.conf
cp /usr/src/redis-${REDIS_VERSION}/sentinel.conf ${DIR}/etc/sentinel_${PORT}.conf
sed -Ei.bak 's/^(bind).*/\1 * -::1/g' ${DIR}/etc/redis_${PORT}.conf
sed -Ei.bak 's/^(protected-mode).*/\1 no/g' ${DIR}/etc/redis_${PORT}.conf
sed -Ei.bak "s@^(port).*@\1 ${PORT}@g" ${DIR}/etc/redis_${PORT}.conf
sed -Ei.bak "s@^(pidfile).*@\1 ${DIR}/redis_${PORT}.pid@g" ${DIR}/etc/redis_${PORT}.conf
sed -Ei.bak "s@^(logfile).*@\1 ${DIR}/log/message${PORT}.log@g" ${DIR}/etc/redis_${PORT}.conf
#sed -Ei.bak "s@^(dir).*@\1 /data/redis${PORT}@g" ${DIR}/etc/redis_${PORT}.conf
sed -Ei.bak "s/^(dbfilename).*/\1 dump${PORT}.rdb/g" ${DIR}/etc/redis_${PORT}.conf
sed -Ei.bak "s/^(appendonly).*/\1 yes/g" ${DIR}/etc/redis_${PORT}.conf
sed -Ei.bak "s/^(appendfilename).*/\1 \"appendonly${PORT}.aof\"/g" ${DIR}/etc/redis_${PORT}.conf
sed -Ei.bak "s@^(dir).*@\1 /data/redis@g" ${DIR}/etc/redis_${PORT}.conf
sed -Ei.bak "/^protected-mode no/a\bind 0.0.0.0" ${DIR}/etc/sentinel_${PORT}.conf
sed -Ei.bak "s@^(port).*@\1 2${PORT}@g" ${DIR}/etc/sentinel_${PORT}.conf
sed -Ei.bak "s@^(daemonize).*@\1 yes@g" ${DIR}/etc/sentinel_${PORT}.conf
sed -Ei.bak "s@^(pidfile).*@\1 /apps/redis/redis-sentinel_${PORT}.pid@g" ${DIR}/etc/sentinel_${PORT}.conf
sed -Ei.bak "s@^(logfile).*@\1 /apps/redis/log/sentinel_${PORT}.log@g" ${DIR}/etc/sentinel_${PORT}.conf
sed -Ei.bak "s@^(sentinel monitor).*@\1 rediscluster ${IP} ${PORT} 2@g" ${DIR}/etc/sentinel_${PORT}.conf
sed -Ei.bak "s@^(sentinel down-after-milliseconds).*@\1 rediscluster 3000@g" ${DIR}/etc/sentinel_${PORT}.conf
sed -Ei.bak "s@^(sentinel parallel-syncs).*@\1 rediscluster 1@g" ${DIR}/etc/sentinel_${PORT}.conf
sed -Ei.bak "s@^(sentinel failover-timeout).*@\1 rediscluster 180000@g" ${DIR}/etc/sentinel_${PORT}.conf
sed -Ei.bak "s@^(SENTINEL master-reboot-down-after-period).*@\1 rediscluster 0@g" ${DIR}/etc/sentinel_${PORT}.conf
#mkdir /data/redis${PORT} -p
#chown redis:redis /data/redis${PORT}
cat > /etc/systemd/system/redis${PORT}.service <<EOF
[Unit]
Description=Redis In-Memory Data Store
After=network.target

[Service]
User=redis
Group=redis
ExecStart=${DIR}/bin/redis-server $DIR/etc/redis_${PORT}.conf --supervised systemd
ExecStop=${DIR}/bin/redis-cli shutdown
Restart=always

[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start redis${PORT}
}

installdb

启用分布式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/bash
#
#************************************
#Author: 白展躺
#Date: 2023-06-27
#FileName: enablerediscluster.sh
#Description: 说明
#Copyright: 2023
#************************************
PORT=$1
function enablecluster(){
if [ -z $PORT ];then
echo '端口不能为空'
exit 1
fi
sed -Ei.bak "s@^# (cluster-enabled).*@\1 yes@g" /apps/redis/etc/redis_${PORT}.conf
sed -Ei.bak "s@^# (cluster-config-file).*@\1 nodes-${PORT}.conf@g" /apps/redis/etc/redis_${PORT}.conf
sed -Ei.bak "s@^# (cluster-require-full-coverage).*@\1 no@g" /apps/redis/etc/redis_${PORT}.conf
}
enablecluster

重启服务查看cluster端口
启动服务

1
2
redis-cli --cluster create 10.1.0.18:6379  10.1.0.19:6380 10.1.0.18:6381 10.1.0.19:6379 10.1.0.18:6380 10.1.0.19:6381 --cluster-replicas 1
#如果启动失败,槽点分布不均匀,则停止所有节点,删除 nodes-6379.conf 信息重新初始化

查看集群状态

1
2
redis-cli --cluster info  10.1.0.18:6379 
redis-cli --cluster check 10.1.0.18:6379

测试

1
2
3
4
5
6
7
8
9
#插入数据
redis-cli -h 10.1.0.18 -c set test value
#查看key的槽点
redis-cli -c -h 10.1.0.18 cluster keyslot test
#查看集群槽点分布
redis-cli -p 6379 cluster slots
cat /data/redis/nodes-6379.conf
#登录槽点所在范围查看
redis-cli -h 10.1.0.19 -p 6380 get test