Redis集群安装

来源:互联网 发布:mac重装系统无法验证 编辑:程序博客网 时间:2024/06/06 15:50

Redis集群安装

  1. 一. Redis是什么

Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。 它支持多种类型的数据结构,如 字符串(strings), 散列(hashes), 列表(lists), 集合(sets), 有序集合(sorted sets) 与范围查询, bitmaps, hyperloglogs 和 地理空间(geospatial) 索引半径查询。 Redis 内置了 复制(replication),LUA脚本(Lua scripting), LRU驱动事件(LRU eviction),事务(transactions) 和不同级别的 磁盘持久化(persistence), 并通过 Redis哨兵(Sentinel)和自动 分区(Cluster)提供高可用性(high availability)。

  1. 下载
    从官网下载最新版本https://redis.io/download
$ tar xzf redis-3.2.6.tar.gz$ cd redis-3.2.6$ make

可能遇到问题
安装make
1、

  CC adlist.o
  /bin/sh: cc: command not found
  make[1]: * [adlist.o] Error 127
  make[1]: Leaving directory `/usr/local/redis-3.2.1/src
  make: * [all] Error 2

 解决办法:GCC没有安装或版本不对,安装一下

yum install gcc
2、

  zmalloc.h:50:31:
  error: jemalloc/jemalloc.h: No such file or directory
  zmalloc.h:55:2: error:

  #error “Newer version of jemalloc required”
  make[1]: * [adlist.o] Error
  1
  make[1]: Leaving directory `/data0/src/redis-2.6.2/src
  make: * [all]
  Error 2

解决办法:原因是没有安装jemalloc内存分配器,可以安装jemalloc 或 直接 输入make MALLOC=libc  && make install

创建 Redis 节点
首先在 192.168.177.101 机器上 /mysoftware/redis-3.2.6 目录下创建 redis_cluster 目录

$ mkdir /mysoftware/redis-3.2.6/redis_cluster 
在 redis_cluster 目录下,创建名为7000、7001、7002、7003、7004、7005的目录,并将 redis.conf 拷贝到这六个目录中

$ mkdir 7000 7001 7002$ cp /usr/local/redis-3.2.6/redis.conf /usr/local/redis-3.2.6/redis_cluster/7000$ cp /usr/local/redis-3.2.6/redis.conf /usr/local/redis-3.2.6/redis_cluster/7001$ cp /usr/local/redis-3.2.6/redis.conf /usr/local/redis-3.2.6/redis_cluster/7002

分别修改这三个配置文件,修改如下内容

port                  7000                        //端口7000,7002,7003        bind                  本机ip                      //默认ip为127.0.0.1,需要改为其他节点机器可访问的ip,否则创建集群时无法访问对应的端口,无法创建集群daemonize             yes                         //redis后台运行pidfile               /var/run/redis_7000.pid     //pidfile文件对应7000,7001,7002cluster-enabled       yes                         //开启集群,把注释#去掉cluster-config-file   nodes_7000.conf             //集群的配置,配置文件首次启动自动生成 7000,7001,7002cluster-node-timeout  15000                       //请求超时,默认15秒,可自行设置appendonly            yes                         //aof日志开启,有需要就开启,它会每次写操作都记录一条日志 

接着在另外两台机器上(192.168.1.9、192.168.1.10)重复以上三步,只是把目录改为7003、7004、7005、7006、7007、7008对应的配置文件也按照这个规则修改即可

启动各个节点
第一台机器上执行

$ /mysoftware/redis-3.2.6/src/redis-server /mysoftware/redis-3.2.6/redis_cluster/7000/redis.conf$ /mysoftware/redis-3.2.6/src/redis-server /mysoftware/redis-3.2.6/redis_cluster/7001/redis.conf$ /mysoftware/redis-3.2.6/src/redis-server /mysoftware/redis-3.2.6/redis_cluster/7002/redis.conf

第二台机器上执行

$ /mysoftware/redis-3.2.6/src/redis-server /mysoftware/redis-3.2.6/redis_cluster/7003/redis.conf$ /mysoftware/redis-3.2.6/src/redis-server /mysoftware/redis-3.2.6/redis_cluster/7004/redis.conf$ /mysoftware/redis-3.2.6/src/redis-server /usr/local/redis-3.2.6/redis_cluster/7005/redis.conf 

第三台机器上执行

$ /mysoftware/redis-3.2.6/src/redis-server /mysoftware/redis-3.2.6/redis_cluster/7006/redis.conf$ /mysoftware/redis-3.2.6/src/redis-server /mysoftware/redis-3.2.6/redis_cluster/7007/redis.conf$ /mysoftware/redis-3.2.6/src/redis-server /usr/local/redis-3.2.6/redis_cluster/7008/redis.conf 

检查各 Redis 启动情况
第一台机器

