资源规划

组件 bigdata-node1 bigdata-node2 bigdata-node3
OS centos7.6 centos7.6 centos7.6
Redis Redis:6001、Redis:6002 Redis:6003、Redis:6004 Redis:6005、Redis:6006

安装介质

版本:redis-5.0.8.tar.gz
下载:https://redis.io/download

环境准备

检查并确认已经安装gcc-4.8.5+版本。(CentOS7.6自带gcc-4.8.5)

  1. sudo yum install gcc

编译安装

  1. # root用户
  2. sudo su
  3. tar -zxvf /share/redis-5.0.8.tar.gz -C /usr/local/
  4. rm -rf /share/redis-5.0.8.tar.gz
  5. mv /usr/local/redis-5.0.8 /usr/local/redis
  6. cd /usr/local/redis
  7. make PREFIX=/usr/local/redis MALLOC=libc install

单机模式

启动Redis服务

前台启动模式

在前台直接运行,若Linux关闭,则Redis服务也随即关闭。Redis默认开启的是前端模式,端口:6379。

  1. # 方式1
  2. cd /usr/local/redis/bin
  3. ./redis-server
  4. # 方式2
  5. cd /usr/local/redis/src
  6. ./redis-server

后台启动模式

编译配置文件(编辑conf文件,将daemonize属性修改为yes,表明需要在后台运行):

  1. mkdir /usr/local/redis/conf
  2. vi /usr/local/redis/conf/redis.conf

配置如下:

  1. daemonize yes

后台启动:

  1. # 方式1
  2. cd /usr/local/redis
  3. ./bin/redis-server conf/redis.conf
  4. # 方式2
  5. cd /usr/local/redis
  6. ./src/redis-server conf/redis.conf

关闭Redis服务

  1. # 方式1
  2. /usr/local/redis/bin/redis-cli shutdown
  3. # 方式2
  4. /usr/local/redis/src/redis-cli shutdown
  5. # 强行终止(强行终止redis进程可能会导致redis持久化数据丢失。正确停止Redis的方式应该是向Redis发送SHUTDOWN命令)
  6. # 方式3
  7. pkill redis-server

验证

  1. # 方式1
  2. cd /usr/local/redis
  3. ./bin/redis-cli
  4. # 方式2
  5. cd /usr/local/redis
  6. ./src/redis-cli
  7. # redis操作
  8. redis> set foo bar
  9. redis> get foo
  10. # 查看Redis进程
  11. ps -ef | grep redis
  12. netstat -lntp | grep 6379
  13. netstat -nlt

设置开机自启动

  1. # root用户
  2. vi /etc/rc.local

配置如下:

  1. # 方式1
  2. /usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf
  3. # 方式2
  4. /usr/local/redis/src/redis-server /usr/local/redis/conf/redis.conf

远程端口配置

开启Redis远程端口配置后,代码访问Linux下的Redis需要开启6379端口。
修改Redis配置:

  1. vi /usr/local/redis/conf/redis.conf

配置如下:

  1. # 0.0.0.0表示不限制访问来源地址,如需限制,直接改为相应IP
  2. bind 0.0.0.0

防火墙设置(开启防火墙6379端口,在CentOS 7中防火墙由firewalld来管理,启用区域端口和协议组合):

  1. # 若防火墙是关闭状态,则无需如下配置
  2. # 1.启用(permanent永久生效,没有此参数重启后失效),成功执行返回“success”
  3. firewall-cmd --zone=public --add-port=6379/tcp --permanent
  4. # 2.重新载入,成功执行返回“success”
  5. firewall-cmd --reload
  6. # 3.查看端口是否开启,成功执行返回“yes”
  7. firewall-cmd --zone=public --query-port=6379/tcp
  8. # 补充:删除
  9. firewall-cmd --zone=public --remove-port=6379/tcp --permanent

伪分布式

方式1(redis-cli)

