Redis6集群安装及其扩容缩容

虚拟机拷贝2-3台centos机器,作为Redis6集群的主备节点

部署规划:
master
192.168.28.132 8001
192.168.28.132 8002
192.168.28.132 8003
slaver
192.168.28.135 8001
192.168.28.135 8002
192.168.28.135 8003

一、安装依赖环境
1、安装编译依赖
yum install gcc tcl tcl-devel  
此处使用yum,也可以下载RPM包后安装
2、安装Ruby和Ruby的redis gem包
说明:
Ruby 是一门语言,可以用来编写脚本,运行于 Ruby 环境。
RubyGems 是管理 Ruby 脚本的工具,可以通过连接 ruby 脚本源,在线下载安装 ruby 脚本。类似于前端的 node、Java 的 maven,Linux的rpm/yum.
redis-xxx.gem 是redis 对接 ruby 的脚本。
redis-trib.rb 是基于 redis 提供的集群命令,封装成的操作工具。

[root@centos7-135 ~]# yum install ruby -y
...
[root@centos7-135 redis-gem]# ll
总用量 52
-rw-r--r--. 1 root root 49664 6月   4 2023 redis-5.0.5.gem
[root@centos7-135 redis-gem]# gem install -l redis-5.0.5.gem 
ERROR:  While executing gem ... (Gem::DependencyError)
    Unable to resolve dependencies: redis requires redis-client (>= 0.9.0)
[root@centos7-135 redis-gem]# gem --version
2.0.14.1
[root@centos7-135 redis-gem]# gem update --system
Updating rubygems-update
Fetching: rubygems-update-3.4.13.gem (100%)
ERROR:  Error installing rubygems-update:
        rubygems-update requires Ruby version >= 2.6.0.
