redis cluster模式搭建


搭建过程中遇到很多问题,特此记录,以备后用

下载

从官网下载,地址:http://download.redis.io/releases/redis-4.0.1.tar.gz,可自主选择版本,本文所用版本为4.0.1
选择安装路径,本文是在/home/redis/redis-cluster/路径下安装

[root@localhost ~]# cd /home/redis/redis-cluster/
[root@localhost redis-cluster]# wget http://download.redis.io/releases/redis-4.0.1.tar.gz
[root@localhost redis-cluster]# tar -zxvf redis-4.0.1.tar.gz 
[root@localhost redis-cluster]# cd redis-4.0.1
[root@localhost redis-4.0.1]# make && make install

到此下载安装已经完成

配置

配置redis.conf文件

	# 监听地址
	bind 127.0.0.1
	# 端口
	port 27001
	
	tcp-backlog 511
	
	timeout 0
	
	tcp-keepalive 300
	# 守护进程
	daemonize yes
	
	supervised no
	
	pidfile /var/run/redis_27001.pid
	
	loglevel notice
	
	logfile "./27001/redis.log"
	
	databases 16
	
	always-show-logo yes
	
	save 900 1
	save 300 10
	save 60 10000
	
	stop-writes-on-bgsave-error yes
	
	rdbcompression yes
	
	rdbchecksum yes
	# 只允许为文件,不允许为路径,否则会报错
	dbfilename dump-27001.rdb
	
	dir ./
	# 和master之间通讯的密码
	masterauth RedisPASS
	
	slave-serve-stale-data yes
	
	slave-read-only yes
	
	repl-diskless-sync no
	
	repl-diskless-sync-delay 5
	
	repl-disable-tcp-nodelay no
	
	slave-priority 100
	# 自身安全所需密码
	requirepass RedisPASS
	
	lazyfree-lazy-eviction no
	lazyfree-lazy-expire no
	lazyfree-lazy-server-del no
	slave-lazy-flush no
	
	appendonly no
	# 只允许为文件,不允许为路径,否则会报错
	appendfilename "appendonly-27001.aof"
	
	appendfsync everysec
	
	no-appendfsync-on-rewrite no
	
	auto-aof-rewrite-percentage 100
	auto-aof-rewrite-min-size 64mb
	
	aof-load-truncated yes
	
	aof-use-rdb-preamble no
	
	lua-time-limit 5000
	# 下面三个为cluster模式所必须的配置
	# 开启集群模式
	cluster-enabled yes
	# 集群的配置文件
	cluster-config-file 27001/nodes-27001.conf
	# 集群之间心跳超时时间
	cluster-node-timeout 10000
	# 非必须,用来设置每个master有多少个slave,默认为1
	cluster-migration-barrier 2
	
	slowlog-max-len 128
	
	latency-monitor-threshold 0
	
	notify-keyspace-events ""
	
	hash-max-ziplist-entries 512
	hash-max-ziplist-value 64
	
	list-max-ziplist-size -2
	
	list-compress-depth 0
	
	set-max-intset-entries 512
	
	zset-max-ziplist-entries 128
	zset-max-ziplist-value 64
	
	hll-sparse-max-bytes 3000
	
	activerehashing yes
	
	client-output-buffer-limit normal 0 0 0
	client-output-buffer-limit slave 256mb 64mb 60
	client-output-buffer-limit pubsub 32mb 8mb 60
	
	hz 10
	
	aof-rewrite-incremental-fsync yes

上面是配置文件的内容,如果想要优化,请结合具体情况

准备节点

因为条件所限,只有一台机器,所以就采用一台机器多个端口的方式实现,所以配置文件中所有的文件及路径全部带上端口以示区别

# 创建文件夹
[root@localhost redis-4.0.1] mkdir 27001 27002 27003 27004 27005 27006
# 拷贝redis.conf到各个文件夹下
[root@localhost redis-4.0.1] cp redis.conf 27001/redis.conf 
[root@localhost redis-4.0.1] cp redis.conf 27002/redis.conf 
[root@localhost redis-4.0.1] cp redis.conf 27003/redis.conf 
[root@localhost redis-4.0.1] cp redis.conf 27004/redis.conf 
[root@localhost redis-4.0.1] cp redis.conf 27005/redis.conf 
[root@localhost redis-4.0.1] cp redis.conf 27006/redis.conf
# 启动redis各个节点
[root@localhost redis-4.0.1] redis-server 27001/redis.conf 
[root@localhost redis-4.0.1] redis-server 27002/redis.conf 
[root@localhost redis-4.0.1] redis-server 27003/redis.conf 
[root@localhost redis-4.0.1] redis-server 27004/redis.conf 
[root@localhost redis-4.0.1] redis-server 27005/redis.conf 
[root@localhost redis-4.0.1] redis-server 27006/redis.conf 
# 连接redis节点
[root@localhost redis-4.0.1] redis-cli -c -h 10.3.50.182 -p 27001 -a RedisPass
# 报错,   信息如下:
Could not connect to Redis at 10.3.50.182:27001: Connection refused
Could not connect to Redis at 10.3.50.182:27001: Connection refused
not connected>

