Redis教程

安装配置redis的cluster集群

本文主要是介绍安装配置redis的cluster集群,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

redis集群会自动实现redis哨兵的功能
redis集群的每个节点的数据是拆分的,每个节点并不是全量的,它是通过CRC16算法取模,把集群的内存划分为16384个槽位
一种方式是:在客户端实现,由客户端决定往哪台redis主节点写
一种方式是:利用代理实现,故障转移、扩容之后,会更改算法,比较危险,不常用

搭建集群
环境前提:
1.每个redis node节点采用相同的硬件配置、相同的密码、相同的redis版本。
2.每个节点必须开启的参数
#必须开启集群状态,开启后redis 进程会有cluster显示
816 cluster-enabled yes
#此文件有redis cluster集群自动创建和维护,不需要任何手动操作
824 cluster-confifig-fifile nodes-6380.conf
3.所有redis服务器必须没有任何数据
4.先启动为单机redis且没有任何key value

目前实验是在单个虚拟机上,运行10个redis实例:
首先配置一个模板配置文件:

[root@redis]#vim /apps/redis6379/etc/redis.conf
69 bind 0.0.0.0
92 port 6379
158 pidfile /apps/redis6379/run/redis_6379.pid
171 logfile /apps/redis6379/logs/redis_6379.log
235 stop-writes-on-bgsave-error no
263 dir /apps/redis6379/data
500 requirepass 123123
814 cluster-enabled yes
822 cluster-config-file nodes-6379.conf

然后配置模板服务启动文件:

[root@redis ~]#vim /usr/lib/systemd/system/redis6379.service 
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
#ExecStart=/usr/bin/redis-server /etc/redis.conf --supervised systemd
ExecStart=/apps/redis6379/bin/redis-server /apps/redis6379/etc/redis.conf --supervised systemd
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target
ExecStop=/bin/kill -s QUIT $MAINPID

复制redis服务目录,修改conf文件:

[root@redis apps]#ll /apps/
total 0
drwxr-xr-x 7 redis redis 63 Sep 17 14:34 redis6379
drwxr-xr-x 7 redis redis 63 Sep 17 14:34 redis6380
drwxr-xr-x 7 redis redis 63 Sep 17 14:34 redis6381
drwxr-xr-x 7 redis redis 63 Sep 17 14:34 redis6382
drwxr-xr-x 7 redis redis 63 Sep 17 14:34 redis6383
drwxr-xr-x 7 redis redis 63 Sep 17 14:34 redis6384
drwxr-xr-x 7 redis redis 63 Sep 17 14:34 redis6385
drwxr-xr-x 7 redis redis 63 Sep 17 14:34 redis6386
drwxr-xr-x 7 redis redis 63 Sep 17 14:34 redis6387
drwxr-xr-x 7 redis redis 63 Sep 17 14:34 redis6388

复制多实例服务启动脚本,修改文件内路径配置:

