集群的概念
Redis集群提供了一种运行Redis安装的方式,其中数据自动分割多个Redis节点。
Redis集群的好处:
- 在多个节点之间自动分割数据集的能力。
- 当节点的一个子集出现故障或无法与集群的其他部分通信时,继续操作的能力。
每个Redis集群节点都需要打开两个TCP连接。用于为客户端提供服务的正常Redis TCP端口,例如6379,加上通过向数据端口添加10000获得的端口,因此在本例中为16379。
这个第二高端口用于集群总线,这是一个使用二进制协议的节点到节点通信通道。节点使用集群总线进行故障检测、配置更新、故障转移授权等等。客户端永远不要尝试与集群总线端口通信,但总是与正常的Redis命令端口,但是确保你打开两个端口在你的防火墙,否则Redis集群节点将无法通信。
注意,如果想要集群按照你想的那样工作,那么集群中的每个节点应该:
- 正常的客户端通信端口(通常是6379)用于和所有可到达集群的所有客户端通信。
- 集群总线端口(the client port + 10000)必须对所有的其它节点是可到达的。
需要注意的是:必须要3个或以上的主节点,否则在创建集群时会失败,并且当存活的主节点数小于总节点数的一半时,整个集群就无法提供服务了
安装
我们采用三主三次,3台服务器,6379表示主节点,7379是从节点。
节点IP | 端口 | 主或者从 | 所属主节点 |
---|---|---|---|
172.20.28.150 | 6379 | master | ------ |
172.20.28.150 | 7379 | slave | 172.20.28.150:6379 |
172.20.28.151 | 6379 | master | ------ |
172.20.28.151 | 7379 | slave | 172.20.28.151:6379 |
172.20.28.152 | 6379 | master | ------ |
172.20.28.152 | 7379 | slave | 172.20.28.152:6379 |
- 下载安装包,传到
/opt/
目录下,并解压
[root@els1 ~]# cp redis-3.2.4.tar.gz /opt
[root@els1 opt]# tar -xzvf redis-3.2.4.tar.gz
- 安装编译需要的依赖
[root@els1 src]# yum -y install gcc gcc-c++ libstdc++-devel
- 编译安装,指定安装到特定目录
[root@els1 src]# make
[root@els1 src]# cd src
[root@els1 src]# make install PREFIX=/opt/redis
- 创建redis集群目录
[root@els1 opt]# mkdir -p redis-cluster/6379
[root@els1 opt]# mkdir -p redis-cluster/7379
- 修改各个节点的配置文件,如,172.20.28.150节点,6379下的配置文件。
◇:
绑定的IP、端口、日志文件、数据文件、PID文件、aof文件,做相应的修改。6379目录改成7379
[root@els1 opt]# cd redis-cluster/6379/
[root@els1 6379]# cat redis-6379.conf | grep -v "#" | grep -v "^$"
bind 172.20.28.150 # 其他节点改IP
protected-mode yes
port 6379 # 7379目录下改端口
tcp-backlog 511
timeout 0
tcp-keepalive 0
daemonize yes
supervised no
pidfile /var/run/redis-6379.pid # PID, 文件改
loglevel notice
logfile "/opt/redis-cluster/6379/6379.log" # 日志文件,改
databases 16
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump6379.rdb
dir /opt/redis-cluster/6379/ # 当前集群节点目录,改
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
maxclients 10000
appendonly no
appendfilename "appendonly6379.aof" # aof文件,改
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
cluster-enabled yes
cluster-config-file nodes-6379.conf # 当前节点配置文件,改
cluster-node-timeout 15000
slowlog-log-slower-than 10000
slowlog-max-len 128
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 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
- 进入src目录,以指定配置文件的方式启动redis
[root@els1 src]# ./redis-server /opt/redis-cluster/6379/redis-6379.conf
[root@els1 src]# ps -ef | grep redis
root 23974 1 0 11:32 ? 00:00:00 ./redis-server 172.20.28.150:6379 [cluster]
在其他两个节点安装redis,配置redis-cluster,修改相应地址及端口,启动其他5个redis节点
[root@els1 src]# ./redis-server /opt/redis-cluster/7379/redis-7379.conf
[root@els2 src]# ./redis-server /opt/redis-cluster/6379/redis-6379.conf
[root@els2 src]# ./redis-server /opt/redis-cluster/7379/redis-7379.conf
[root@els3 src]# ./redis-server /opt/redis-cluster/6379/redis-6379.conf
[root@els3 src]# ./redis-server /opt/redis-cluster/7379/redis-7379.conf
在其中一个节点上建立集群
[root@els1 bin]# cd /opt/redis-3.2.4/src/
[root@els1 src]# ./redis-trib.rb create --replicas 1 172.20.28.150:6379 172.20.28.151:6379 172.20.28.152:6379 172.20.28.150:7379 172.20.28.151:7379 172.20.28.152:7379
/usr/bin/env: ruby: No such file or directory
- 因为redis-trib.rb是ruby脚本写的,需要安装ruby依赖,之后继续报错
[root@els1 src]# yum install ruby
[root@els1 src]# ./redis-trib.rb create --replicas 1 172.20.28.150:6379 172.20.28.151:6379 172.20.28.152:6379 172.20.28.150:7379 172.20.28.151:7379 172.20.28.152:7379
/usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- redis (LoadError)
from /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in `require'
from ./redis-trib.rb:25:in `<main>'
查看得知 Ruby的redis接口没有安装,需要安装Redis接口,输入命令 " gem install redis " 进行安装
[root@els1 src]# gem install redis
Fetching: redis-4.2.5.gem (100%)
ERROR: Error installing redis:
redis requires Ruby version >= 2.3.0
[root@els1 src]# yum install rubygem
.........
Package rubygems-2.0.14.1-36.el7.noarch already installed and latest version
改rvm下载 ruby 的源,到镜像 ruby china
[root@arslinux-01 ~]# gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/
安装rvm, 提示拒绝
[root@els1 home]# curl -sSL https://get.rvm.io | bash -s stable
curl: (7) Failed connect to raw.githubusercontent.com:443; Connection refused
查看后发现 raw.githubusercontent.com的IP是美国地址,手动添加域名解析(设置host)
[root@els1 home]# vim /etc/hosts
199.232.28.133 raw.githubusercontent.com
再次执行curl命令,并测试版本
[root@els1 home]# curl -sSL https://get.rvm.io | bash -s stable
Installation of RVM in /usr/local/rvm/ is almost complete:
* First you need to add all users that will be using rvm to 'rvm' group,
and logout - login again, anyone using rvm will be operating with `umask u=rwx,g=rwx,o=rx`.
* To start using RVM you need to run `source /etc/profile.d/rvm.sh`
in all your open shell windows, in rare cases you need to reopen all shell windows.
* Please do NOT forget to add your users to the rvm group.
The installer no longer auto-adds root or users to the rvm group. Admins must do this.
Also, please note that group memberships are ONLY evaluated at login time.
This means that users must log out then back in before group membership takes effect!
Thanks for installing RVM 🙏
Please consider donating to our open collective to help us maintain RVM.
👉 Donate: https://opencollective.com/rvm/donate
[root@els1 home]# useradd -g rvm rvm
[root@els1 home]# source /etc/profile.d/rvm.sh
[root@els1 home]# rvm list known
...
[ruby-]2.1[.10]
[ruby-]2.2[.10]
[ruby-]2.3[.8]
[ruby-]2.4[.9]
[ruby-]2.5[.7]
[ruby-]2.6[.5]
[ruby-]2.7[.0]
...
安装大于2.3.0版本的ruby
[root@els1 home]# rvm install 2.5.7
[root@els1 home]# ruby -v
ruby 2.5.7p206 (2019-10-01 revision 67816) [x86_64-linux]
[root@els1 home]# gem -v
3.0.9
建立集群,依旧报错 = =~!
[root@els1 src]# ./redis-trib.rb
Traceback (most recent call last):
2: from ./redis-trib.rb:25:in `<main>'
1: from /usr/local/rvm/rubies/ruby-2.5.7/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/usr/local/rvm/rubies/ruby-2.5.7/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- redis (LoadError)
原因是忘了给redis注册接口了
[root@els1 src]#gem install redis
建立集群
[root@els1 src]# ./redis-trib.rb create --replicas 1 172.20.28.150:6379 172.20.28.151:6379 172.20.28.152:6379 172.20.28.150:7379 172.20.28.151:7379 172.20.28.152:7379
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
172.20.28.150:6379
172.20.28.151:6379
172.20.28.152:6379
Adding replica 172.20.28.151:7379 to 172.20.28.150:6379
Adding replica 172.20.28.150:7379 to 172.20.28.151:6379
Adding replica 172.20.28.152:7379 to 172.20.28.152:6379
M: 68a7aef7794b654f713f9ddb5ebad7875fdfd90f 172.20.28.150:6379
slots:0-5460 (5461 slots) master
M: 5284e6b20d9a5b13ae4a2e4aba004b37f2757add 172.20.28.151:6379
slots:5461-10922 (5462 slots) master
M: ae8c234b39d945c16e2c89713839f77941c8af23 172.20.28.152:6379
slots:10923-16383 (5461 slots) master
S: 533f3d4e32ed95930b96347753e7a56c67f22c03 172.20.28.150:7379
replicates 5284e6b20d9a5b13ae4a2e4aba004b37f2757add
S: e5ed31e5e24b5b2e97dbc0cd75672660150318e9 172.20.28.151:7379
replicates 68a7aef7794b654f713f9ddb5ebad7875fdfd90f
S: acacdc42ec8bcd7bfdfa0a7fe0dd39fa4908421d 172.20.28.152:7379
replicates ae8c234b39d945c16e2c89713839f77941c8af23
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.28.150:6379)
M: 68a7aef7794b654f713f9ddb5ebad7875fdfd90f 172.20.28.150:6379
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: ae8c234b39d945c16e2c89713839f77941c8af23 172.20.28.152:6379
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 533f3d4e32ed95930b96347753e7a56c67f22c03 172.20.28.150:7379
slots: (0 slots) slave
replicates 5284e6b20d9a5b13ae4a2e4aba004b37f2757add
S: e5ed31e5e24b5b2e97dbc0cd75672660150318e9 172.20.28.151:7379
slots: (0 slots) slave
replicates 68a7aef7794b654f713f9ddb5ebad7875fdfd90f
M: 5284e6b20d9a5b13ae4a2e4aba004b37f2757add 172.20.28.151:6379
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: acacdc42ec8bcd7bfdfa0a7fe0dd39fa4908421d 172.20.28.152:7379
slots: (0 slots) slave
replicates ae8c234b39d945c16e2c89713839f77941c8af23
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
到此redis集群建立完成,可查看集群状态
[root@els1 src]# ./redis-trib.rb check 172.20.28.150:6379