exit退出
经检查发现是redis.conf中bind配置错误,监听地址应为10.3.50.182,遂修改之(六个文件夹中的redis.conf全部修改,头疼。所以需要仔细检查一遍之后再拷贝)。

[root@localhost redis-4.0.1]# redis-cli -c -h 10.3.50.182 -p 27001 -a RedisPass
10.3.50.182:27001>

此时便可以正常登陆

cluster模式的坑

登陆之后,尝试操作数据,发现还是不行,报错信息如下:

[root@localhost redis-4.0.1] redis-cli -c -h 10.3.50.182 -p 27001 -a RedisPass
10.3.50.182:27001> set key value
(error) CLUSTERDOWN Hash slot not served
10.3.50.182:27001> get key
(error) CLUSTERDOWN Hash slot not served

百度之后,认为是缺少ruby语言插件

[root@localhost redis-4.0.1] ruby -v
-bash: ruby: 未找到命令
# 发现果然没有,遂安装
[root@localhost redis-4.0.1] yum install ruby
.
.
.
完毕!
[root@localhost redis-4.0.1]# ruby -v
ruby 2.0.0p648 (2015-12-16) [x86_64-linux]

安装成功,显示版本为2.0.0,记住它
重新连接redis,操作数据测试

[root@localhost redis-4.0.1] redis-cli -c -h 10.3.50.182 -p 27001 -a RedisPass
10.3.50.182:27001> set key value
(error) CLUSTERDOWN Hash slot not served
10.3.50.182:27001> get key
(error) CLUSTERDOWN Hash slot not served

还是不行,继续百度,发现需要使用redis-trib.rb构建集群,命令使用如下:

# 首先一定要进入redis目录下的src目录
[root@localhost redis-4.0.1]# cd src/
[root@localhost src]# ./redis-trib.rb create --replicas 1 10.3.50.182:27001 10.3.50.182:27002 10.3.50.182:27003 10.3.50.182:27004 10.3.50.182:27005 10.3.50.182:27006
/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:in `require'
	from ./redis-trib.rb:25:in `<main>'

还是报错,很气。又是百度,是没装redis的第三方接口!既然ruby程序要访问redis数据库,总得有个连接接口。于是开始安装第三方接口。

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

继续报错… 这时候不用百度也会发现是版本不支持。而Centos7对ruby的支持只到2.0.0,也就是刚刚记住的那个点。没办法又是一系列命令