[root@redis ~]#ll /usr/lib/systemd/system/redis*
-rw-r--r-- 1 root root 539 Sep 17 15:03 /usr/lib/systemd/system/redis6379.service
-rw-r--r-- 1 root root 539 Sep 17 15:13 /usr/lib/systemd/system/redis6380.service
-rw-r--r-- 1 root root 539 Sep 17 15:21 /usr/lib/systemd/system/redis6381.service
-rw-r--r-- 1 root root 539 Sep 17 15:21 /usr/lib/systemd/system/redis6382.service
-rw-r--r-- 1 root root 539 Sep 17 15:21 /usr/lib/systemd/system/redis6383.service
-rw-r--r-- 1 root root 539 Sep 17 15:21 /usr/lib/systemd/system/redis6384.service
-rw-r--r-- 1 root root 539 Sep 17 15:22 /usr/lib/systemd/system/redis6385.service
-rw-r--r-- 1 root root 539 Sep 17 15:25 /usr/lib/systemd/system/redis6386.service
-rw-r--r-- 1 root root 539 Sep 17 15:22 /usr/lib/systemd/system/redis6387.service
-rw-r--r-- 1 root root 539 Sep 17 15:22 /usr/lib/systemd/system/redis6388.service
[root@redis ~]#cat /usr/lib/systemd/system/redis63* |grep "^ExecStart"
ExecStart=/apps/redis6379/bin/redis-server /apps/redis6379/etc/redis.conf --supervised systemd
ExecStart=/apps/redis6380/bin/redis-server /apps/redis6380/etc/redis.conf --supervised systemd
ExecStart=/apps/redis6381/bin/redis-server /apps/redis6381/etc/redis.conf --supervised systemd
ExecStart=/apps/redis6382/bin/redis-server /apps/redis6382/etc/redis.conf --supervised systemd
ExecStart=/apps/redis6383/bin/redis-server /apps/redis6383/etc/redis.conf --supervised systemd
ExecStart=/apps/redis6384/bin/redis-server /apps/redis6384/etc/redis.conf --supervised systemd
ExecStart=/apps/redis6385/bin/redis-server /apps/redis6385/etc/redis.conf --supervised systemd
ExecStart=/apps/redis6386/bin/redis-server /apps/redis6386/etc/redis.conf --supervised systemd
ExecStart=/apps/redis6387/bin/redis-server /apps/redis6387/etc/redis.conf --supervised systemd
ExecStart=/apps/redis6388/bin/redis-server /apps/redis6388/etc/redis.conf --supervised systemd

如果是多主机方式,就打包,没有日志文件、快照文件、配置文件等等

tar czvf redis.tar.gz /apps/redis
scp redis.tar.gz 1**.***.***.***:/apps/

在开启了redist cluster功能之后,redis在数据端口(6379)的基础之上+10000,而监听的集群端口,用于集群选举

[root@redis ~]#ss -lntp
State       Recv-Q Send-Q                                                 Local Address:Port                                                                Peer Address:Port              
LISTEN      0      511                                                                *:6379                                                                           *:*                   users:(("redis-server",pid=34447,fd=6))
LISTEN      0      511                                                                *:6380                                                                           *:*                   users:(("redis-server",pid=34576,fd=6))
LISTEN      0      511                                                                *:6381                                                                           *:*                   users:(("redis-server",pid=35467,fd=6))
LISTEN      0      511                                                                *:6382                                                                           *:*                   users:(("redis-server",pid=35479,fd=6))
LISTEN      0      511                                                                *:6383                                                                           *:*                   users:(("redis-server",pid=35828,fd=6))
LISTEN      0      511                                                                *:6384                                                                           *:*                   users:(("redis-server",pid=35839,fd=6))
LISTEN      0      511                                                                *:6385                                                                           *:*                   users:(("redis-server",pid=35851,fd=6))
LISTEN      0      511                                                                *:6386                                                                           *:*                   users:(("redis-server",pid=35862,fd=6))
LISTEN      0      511                                                                *:6387                                                                           *:*                   users:(("redis-server",pid=35874,fd=6))
LISTEN      0      511                                                                *:6388                                                                           *:*                   users:(("redis-server",pid=35887,fd=6))
LISTEN      0      128                                                                *:22                                                                             *:*                   users:(("sshd",pid=8654,fd=3))
LISTEN      0      100                                                        127.0.0.1:25                                                                             *:*                   users:(("master",pid=9369,fd=13))
LISTEN      0      511                                                                *:16379                                                                          *:*                   users:(("redis-server",pid=34447,fd=8))
LISTEN      0      511                                                                *:16380                                                                          *:*                   users:(("redis-server",pid=34576,fd=8))
LISTEN      0      511                                                                *:16381                                                                          *:*                   users:(("redis-server",pid=35467,fd=8))
LISTEN      0      511                                                                *:16382                                                                          *:*                   users:(("redis-server",pid=35479,fd=8))
LISTEN      0      511                                                                *:16383                                                                          *:*                   users:(("redis-server",pid=35828,fd=8))
LISTEN      0      511                                                                *:16384                                                                          *:*                   users:(("redis-server",pid=35839,fd=8))
LISTEN      0      511                                                                *:16385                                                                          *:*                   users:(("redis-server",pid=35851,fd=8))
LISTEN      0      511                                                                *:16386                                                                          *:*                   users:(("redis-server",pid=35862,fd=8))
LISTEN      0      511                                                                *:16387                                                                          *:*                   users:(("redis-server",pid=35874,fd=8))
LISTEN      0      511                                                                *:16388                                                                          *:*                   users:(("redis-server",pid=35887,fd=8))
LISTEN      0      128                                                               :::22                                                                            :::*                   users:(("sshd",pid=8654,fd=4))
LISTEN      0      100                                                              ::1:25                                                                            :::*                   users:(("master",pid=9369,fd=14))

