文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Redis5.x 集群部署实战

2024-04-02 19:55

关注

实验环境

主机名

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进行自建,可以做成一台,打包成基础或可以使用Salt或Ansible进行批量部署

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 <master-password>#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."

 


阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-数据库
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