并且安装,开始部署之前

务必先看第6条 1.从官网下载和编译redis安装包 $ wget,开始部署之前,2.1 安装redis,并且安装

redis集群部署以及一些坑

开始部署之前,务必先看第6条

1.从官网下载和编译redis安装包

 $ wget http://download.redis.io/releases/redis-3.2.5.tar.gz  $ tar xzf redis-3.2.5.tar.gz $ cd redis-3.2.5 $ make

2.配置redis

 port 7000  cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes

如果你不想做持久化的话,appendonly yes -> appendonly no

假设我们部署3个redis nodes, 那么需要创建6个redis实例,其中3个node为另外3个nodes的slave;

 mkdir cluster-test cd cluster-test mkdir 7000 7001 7002 7003 7004 7005

保证这6个目录下的redis.conf的port 分别为7000~7005 

3.启动所有redis实例

分别进入六个目录,启动相应的redis-server

cd 7000../redis-server ./redis.conf

4.安装redis-trib.rb运行环境

 redis-trib.rb是ruby代码,所以先要安装ruby

 yum install ruby  gem install redis -- 安装redis依赖

因为墙的问题,gem install redis 可能会失败,改用国内的源

 gem sources --remove https://rubygems.org/ 删掉原来的源 gem sources -a http://gems.ruby-china.org  改用国内的源  gem sources -l 查看现有的源  gem install redis 安装redis依赖

5.创建集群

 ./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 

6.注意的地方

(1) 使用redis-trib.rb构建集群,完成前不要配置密码

(2) 集群构建完再通过config set + config rewrite命令逐个实例设置密码

(3) 对集群设置密码,requirepass和masterauth都需要设置

(4) 各个节点密码都必须一致,否则Redirected就会失败

 config set masterauth  yourpasswd  config set requirepass  yourpasswd auth yourpasswd config rewrite

(5) 如果提示一下错误,那么是redis依赖版本错误,移除当前redis依赖,安装指定版本即可

/usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis/connection/ruby.rb:111:in `rescue in _write_to_socket': Connection timed out (Redis::TimeoutError)     from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis/connection/ruby.rb:104:in `_write_to_socket'     from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis/connection/ruby.rb:131:in `block in write'     from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis/connection/ruby.rb:130:in `loop'     from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis/connection/ruby.rb:130:in `write'     from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis/connection/ruby.rb:374:in `write'     from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis/client.rb:271:in `block in write'     from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis/client.rb:250:in `io'     from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis/client.rb:269:in `write'     from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis/client.rb:228:in `block (3 levels) in process'     from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis/client.rb:222:in `each'     from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis/client.rb:222:in `block (2 levels) in process'     from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis/client.rb:367:in `ensure_connected'     from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis/client.rb:221:in `block in process'     from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis/client.rb:306:in `logging'     from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis/client.rb:220:in `process'     from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis/client.rb:120:in `call'     from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis.rb:2705:in `block in method_missing'     from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis.rb:58:in `block in synchronize'     from /usr/local/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'     from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis.rb:58:in `synchronize'     from /usr/local/lib/ruby/gems/2.3.0/gems/redis-3.3.2/lib/redis.rb:2704:in `method_missing'     from ./redis-trib.rb:212:in `flush_node_config'     from ./redis-trib.rb:776:in `block in flush_nodes_config'     from ./redis-trib.rb:775:in `each'     from ./redis-trib.rb:775:in `flush_nodes_config'     from ./redis-trib.rb:1296:in `create_cluster_cmd'     from ./redis-trib.rb:1701:in ` 

那是gem 删除 redis-3.3.2 插件版本,改用3.0.0

gem list

 gem uninstall redis --version 3.3.2

 gem install redis --version 3.0.0

 gem list

开始部署之前,务必先看第6条 1.从官网下载和编译redis安装包 $ wget
$ tar…

部署哨兵之前需要了解的基本事情

  • 1.一个健壮的部署至少需要三个哨兵实例,并且使用奇数个Sentinel.
  • 2.三个哨兵实例应该放置在客户使用独立方式确认故障的计算机或虚拟机中。例如不同的物理机或不同可用区域的虚拟机。
  • 3.sentinel +
    Redis实例不保证在故障期间保留确认的写入,因为Redis使用异步复制。然而有方式部署哨兵使丢失数据限制在特定时刻,虽然有更安全的方式部署它。
  • 4.你的客户端要支持哨兵,流行的客户端都支持哨兵,但不是全部。
  • 5.没有HA设置是安全的,如果你不经常的在开发环境测试,在生产环境他们会更好。你可能会有一个明显的错误配置只是当太晚的时候。
  • 6.Sentinel,Docker,或者其他形式的网络地址交换或端口映射需要加倍小心:Docker执行端口重新映射,破坏Sentinel自动发现其他的哨兵进程和master的slave列表。稍后在这个文档里检查关于Sentinel和Docker的部分,了解更多信息。

1.redis环境:

  • 操作系统:阿里云Centos 6.8
  • Redis版本:Redis-3.2.9
角色 IP地址 端口号
Redis Master 10.169.28.143 6379
Redis Slave01 10.28.10.103 6379
Redis Slave02 10.174.238.225 6379
Redis Sentinel 10.169.28.143 26379
Redis Sentinel 10.28.10.103 26379
Redis Sentinel 10.174.238.225 26379

2.安装Redis主从系统:

从官网下载redis-3.2.9,并且安装。
2.1 安装redis

$ wget
http://download.redis.io/releases/redis-3.2.9.tar.gz
$ tar xzf redis-3.2.9.tar.gz
$ cd redis-3.2.9
$ make && make install

2.2 配置master上的redis.conf

bind 10.169.28.143
port 6379
daemonize yes //启动redis后台运行
masterauth 123456
//master的密码,如果redis设置了密码,主从密码要设置成一样的。
requirepass 123456
logfile “/var/log/redis/redis.log”

2.3 配置slave上的redis.conf。另外一个类似

bind 10.28.10.103
port 6379
daemonize yes //启动redis后台运行
slave 10.169.28.143 6379
masterauth 123456
//master的密码,如果redis设置了密码,主从密码要设置成一样的。
requirepass 123456
logfile “/var/log/redis/redis.log”

2.4 配置完成以后测试一下主从是否同步正确。
依此启动master redis和slave redis
启动方法:

$ ./src/redis-server redis.conf

在mster上创建:

$ redis-cli -h 10.169.28.143 -p 6379 -a 123456
10.169.28.143:6379> set name abc
OK
10.169.28.143:6379> get name
“abc”

在从库上面查看:

$ redis-cli -h 10.28.10.103 -p 6379 -a 123456
10.28.10.103:6379>get name
“abc”