最近部署项目需要使用到redis集群,做个笔记。步骤比较详细,篇幅较长,可以先看看大纲或者目录,有一个大概框架后再尝试部署。
部署主要步骤:
1 部署单机版redis
2 将已部署好的单机版redis复制多个副本(6个)
3 创建集群
4集群验证
集群规划(测试环境ip192.168.19.128,一台部署6个节点,正式环境和测试环境一样的部署方式,无非把节点分配到其他服务器)
节点 | 部署路径 | 端口 |
节点一 | /usr/local/redis-cluster/6371 | 6071 |
节点二 | /usr/local/redis-cluster/6372 | 6072 |
节点三 | /usr/local/redis-cluster/6373 | 6073 |
节点四 | /usr/local/redis-cluster/6374 | 6074 |
节点五 | /usr/local/redis-cluster/6375 | 6075 |
节点六 | /usr/local/redis-cluster/6376 | 6076 |
目录
正文
1、创建单机redis
1.1、下载redis
方案一:官网下载: https://redis.io/download
方案二:已上传的csdn资源redis-5.0.13-tcl-8.5.13.tar.gz(包括编译时需要用到的tcl rpm包(适用于centos,已在centos7.6验证通过))
1.2、解压编译
上传文件包到/opt/soft并解压得到redis源码包和所需的tcl rpm包 如下,
[root@centos01 soft]# tar -xvzf redis-5.0.13-tcl-8.5.13.tar.gz
redis--5.0.13.tar.gz
tcl-8.5.13-4.el7.x86_64.rpm
[root@centos01 soft]#
[root@centos01 soft]#
1.2.1、安装所需tcl rpm包
由于后续编译时需要用到tcl包,所以提前检查,没有就需要安装,执行rpm -qa|grep tcl检查是否已安装,入下图第二行所示,若有结果标识已安装,无结果则运行 rpm -ivh tcl-8.5.13-4.el7.x86_64.rpm --nodeps --force 进行安装(--nodeps --force )两个参数标识忽略依赖并强制安装
[root@centos01 soft]# rpm -qa|grep tcl
targetcli-2.1.fb46-1.el7.noarch
tcl-8.5.13-4.el7.x86_64
[root@centos01 soft]# rpm -ivh tcl-8.5.13-4.el7.x86_64.rpm --nodeps --force
准备中... ################################# [100%]
正在升级/安装...
1:tcl-1:8.5.13-4.el7 ################################# [100%]
[root@centos01 soft]#
1.2.2、编译安装redis
继续解压redis源码包得到redis-5.0.13文件夹,并执行mv /opt/soft/redis-5.0.13 /usr/local/ 将文件夹移动到/usr/local目录
[root@centos01 soft]#tar -xvzf redis--5.0.13.tar.gz
[root@centos01 soft]#
drwxrwxr-x 6 root root 334 7月 22 02:07 redis-5.0.13
-rwxrwxrwx 1 root root 1995566 7月 31 18:28 redis--5.0.13.tar.gz
-rw-r--r-- 1 root root 3852010 8月 2 18:03 redis-5.0.13-tcl-8.5.13.tar.gz
-rwxrwxrwx 1 root root 1975980 7月 31 19:30 tcl-8.5.13-4.el7.x86_64.rpm
进入/usr/local/redis-5.0.13目录执行make进行编译(根据服务器性能 一般一到几分钟不等,编译需要gcc环境,如果没有请配置本地或网络yum源进行安装 yum install -y gcc),成功编译后最后输出如下所示
LINK redis-server
INSTALL redis-sentinel
CC redis-cli.o
LINK redis-cli
CC redis-benchmark.o
LINK redis-benchmark
INSTALL redis-check-rdb
INSTALL redis-check-aof
Hint: It's a good idea to run 'make test' ;)
make[1]: 离开目录“/opt/soft/redis-5.0.13/src”
[root@centos01 redis-5.0.13]#
1.2.3、安装测试
按提示执行make test进行安装测试,入下图所示标识成功(根据服务器性能 一般一到几分钟不等,忘截图了 网上找了一张)
1.3、redis验证
进入/usr/local/redis-5.0.13/src/目录 执行 ./redis-server启动redis服务,如下
[root@centos01 ~]# cd /usr/local/redis-5.0.13/src/
[root@centos01 src]# ./redis-server
9987:C 02 Aug 2021 18:53:18.927 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
9987:C 02 Aug 2021 18:53:18.927 # Redis version=5.0.13, bits=64, commit=00000000, modified=0, pid=9987, just started
9987:C 02 Aug 2021 18:53:18.927 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
9987:M 02 Aug 2021 18:53:18.931 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 5.0.13 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 9987
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
9987:M 02 Aug 2021 18:53:18.933 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
9987:M 02 Aug 2021 18:53:18.933 # Server initialized
9987:M 02 Aug 2021 18:53:18.933 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
9987:M 02 Aug 2021 18:53:18.933 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
9987:M 02 Aug 2021 18:53:19.048 * DB loaded from disk: 0.114 seconds
9987:M 02 Aug 2021 18:53:19.048 * Ready to accept connections
进入/usr/local/redis-5.0.13/src/目录执行./redis-cli 如下,至此表示redis服务编译安装成功
./redis-cli -p 6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379>
2、将已部署好的单机版redis复制多个副本(6个)
2.1、创建集群文件夹,并复制已创建的redis 6个副本
2.1.1、创建所需文件夹
执行如下命令创建6个节点所需部署路径文件夹及数据文件夹,及启动文件目录(bin)
[root@centos01 redis-cluster]# mkdir -p /usr/local/redis-cluster/6371/data/
[root@centos01 redis-cluster]# mkdir -p /usr/local/redis-cluster/6372/data/
[root@centos01 redis-cluster]# mkdir -p /usr/local/redis-cluster/6373/data/
[root@centos01 redis-cluster]# mkdir -p /usr/local/redis-cluster/6374/data/
[root@centos01 redis-cluster]# mkdir -p /usr/local/redis-cluster/6375/data/
[root@centos01 redis-cluster]# mkdir -p /usr/local/redis-cluster/6376/data/
[root@centos01 redis-cluster]# mkdir -p /usr/local/redis-cluster/bin/
[root@centos01 redis-cluster]# ll
总用量 24
drwxr-xr-x. 7 root root 4096 8月 2 15:32 6371
drwxr-xr-x. 7 root root 4096 8月 2 15:33 6372
drwxr-xr-x. 7 root root 4096 8月 2 12:55 6373
drwxr-xr-x. 7 root root 4096 8月 2 12:55 6374
drwxr-xr-x. 7 root root 4096 8月 2 12:55 6375
drwxr-xr-x. 7 root root 4096 8月 2 12:55 6376
drwxr-xr-x. 2 root root 133 8月 2 12:47 bin
2.2.2、复制启动文件到集群bin目录
进入源码编译目录/usr/local/redis-5.0.13/src/ 将脚本复制到集群bin目录/usr/local/redis-cluster/bin
cp /usr/local/redis-5.0.13/src/mkreleasehdr.sh redis-benchmark redis-check-aof redis-cli redis-server redis-trib.rb /usr/local/redis-cluster/bin/
也可以直接将整个src目录复制到bin目录 如下
cp /usr/local/redis-5.0.13/src/* /usr/local/redis-cluster/bin/
2.2.3、复制redis副本
执行如下语句分别将已安装成功的redis复制到6个节点文件夹
[root@centos01 redis-cluster]# cp -r /usr/local/redis-5.0.13/* /usr/local/redis-cluster/6371/
[root@centos01 redis-cluster]# cp -r /usr/local/redis-5.0.13/* /usr/local/redis-cluster/6372/
[root@centos01 redis-cluster]# cp -r /usr/local/redis-5.0.13/* /usr/local/redis-cluster/6373/
[root@centos01 redis-cluster]# cp -r /usr/local/redis-5.0.13/* /usr/local/redis-cluster/6374/
[root@centos01 redis-cluster]# cp -r /usr/local/redis-5.0.13/* /usr/local/redis-cluster/6375/
[root@centos01 redis-cluster]# cp -r /usr/local/redis-5.0.13/* /usr/local/redis-cluster/6376/
2.2.4、修改每一个节点下redis.conf配置文件,
如下所示,6371是当前节点的端口号,请根据节点进行更改
port 6371 #端口
pidfile /var/run/redis_6371.pid
dir /usr/local/redis-cluster/6371/data/ #数据存放目录
cluster-config-file nodes-6371.conf #节点信息
#bind 127.0.0.1 # ip限制 不限制就注释掉
daemonize yes #后台启动模式
requirepass 123456 #redis登陆密码,不要密码请注释
appendonly yes
appendfsync always
cluster-enabled yes 开启集群
2.2.5 集群节点启动验证
执行如下脚本 启动6个节点(启动过程略)
[root@centos01 redis-5.0.13]# /usr/local/redis-cluster/bin/redis-server /usr/local/redis-cluster/6371/redis.conf
[root@centos01 redis-5.0.13]# /usr/local/redis-cluster/bin/redis-server /usr/local/redis-cluster/6372/redis.conf
[root@centos01 redis-5.0.13]# /usr/local/redis-cluster/bin/redis-server /usr/local/redis-cluster/6373/redis.conf
[root@centos01 redis-5.0.13]# /usr/local/redis-cluster/bin/redis-server /usr/local/redis-cluster/6374/redis.conf
[root@centos01 redis-5.0.13]# /usr/local/redis-cluster/bin/redis-server /usr/local/redis-cluster/6375/redis.conf
[root@centos01 redis-5.0.13]# /usr/local/redis-cluster/bin/redis-server /usr/local/redis-cluster/6376/redis.conf
进入任意节点下的src目录 登陆如下表示正常
[root@centos01 src]# cd /usr/local/redis-cluster/6371/src/
[root@centos01 src]# ./redis-cli -p 6371 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6371>
3、创建集群
备注1:redis5.0之前的版本创建集群需要用到ruby 命令,5.0之后不需要,我们的是5.0.13 所以直接跳过ruby,
备注2:redis5.0之前的版本创建集群使用redis-trib.rb命令,5.0之后的版本直接使用redis-cli命令
[root@centos01 src]# /usr/local/redis-cluster/bin/redis-cli --cluster create 192.168.19.128:6371 192.168.19.128:6372 192.168.19.128:6373 192.168.19.128:6374 192.168.19.128:6375 192.168.19.128:6376 --cluster-replicas 1 -a 123456
--cluster-replicas 1 :表示主从复制比例为 1:1,即一个主节点对应一个从节点,我们有6个节点,即默认前3个未主,后3个为从
-a 123456 :redis密码参数,若redis无密码则不需要此参数
执行后会有提示需要输入”yes“, 之后打印如下信息标识创建完成
......
Can I set the above configuration? (type 'yes' to accept): yes
......
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
4、集群验证
4.1、任意节点创建key
任意节点登陆redi创建一个名字为name的key,并设置值为text
[root@centos01 src]# /usr/local/redis-cluster/bin/redis-cli -c -h 192.168.19.128 -p 6371 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.19.128:6371> set name text
-> Redirected to slot [5798] located at 192.168.19.128:6372
OK
192.168.19.128:6372>
4.2数据同步验证
登陆其他节点(非4.1中登陆的节点),查看数据,可以看见如下图所示,登陆节点5(6375)能正常查看到4.1步骤在节点1(6371)创建的名为name值为text的key,至此标识集群创建成功。
[root@centos01 src]# /usr/local/redis-cluster/bin/redis-cli -c -h 192.168.19.128 -p 6375 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.19.128:6375> get name
-> Redirected to slot [5798] located at 192.168.19.128:6372
"text"
192.168.19.128:6372>