redis集群搭建

一.资源准备

  1. 准备三台机器(能连外网,能相互ping通)

172.29.2.10 172.29.2.11 172.29.2.12

  1. 分别安装依赖包,关闭防火墙
[root@localhost src]# systemctl disable firewalld
[root@localhost src]# systemctl stop firewalld
[root@localhost redis]# yum install gcc-c++
[root@localhost redis]# yum install ruby
[root@localhost redis]# yum install rubygems

二.每台机器先安装单机redis

[root@localhost redis]# cd /usr/local
[root@localhost redis]# wget http://download.redis.io/releases/redis-4.0.10.tar.gz
[root@localhost redis]# tar -zxvf redis-4.0.10.tar.gz
[root@localhost redis]# mv redis-4.0.10 redis
[root@localhost redis]# cd redis
[root@localhost redis]# make MALLOC=libc
[root@localhost redis]# make install

注意上面操作是3台机器都安装
安装完成,这时候会在/usr/local/bin/目录下看到redis-server、redis-cli等可执行脚本,进入看一下,如果没有,就要去解压目录复制进去了。

[root@localhost redis]# cd /usr/local/bin/
[root@localhost bin]# ll
总用量 11536
-rwxr-xr-x. 1 root root  353680 1月   4 09:21 redis-benchmark
-rwxr-xr-x. 1 root root 3643680 1月   4 09:21 redis-check-aof
-rwxr-xr-x. 1 root root 3643680 1月   4 09:21 redis-check-rdb
-rwxr-xr-x. 1 root root  519880 1月   4 09:21 redis-cli
lrwxrwxrwx. 1 root root      12 1月   4 09:21 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 3643680 1月   4 09:21 redis-server

三.构建redis集群

  1. 建立集群文件夹和配置(三台机器都要配置)
# redis 占用端口,对应自己目录
port 7000
bind 0.0.0.0
daemonize yes   
##如果配置yes则开启集群功能,此redis实例作为集群的一个节点,否则,它是一个普通的单一的redis实例。
cluster-enabled yes  

#它是集群节点自动维护的文件,主要用于记录集群中有哪些节点、#他们的状态以及一些持久化参数等,方便在重启时恢复这些状态。通常是在收到请求之后这个文件就会被更新。
cluster-config-file nodes_7000.conf                                                        


#这是集群中的节点能够失联的最大时间,超过这个时间,该节点就会被认为故障。如果主节点超过这个时间还#是不可达,则用它的从节点将启动故障迁移,升级成主节点。注意,任何一个节点在这个>时间之内如果还是没
#有连上大部分的主节点,则此节点将停止接收任何请求。一般设置为15秒即可。
cluster-node-timeout 15000   
#开启aof持久化模式,每次写操作请求都追加到appendonly.aof文件中
appendonly yes  
#每次有写操作的时候都同步
appendfsync always  
#redis服务日志 
logfile /usr/local/app/redis-cluster/7000/redis.log
#pidfile文件对应7000,7001,7002
pidfile /var/run/redis_7000.pid

分别建立的redis.conf配置文件位置:
/usr/local/app/redis-cluster/7000/redis.conf
/usr/local/app/redis-cluster/7001/redis.conf
/usr/local/app/redis-cluster/7002/redis.conf

注意修改redis.conf 内容 : 7000文件夹对应7000 ,7001对应7001 , 7002对应7002

  1. 编写redis实例启动脚本(三台机器都配置)
[root@localhost redis-cluster]# cd /usr/local/app/redis-cluster
[root@localhost redis-cluster]# vim start.sh
#/bin/bash
for ((i=0;i<3;i++));
do redis-server /usr/local/app/redis-cluster/700$i/redis.conf
done


[root@localhost redis-cluster]# chmod +x start.sh
[root@localhost redis-cluster]#start.sh
[root@localhost redis-cluster]# ps -ef |grep redis
root      2681     1  0 11:33 ?        00:00:00 redis-server 0.0.0.0:7000 [cluster]
root      2683     1  0 11:33 ?        00:00:00 redis-server 0.0.0.0:7001 [cluster]
root      2685     1  0 11:33 ?        00:00:00 redis-server 0.0.0.0:7002 [cluster]
root      2696  1508  0 11:33 pts/0    00:00:00 grep --color=auto redis

这时我们只是启动了9个单独的redis实例,它们还不是一个集群,下面就说明创建集群

  1. 构建集群

在一台机器上用redis-trib.rb 这个工具,就在redis解压目录的 src 目录中

[root@localhost src]# cd /usr/local/redis/src
[root@localhost src]#  ./redis-trib.rb create --replicas 1 172.29.2.10:7000 172.29.2.10:7001 172.29.2.10:7002 172.29.2.11:7000 172.29.2.11:7001 172.29.2.11:7002 172.29.2.12:7000 172.29.2.12:7001 172.29.2.12:7002

执行可能会报错

