Centos7安装Redis-4.0.1集群

试验机操作系统:CentOS Linux release 7.5.1804 (Core)

下载Redis

下载地址:https://redis.io/download

下载版本:redis-4.0.1.tar.gz

[root@Centos7-1 ~]# wget http://download.redis.io/releases/redis-4.0.1.tar.gz

安装gcc与psmisc

yum install gcc psmisc

解压tar包

[root@Centos7-1 ~]# tar zxvf redis-4.0.1.tar.gz

[root@Centos7-1 ~]# mv redis-4.0.1 /usr/local/rediscd /soft

编译安装

[root@Centos7-1 ~]#cd /usr/local/redis

[root@Centos7-1 redis]#make MALLOC=libc

[root@Centos7-1 redis]#make & make install

说明:如果在make的时候不指定内存管理方式,会报错的:

zmalloc.h:50:31: 致命错误:jemalloc/jemalloc.h:没有那个文件或目录。

malloc是管理内存碎片的。

使用systemctl管理服务

[root@Centos7-1 redis]# vim /lib/systemd/system/redis.service

[Unit] Description=Redis After=network.target [Service] ExecStart=/usr/local/bin/redis-server /usr/local/redis/redis.conf --daemonize no ExecStop=/usr/local/bin/redis-cli -h 127.0.0.1 -p 6379 shutdown [Install] WantedBy=multi-user.target

*   [Unit] 表示这是基础信息

*   Description 是描述
*   After 是在那个服务后面启动,一般是网络服务启动后启动

*   [Service] 表示这里是服务信息

*   ExecStart 是启动服务的命令
*   ExecStop 是停止服务的指令

*   [Install] 表示这是是安装相关信息

*   WantedBy 是以哪种方式启动:multi-user.target表明当系统以多用户方式(默认的运行级别)启动时,这个服务需要被自动运行。

刷新systemctl配置

[root@Centos7-1 redis]#systemctl daemon-reload

启动reids

[root@Centos7-1 redis]#systemctl start redis; systemctl enable redis

[root@Centos7-1 redis]#systemctl status redis

===========================================

Redis集群安装配置

redis集群最小是3主3从。

192.168.1.196      CentOS Linux release 7.5.1804 (Core)

192.168.1.127      CentOS Linux release 7.5.1804 (Core)

192.168.1.210      CentOS Linux release 7.5.1804 (Core)

192.168.1.46       CentOS Linux release 7.5.1804 (Core)

192.168.1.172      CentOS Linux release 7.5.1804 (Core)

192.168.1.173      CentOS Linux release 7.5.1804 (Core)

在一台机器上装好redis,然后拷贝到其他服务器上去。

安装ruby

创建redis集群需要用Ruby运行redis-trib.rb

[root@Centos7-1 redis]# yum install centos-release-scl-rh -y

[root@Centos7-1 redis]# yum install rh-ruby23 rh-ruby23-ruby-devel rubygems rpm-build -y

[root@Centos7-1 redis]# scl enable rh-ruby23 bash
1.移除现有的镜像

[root@Centos7-1 redis]# gem sources --remove https://rubygems.org/

2.使用淘宝镜像

[root@Centos7-1 redis]# gem sources -a https://ruby.taobao.org/

3.验证当前ruby版本

[root@Centos7-1 redis]# gem sources -l

[root@Centos7-1 redis]# gem install redis

修改redis.conf

打开redis.conf,把下面列出来的配置项全部注释,完了拷贝下面7条配置到redis.conf文件末尾

port 6379

bind 192.168.1.196

daemonize yes

cluster-enabled yes

cluster-config-file nodes-6379.conf

cluster-node-timeout 15000

pidfile /var/run/redis_6379.pid

把每台机器上的redis.conf中的bind修改成对于的IP

启动redis

每台都要启动

[root@Centos7-1 redis]# systemctl restart redis

启动集群

[root@Centos7-1 redis]# /usr/local/redis/src/redis-trib.rb create --replicas 1 192.168.1.196:6379 192.168.1.127:6379 192.168.1.210:6379 192.168.1.46:6379 192.168.1.172:6379 192.168.1.173:6379

>>> Creating cluster

>>> Performing hash slots allocation on 6 nodes...

Using 3 masters:

192.168.1.196:6379

192.168.1.127:6379

192.168.1.210:6379

Adding replica 192.168.1.172:6379 to 192.168.1.196:6379

Adding replica 192.168.1.173:6379 to 192.168.1.127:6379

Adding replica 192.168.1.46:6379 to 192.168.1.210:6379

M: 8f665799d22539e4fb817c2bbbd2762fba8a452d 192.168.1.196:6379

slots:0-5460 (5461 slots) master

M: 124c4d42b375bc0eb9341ec350c85eb178037a5c 192.168.1.127:6379

