Redis集群的搭建

Redis集群的搭建

Redis集群的搭建

分析:

由于当集群中的某一台redis主机挂了,需要其他主机投票,当某台主机的得票数达到半数以上,其成为主机的要求,所有至少需要三台机器;又由于redis服务器要保证高可用,需要满足拥有16384个槽,若一台机器挂了,槽不完整,整个集群都不可用,所有每个主机需要一个备份机,及一共需要6台主机。

搭建伪分布式集群,可以在一台主机上运行多个redis实例。需要修改redis得端口号:7001-7006;

步骤:

1、复制多份redis实例到文件夹redis-cluster下

2、修改配置文件redis.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
修改端口:

# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port 7001


改为后台启动
################################# GENERAL #####################################

# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes


打开集群模式:
################################ REDIS CLUSTER ###############################
#
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# WARNING EXPERIMENTAL: Redis Cluster is considered to be stable code, however
# in order to mark it as "mature" we need to wait for a non trivial percentage
# of users to deploy it in production.
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
# Normal Redis instances can't be part of a Redis Cluster; only nodes that are
# started as cluster nodes can. In order to start a Redis instance as a
# cluster node enable the cluster support uncommenting the following:
#
cluster-enabled yes

3、复制多分redis实例

image

4、修改redis.conf文件(如步骤2中),端口分别位7002、7003、7004、7005、7006;

5、启动所有实例;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
cd redis01
./redis-server redis.conf
cd ..

cd redis02
./redis-server redis.conf
cd ..

cd redis03
./redis-server redis.conf
cd ..
cd redis04
./redis-server redis.conf
cd ..
cd redis05
./redis-server redis.conf
cd ..
cd redis06
./redis-server redis.conf
cd ..
~
~

查看进程:

1
2
3
4
5
6
7
8
9
[root@localhost redis-cluster]# ps -ef|grep redis
root 3261 1 0 06:19 ? 00:00:00 ./redis-server 127.0.0.1:7001 [cluster]
root 3265 1 0 06:19 ? 00:00:00 ./redis-server 127.0.0.1:7002 [cluster]
root 3269 1 0 06:19 ? 00:00:00 ./redis-server 127.0.0.1:7003 [cluster]
root 3273 1 0 06:19 ? 00:00:00 ./redis-server 127.0.0.1:7004 [cluster]
root 3277 1 0 06:19 ? 00:00:00 ./redis-server 127.0.0.1:7005 [cluster]
root 3279 1 0 06:19 ? 00:00:00 ./redis-server 127.0.0.1:7006 [cluster]
root 3285 3026 0 06:19 pts/1 00:00:00 grep --color=auto redis
[root@localhost redis-cluster]# ll

6、使用ruby脚本搭建集群。需要ruby的运行环境。

安装ruby

yum install ruby

yum install rubygems

(gem install redis-3.0.0.gem)

7、运行:

./redis-trib.rb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006

image

yes

image

至此,集群搭建完成;那么如何连接集群?

随便进入一个redis实例内:

执行命令:-c参数不能缺少

./redis-cli -p 7006 -c

image

一些集群的命令:

cluster info
cluster nodes
文章目录
  1. 1. Redis集群的搭建
    1. 1.1. 分析: