由于工作原因需要安装redis集群,而redis3.0版本及以上才有cluster集群功能,于是我尝试安装redis3.0.0版本
第一步,下载安装redis
$ wget http://download.redis.io/releases/redis-3.0.0.tar.gz
-->下载redis
$ tar -zxvf redis-3.0.0.tar.gz
-->解压redis
$ cd redis-3.0.0
$ make
第二步,配置redis
1.新建相关目录
$ mkdir -p /usr/local/redis/
-->放置redis的启动文件,redis-server、redis-cli等
$
mkdir -p /usr/local/redis/conf/ -->放置redis的配置文件,redis.conf
$
mkdir -p /usr/local/redis/db/
-->放置appendonly.aof、dump.rdb、nodes.conf(cluster)等
$
mkdir -p /usr/local/redis/run/
-->放置redis.pid
文件
2.复制配置文件和一些启动文件到相应目录
$ cp redis.conf
/usr/local/redis/conf/
$
cd src
$
cp mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-dump redis-cli redis-sentinel redis-server redis-trib.rb
/usr/local/redis/
3.修改配置文件
由于是搭建cluster集群, 官网说明文档集群需要六个节点,要让集群正常工作至少需要3个主节点,在这里我们要创建6个redis节点,其中三个为主节点,三个为从节点。
于是有设定六个端口,分别是7000,7001
,7002
,7003,7004,7005
$ cd /usr/local/redis/conf/
$ mkdir 7000 7001 7002 7003 7004 7005
-->新建目录,分别存放对应的redis配置文件
$ cp redis.conf
7000
/
redis7000.conf
-->复制并重命名配置文件,并修改文件名标识为
对应端口号
7000
$ cp redis.conf
7001
/
redis7001.conf
-->
复制并重命名配置文件,并修改文件名标识为
对应端口号
7001
$ cp redis.conf
7002
/
r
edis7002.conf
-->
复制并重命名配置文件,并修改文件名标识为
对应端口号
7002
$ cp redis.conf
7003
/
redis7003.conf
-->
复制并重命名配置文件,并修改文件名标识为
对应端口号
7003
$ cp redis.conf
7004
/
r
edis7004.conf
-->
复制并重命名配置文件,并修改文件名标识为
对应端口号
7004
$ cp redis.conf
7005
/
r
edis7005.conf
-->
复制并重命名配置文件,并修改文件名标识为
对应端口号
7005
$ vim
7000
/
r
edis7000.conf
-->修改配置文件,全部配置文件都要修改,此处只做示范
修改如下:
注意:不需要修改的地方此处并未贴出
################################ 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 no
-->修改为yes,
改为
daemonize yes
# When running daemonized, Redis writes a pid file in /var/run/redis.pid by
# default. You can specify a custom pid file location here.
pidfile /var/run/redis.pid
-->建议修改路径,此处我修改为,pidfile /usr/local/redis/run/redis-7000.pid
# Accept connections on the specified port, default is 6379.
# If port 0 is specified Redis will not listen on a TCP socket.
port 6379
-->修改端口,此处我修改为,port
7000
################################ SNAPSHOTTING ################################
#
# Save the DB on disk:
#
# save <seconds> <changes>
#
# Will save the DB if both the given number of seconds and the given
# number of write operations against the DB occurred.
#
# In the example below the behaviour will be to save:
# after 900 sec (15 min) if at least 1 key changed
# after 300 sec (5 min) if at least 10 keys changed
# after 60 sec if at least 10000 keys changed
#
# Note: you can disable saving completely by commenting out all "save" lines.
#
# It is also possible to remove all the previously configured save
# points by adding a save directive with a single empty string argument
# like in the following example:
#
# save ""
-->根据给定的时间间隔和写入次数将数据保存到磁盘,此处建议不保存,操作是将下面三句注释,将save "" 放开注释
save 900 1
save 300 10
save 60 10000
# The filename where to dump the DB
dbfilename dump.rdb
-->建议修改文件名,此处我修改为,dbfilename dump-7000.rdb
# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir ./
-->设置appendonly.aof、dump.rdb、nodes.conf(cluster)等文件的存放路径,建议设置为绝对路径,此处我修改为 dir /usr/local/redis/db/
################################### LIMITS ####################################
# Don't use more memory than the specified amount of bytes.
# When the memory limit is reached Redis will try to remove keys
# according to the eviction policy selected (see maxmemory-policy).
#
# If Redis can't remove keys according to the policy, or if the policy is
# set to 'noeviction', Redis will start to reply with errors to commands
# that would use more memory, like SET, LPUSH, and so on, and will continue
# to reply to read-only commands like GET.
#
# This option is usually useful when using Redis as an LRU cache, or to set
# a hard memory limit for an instance (using the 'noeviction' policy).
#
# WARNING: If you have slaves attached to an instance with maxmemory on,
# the size of the output buffers needed to feed the slaves are subtracted
# from the used memory count, so that network problems / resyncs will
# not trigger a loop where keys are evicted, and in turn the output
# buffer of slaves is full with DELs of keys evicted triggering the deletion
# of more keys, and so forth until the database is completely emptied.
#
# In short... if you have slaves attached it is suggested that you set a lower
# limit for maxmemory so that there is some free RAM on the system for slave
# output buffers (but this is not needed if the policy is 'noeviction').
#
# maxmemory <bytes>
-->根据服务器内存情况酌情分配maxmemory,由于我的服务器内存是8G,已使用3G多,剩余4G多,并有六个redis实例需要分配,故,我打算使用3.6G均分到六个实例作为redis的内存分配,此处我修改为maxmemory 614mb
############################## APPEND ONLY MODE ###############################
# By default Redis asynchronously dumps the dataset on disk. This mode is
# good enough in many applications, but an issue with the Redis process or
# a power outage may result into a few minutes of writes lost (depending on
# the configured save points).
#
# The Append Only File is an alternative persistence mode that provides
# much better durability. For instance using the default data fsync policy
# (see later in the config file) Redis can lose just one second of writes in a
# dramatic event like a server power outage, or a single write if something
# wrong with the Redis process itself happens, but the operating system is
# still running correctly.
#
# AOF and RDB persistence can be enabled at the same time without problems.
# If the AOF is enabled on startup Redis will load the AOF, that is the file
# with the better durability guarantees.
#
# Please check http://redis.io/topics/persistence for more information.
appendonly no
-->修改为yes,
改为
appendonly yes,
搭建cluster集群重要参数
# The name of the append only file (default: "appendonly.aof")
appendfilename
"appendonly.aof"
-->修改文件名,改为appendonly-7000.aof
################################ 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
-->启用或停用集群,
放开注释,搭建cluster集群重要参数
# Every cluster node has a cluster configuration file. This file is not
# intended to be edited by hand. It is created and updated by Redis nodes.
# Every Redis Cluster node requires a different cluster configuration file.
# Make sure that instances running in the same system do not have
# overlapping cluster configuration file names.
#
# cluster-config-file nodes-6379.conf
-->集群节点配置文件,放开注释,并修改节点配置文件名,此处改为,cluster-config-file nodes-7000.conf,
搭建cluster集群重要参数
# Cluster node timeout is the amount of milliseconds a node must be unreachable
# for it to be considered in failure state.
# Most other internal time limits are multiple of the node timeout.
#
# cluster-node-timeout 15000
-->集群节点最大的时间量,此处我改为cluster-node-timeout 5000,
搭建cluster集群重要参数
|
保存退出,其他五个配置文件也按照此方法修改
4.设置防火墙,此处开启对内网的端口开放
$ vim /etc/sysconfig/iptables
添加一条命令,-A INPUT -p tcp -s 172.17.0.0/16 -m multiport --dport 7000,7001,7002,7003,7004,7005 -j ACCEPT,保存退出,重启iptables,命令是
$ service iptables restart
第三步,启动redis
1.为了能确保redis集群正常启动,需要依赖一些环境,这里一并下载安装,命令是:
$ yum install -y ruby rubygems
$ gem install redis
2. 创建集群, 默认前三台为主节点,后三台为从节点. 选项 --replicas 1 表示我们希望为集群中的每个主节点创建一个从节点
$ ./redis-trib.rb create --replicas 1 127.0.0.1:7000 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 -->此处ip不建议使用回送地址127.0.0.1或是本地主机localhost,为避免非本机连接使用redis报连接错误,建议直接使用ip
$ yes
到此redis集群搭建完成