zoukankan      html  css  js  c++  java
  • CentOS7.6搭建redis4.0.1 cluster集群(带密码)

    1. 操作系统信息:

    $ uname -a
      Linux iZbp11d57wmumnwuihb2czZ 3.10.0-957.5.1.el7.x86_64 #1 SMP Fri Feb 1 14:54:57 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
    $ cat /proc/version
      Linux version 3.10.0-957.5.1.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) ) #1 SMP Fri Feb 1 14:54:57 UTC 2019
    $ cat /etc/redhat-release
      CentOS Linux release 7.6.1810 (Core)

    三台ECS机器:172.16.214.160, 172.16.214.161, 172.16.214.163

    2.  安裝 GCC 编译工具

    $ yum install -y gcc g++ gcc-c++ make

    升级所有的包,防止出现版本过久不兼容问题

    $ yum -y update

    关闭防火墙 节点之前需要开放指定端口

    systemctl stop firewalld.service

    3.1. 安装redis,下载,解压,编译安装

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

    如果因为上次编译失败,有残留的文件

    $ make distclean

    172.16.214.160机器上创建redis目录

    $ mkdir /opt/redis-4.0.1/redis-cluster
    $ cd /opt/redis-4.0.1/redis-cluster
    $ mkdir 7000 7001 7002

    172.16.214.161机器上创建redis目录

    $ mkdir /opt/redis-4.0.1/redis-cluster
    $ cd /opt/redis-4.0.1/redis-cluster
    $ mkdir 7003 7004 7005

    172.16.214.163机器上创建redis目录

    $ mkdir /opt/redis-4.0.1/redis-cluster
    $ cd /opt/redis-4.0.1/redis-cluster
    $ mkdir 7006 7007 7008

    每个目录下创建一个redis.conf文件,内容如下(不同的端口和ip需要设置做修改)

    #端口号
    port 7000
    
    #默认ip为127.0.0.1,需要改为其他节点机器可访问的ip,否则创建集群时无法访问对应的端口,无法创建集群
    bind 172.16.214.160
    
    #redis后台运行
    daemonize yes

    #访问密码
    masterauth passwd123
    requirepass passwd123

    #pidfile文件
    pidfile /var/run/redis_7000.pid
    
    #开启集群
    cluster-enabled yes
    
    #集群的配置,配置文件首次启动自动生成  
    cluster-config-file nodes_7000.conf
    
    #请求超时,默认15秒,可自行设置 
    cluster-node-timeout 15000    
    
    #aof日志开启,有需要就开启,它会每次写操作都记录一条日志
    appendonly yes

    安装ruby (只需要在其中一台上安装)

    $ yum -y install ruby ruby-devel rubygems rpm-build
    $ gem install redis

    Fetching: redis-4.1.0.gem (100%)
    ERROR: Error installing redis:
    redis requires Ruby version >= 2.2.2.

    这一步有可能会出错,原因是ruby版本太低,解决办法安装高版本ruby

    $ yum install curl
    $ gpg2 --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
    $ curl -L get.rvm.io | bash -s stable
    $ find / -name rvm -print
    /usr/local/rvm
    /usr/local/rvm/bin/rvm
    /usr/local/rvm/scripts/rvm
    /usr/local/rvm/lib/rvm
    /usr/local/rvm/src/rvm
    /usr/local/rvm/src/rvm/bin/rvm
    /usr/local/rvm/src/rvm/scripts/rvm
    /usr/local/rvm/src/rvm/lib/rvm
    $ source /usr/local/rvm/scripts/rvm
    $ rvm list known
    $ rvm install 2.3.8
    $ rvm use 2.3.8
    $ rvm use 2.3.8 --default
    $ ruby --version
    $ rvm remove 2.0.0
    $ gem install redis

    设置ruby访问的密码

    $  cd /usr/local/rvm/gems/ruby-2.3.8/gems/redis-4.1.1/lib/redis/
    
    $ vim client.rb

    将“:password”后的内容设置为你的密码, 保存退出

    3.2 创建用户并设置密码,并切换到该用户身份, 修改redis目录所有权

      

    $ useradd admin 
    
    $ passwd admin

      $ chown -R admin:admin /opt/redis-4.0.1

      $ su admin

    4. 为了安全,用admin用户身份启动集群

    #第一台机器上执行 3个节点
    $ for((i=0;i<=2;i++)); do /opt/redis-4.0.1/src/redis-server /opt/redis-4.0.1/redis-cluster/700$i/redis.conf; done
    
    #第二台机器上执行 3个节点
    $ for((i=3;i<=5;i++)); do /opt/redis-4.0.1/src/redis-server /opt/redis-4.0.1/redis-cluster/700$i/redis.conf; done
    
    #第三台机器上执行 3个节点 
    $ for((i=6;i<=8;i++)); do /opt/redis-4.0.1/src/redis-server /opt/redis-4.0.1/redis-cluster/700$i/redis.conf; done

    检查各 Redis 各个节点启动情况

    $ ps -ef | grep redis           //redis是否启动成功
    $ netstat -tnlp | grep redis    //监听redis端口

    创建集群

    $ /opt/redis-4.0.1/src/redis-trib.rb create --replicas 1 172.16.214.160:7000 172.16.214.160:7001 172.16.214.160:7002 172.16.214.161:7003 172.16.214.161:7004 172.16.214.161:7005 172.16.214.163:7006 172.16.214.163:7007 172.16.214.163:7008

    >>> Creating cluster
    >>> Performing hash slots allocation on 9 nodes...
    Using 4 masters:
    172.16.214.160:7000
    172.16.214.161:7003
    172.16.214.163:7006
    172.16.214.160:7001
    Adding replica 172.16.214.161:7004 to 172.16.214.160:7000
    Adding replica 172.16.214.163:7007 to 172.16.214.161:7003
    Adding replica 172.16.214.160:7002 to 172.16.214.163:7006
    Adding replica 172.16.214.161:7005 to 172.16.214.160:7001
    Adding replica 172.16.214.163:7008 to 172.16.214.160:7000
    M: 7c622ac191edd40dd61d9b79b27f6f69d02a5bbf 172.16.214.160:7000
    slots:0-4095 (4096 slots) master
    M: 44c81c15b01d992cb9ede4ad35477ec853d70723 172.16.214.160:7001
    slots:12288-16383 (4096 slots) master
    S: 38f03c27af39723e1828eb62d1775c4b6e2c3638 172.16.214.160:7002
    replicates f1abb62a8c9b448ea14db421bdfe3f1d8075189c
    M: 987965baf505a9aa43e50e46c76189c51a8f17ec 172.16.214.161:7003
    slots:4096-8191 (4096 slots) master
    S: 6555292fed9c5d52fcf5b983c441aff6f96923d5 172.16.214.161:7004
    replicates 7c622ac191edd40dd61d9b79b27f6f69d02a5bbf
    S: 2b5ba254a0405d4efde4c459867b15176f79244a 172.16.214.161:7005
    replicates 44c81c15b01d992cb9ede4ad35477ec853d70723
    M: f1abb62a8c9b448ea14db421bdfe3f1d8075189c 172.16.214.163:7006
    slots:8192-12287 (4096 slots) master
    S: eb4067373d36d8a8df07951f92794e67a6aac022 172.16.214.163:7007
    replicates 987965baf505a9aa43e50e46c76189c51a8f17ec
    S: 2919e041dd3d1daf176d6800dcd262f4e727f366 172.16.214.163:7008
    replicates 7c622ac191edd40dd61d9b79b27f6f69d02a5bbf
    Can I set the above configuration? (type 'yes' to accept): yes

     

    输入yes,然后回车

    如何关闭集群:

    $ pkill redis

    或者逐个关闭

    $ for((i=0;i<=2;i++)); do /opt/redis-4.0.1/src/redis-cli -c -h 172.16.214.160 -p 700$i -a passwd123 shutdown; done
    
    $ for((i=3;i<=5;i++)); do /opt/redis-4.0.1/src/redis-cli -c -h 172.16.214.161 -p 700$i -a passwd123 shutdown; done
    
    $ for((i=6;i<=8;i++)); do /opt/redis-4.0.1/src/redis-cli -c -h 172.16.214.163 -p 700$i -a passwd123 shutdown; done

    5.集群验证

    参数 -C 可连接到集群,因为 redis.conf 将 bind 改为了ip地址,所以 -h 参数不可以省略,-p 参数为端口号

    $ /opt/redis-4.0.1/src/redis-cli -h 172.16.214.160 -c -p 7000 -a passwd123
    172.16.214.160:7000> set name justfortest
    -> Redirected to slot [5798] located at 172.16.214.161:7003
    OK
    172.16.214.161:7003> get name
    "justfortest"
    172.16.214.161:7003>

    发现redis get name 重定向,说明集群已经是可用。

    检查集群状态

    $ /opt/redis-4.0.1/src/redis-trib.rb check 172.16.214.160:7000
    >>> Performing Cluster Check (using node 172.16.214.160:7000)
    M: 8a78f3b5c1694caecfe9404f131234328481136f 172.16.214.160:7000
       slots:0-4095 (4096 slots) master
       2 additional replica(s)
    M: d4f15c233b82ec71997f72bb9c04a4fb1f44eea8 172.16.214.161:7003
       slots:4096-8191 (4096 slots) master
       1 additional replica(s)
    M: 729f02e82887746b85d709e36f9b07148e6af442 172.16.214.163:7006
       slots:8192-12287 (4096 slots) master
       1 additional replica(s)
    S: 33a6c6f63ad565e6a9bc76712956d2cbc9819b93 172.16.214.160:7002
       slots: (0 slots) slave
       replicates 729f02e82887746b85d709e36f9b07148e6af442
    S: 0860ba2d9cadd23757b2052fe653857fd64e4363 172.16.214.163:7008
       slots: (0 slots) slave
       replicates 8a78f3b5c1694caecfe9404f131234328481136f
    S: e5c21b38ed2b6b4870df5f8438259dde0687b363 172.16.214.161:7005
       slots: (0 slots) slave
       replicates 1ec686b61ce3b8a1161382503c62748fc1985553
    M: 1ec686b61ce3b8a1161382503c62748fc1985553 172.16.214.160:7001
       slots:12288-16383 (4096 slots) master
       1 additional replica(s)
    S: 37cf628f031d47fc80dd02247d555eb6ab179686 172.16.214.161:7004
       slots: (0 slots) slave
       replicates 8a78f3b5c1694caecfe9404f131234328481136f
    S: b586d779805f2f75784bb526ae205cd672534b7a 172.16.214.163:7007
       slots: (0 slots) slave
       replicates d4f15c233b82ec71997f72bb9c04a4fb1f44eea8
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.

    列出集群节点

    $ /opt/redis-4.0.1/src/redis-cli -h 172.16.214.160 -c -p 7000 -a passwd123
    172.16.214.160:7000> cluster nodes
    d4f15c233b82ec71997f72bb9c04a4fb1f44eea8 172.16.214.161:7003@17003 master - 0 1556073732261 4 connected 4096-8191
    729f02e82887746b85d709e36f9b07148e6af442 172.16.214.163:7006@17006 master - 0 1556073729000 7 connected 8192-12287
    33a6c6f63ad565e6a9bc76712956d2cbc9819b93 172.16.214.160:7002@17002 slave 729f02e82887746b85d709e36f9b07148e6af442 0 1556073731260 7 connected
    0860ba2d9cadd23757b2052fe653857fd64e4363 172.16.214.163:7008@17008 slave 8a78f3b5c1694caecfe9404f131234328481136f 0 1556073730000 9 connected
    e5c21b38ed2b6b4870df5f8438259dde0687b363 172.16.214.161:7005@17005 slave 1ec686b61ce3b8a1161382503c62748fc1985553 0 1556073732061 6 connected
    1ec686b61ce3b8a1161382503c62748fc1985553 172.16.214.160:7001@17001 master - 0 1556073729000 2 connected 12288-16383
    37cf628f031d47fc80dd02247d555eb6ab179686 172.16.214.161:7004@17004 slave 8a78f3b5c1694caecfe9404f131234328481136f 0 1556073731000 5 connected
    b586d779805f2f75784bb526ae205cd672534b7a 172.16.214.163:7007@17007 slave d4f15c233b82ec71997f72bb9c04a4fb1f44eea8 0 1556073730000 8 connected
    8a78f3b5c1694caecfe9404f131234328481136f 172.16.214.160:7000@17000 myself,master - 0 1556073728000 1 connected 0-4095

    打印集群信息

    $ 172.16.214.160:7000> cluster info
    cluster_state:ok
    cluster_slots_assigned:16384
    cluster_slots_ok:16384
    cluster_slots_pfail:0
    cluster_slots_fail:0
    cluster_known_nodes:9
    cluster_size:4
    cluster_current_epoch:9
    cluster_my_epoch:1
    cluster_stats_messages_ping_sent:58093
    cluster_stats_messages_pong_sent:57405
    cluster_stats_messages_sent:115498
    cluster_stats_messages_ping_received:57397
    cluster_stats_messages_pong_received:58093
    cluster_stats_messages_meet_received:8
    cluster_stats_messages_publish_received:2
    cluster_stats_messages_received:115500

     6. 为了安全,最后可以设置admin用户身份不能远程登录

    chsh admin -s /sbin/nologin

  • 相关阅读:
    g_pLog
    win-socket
    时间转字符串 各种格式
    linux自启动脚本.sh
    openssl之aes对称加密
    openssl生成密钥/证书
    win10下安装openssl
    GUI的最终选择 Tkinter(二):Label和Button组件
    GUI的最终选择 Tkinter(一):Tkinter最初体验
    练习十三:水仙花数,用for循环实现
  • 原文地址:https://www.cnblogs.com/cs99lzzs/p/10761049.html
Copyright © 2011-2022 走看看