后面必须带有cluster标识

[root@redis ~]#ps -ef |grep redis
redis     34447      1  0 15:13 ?        00:00:05 /apps/redis6379/bin/redis-server 0.0.0.0:6379 [cluster]
redis     34576      1  0 15:14 ?        00:00:05 /apps/redis6380/bin/redis-server 0.0.0.0:6380 [cluster]
redis     35467      1  0 15:31 ?        00:00:03 /apps/redis6381/bin/redis-server 0.0.0.0:6381 [cluster]
redis     35479      1  0 15:31 ?        00:00:03 /apps/redis6382/bin/redis-server 0.0.0.0:6382 [cluster]
redis     35828      1  0 15:38 ?        00:00:03 /apps/redis6383/bin/redis-server 0.0.0.0:6383 [cluster]
redis     35839      1  0 15:38 ?        00:00:03 /apps/redis6384/bin/redis-server 0.0.0.0:6384 [cluster]

一、在3.*和4.*的大版本下,redis官方提供的集群管理工具在源码包里,但是对ruby的版本有要求,不能太低:
#创建redis cluster命令,在集群的任一节点执行一次即可

[root@mini73 ~]#cp /usr/local/src/redis-4.0.9/src/redis-trib.rb /usr/bin/

#解决ruby版本较低问题:

[root@redis src]#wget https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.5.tar.gz
[root@redis src]# tar xf ruby-2.5.5.tar.gz
[root@redis src]# cd ruby-2.5.5
[root@redis ruby-2.5.5]# ./configure
[root@redis ruby-2.5.5]# make -j 2
[root@redis ruby-2.5.5]# make install
[root@redis ruby-2.5.5]# gem install redis

#如果无法在线安装,可以下载reids模块安装包离线安装
#https://rubygems.org/gems/redis #先下载redis模块安装包
#gem install -l redis-3.3.0.gem #安装redis模块
验证redis-trib.rb命令是否可执行:

[root@s1 ruby-2.5.4]# redis-trib.rb
Usage: redis-trib <command> <options> <arguments ...>

至此,初始环境,接下来是创建使用;
修改密码redis 登录密码,所有的节点,密码保持一致

[root@redis ruby-2.5.5]# vim /usr/local/lib/ruby/gems/2.5.0/gems/redis-3.3.0/lib/redis/client.rb
15       :password => 123123,

创建redis cluster集群:

[root@redis ~]#redis-trib.rb create --replicas 1 172.18.5.82:6379 172.18.5.82:6380 172.18.5.82:6381 172.18.5.82:6382 172.18.5.82:6383 172.18.5.82:6384
>>> Creating cluster
/usr/local/lib/ruby/gems/2.5.0/gems/redis-3.3.0/lib/redis/client.rb:459: warning: constant ::Fixnum is deprecated
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
172.18.5.82:6379
172.18.5.82:6380
172.18.5.82:6381
Adding replica 172.18.5.82:6383 to 172.18.5.82:6379
Adding replica 172.18.5.82:6384 to 172.18.5.82:6380
Adding replica 172.18.5.82:6382 to 172.18.5.82:6381
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: eb9c437f3d2301307ee5642014de061f21b57c98 172.18.5.82:6379
   slots:0-5460 (5461 slots) master
