redis cluster单机伪分布式搭

2023-01-31 03:01:28 redis 分布式 单机

最近公司引进微服务框架,之前的一台Redis的预存60G已经无法满足现在的260G业务需要,经过一番考虑搭建了这套集群 . 为了方便我就用一台服务器演示,生产环境中不建议这么做(没啥用),只为记录一下过程,至于精细化的配置需要在生产中自行研究

演示环境

[root@localhost ~]# cat /etc/redhat-release   
Centos linux release 7.4.1708 (Core)  

redis服务部署过程

  • 我下载的是redis-4.0.14.tar.gz版本的包
  • 解压安装
    tar -zxvf redis-4.0.14.tar.gz
    make install
  • 启动服务
    ./redis-server
  • 验证,启动客户端测试
    ./redis-cli  
    127.0.0.1:6379> set ab b  
    OK  
    127.0.0.1:6379> get ab  
    "b"  
    127.0.0.1:6379> keys *  
    "ab"  
    "foo"  
  • 关闭redis服务
    ./redis-cli shutdown
  • 设置redis后台启动,在/usr/local/bin目录下
    cp /usr/local/redis/redis-4.0.8/redis.conf ./
    修改redis.conf这个配置文件,找到 daemonize no 这一行,将 no 改成 yes 保存
    然后启动redis时携带配置文件,./redis-server redis.conf
  • 在/usr/local/bin目录下修改好redis.conf配置(主要是设置后台启动、bind、密码、保护模式等)后
    mkdir /etc/redis
    cp /usr/local/bin/redis.conf /etc/redis/6379.conf
  • 将redis自带的启动脚本复制到/etc/init.d目录下,命名为redisd(通常都以d结尾表示是后台自启动服务)
    cp /usr/local/redis/redis-4.0.8/utils/redis_init_script /etc/init.d/redisd
  • 设置为开机自启动 chkconfig redisd on
    如果报错redisd 不支持 chkconfig,在/etc/init.d/redisd中增加如下片段,即可再设置开机自启动
    #!/bin/sh  
    #chkconfig:   2345 90 10  
    #description:  Redis is a persistent key-value database
  • chkconfig redisd on 成功之后即可以使用启动脚本来启动redis

    分布式集群的搭建

    搭建一个最小的集群需要6台机器(由节点投票机制决定至少需要3台主节点,另外为了高可用每个主节点至少要备一个从节点,所以总共需要6个节点。这里6个节点全部设在一台机上,通过端口号来分别。实现伪分布式。)

  • 在/usr/local/目录下创建文件夹redisCluster,并在redisCluster目录下创建6个文件夹
    mkdir /usr/local/redisCluster
    mkdir /usr/local/redisCluster/redis0{1,2,3,4,5,6}
  • 复制单机单实例下可正常启动redis情况下的 /usr/local/bin 目录下的所有文件分别到上面创建的6个文件夹中
    cp /usr/local/bin/* /usr/local/redisCluster/redis01/  
    cp /usr/local/bin/* /usr/local/redisCluster/redis02/  
    cp /usr/local/bin/* /usr/local/redisCluster/redis03/  
    cp /usr/local/bin/* /usr/local/redisCluster/redis04/  
    cp /usr/local/bin/* /usr/local/redisCluster/redis05/  
    cp /usr/local/bin/* /usr/local/redisCluster/redis06/
  • 再对每个实例里面的redis.conf进行修改(这里修改其中一份,然后其他5份进行复制之后修改下端口号即可)
    vi /usr/local/redisCluster/redis01/redis.conf  
    第1处: port 6379                  ————> 值 改成 7001  
    第2处: daemonize no               ————> 值 改成 yes  
    第3处: pidfile /var/run/redis_6379.pid        ————> 值 改成 ./redis_7001.pid  
    第4处: logfile ""                     ————> 值 改成 "./logs/redis_7001.log"  
    第5处: bind 127.0.0.1                 ————> 值 改成 本机IP(如我这里是192.168.25.129)  
    第6处: protected-mode yes                 ————> 值 改成 no  
    第7处: # cluster-enabled yes          ————> 去掉注释  
    第8处: # cluster-config-file nodes-6379.conf    ————> 去掉注释,并把数字改成对应端口号,如这里是7001  
    第9处: # cluster-node-timeout 15000               ————> 去掉注释   
    第10处:  appendonly no                ————> 值 改成 yes
  • 将上面编辑号保存的/usr/local/redisCluster/redis01/目录下的redis.conf文件重命名为7001.conf,然后复制5份分别放到redis02、redis03、redis04、redis05、redis06文件夹下面(可删除掉原文件夹中的redis.conf文件),然后再对每个文件夹下的.conf文件重命名为对应端口号.conf,接着编辑里面的内容,将之前修改为7001的全部修改成对应端口号保存即可
  • 将ruby脚本拷贝到redisCluster目录下
    cp /usr/local/redis/redis-4.0.8/src/redis-trib.rb /usr/local/redisCluster/
  • 创建redis集群启动、停止的脚本以及ruby创建集群的脚本并授权
    touch /usr/local/redisCluster/startCluster.sh  
    touch /usr/local/redisCluster/shutdownCluster.sh  
    touch /usr/local/redisCluster/createCluster.sh  
    chmod 777 /usr/local/redisCluster/*.sh

    startcluster.sh 内容如下:

    /usr/local/rediscluster/redis01/redis-server /usr/local/rediscluster/redis01/7001.conf  
    /usr/local/rediscluster/redis02/redis-server /usr/local/rediscluster/redis02/7002.conf  
    /usr/local/rediscluster/redis03/redis-server /usr/local/rediscluster/redis03/7003.conf  
    /usr/local/rediscluster/redis04/redis-server /usr/local/rediscluster/redis04/7004.conf  
    /usr/local/rediscluster/redis05/redis-server /usr/local/rediscluster/redis05/7005.conf  
    /usr/local/rediscluster/redis06/redis-server /usr/local/rediscluster/redis06/7006.conf 

    shutdowncluster.sh 内容如下:

    /usr/local/rediscluster/redis01/redis-cli -c -h 10.11.1.103 -p 7001 shutdown  
    /usr/local/rediscluster/redis02/redis-cli -c -h 10.11.1.103 -p 7002 shutdown  
    /usr/local/rediscluster/redis03/redis-cli -c -h 10.11.1.103 -p 7003 shutdown  
    /usr/local/rediscluster/redis04/redis-cli -c -h 10.11.1.103 -p 7004 shutdown  
    /usr/local/rediscluster/redis05/redis-cli -c -h 10.11.1.103 -p 7005 shutdown  
    /usr/local/rediscluster/redis06/redis-cli -c -h 10.11.1.103 -p 7006 shutdown 

    createcluster.sh 内容如下:
    ruby redis-trib.rb create --replicas 1 10.11.1.103:7001 10.11.1.103:7002 10.11.1.103:7003 10.11.1.103:7004 10.11.1.103:7005 10.11.1.103:7006

  • 用刚刚创建的脚本启动6个实例,再用ruby脚本创建集群
    cd /usr/local/redisCluster/  
    ./startCluster.sh  
    ./createcluster.sh(输入yes)
  • 验证,查看进程
    [root@localhost rediscluster]# ps -ef |  grep redis|grep -v grep  
    root     23716     1  0 Aug08 ?        00:10:55 /usr/local/rediscluster/redis01/redis-server *:7001 [cluster]  
    root     23721     1  0 Aug08 ?        00:10:53 /usr/local/rediscluster/redis02/redis-server *:7002 [cluster]  
    root     23723     1  0 Aug08 ?        00:10:51 /usr/local/rediscluster/redis03/redis-server *:7003 [cluster]  
    root     23731     1  0 Aug08 ?        00:13:37 /usr/local/rediscluster/redis04/redis-server *:7004 [cluster]  
    root     23736     1  0 Aug08 ?        00:10:55 /usr/local/rediscluster/redis05/redis-server *:7005 [cluster]  
    root     23741     1  0 Aug08 ?        00:08:59 /usr/local/rediscluster/redis06/redis-server *:7006 [cluster]  
  • 集群就搭建成功了。登录任意一个节点客户端验证(-c 表示以集群方式登录),也可以用redis客户端工具RedisDesktopManager来可视化地管理redis数据库
    cd /usr/local/redisCluster/redis01/  
    ./redis-cli -h 10.11.1.103 -p 7001 -c  
    10.11.1.103:7001> set aaaa b  
    OK  
    10.11.1.103:7002> get aaaa  
    -> Redirected to slot [1953] located at 10.11.1.103:7001  
    "b"  
    10.11.1.103:7001> cluster info  
    cluster_state:ok  
    cluster_slots_assigned:16384  
    cluster_slots_ok:16384  
    cluster_slots_pfail:0  
    cluster_slots_fail:0  
    cluster_known_nodes:6  
    cluster_size:3  
    cluster_current_epoch:6  
    cluster_my_epoch:1  
    cluster_stats_messages_ping_sent:367555  
    cluster_stats_messages_pong_sent:342015  
    cluster_stats_messages_publish_sent:903835  
    cluster_stats_messages_sent:1613405  
    cluster_stats_messages_ping_received:342015  
    cluster_stats_messages_pong_received:367550  
    cluster_stats_messages_publish_received:1084495  
    cluster_stats_messages_received:1794060  
    10.11.1.103:7001>  
  • 在redis01 02 03 里配置哨兵,这里只配置了redis01的剩余的按照这个自行配置即可,需要注意的是监视的名称不可重复(已标出)
    sentinel.conf 配置如下:
    protected-mode no  
    port 27001   
    sentinel deny-scripts-reconfig yes  
    sentinel monitor **mymaster1** 10.11.1.103 7001 2  
    sentinel down-after-milliseconds **mymaster1** 10000  
    logfile "/var/log/sentinel1.log"  
    dir "/usr/local/rediscluster/redis01"  
    sentinel failover-timeout **mymaster1** 15000  
    sentinel config-epoch **mymaster1** 0  
    sentinel leader-epoch **mymaster1** 0  
    sentinel known-slave **mymaster1 10.11.1.103 7004**  
    sentinel current-epoch 0  
  • 在rediscluster目录下面创建哨兵启动和关闭的脚本
    startsentinel.sh 的内容如下
    /usr/local/rediscluster/redis01/redis-sentinel /usr/local/rediscluster/redis01/sentinel.conf &  
    /usr/local/rediscluster/redis02/redis-sentinel /usr/local/rediscluster/redis02/sentinel.conf &  
    /usr/local/rediscluster/redis03/redis-sentinel /usr/local/rediscluster/redis03/sentinel.conf &  

    stopsentinel.sh 脚本的内容如下:

    kill -9 `ps -ef |  grep sentinel| grep -v grep|awk '{print $2}'`  
  • 再次查看进程
    [root@localhost rediscluster]# ps -ef |  grep redis|grep -v grep  
    root     23716     1  0 Aug08 ?        00:10:55 /usr/local/rediscluster/redis01/redis-server *:7001 [cluster]  
    root     23721     1  0 Aug08 ?        00:10:53 /usr/local/rediscluster/redis02/redis-server *:7002 [cluster]  
    root     23723     1  0 Aug08 ?        00:10:51 /usr/local/rediscluster/redis03/redis-server *:7003 [cluster]  
    root     23731     1  0 Aug08 ?        00:13:37 /usr/local/rediscluster/redis04/redis-server *:7004 [cluster]  
    root     23736     1  0 Aug08 ?        00:10:55 /usr/local/rediscluster/redis05/redis-server *:7005 [cluster]  
    root     23741     1  0 Aug08 ?        00:08:59 /usr/local/rediscluster/redis06/redis-server *:7006 [cluster]  
    root     23924     1  0 Aug08 ?        00:09:55 /usr/local/rediscluster/redis01/redis-sentinel *:27001 [sentinel]  
    root     23925     1  0 Aug08 ?        00:09:38 /usr/local/rediscluster/redis02/redis-sentinel *:27002 [sentinel]  
    root     23926     1  0 Aug08 ?        00:10:22 /usr/local/rediscluster/redis03/redis-sentinel *:27003 [sentinel] 
  • 这样就完成搭建,以下是需要联网安装的软件
    yum -y install GCc                    ## make install 命令需要此环境  
    yum -y intall ruby                    ## 安装ruby(此时安装的可能使低版本的)  
    curl -L get.rvm.io | bash -s stable   ## 安装rvm  
    source /usr/local/rvm/scripts/rvm     ## 使安装的rvm立即生效  
    rvm install 2.4.1                 ## 通过rvm来安装ruby高级版本  
    gem install redis                     ## 通过ruby的工具包gem来安装redis接口  
  • 需要注意的是,在关闭redis集群之后,再次重复步骤启动集群时会报错,此时只需要删除redis各个实例中生成的文件即可再次按步骤启动集群

相关文章