1. 创建空目录

  1. mkdir -p /usr/local/redis_cluster
  2. cd /usr/local/redis_cluster
  3. mkdir 6001 6002 6003 6004 6005 6006

2. 初始化配置

  1. cp /usr/local/redis/bin/redis-server /usr/local/redis_cluster/6001
  2. cp /usr/local/redis/conf/redis.conf /usr/local/redis_cluster/6001
  3. cp /usr/local/redis/bin/redis-server /usr/local/redis_cluster/6002
  4. cp /usr/local/redis/conf/redis.conf /usr/local/redis_cluster/6002
  5. cp /usr/local/redis/bin/redis-server /usr/local/redis_cluster/6003
  6. cp /usr/local/redis/conf/redis.conf /usr/local/redis_cluster/6003
  7. cp /usr/local/redis/bin/redis-server /usr/local/redis_cluster/6004
  8. cp /usr/local/redis/conf/redis.conf /usr/local/redis_cluster/6004
  9. cp /usr/local/redis/bin/redis-server /usr/local/redis_cluster/6005
  10. cp /usr/local/redis/conf/redis.conf /usr/local/redis_cluster/6005
  11. cp /usr/local/redis/bin/redis-server /usr/local/redis_cluster/6006
  12. cp /usr/local/redis/conf/redis.conf /usr/local/redis_cluster/6006

3. 修改配置

  1. vi /usr/local/redis_cluster/6001/redis.conf
  2. vi /usr/local/redis_cluster/6002/redis.conf
  3. vi /usr/local/redis_cluster/6003/redis.conf
  4. vi /usr/local/redis_cluster/6004/redis.conf
  5. vi /usr/local/redis_cluster/6005/redis.conf
  6. vi /usr/local/redis_cluster/6006/redis.conf

配置如下:

  1. # 设置端口号
  2. port 6001
  3. # 数据存放位置,必须指定
  4. dir /usr/local/redis_cluster/6001/
  5. # 启动集群模式
  6. cluster-enabled yes
  7. # 集群节点信息文件
  8. cluster-config-file nodes-6001.conf
  9. cluster-node-timeout 5000
  10. bind 0.0.0.0
  11. # 关闭保护模式
  12. protected-mode no
  13. # 开启AOF模式,默认的持久化RDB
  14. appendonly yes
  15. daemonize yes
  16. requirepass hadoopredis

4. 启动服务

  1. /usr/local/redis_cluster/6001/redis-server /usr/local/redis_cluster/6001/redis.conf
  2. /usr/local/redis_cluster/6002/redis-server /usr/local/redis_cluster/6002/redis.conf
  3. /usr/local/redis_cluster/6003/redis-server /usr/local/redis_cluster/6003/redis.conf
  4. /usr/local/redis_cluster/6004/redis-server /usr/local/redis_cluster/6004/redis.conf
  5. /usr/local/redis_cluster/6005/redis-server /usr/local/redis_cluster/6005/redis.conf
  6. /usr/local/redis_cluster/6006/redis-server /usr/local/redis_cluster/6006/redis.conf
  7. # 验证服务是否启动
  8. ps -ef |grep redis

5. 创建伪集群

  1. cd /usr/local/redis
  2. ./bin/redis-cli -a hadoopredis --cluster create 127.0.0.1:6001 127.0.0.1:6002 127.0.0.1:6003 127.0.0.1:6004 127.0.0.1:6005 127.0.0.1:6006 --cluster-replicas 1