M: 292c0a3ce21370745e5b1a507870d74d4ba042cb 172.18.5.82:6380
   slots:5461-10922 (5462 slots) master
M: ec6eafdde964dcc1520f38d22471fc832a042d9f 172.18.5.82:6381
   slots:10923-16383 (5461 slots) master
S: b3c24d40a2f39c4442c834a806aee618cbfe589f 172.18.5.82:6382
   replicates eb9c437f3d2301307ee5642014de061f21b57c98
S: f668ad6c34d219ca6cb261c283b2719ec0584e6f 172.18.5.82:6383
   replicates 292c0a3ce21370745e5b1a507870d74d4ba042cb
S: 31bd8b6cc63466828a13dfb510713ae5666da475 172.18.5.82:6384
   replicates ec6eafdde964dcc1520f38d22471fc832a042d9f
Can I set the above configuration? (type 'yes' to accept): 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.18.5.82:6379)
M: eb9c437f3d2301307ee5642014de061f21b57c98 172.18.5.82:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 31bd8b6cc63466828a13dfb510713ae5666da475 172.18.5.82:6384
   slots: (0 slots) slave
   replicates ec6eafdde964dcc1520f38d22471fc832a042d9f
S: b3c24d40a2f39c4442c834a806aee618cbfe589f 172.18.5.82:6382
   slots: (0 slots) slave
   replicates eb9c437f3d2301307ee5642014de061f21b57c98
S: f668ad6c34d219ca6cb261c283b2719ec0584e6f 172.18.5.82:6383
   slots: (0 slots) slave
   replicates 292c0a3ce21370745e5b1a507870d74d4ba042cb
M: 292c0a3ce21370745e5b1a507870d74d4ba042cb 172.18.5.82:6380
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: ec6eafdde964dcc1520f38d22471fc832a042d9f 172.18.5.82:6381
   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@redis ~]#redis-trib.rb check 172.18.5.82:6379
/usr/local/lib/ruby/gems/2.5.0/gems/redis-3.3.0/lib/redis/client.rb:459: warning: constant ::Fixnum is deprecated
>>> Performing Cluster Check (using node 172.18.5.82:6379)
M: eb9c437f3d2301307ee5642014de061f21b57c98 172.18.5.82:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 31bd8b6cc63466828a13dfb510713ae5666da475 172.18.5.82:6384
   slots: (0 slots) slave
   replicates ec6eafdde964dcc1520f38d22471fc832a042d9f
S: b3c24d40a2f39c4442c834a806aee618cbfe589f 172.18.5.82:6382
   slots: (0 slots) slave
   replicates eb9c437f3d2301307ee5642014de061f21b57c98
S: f668ad6c34d219ca6cb261c283b2719ec0584e6f 172.18.5.82:6383
   slots: (0 slots) slave
   replicates 292c0a3ce21370745e5b1a507870d74d4ba042cb
M: 292c0a3ce21370745e5b1a507870d74d4ba042cb 172.18.5.82:6380
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: ec6eafdde964dcc1520f38d22471fc832a042d9f 172.18.5.82:6381
   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@redis ~]#redis-trib.rb info 172.18.5.82:6379