[root@localhost src]# yum install curl
.
.
.
完毕!
[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    210      0 --:--:-- --:--:-- --:--:--   210
100 24361  100 24361    0     0   5874      0  0:00:04  0:00:04 --:--:-- 11093
Downloading https://github.com/rvm/rvm/archive/1.29.4.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.4/1.29.4.tar.gz.asc
gpg: 已创建目录‘/root/.gnupg’
gpg: 新的配置文件‘/root/.gnupg/gpg.conf’已建立
gpg: 警告:在‘/root/.gnupg/gpg.conf’里的选项于此次运行期间未被使用
gpg: 钥匙环‘/root/.gnupg/pubring.gpg’已建立
gpg: 于 2018年07月02日 星期一 03时41分26秒 CST 创建的签名,使用 RSA,钥匙号 BF04FF17
gpg: 无法检查签名:没有公钥
Warning, RVM 1.26.0 introduces signed releases and automated check of signatures when GPG software found. Assuming you trust Michal Papis import the mpapis public key (downloading the signatures).

GPG signature verification failed for '/usr/local/rvm/archives/rvm-1.29.4.tgz' - 'https://github.com/rvm/rvm/releases/download/1.29.4/1.29.4.tar.gz.asc'! Try to install GPG v2 and then fetch the public key:

    gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3

or if it fails:

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

the key can be compared with:

    https://rvm.io/mpapis.asc
    https://keybase.io/mpapis

NOTE: GPG version 2.1.17 have a bug which cause failures during fetching keys from remote server. Please downgrade or upgrade to newer version (if available) or use the second method described above.
[root@localhost src]# rvm list known
-bash: rvm: 未找到命令

又双叒叕出现错误,而后发现上面显示 gpg: 无法检查签名:没有公钥

[root@localhost src] gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
[root@localhost src] curl -L get.rvm.io | bash -s stable
# 此时就没有了上面的显示信息
[root@localhost src] source /usr/local/rvm/scripts/rvm 
[root@localhost src] rvm list known
# 省略显示很多的版本信息
.
.
.
# 根据具体情况选择一个合适的版本,本文选择的是2.5.1
[root@localhost src] rvm install 2.5.1
# 漫长的等待......
.
.
.
# 终于成功的安装redis的第三方接口
[root@localhost src] gem install redis
# 继续构建集群
[root@localhost src]# ./redis-trib.rb create --replicas 1 10.3.50.182:27001 10.3.50.182:27002 10.3.50.182:27003 10.3.50.182:27004 10.3.50.182:27005 10.3.50.182:27006
>>> Creating cluster
[ERR] Sorry, can't connect to node 10.3.50.182:27001

还是报错,崩溃ing······
忽然想起来我的redis是配置了密码的,难道是这个原因

# 通过help,看看有没有配置密码的选项
[root@localhost src]# ./redis-trib.rb help
Usage: redis-trib <command> <options> <arguments ...>

  create          host1:port1 ... hostN:portN
                  --replicas <arg>
  check           host:port
  info            host:port
  fix             host:port
                  --timeout <arg>
  reshard         host:port
                  --from <arg>
                  --to <arg>
                  --slots <arg>
                  --yes
                  --timeout <arg>
                  --pipeline <arg>
  rebalance       host:port
                  --weight <arg>
                  --auto-weights
                  --use-empty-masters
                  --timeout <arg>
                  --simulate
                  --pipeline <arg>
                  --threshold <arg>
  add-node        new_host:new_port existing_host:existing_port
                  --slave
                  --master-id <arg>
  del-node        host:port node_id
  set-timeout     host:port milliseconds
  call            host:port command arg arg .. arg
  import          host:port
                  --from <arg>
                  --copy
                  --replace
  help            (show this help)

For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.

然鹅并没有,无奈,再次百度(感谢百度)

# 修改文件,加入密码
[root@localhost src]# vim /usr/local/rvm/gems/ruby-2.5.1/gems/redis-4.0.3/lib/redis/client.rb
class Redis
  class Client

    DEFAULTS = {
      :url => lambda { ENV["REDIS_URL"] },
      :scheme => "redis",
      :host => "127.0.0.1",
      :port => 6379,
      :path => nil,
      :timeout => 5.0,
      :password => "RedisPass",
      :db => 0,
      :driver => nil,
      :id => nil,
      :tcp_keepalive => 0,
      :reconnect_attempts => 1,
      :reconnect_delay => 0,
      :reconnect_delay_max => 0.5,
      :inherit_socket => false
    }

:password 后面的 nil 修改为 RedisPass,然后保存。

[root@localhost src]# ./redis-trib.rb create --replicas 1 10.3.50.182:27001 10.3.50.182:27002 10.3.50.182:27003 10.3.50.182:27004 10.3.50.182:27005 10.3.50.182:27006
.
.
.
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost src]# redis-cli -c -h 10.3.50.182 -p 27001 -a RedisPass
10.1.2.159:27001> set key value
-> Redirected to slot [15495] located at 10.1.2.159:27003
OK
10.1.2.159:27003> get key
"value"
10.1.2.159:27003> exit
[root@localhost src]# 

成功!

12月8号更新

后来我尝试在三台机器上搭建三个master,六个slave的集群。并使每台机器上有一个master和其他两个master的slave。
但是使用./redis-trib.rb create命令的时候,主从的分布又是完全随机的。

# 通过help,发现可以通过add-node增加节点
[root@localhost src]# ./redis-trib.rb help
Usage: redis-trib <command> <options> <arguments ...>

  create          host1:port1 ... hostN:portN
                  --replicas <arg>
  check           host:port
  info            host:port
  fix             host:port
                  --timeout <arg>
  reshard         host:port
                  --from <arg>
                  --to <arg>
                  --slots <arg>
                  --yes
                  --timeout <arg>
                  --pipeline <arg>
  rebalance       host:port
                  --weight <arg>
                  --auto-weights
                  --use-empty-masters
                  --timeout <arg>
                  --simulate
                  --pipeline <arg>
                  --threshold <arg>
  add-node        new_host:new_port existing_host:existing_port
                  --slave
                  --master-id <arg>
  del-node        host:port node_id
  set-timeout     host:port milliseconds
  call            host:port command arg arg .. arg
  import          host:port
                  --from <arg>
                  --copy
                  --replace
  help            (show this help)

For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.

通过./redis-trib.rb add-nodes指定为slave,并指定相应的master。那么问题就简单多了。

  1. 如果是还没有进行./redis-trib.rb create操作之前,可以创建的时候先只加入三个节点,那么这三个节点都是主节点。而后通过add-node一个个指定相应的master手动添加。(这个我只是理论推测,并没有实际操作)
  2. 如果已经做了create操作,还可以将不是理想状态的节点通过./redis-trib.rb del-node删除掉,然后再通过add-node指定相应的master添加。

命令实例如下:

# 删除节点命令
# ./src/redis-trib.rb del-node [host:port] [node_id]
./src/redis-trib.rb del-node 10.1.43.54:27002 8cf64ccdc65f443143d26eaf70cc1f639391d8a9

# 添加节点命令
# ./src/redis-trib.rb add-node --slave --master-id [master_node_id] [new_host:new_port] [existing_host:existing_port]
./src/redis-trib.rb add-node --slave --master-id a6bb9b4624692b332f6887d12f7707c9b13a398b 10.1.43.54:27002 10.1.43.55:27001

如上!

猜你喜欢

转载自blog.csdn.net/qq_39800434/article/details/84982657