/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:inrequire'
from ./redis-trib.rb:25:in `

'

原因就是安装redis-trib.rb运行依赖的ruby的包redis-4.1.3.gem

[root@localhost src]# gem install redis

但是又报错了

[root@localhost src]# gem install redis
Fetching: redis-4.2.5.gem (100%)
ERROR:  Error installing redis:
    redis requires Ruby version >= 2.3.0.

好家伙,很搞心态。于是我们又进行下面操作

[root@localhost src]# yum install curl
[root@localhost src]# curl -L get.rvm.io | bash -s stable

如果 上面步骤报错

curl: (7) Failed connect to raw.githubusercontent.com:443; 拒绝连接

说明dns解析被污染了,我们需要在https://www.ipaddress.com/查询raw.githubusercontent.com的真实IP,然后修改hosts

[root@localhost src]#vim /etc/hosts
199.232.96.133 raw.githubusercontent.com

继续执行

[root@localhost src]# curl -L get.rvm.io | bash -s stable

要等几分钟, 然而又会报错

[root@localhost src]# curl -L get.rvm.io | bash -s stable
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   194  100   194    0     0    256      0 --:--:-- --:--:-- --:--:--   256
100 24535  100 24535    0     0   9947      0  0:00:02  0:00:02 --:--:-- 21559
Downloading https://github.com/rvm/rvm/archive/1.29.11.tar.gz

Downloading https://github.com/rvm/rvm/releases/download/1.29.11/1.29.11.tar.gz.asc
gpg: 于 2020年12月29日 星期二 21时40分04秒 CST 创建的签名,使用 RSA,钥匙号 39499BDB
gpg: 无法检查签名:没有公钥
GPG signature verification failed for '/usr/local/rvm/archives/rvm-1.29.11.tgz' - 'https://github.com/rvm/rvm/releases/download/1.29.11/1.29.11.tar.gz.asc'! Try to install GPG v2 and then fetch the public key:

    gpg2 --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

or if it fails:

    command curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -
    command curl -sSL https://rvm.io/pkuczynski.asc | gpg2 --import -

In case of further problems with validation please refer to https://rvm.io/rvm/security

此时我们需要这样

[root@localhost src]# gpg2 --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

然后继续执行

[root@localhost src]# curl -L get.rvm.io | bash -s stable

终于成功了


image.png

继续执行

[root@localhost src]# source /usr/local/rvm/scripts/rvm
[root@localhost src]# rvm install 2.4.0
[root@localhost src]# gem install redis

构建集群

[root@localhost src]# ./redis-trib.rb create --replicas 1 172.29.2.10:7000 172.29.2.10:7001 172.29.2.10:7002 172.29.2.11:7000 172.29.2.11:7001 172.29.2.11:7002 172.29.2.12:7000 172.29.2.12:7001 172.29.2.12:7002

测试连接

[root@localhost src]# redis-cli -c --raw -h 172.29.2.11 -p 7000
172.29.2.11:7000> set a 12
-> Redirected to slot [15495] located at 172.29.2.10:7001
OK

列出集群节点 cluster nodes

172.29.2.10:7001> cluster nodes
8652274729376e82f1a428e1925c58406957645a 172.29.2.10:7002@17002 slave 78601835e590953792faa9a727b0d2270c86b79e 0 1609738590000 4 connected
81cd0a98e5949d75b26622d2033060ea3d681c49 172.29.2.12:7000@17000 master - 0 1609738591000 7 connected 8192-12287
48483d9068b7666c6dac3fc9b02efe14e49a4bab 172.29.2.11:7002@17002 slave 81cd0a98e5949d75b26622d2033060ea3d681c49 0 1609738593251 7 connected
e13044c18301d106a5da98ea5cd13d02d859f43e 172.29.2.10:7000@17000 master - 0 1609738595272 1 connected 0-4095
509f037180874459355a818fcabe28095f007d26 172.29.2.10:7001@17001 myself,master - 0 1609738585000 2 connected 12288-16383
78601835e590953792faa9a727b0d2270c86b79e 172.29.2.11:7000@17000 master - 0 1609738593000 4 connected 4096-8191
d2334d3cd6bfca1a4a25ba4f959b7804f5ceb290 172.29.2.11:7001@17001 slave e13044c18301d106a5da98ea5cd13d02d859f43e 0 1609738594263 5 connected
5bef09d11868b12defed1d6dfc01c6f34b1bdc6f 172.29.2.12:7002@17002 slave 509f037180874459355a818fcabe28095f007d26 0 1609738592242 2 connected
a232ca2b2a62c1428f2ec5ff28c9d020002d4fbe 172.29.2.12:7001@17001 slave e13044c18301d106a5da98ea5cd13d02d859f43e 0 1609738591234 8 connected

查看集群信息 cluster info

172.29.2.10:7001> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:9
cluster_size:4
cluster_current_epoch:9
cluster_my_epoch:2
cluster_stats_messages_ping_sent:507
cluster_stats_messages_pong_sent:496
cluster_stats_messages_meet_sent:5
cluster_stats_messages_sent:1008
cluster_stats_messages_ping_received:492
cluster_stats_messages_pong_received:512
cluster_stats_messages_meet_received:4
cluster_stats_messages_received:1008

到此,redis cluster模式已经搭建成功。

《 上善若水。水善利万物而不争,处众人之所恶,故几于道 》
释义:最善的人好像水一样。水善于滋润万物而不与万物相争,停留在众人都不喜欢的地方,所以最接近于“道”。

你可能感兴趣的:(redis集群搭建)