/usr/local/lib/ruby/gems/2.5.0/gems/redis-3.3.0/lib/redis/client.rb:459: warning: constant ::Fixnum is deprecated
172.18.5.82:6379 (eb9c437f...) -> 0 keys | 5461 slots | 1 slaves.
172.18.5.82:6380 (292c0a3c...) -> 0 keys | 5462 slots | 1 slaves.
172.18.5.82:6381 (ec6eafdd...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
此时集群虽然建立,但是主从关系没有真正建立,需要在三个(所有)的slave上配置主节点密码,直到INFO出现UP:
[root@redis ~]#redis-cli -p 6384
127.0.0.1:6384> AUTH 123123
OK
127.0.0.1:6384> CONFIG SET masterauth 123123
OK
127.0.0.1:6384> INFO
# Replication
role:slave
master_host:172.18.5.82
master_port:6381
master_link_status:up
验证master状态:
[root@redis ~]#redis-cli -p 6379
127.0.0.1:6379> AUTH
(error) ERR wrong number of arguments for 'auth' command
127.0.0.1:6379> AUTH 123123
OK
127.0.0.1:6379> INFO
# Replication
role:master
connected_slaves:1
slave0:ip=172.18.5.82,port=6382,state=online,offset=56,lag=0
master_replid:6538badbdd07a1ff66e88a2dbddd702a6abf92b4
master_replid2:0000000000000000000000000000000000000000

二、在5.*的大版本下,直接使用redi-cli命令即可

[root@redis-s1 ~]# redis-cli -a 123123 --cluster create 172.22.6.11:6379 192.168.7.101:6380 192.168.7.102:6379 192.168.7.102:6380 192.168.7.103:6379 192.168.7.103:6380 --cluster-replicas 1
Warning: Using a password with '-a' or '-u' option on the command line interface may not
be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.7.102:6380 to 192.168.7.101:6379
Adding replica 192.168.7.101:6380 to 192.168.7.102:6379
Adding replica 192.168.7.103:6380 to 192.168.7.103:6379
>>> Trying to optimize slaves allocation for anti-affinity
[OK] Perfect anti-affinity obtained!
M: f4cfc5cf821c0d855016488d6fbfb62c03a14fda 192.168.7.101:6379 #带M的为masterslots:[0-5460] (5461 slots) master #当前master的槽位起始和结束位
S: 2b6e5d9c3944d79a5b64a19e54e52f83d48438d6 192.168.7.101:6380 #带S的slave
replicates 70de3821dde4701c647bd6c23b9dd3c5c9f24a62
M: 116c4c6de036fdbac5aaad25eb1a61ea262b64af 192.168.7.102:6379
slots:[5461-10922] (5462 slots) master #当前master的槽位起始和结束位
S: 7186c6d03dd9a5e3be658f2d08e800bc55b04a09 192.168.7.102:6380
replicates f4cfc5cf821c0d855016488d6fbfb62c03a14fda
M: 70de3821dde4701c647bd6c23b9dd3c5c9f24a62 192.168.7.103:6379
slots:[10923-16383] (5461 slots) master #当前master的槽位起始和结束位
S: 7eda0bcf0c01bb7343313b81267c42dd1b26c8a6 192.168.7.103:6380
replicates 116c4c6de036fdbac5aaad25eb1a61ea262b64af
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 192.168.7.101:6379)
M: f4cfc5cf821c0d855016488d6fbfb62c03a14fda 192.168.7.101:6379 #master的ID及端口
slots:[0-5460] (5461 slots) master #已经分配的槽位
1 additional replica(s) #分配了一个slave
S: 7186c6d03dd9a5e3be658f2d08e800bc55b04a09 192.168.7.102:6380
slots: (0 slots) slave #slave没有分配槽位
replicates f4cfc5cf821c0d855016488d6fbfb62c03a14fda
M: 70de3821dde4701c647bd6c23b9dd3c5c9f24a62 192.168.7.103:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: 116c4c6de036fdbac5aaad25eb1a61ea262b64af 192.168.7.102:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 2b6e5d9c3944d79a5b64a19e54e52f83d48438d6 192.168.7.101:6380
slots: (0 slots) slave
replicates 70de3821dde4701c647bd6c23b9dd3c5c9f24a62
S: 7eda0bcf0c01bb7343313b81267c42dd1b26c8a6 192.168.7.103:6380
slots: (0 slots) slave
replicates 116c4c6de036fdbac5aaad25eb1a61ea262b64af
[OK] All nodes agree about slots configuration. #所有节点槽位分配完成
>>> Check for open slots... #检查打开的槽位
>>> Check slots coverage... #检查插槽覆盖范围
[OK] All 16384 slots covered. #所有槽位(16384个)分配完成

故障转移,启动恢复,都是自动完成

这篇关于安装配置redis的cluster集群的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!