参数说明:

  • -a:密码 如果redis.conf中配置了密码创建集群时需要添加
  • —cluster-replicas:从节点个数(需写在末尾,否则报错)

    方式2(create-cluster)

    redis-5.x版本,集群搭建不需要我们安装ruby就可以搭建成功,并且redis给我们提供了快速搭建脚本,执行utils/create-cluster目录下的create-cluster脚本 就可以快速搭建,该脚本提供以下几个命令供我们使用。

  • start:创建redis集群实例。

  • create:搭建redis集群。
  • stop:停止redis集群实例。
  • watch:显示集群节点(第一个节点)输出(前30行)。
  • tail :运行tail -f,基于port + ID。
  • clean:删除生成的日志,配置,数据文件。
  • clean-logs:只删除日志。

    1. 修改脚本

    1. vi /usr/local/redis/utils/create-cluster/create-cluster

    start部分修改

  • —bind:指定绑定的ip(服务器ip)(—bind 192.168.0.101)

  • —requirepass:指定密码(—requirepass hadoopredis)
  • —protected-mode no:关闭安全模式(外部应用可以连接到redis)(—protected-mode no)

    1. if [ "$1" == "start" ]
    2. then
    3. while [ $((PORT < ENDPORT)) != "0" ]; do
    4. PORT=$((PORT+1))
    5. echo "Starting $PORT"
    6. ../../src/redis-server --port $PORT --bind 192.168.0.101 --requirepass hadoopredis --protected-mode no --cluster-enabled yes --cluster-config-file nodes-${PORT}.conf --cluster-node-timeout $TIMEOUT --appendonly yes --appendfilename appendonly-${PORT}.aof --dbfilename dump-${PORT}.rdb --logfile ${PORT}.log --daemonize yes
    7. done
    8. exit 0
    9. fi

    create部分修改

  • HOSTS :指定绑定的ip(服务器ip)(**HOSTS=”$HOSTS 192.168.0.101:$PORT”**)

  • -a:指定密码 **-a hadoopredis)**

    1. if [ "$1" == "create" ]
    2. then
    3. HOSTS=""
    4. while [ $((PORT < ENDPORT)) != "0" ]; do
    5. PORT=$((PORT+1))
    6. HOSTS="$HOSTS 192.168.0.101:$PORT"
    7. done
    8. ../../src/redis-cli -a hadoopredis --cluster create $HOSTS --cluster-replicas $REPLICAS
    9. exit 0
    10. fi

    stop部分修改

  • -h:指定绑定的ip(服务器ip)(-h 192.168.0.101)

  • -a:指定密码(-a hadoopredis)

    1. if [ "$1" == "stop" ]
    2. then
    3. while [ $((PORT < ENDPORT)) != "0" ]; do
    4. PORT=$((PORT+1))
    5. echo "Stopping $PORT"
    6. ../../src/redis-cli -h 192.168.0.101 -p $PORT -a hadoopredis shutdown nosave
    7. done
    8. exit 0
    9. fi

    2. 启动服务

    1. cd /usr/local/redis/utils/create-cluster
    2. ./create-cluster start

    3. 创建集群

    1. cd /usr/local/redis/utils/create-cluster
    2. ./create-cluster create

    关闭Redis服务

    1. # 方式1
    2. /usr/local/redis/bin/redis-cli -a hadoopredis -c -p 6001 shutdown
    3. /usr/local/redis/bin/redis-cli -a hadoopredis -c -p 6002 shutdown
    4. /usr/local/redis/bin/redis-cli -a hadoopredis -c -p 6003 shutdown
    5. /usr/local/redis/bin/redis-cli -a hadoopredis -c -p 6004 shutdown
    6. /usr/local/redis/bin/redis-cli -a hadoopredis -c -p 6005 shutdown
    7. /usr/local/redis/bin/redis-cli -a hadoopredis -c -p 6006 shutdown
    8. # 方式2
    9. /usr/local/redis/src/redis-cli -a hadoopredis -c -p 6001 shutdown
    10. /usr/local/redis/src/redis-cli -a hadoopredis -c -p 6002 shutdown
    11. /usr/local/redis/src/redis-cli -a hadoopredis -c -p 6003 shutdown
    12. /usr/local/redis/src/redis-cli -a hadoopredis -c -p 6004 shutdown
    13. /usr/local/redis/src/redis-cli -a hadoopredis -c -p 6005 shutdown
    14. /usr/local/redis/src/redis-cli -a hadoopredis -c -p 6006 shutdown
    15. # 方式3
    16. pkill redis-server
    17. # 方式4(仅适用于create-cluster方式创建)
    18. cd /usr/local/redis/utils/create-cluster
    19. ./create-cluster stop

    验证

    1. cd /usr/local/redis
    2. # 注意IP要和配置中保持一致
    3. ./bin/redis-cli -a hadoopredis -h 127.0.0.1 -p 6001 -c
    4. ./bin/redis-cli -a hadoopredis -h 192.168.0.101 -p 30001 -c
    5. # 查看集群状态
    6. redis>> cluster info
    7. # 查看主从关系
    8. redis>> cluster nodes

    集群模式

    官方推荐集群至少需要六个节点,即三主三从。六个节点的配置文件基本相同,只需要修改端口号。由于Redis cluster是redis官方提供的,目前官方一直都在维护中,具有代表性,建议生产使用

    1. 创建空目录

    1. mkdir -p /usr/local/redis_cluster
    2. cd /usr/local/redis_cluster
    3. # bigdata-node1
    4. mkdir 6001 6002
    5. # bigdata-node2
    6. mkdir 6003 6004
    7. # bigdata-node3
    8. mkdir 6005 6006

    2. 初始化配置

    1. mkdir /usr/local/redis/conf
    2. vi /usr/local/redis/conf/redis.conf

    配置如下:

    1. daemonize yes

    拷贝及分发配置:

    1. # bigdata-node1
    2. cp /usr/local/redis/bin/redis-server /usr/local/redis_cluster/6001
    3. cp /usr/local/redis/conf/redis.conf /usr/local/redis_cluster/6001
    4. cp /usr/local/redis/bin/redis-server /usr/local/redis_cluster/6002
    5. cp /usr/local/redis/conf/redis.conf /usr/local/redis_cluster/6002
    6. # bigdata-node2
    7. scp -r /usr/local/redis/bin/redis-server root@bigdata-node2:/usr/local/redis_cluster/6003/
    8. scp -r /usr/local/redis/conf/redis.conf root@bigdata-node2:/usr/local/redis_cluster/6003/
    9. scp -r /usr/local/redis/bin/redis-server root@bigdata-node2:/usr/local/redis_cluster/6004/
    10. scp -r /usr/local/redis/conf/redis.conf root@bigdata-node2:/usr/local/redis_cluster/6004/
    11. # bigdata-node3
    12. scp -r /usr/local/redis/bin/redis-server root@bigdata-node3:/usr/local/redis_cluster/6005/
    13. scp -r /usr/local/redis/conf/redis.conf root@bigdata-node3:/usr/local/redis_cluster/6005/
    14. scp -r /usr/local/redis/bin/redis-server root@bigdata-node3:/usr/local/redis_cluster/6006/
    15. scp -r /usr/local/redis/conf/redis.conf root@bigdata-node3:/usr/local/redis_cluster/6006/

    3. 修改配置

    1. # bigdata-node1
    2. vi /usr/local/redis_cluster/6001/redis.conf
    3. vi /usr/local/redis_cluster/6002/redis.conf
    4. # bigdata-node2
    5. vi /usr/local/redis_cluster/6003/redis.conf
    6. vi /usr/local/redis_cluster/6004/redis.conf
    7. # bigdata-node3
    8. vi /usr/local/redis_cluster/6005/redis.conf
    9. vi /usr/local/redis_cluster/6006/redis.conf

    配置如下:

    1. # 设置端口号
    2. port 6000
    3. # 数据存放位置,必须指定
    4. dir /usr/local/redis_cluster/6000/
    5. # 启动集群模式
    6. cluster-enabled yes
    7. # 集群节点信息文件
    8. cluster-config-file nodes-6000.conf
    9. cluster-node-timeout 5000
    10. # 默认ip为127.0.0.1 需要改为其他节点机器可访问的ip 否则创建集群时无法访问对应的端口,无法创建集群
    11. bind 192.168.0.101
    12. # 关闭保护模式
    13. protected-mode no
    14. # 开启AOF模式(它会每次写操作都记录一条日志 ),默认的持久化RDB
    15. appendonly yes
    16. # redis后台运行
    17. daemonize yes
    18. # 设置密码
    19. requirepass hadoopredis
    20. # pidfile文件
    21. pidfile /var/run/redis_6000.pid
  • 6001(# bigdata-node1

    1. port 6001
    2. dir /usr/local/redis_cluster/6001/
    3. cluster-enabled yes
    4. cluster-config-file nodes-6001.conf
    5. cluster-node-timeout 5000
    6. bind 192.168.0.101
    7. protected-mode no
    8. appendonly yes
    9. daemonize yes
    10. requirepass hadoopredis
    11. pidfile /var/run/redis-6001.pid
  • 6002(# bigdata-node1

    1. port 6002
    2. dir /usr/local/redis_cluster/6002/
    3. cluster-enabled yes
    4. cluster-config-file nodes-6002.conf
    5. cluster-node-timeout 5000
    6. bind 192.168.0.101
    7. protected-mode no
    8. appendonly yes
    9. daemonize yes
    10. requirepass hadoopredis
    11. pidfile /var/run/redis-6002.pid
  • 6003(# bigdata-node2

    1. port 6003
    2. dir /usr/local/redis_cluster/6003/
    3. cluster-enabled yes
    4. cluster-config-file nodes-6003.conf
    5. cluster-node-timeout 5000
    6. bind 192.168.0.102
    7. protected-mode no
    8. appendonly yes
    9. daemonize yes
    10. requirepass hadoopredis
    11. pidfile /var/run/redis-6003.pid
  • 6004(# bigdata-node2

    1. port 6004
    2. dir /usr/local/redis_cluster/6004/
    3. cluster-enabled yes
    4. cluster-config-file nodes-6004.conf
    5. cluster-node-timeout 5000
    6. bind 192.168.0.102
    7. protected-mode no
    8. appendonly yes
    9. daemonize yes
    10. requirepass hadoopredis
    11. pidfile /var/run/redis-6004.pid
  • 6005(# bigdata-node3

    1. port 6005
    2. dir /usr/local/redis_cluster/6005/
    3. cluster-enabled yes
    4. cluster-config-file nodes-6005.conf
    5. cluster-node-timeout 5000
    6. bind 192.168.0.103
    7. protected-mode no
    8. appendonly yes
    9. daemonize yes
    10. requirepass hadoopredis
    11. pidfile /var/run/redis-6005.pid
  • 6006(# bigdata-node3

    1. port 6006
    2. dir /usr/local/redis_cluster/6006/
    3. cluster-enabled yes
    4. cluster-config-file nodes-6006.conf
    5. cluster-node-timeout 5000
    6. bind 192.168.0.103
    7. protected-mode no
    8. appendonly yes
    9. daemonize yes
    10. requirepass hadoopredis
    11. pidfile /var/run/redis-6006.pid

    4. 启动服务

    1. # bigdata-node1
    2. /usr/local/redis_cluster/6001/redis-server /usr/local/redis_cluster/6001/redis.conf
    3. /usr/local/redis_cluster/6002/redis-server /usr/local/redis_cluster/6002/redis.conf
    4. # bigdata-node2
    5. /usr/local/redis_cluster/6003/redis-server /usr/local/redis_cluster/6003/redis.conf
    6. /usr/local/redis_cluster/6004/redis-server /usr/local/redis_cluster/6004/redis.conf
    7. # bigdata-node3
    8. /usr/local/redis_cluster/6005/redis-server /usr/local/redis_cluster/6005/redis.conf
    9. /usr/local/redis_cluster/6006/redis-server /usr/local/redis_cluster/6006/redis.conf
    10. # 验证服务是否启动
    11. ps -ef |grep redis
    12. netstat -tnlp | grep redis

    5. 创建集群

    1. cd /usr/local/redis
    2. ./bin/redis-cli -a hadoopredis --cluster create 192.168.0.101:6001 192.168.0.101:6002 192.168.0.102:6003 192.168.0.102:6004 192.168.0.103:6005 192.168.0.103:6006 --cluster-replicas 1

    6. 验证

    1. cd /usr/local/redis
    2. ./bin/redis-cli -a hadoopredis -h 192.168.0.101 -p 6001 -c
    3. # 查看集群状态
    4. redis>> cluster info
    5. # 查看主从关系
    6. redis>> cluster nodes

    7. 关闭Redis服务

    1. # 方式1
    2. # bigdata-node1
    3. /usr/local/redis/bin/redis-cli -a hadoopredis -c -h 192.168.0.101 -p 6001 shutdown
    4. /usr/local/redis/bin/redis-cli -a hadoopredis -c -h 192.168.0.101 -p 6002 shutdown
    5. /usr/local/redis/bin/redis-cli -a hadoopredis -c -h 192.168.0.102 -p 6003 shutdown
    6. /usr/local/redis/bin/redis-cli -a hadoopredis -c -h 192.168.0.102 -p 6004 shutdown
    7. /usr/local/redis/bin/redis-cli -a hadoopredis -c -h 192.168.0.103 -p 6005 shutdown
    8. /usr/local/redis/bin/redis-cli -a hadoopredis -c -h 192.168.0.103 -p 6006 shutdown
    9. # 方式2
    10. /usr/local/redis/src/redis-cli -a hadoopredis -c -h 192.168.0.101 -p 6001 shutdown
    11. /usr/local/redis/src/redis-cli -a hadoopredis -c -h 192.168.0.101 -p 6002 shutdown
    12. /usr/local/redis/src/redis-cli -a hadoopredis -c -h 192.168.0.102 -p 6003 shutdown
    13. /usr/local/redis/src/redis-cli -a hadoopredis -c -h 192.168.0.102 -p 6004 shutdown
    14. /usr/local/redis/src/redis-cli -a hadoopredis -c -h 192.168.0.103 -p 6005 shutdown
    15. /usr/local/redis/src/redis-cli -a hadoopredis -c -h 192.168.0.103 -p 6006 shutdown
    16. # 方式3
    17. pkill redis-server

    哨兵模式

    参考:(不推荐,生产环境推荐使用集群模式)
    https://blog.csdn.net/lihongtai/article/details/82826809

    密码设置

    临时密码

    1. # 查看是否设置密码(若已设置密码则返回“(error) NOAUTH Authentication required.”)
    2. config get requirepass
    3. # 设置密码
    4. config set requirepass hadoopredis
    5. # 密码验证
    6. auth hadoopredis

    永久密码

    1. vi /usr/local/redis/conf/redis.conf

    配置如下:

    1. requirepass hadoopredis

    完成配置后重启Redis。

    问题修复

    问题1

    错误信息:“[ERR] Node xxx.xxx.xxx.xxx:pppp is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
    解决方案:

  1. 将需要新增的节点下aof、rdb等本地备份文件删除。
  2. 同时将新Node的集群配置文件删除,即:删除你redis.conf里面cluster-config-file所在的文件。
  3. 再次添加新节点如果还是报错,则登录新节点(./redis-cli –h xxx.xxx.xxx.xxx –p pppp -a password -c)对数据库进行清除(flushdb)。

    参考

    博文:redis5.0.4集群搭建
    https://www.cnblogs.com/fousor/p/10598400.html
    博文:linux环境redis5.0.8集群搭建
    https://www.cnblogs.com/lyf-12/articles/12839623.html
    博文:Centos7 安装redis集群哨兵模式
    https://blog.csdn.net/lihongtai/article/details/82826809