Redis下载
-
官网地址:http://redis.io/
-
中文官网地址:http://www.redis.cn/
-
下载地址:http://download.redis.io/releases/
安装
# (三台)安装 C 语言需要的 GCC 环境
yum install -y gcc-c++
yum install -y wget
# 下载并解压缩 Redis 源码压缩包
wget http://download.redis.io/releases/redis-5.0.5.tar.gz
tar -zxf redis-5.0.5.tar.gz -C ../servers/
# 编译 Redis 源码,进入 redis-5.0.5 目录,执行编译命令
cd redis-5.0.5/src
make
# 安装 Redis ,需要通过 PREFIX 指定安装路径
mkdir /opt/bigdata/servers/redis-cluster/master1 -p
make install PREFIX=/opt/bigdata/servers/redis-cluster/master1
注error
:
# make 时 出现:
# zmalloc.h:50:31: fatal error: jemalloc/jemalloc.h: No such file or directory
make MALLOC=libc
# You need tcl 8.5 or newer in order to run the Redis test
yum install tcl -y
需求
(1)搭建Redis5.0集群,要求三主三从,记录下安装步骤
(2)能够添加一主一从(Master4和Slaver4),记录下安装步骤
(3)能够通过JedisCluster向RedisCluster添加数据和取出数据
解答
集群规划
虚拟机ip | 持有角色 | 端口 |
---|---|---|
linux01:192.168.80.121 | master1 slaver1 | 6379 6380 |
linux02:192.168.80.122 | master2 slaver2 | 6379 6380 |
linux03:192.168.80.123 | master3 slaver3 | 6379 6380 |
linux01:192.168.80.121(准备添加主从) | master4 slaver4 | 7000 7001 |
编辑配置文件
# 1.拷贝conf文件到bin下
[root@linux02 redis-5.0.5]# cp redis.conf ../redis/bin/
# 2.编辑conf文件69 # bind 127.0.0.1 # 注掉bind 允许外界访问
88 protected-mode no # 将yes改成no,允许外界访问
92 port 6379 # 端口号
136 daemonize yes # 将no改成yes,允许后台启动
832 cluster-enabled yes # 开启cluster
分配启动目录
# 拷贝出从机
[root@linux01 redis-cluster]$ cp -r master1/ slaver1
# 修改 端口号
[root@linux01 ~]$ sed -i '92c port 6380' /opt/bigdata/servers/redis-cluster/slaver1/bin/redis.conf
# 查看 修改结果
[root@linux01 ~]$ sed -n '92p' /opt/bigdata/servers/redis-cluster/slaver1/bin/redis.conf
# 发送给 其他两台
[root@linux01 servers]$ rsync -rvl /opt/bigdata/servers/redis-cluster root@linux02:/opt/bigdata/servers/
[root@linux01 servers]$ rsync -rvl /opt/bigdata/servers/redis-cluster root@linux03:/opt/bigdata/servers/
# 修改名称
[root@linux01 servers]$ ssh root@linux02 mv /opt/bigdata/servers/redis-cluster/master1/ /opt/bigdata/servers/redis-cluster/master2/
[root@linux01 servers]$ ssh root@linux02 mv /opt/bigdata/servers/redis-cluster/slaver1/ /opt/bigdata/servers/redis-cluster/slaver2
[root@linux01 servers]$ ssh root@linux03 mv /opt/bigdata/servers/redis-cluster/master1/ /opt/bigdata/servers/redis-cluster/master3/
[root@linux01 servers]$ ssh root@linux03 mv /opt/bigdata/servers/redis-cluster/slaver1/ /opt/bigdata/servers/redis-cluster/slaver3/
# 准备 master4 slaver4
[root@linux01 servers]$ cd /opt/bigdata/servers/redis-cluster/
[root@linux01 redis-cluster]$ cp -r master1/ master4
[root@linux01 redis-cluster]$ cp -r master1/ slaver4
[root@linux01 redis-cluster]$ sed -i '92c port 7000' /opt/bigdata/servers/redis-cluster/master4/bin/redis.conf
[root@linux01 redis-cluster]$ sed -i '92c port 7001' /opt/bigdata/servers/redis-cluster/slaver4/bin/redis.conf
编辑起服务脚本
start-rediscluster.sh
#!/bin/bash
pdir='/opt/bigdata/servers/redis-cluster'
user=`whoami`echo $pdir
for((i=1; i<4; i++)); doecho ---------------- linux0$i starting -------------------ssh $user@linux0$i 'cd '$pdir'/master'$i'/bin; ./redis-server ./redis.conf'ssh $user@linux0$i 'cd '$pdir'/slaver'$i'/bin; ./redis-server ./redis.conf'echo ---------------- linux0$i end -------------------
done
启动集群
# 启动集群chmod 777 start-rediscluster.sh
./ start-rediscluster.sh[root@linux01 redis-cluster]# ./start-rediscluster.sh
/opt/bigdata/servers/redis-cluster
---------------- linux01 starting -------------------
8067:C 22 Dec 2020 13:12:34.458 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8067:C 22 Dec 2020 13:12:34.458 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=8067, just started
8067:C 22 Dec 2020 13:12:34.458 # Configuration loaded
8080:C 22 Dec 2020 13:12:34.648 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8080:C 22 Dec 2020 13:12:34.648 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=8080, just started
8080:C 22 Dec 2020 13:12:34.648 # Configuration loaded
---------------- linux01 end -------------------
---------------- linux02 starting -------------------
7909:C 22 Dec 2020 13:12:34.837 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7909:C 22 Dec 2020 13:12:34.837 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=7909, just started
7909:C 22 Dec 2020 13:12:34.837 # Configuration loaded
7921:C 22 Dec 2020 13:12:35.030 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7921:C 22 Dec 2020 13:12:35.030 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=7921, just started
7921:C 22 Dec 2020 13:12:35.030 # Configuration loaded
---------------- linux02 end -------------------
---------------- linux03 starting -------------------
7902:C 22 Dec 2020 13:12:35.238 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7902:C 22 Dec 2020 13:12:35.238 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=7902, just started
7902:C 22 Dec 2020 13:12:35.238 # Configuration loaded
7914:C 22 Dec 2020 13:12:35.415 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7914:C 22 Dec 2020 13:12:35.415 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=7914, just started
7914:C 22 Dec 2020 13:12:35.415 # Configuration loaded
---------------- linux03 end -------------------
# 添加 主从关系
[root@linux01 redis-cluster]# ./master1/bin/redis-cli --cluster create 192.168.80.128:6379 192.168.80.129:6379 192.168.80.130:6379 192.168.80.128:6380 192.168.80.129:6380 192.168.80.130:6380 --cluster-replicas 1
# 命令客户端连接集群
[root@linux01 redis-cluster]# ./master1/bin/redis-cli -p 6379 -c# 查看节点状态
127.0.0.1:6379> cluster nodes
b5a44319b12c3eaef610a28ca87033bb3445de94 192.168.80.129:6379@16379 master - 0 1608615527906 2 connected 5461-10922
5b5ac4ecd698bf1c02d8e931d267ec4bf9480564 192.168.80.128:6380@16380 slave cb05fcf924e135627ef2ea065911e3429cdbba0a 0 1608615528000 4 connected
923406a5110e9effe574e225785c973bab37df13 192.168.80.128:6379@16379 myself,master - 0 1608615527000 1 connected 0-5460
6e1d97680c0e8b6cc25b70c838a5a31c5d30f61a 192.168.80.130:6380@16380 slave b5a44319b12c3eaef610a28ca87033bb3445de94 0 1608615526895 6 connected
76dda73e1889613a17250aefaa10c2d60c3689f5 192.168.80.129:6380@16380 slave 923406a5110e9effe574e225785c973bab37df13 0 1608615528917 5 connected
cb05fcf924e135627ef2ea065911e3429cdbba0a 192.168.80.130:6379@16379 master - 0 1608615525000 3 connected 10923-16383
注error: 不能用ip 代理名
扩容
# 启动 master4 slaver4
[root@linux01 master4]# ./bin/redis-server ./bin/redis.conf
8174:C 22 Dec 2020 13:44:30.996 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8174:C 22 Dec 2020 13:44:30.996 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=8174, just started
8174:C 22 Dec 2020 13:44:30.996 # Configuration loaded[root@linux01 slaver4]# ./bin/redis-server ./bin/redis.conf
8179:C 22 Dec 2020 13:44:54.980 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8179:C 22 Dec 2020 13:44:54.980 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=8179, just started
8179:C 22 Dec 2020 13:44:54.980 # Configuration loaded# 连接集群
## 添加 master4
[root@linux01 redis-cluster]# ./master4/bin/redis-cli --cluster add-node 192.168.80.128:7000 192.168.80.128:6379
## 添加 slaver4
[root@linux01 slaver4]# ./bin/redis-cli --cluster add-node --cluster-slave --cluster-master-id 951955fc1a9df7d4b30cdaec5ba2624ed7d2786e 192.168.80.128:7001 192.168.80.128:7000
# 查看节点
127.0.0.1:6379> cluster nodes
951955fc1a9df7d4b30cdaec5ba2624ed7d2786e 192.168.80.128:7000@17000 master - 0 1608616973959 7 connected
5b5ac4ecd698bf1c02d8e931d267ec4bf9480564 192.168.80.128:6380@16380 slave cb05fcf924e135627ef2ea065911e3429cdbba0a 0 1608616972949 4 connected
cb05fcf924e135627ef2ea065911e3429cdbba0a 192.168.80.130:6379@16379 master - 0 1608616969930 3 connected 10923-16383
d055900e46fa866bf3b69c14d4d96f40ccb74ab8 192.168.80.128:7001@17001 slave 951955fc1a9df7d4b30cdaec5ba2624ed7d2786e 0 1608616971940 7 connected
b5a44319b12c3eaef610a28ca87033bb3445de94 192.168.80.129:6379@16379 myself,master - 0 1608616967000 2 connected 5461-10922
923406a5110e9effe574e225785c973bab37df13 192.168.80.128:6379@16379 master - 0 1608616970000 1 connected 0-5460
6e1d97680c0e8b6cc25b70c838a5a31c5d30f61a 192.168.80.130:6380@16380 slave b5a44319b12c3eaef610a28ca87033bb3445de94 0 1608616971000 6 connected
76dda73e1889613a17250aefaa10c2d60c3689f5 192.168.80.129:6380@16380 slave 923406a5110e9effe574e225785c973bab37df13 0 1608616972000 5 connected
---------------------
作者:Wang·Br
来源:CSDN
原文:https://blog.csdn.net/weixin_44847293/article/details/111537001
版权声明:本文为作者原创文章,转载请附上博文链接!
内容解析By:CSDN,CNBLOG博客文章一键转载插件