slots:5461-10922 (5462 slots) master

M: 17a0cfe1dddcbaff42d678dbd887409ad8059011 192.168.1.210:6379

slots:10923-16383 (5461 slots) master

S: 9d0a59860f1aa7affceaa1a2e7f22c3bd16ecf87 192.168.1.46:6379

replicates 17a0cfe1dddcbaff42d678dbd887409ad8059011

S: 2fb0663db810e5eef383ceb75022c69a10a73cfa 192.168.1.172:6379

replicates 8f665799d22539e4fb817c2bbbd2762fba8a452d

S: fd072f90fd19fbe56e133dd3893d7854f6749358 192.168.1.173:6379

replicates 124c4d42b375bc0eb9341ec350c85eb178037a5c

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 192.168.1.196:6379)

M: 8f665799d22539e4fb817c2bbbd2762fba8a452d 192.168.1.196:6379

slots:0-5460 (5461 slots) master

1 additional replica(s)

S: 2fb0663db810e5eef383ceb75022c69a10a73cfa 192.168.1.172:6379

slots: (0 slots) slave

replicates 8f665799d22539e4fb817c2bbbd2762fba8a452d

M: 124c4d42b375bc0eb9341ec350c85eb178037a5c 192.168.1.127:6379

slots:5461-10922 (5462 slots) master

1 additional replica(s)

S: fd072f90fd19fbe56e133dd3893d7854f6749358 192.168.1.173:6379

slots: (0 slots) slave

replicates 124c4d42b375bc0eb9341ec350c85eb178037a5c

S: 9d0a59860f1aa7affceaa1a2e7f22c3bd16ecf87 192.168.1.46:6379

slots: (0 slots) slave

replicates 17a0cfe1dddcbaff42d678dbd887409ad8059011

M: 17a0cfe1dddcbaff42d678dbd887409ad8059011 192.168.1.210:6379

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.

连接redis集群

[root@localhost redis]# src/redis-cli -h 192.168.1.196 -c -p 6379

必须要加:-c参数

在192.168.1.196节点上写一条数据:

[root@Centos7-1 redis]# redis-cli -h 192.168.1.196 -c -p 6379

192.168.1.196:6379> set key test

-> Redirected to slot [12539] located at 192.168.1.210:6379

OK

192.168.1.210:6379> get key

"test"

192.168.1.210:6379> set key2 test2

-> Redirected to slot [4998] located at 192.168.1.196:6379

OK

192.168.1.196:6379> get key2

"test2"

可以看到插入键值在196机器上,获取键值的时候,自动跳到其他机器上

获取集群信息

192.168.1.210:6379> cluster info

cluster_state:ok

cluster_slots_assigned:16384

cluster_slots_ok:16384

cluster_slots_pfail:0

cluster_slots_fail:0

cluster_known_nodes:6

cluster_size:3

cluster_current_epoch:6

cluster_my_epoch:3

cluster_stats_messages_ping_sent:1624

cluster_stats_messages_pong_sent:1665

cluster_stats_messages_meet_sent:3

cluster_stats_messages_sent:3292

cluster_stats_messages_ping_received:1662

cluster_stats_messages_pong_received:1627

cluster_stats_messages_meet_received:3

cluster_stats_messages_received:3292

查看集群节点

192.168.1.210:6379> cluster nodes

124c4d42b375bc0eb9341ec350c85eb178037a5c 192.168.1.127:6379@16379 master - 0 1530001192724 2 connected 5461-10922

9d0a59860f1aa7affceaa1a2e7f22c3bd16ecf87 192.168.1.46:6379@16379 slave 17a0cfe1dddcbaff42d678dbd887409ad8059011 0 1530001190720 4 connected

8f665799d22539e4fb817c2bbbd2762fba8a452d 192.168.1.196:6379@16379 master - 0 1530001195729 1 connected 0-5460

17a0cfe1dddcbaff42d678dbd887409ad8059011 192.168.1.210:6379@16379 myself,master - 0 1530001190000 3 connected 10923-16383

2fb0663db810e5eef383ceb75022c69a10a73cfa 192.168.1.172:6379@16379 slave 8f665799d22539e4fb817c2bbbd2762fba8a452d 0 1530001196732 5 connected

fd072f90fd19fbe56e133dd3893d7854f6749358 192.168.1.173:6379@16379 slave 124c4d42b375bc0eb9341ec350c85eb178037a5c 0 1530001194727 6 connected

如果重启服务器后,再次执行redis-trib.rb会报错如下错误:

[ERR] Node 192.168.1.80:6379 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.

解决方法:

删除dump.rdb和nodes-6379.conf这两个文件,然后重启redis服务,最后启动集群。

推荐阅读更多精彩内容