ERROR:  While executing gem ... (NoMethodError)
    undefined method `version' for nil:NilClass
#移除包,使用源码方式
[root@centos7-135 redis-gem]# yum erase ruby

Ruby下载地址
此处下载Ruby 2.7.8
sha256: c2dab63cbc8f2a05526108ad419efa63a67ed4074dbbcf9fc2b1ca664cb45ba0
[root@centos7-135 ruby]# sha256sum ruby-3.1.4.tar.gz
a3d55879a0dfab1d7141fdf10d22a07dbf8e5cdc4415da1bde06127d5cc3c7b6  ruby-3.1.4.tar.gz
通过源码编译安装
$ ./configure
$ make
$ sudo make install
默认情况下,Ruby 安装到 /usr/local 目录。如果想使用其他目录,可以把 --prefix=DIR 选项传给 ./configure 脚本。
因为无法使用任何工具来管理通过源码编译安装的 Ruby,所以使用第三方工具或者包管理器或许是更好的选择。

[root@centos7-135 ruby]# sha256sum ruby-2.7.8.tar.gz
c2dab63cbc8f2a05526108ad419efa63a67ed4074dbbcf9fc2b1ca664cb45ba0  ruby-2.7.8.tar.gz
[root@centos7-135 ruby]# tar zxf ruby-2.7.8.tar.gz 
[root@centos7-135 ruby]# cd ruby-2.7.8
[root@centos7-135 ruby]# make
[root@centos7-135 ruby]# make install
#验证
[root@centos7-135 bin]# ./ruby -v
ruby 2.7.8p225 (2023-03-30 revision 1f4d455848) [x86_64-linux]
[root@centos7-135 bin]# vim /etc/profile
#在文件最后 PATH上添加ruby安装路径。没有PATH,则这样写。
export PATH=$PATH:/app/ruby/bin
[root@centos7-135 bin]# . /etc/profile
[root@centos7-135 ~]# ruby -v
ruby 2.7.8p225 (2023-03-30 revision 1f4d455848) [x86_64-linux]
[root@centos7-135 ~]# 

3、安装 RubyGems

RubyGems下载地址

[root@centos7-135 rubygems]# tar zxf rubygems-3.4.13.tgz 
[root@centos7-135 rubygems]# cd rubygems-3.4.13
[root@centos7-135 rubygems-3.4.13]# ruby setup.rb
  Successfully built RubyGem
  Name: bundler
  Version: 2.4.13
  File: bundler-2.4.13.gem
Bundler 2.4.13 installed
RubyGems 3.4.13 installed
Regenerating binstubs
Regenerating plugins
Parsing documentation for rubygems-3.4.13
....
[root@centos7-135 bin]# which gem
/app/ruby/bin/gem
[root@centos7-135 bin]# 
[root@centos7-135 ~]# gem -v
3.4.13
[root@centos7-135 ~]# 

4、安装 redis-3.3.5.gem

[root@centos7-135 redis-gem]# gem install -l redis-3.3.5.gem 
Successfully installed redis-3.3.5
Parsing documentation for redis-3.3.5
Installing ri documentation for redis-3.3.5
Done installing documentation for redis after 0 seconds
1 gem installed
[root@centos7-135 redis-gem]# ll
总用量 144
-rw-r--r--. 1 root root 92672 6月   4 2023 redis-3.3.5.gem
-rw-r--r--. 1 root root 49664 6月   4 2023 redis-5.0.5.gem
[root@centos7-135 redis-gem]# 

二、编译安装Redis6

Redis下载地址
此处下载 redis-6.2.12.tar.gz

[root@centos7-135 app]# tar zxf redis-6.2.12.tar.gz
[root@centos7-135 app]# cd redis-6.2.12
# make distclean 如果之前有make失败,需清除缓存再执行make命令
[root@centos7-135 redis-6.2.12]# make -j8
    LINK redis-benchmark
    LINK redis-server
    INSTALL redis-sentinel
    INSTALL redis-check-rdb
    INSTALL redis-check-aof
    LINK redis-cli
Hint: It's a good idea to run 'make test' ;)
make[1]: 离开目录“/app/redis-6.2.12/src”
[root@centos7-135 redis-6.2.12]# make PREFIX=/app/redis install
cd src && make install
make[1]: 进入目录“/app/redis-6.2.12/src”
    CC Makefile.dep
make[1]: 离开目录“/app/redis-6.2.12/src”
make[1]: 进入目录“/app/redis-6.2.12/src”
Hint: It's a good idea to run 'make test' ;)
    INSTALL redis-server
    INSTALL redis-benchmark
    INSTALL redis-cli
make[1]: 离开目录“/app/redis-6.2.12/src”
[root@centos7-135 redis-6.2.12]# cp src/redis-trib.rb  /app/redis/bin/
[root@centos7-135 redis-6.2.12]# make test
[root@centos7-135 bin]# vim /etc/profile
# 把/app/redis/bin加入到PATH
# vi $HOME/.profile
export PATH=$PATH:/app/redis/bin
[root@centos7-135 bin]# . /etc/profile
[root@centos7-135 bin]# redis-cli -v
redis-cli 6.2.12
[root@centos7-135 bin]# redis-server -v
Redis server v=6.2.12 sha=00000000:0 malloc=jemalloc-5.1.0 bits=64 build=2dbe6f900c3eed0e

三、安装Redis6集群

基本操作均整合成为脚本,方便操作。

1、创建Redis节点
# 创建节点目录并写入redis.conf

[root@centos7-135 bin]# cat >genredis.sh

#!/bin/bash
cd /app/redis/
seq 8001 8003 | while read rport
do
mkdir $rport
cat > $rport/redis.conf<<EOF
bind 0.0.0.0
port 8000
protected-mode no
tcp-backlog 32768
timeout 300
tcp-keepalive 60
daemonize yes
supervised no
pidfile "/app/redis/8000/redis.pid"
loglevel notice
logfile "/app/redis/8000/redis.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error no
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir /app/redis/8000
masterauth "123456"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-ping-slave-period 10
repl-timeout 60
repl-disable-tcp-nodelay no
repl-backlog-size 100mb
repl-backlog-ttl 3600
slave-priority 100
#requirepass "123456"
maxclients 10000
maxmemory-policy allkeys-lfu
maxmemory-samples 5
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
cluster-enabled yes
cluster-config-file "/app/redis/8000/nodes.conf"
cluster-node-timeout 15000
cluster-slave-validity-factor 100
cluster-migration-barrier 1
cluster-require-full-coverage no
cluster-slave-no-failover no
slowlog-log-slower-than 10000
slowlog-max-len 1280
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 0 0 0
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
EOF
sed -i "s/8000/$rport/g" $rport/redis.conf
done

执行脚本安装Redis主节点

[root@centos7-135 bin]# chmod +x genredis.sh
[root@centos7-135 bin]# sh genredis.sh
[root@centos7-135 bin]# cd ..
[root@centos7-135 redis]# ll
总用量 0
drwxr-xr-x. 2 root root  24 6月   3 21:53 8001
drwxr-xr-x. 2 root root  24 6月   3 21:53 8002
drwxr-xr-x. 2 root root  24 6月   3 21:53 8003
drwxr-xr-x. 2 root root 174 6月   3 21:47 bin

2、创建Redis启动脚本

[root@centos7-135 redis]# cat >start-redis.sh

#!/bin/bash
. /etc/profile
#. $HOME/.profile
REDIS_HOME=/app/redis
REDIS_SERVER="$REDIS_HOME/bin/redis-server"
case "$1" in
8001)
$REDIS_SERVER $REDIS_HOME/8001/redis.conf
echo "Redis Server 8001 Started"
;;
8002)
$REDIS_SERVER $REDIS_HOME/8002/redis.conf
echo "Redis Server 8002 Started"
;;
8003)
$REDIS_SERVER $REDIS_HOME/8003/redis.conf
echo "Redis Server 8003 Started"
;;
*)
echo "Usage: $0 {8001|8002|8003}"
exit 1
esac
exit 0

启动Redis主节点

[root@centos7-135 redis]# chmod +x start-redis.sh 
[root@centos7-135 redis]# /app/redis/start-redis.sh 8001
Redis Server 8001 Started
[root@centos7-135 redis]# /app/redis/start-redis.sh 8002
Redis Server 8002 Started
[root@centos7-135 redis]# /app/redis/start-redis.sh 8003
Redis Server 8003 Started
[root@centos7-135 redis]# ps -ef|grep redis |grep -v grep 
root      22097      1  0 22:05 ?        00:00:00 /app/redis/bin/redis-server 0.0.0.0:8001 [cluster]
root      22120      1  0 22:05 ?        00:00:00 /app/redis/bin/redis-server 0.0.0.0:8002 [cluster]
root      22142      1  0 22:05 ?        00:00:00 /app/redis/bin/redis-server 0.0.0.0:8003 [cluster]
[root@centos7-135 redis]# 

3、安装Redis备节点

[root@centos7-135 app]# scp -r redis root@192.168.28.132:/app/
[root@www redis]# ll
总用量 4
drwxr-xr-x 2 root root  76 6月   3 22:19 8001
drwxr-xr-x 2 root root  76 6月   3 22:19 8002
drwxr-xr-x 2 root root  76 6月   3 22:19 8003
drwxr-xr-x 2 root root 174 6月   3 22:19 bin
-rwxr-xr-x 1 root root 454 6月   3 22:19 start-redis.sh
[root@www 8001]# rm  nodes.conf redis.log redis.pid -f
[root@www 8001]# cd ../8002
[root@www 8002]# rm  nodes.conf redis.log redis.pid -f
[root@www 8002]# cd ../8003/
[root@www 8003]# rm  nodes.conf redis.log redis.pid -f
[root@www 8003]# /app/redis/start-redis.sh 8001
Redis Server 8001 Started
[root@www 8003]# /app/redis/start-redis.sh 8002
Redis Server 8002 Started
[root@www 8003]# /app/redis/start-redis.sh 8003
Redis Server 8003 Started
[root@www 8003]# ps -ef|grep redis |grep -v gre
root       3350      1  0 22:31 ?        00:00:00 /app/redis/bin/redis-server 0.0.0.0:8001 [cluster]
root       3372      1  0 22:31 ?        00:00:00 /app/redis/bin/redis-server 0.0.0.0:8002 [cluster]
root       3393      1  0 22:31 ?        00:00:00 /app/redis/bin/redis-server 0.0.0.0:8003 [cluster]

4、创建仅master节点的Redis cluster(root)

说明:如果由Redis自动创建主备节点,则节点可能不能按照期望的均衡分布在不同机器,达到容错的效果。所以此处只创建主节点,后续手工按期望创建备节点。

[root@centos7-135 redis]# redis-trib.rb create --replicas 0 192.168.28.135:8001 192.168.28.135:8002 192.168.28.135:8003
WARNING: redis-trib.rb is not longer available!
You should use redis-cli instead.

All commands and features belonging to redis-trib.rb have been moved
to redis-cli.
In order to use them you should call redis-cli with the --cluster
option followed by the subcommand name, arguments and options.

Use the following syntax:
redis-cli --cluster SUBCOMMAND [ARGUMENTS] [OPTIONS]

Example:
redis-cli --cluster create 192.168.28.135:8001 192.168.28.135:8002 192.168.28.135:8003 --cluster-replicas 0

To get help about all subcommands, type:
redis-cli --cluster help

#查证发现redis5后已经不在依赖Ruby安装集群。前文安装的Ruby在Redis6属于无用功。但是Redis5之前仍可使用,故本文没有去除这部分。

[root@centos7-135 redis]# redis-cli --cluster create 192.168.28.135:8001 192.168.28.135:8002 192.168.28.135:8003 --cluster-replicas 0
>>> Performing hash slots allocation on 3 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
M: a32d7ada6b880161c04cd43da62b409675b934ee 192.168.28.135:8001
   slots:[0-5460] (5461 slots) master
M: 3ad04092aa1f5080baa6b3777d580e710cf998f8 192.168.28.135:8002
   slots:[5461-10922] (5462 slots) master
M:
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值