$ ps -ef | grep redis           //redis是否启动成功$ netstat -tnlp | grep redis    //监听redis端口
hadoop@slave01:/mysoftware/redis-4.0.1$ ps -aux | grep redisredis      819  0.2  0.6  40140  6076 ?        Ssl  21:24   0:21 /usr/bin/redis-sentinel 127.0.0.1:26379 [sentinel]redis      820  0.1  0.6  40140  6548 ?        Ssl  21:24   0:15 /usr/bin/redis-server 127.0.0.1:6379hadoop    1346  0.1  0.3  41724  4012 ?        Ssl  23:49   0:00 src/redis-server 192.168.177.101:7000 [cluster]hadoop    1351  0.0  0.4  41724  4128 ?        Ssl  23:50   0:00 src/redis-server 192.168.177.101:7001 [cluster]hadoop    1358  0.0  0.4  41724  4136 ?        Ssl  23:52   0:00 src/redis-server 192.168.177.101:7002 [cluster]hadoop    1363  0.2  0.3  41724  3956 ?        Ssl  23:52   0:00 src/redis-server 192.168.177.101:7003 [cluster]hadoop    1368  0.1  0.3  41724  3920 ?        Ssl  23:52   0:00 src/redis-server 192.168.177.101:7004 [cluster]hadoop    1373  0.0  0.4  41724  4088 ?        Ssl  23:52   0:00 src/redis-server 192.168.177.101:7005 [cluster]hadoop    1378  0.0  0.0  14228   976 pts/3    S+   23:52   0:00 grep --color=auto redis安装 Ruby$ sudo apt-get install ruby$ gem install -l redis-4.0.0.gemredis-4.0.0.gem下载地址http://www.redis.cn/download.html注:创建集群时需要安装 Ruby 运行redis-trib.rb**创建集群**hadoop@slave01:/mysoftware/redis-4.0.1$ /mysoftware/redis-4.0.1/src/redis-trib.rb  create  --replicas 1 192.168.177.101:7000 192.168.177.101:7001 192.168.177.101:7002 192.168.177.101:7003 192.168.177.101:7004 192.168.177.101:7005>>> Creating cluster>>> Performing hash slots allocation on 6 nodes...Using 3 masters:192.168.177.101:7006192.168.177.101:7008192.168.177.101:7009Adding replica 192.168.177.101:7010 to 192.168.177.101:7006Adding replica 192.168.177.101:7011 to 192.168.177.101:7008Adding replica 192.168.177.101:7012 to 192.168.177.101:7009M: 7f88fb4b00a5789af37cbcf4e2c66cb514df2e24 192.168.177.101:7006   slots:0-5460 (5461 slots) masterM: 06ee46eb5c0e9232f050d27c6c37674e3d25bdf7 192.168.177.101:7008   slots:5461-10922 (5462 slots) masterM: 65cada26e0a3e0c6c1b6af245642d9f67ba7803f 192.168.177.101:7009   slots:10923-16383 (5461 slots) masterS: b8227670d690efbf9412375accc4474fb015eb26 192.168.177.101:7010   replicates 7f88fb4b00a5789af37cbcf4e2c66cb514df2e24S: 87b3f190bbab8f9bf8017820aa335ceb1221653c 192.168.177.101:7011   replicates 06ee46eb5c0e9232f050d27c6c37674e3d25bdf7S: f6641bc4a7088fb9be8347c8d01a04ae94ef9d1b 192.168.177.101:7012   replicates 65cada26e0a3e0c6c1b6af245642d9f67ba7803fCan 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 clusterWaiting for the cluster to join.....>>> Performing Cluster Check (using node 192.168.177.101:7006)M: 7f88fb4b00a5789af37cbcf4e2c66cb514df2e24 192.168.177.101:7006   slots:0-5460 (5461 slots) master   1 additional replica(s)S: f6641bc4a7088fb9be8347c8d01a04ae94ef9d1b 192.168.177.101:7012   slots: (0 slots) slave   replicates 65cada26e0a3e0c6c1b6af245642d9f67ba7803fM: 06ee46eb5c0e9232f050d27c6c37674e3d25bdf7 192.168.177.101:7008   slots:5461-10922 (5462 slots) master   1 additional replica(s)M: 65cada26e0a3e0c6c1b6af245642d9f67ba7803f 192.168.177.101:7009   slots:10923-16383 (5461 slots) master   1 additional replica(s)S: 87b3f190bbab8f9bf8017820aa335ceb1221653c 192.168.177.101:7011   slots: (0 slots) slave   replicates 06ee46eb5c0e9232f050d27c6c37674e3d25bdf7S: b8227670d690efbf9412375accc4474fb015eb26 192.168.177.101:7010   slots: (0 slots) slave   replicates 7f88fb4b00a5789af37cbcf4e2c66cb514df2e24[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.在机器上连接集群的7000节点,打开另一个连接7004节点,连接方式为:##加参数 -C 可连接到集群,因为 redis.conf 将 bind 改为了ip地址,所以 -h 参数不可以省略,-p 参数为端口号$ /mysoftware/redis-3.2.6/src/redis-cli -h 192.168.1.8 -c -p 70007004节点执行命令:192.168.177.101:7004> set name redis然后在另两台70007007端口,查看 key 为 name 的内容192.168.1.8:7000> get name出现结果 redis

集群完毕