zoukankan      html  css  js  c++  java
  • [Linux/Redis]搭建Redis集群

    1 基本信息

    • 3台服务器(CentOS7.3)
    192.168.1.101 node101
    192.168.1.102 node102
    192.168.1.103 node103
    

    2 单机 安装 Redis

    • 安装依赖

    配置yum源,详见: [Linux]CentOS7搭建/配置:YUM仓库/源[本地源/Web源(Apache HTTP(D))/自建源仓库] - 博客园

    yum -y install gcc gcc-c++ net-tools tcl
    
    • 下载 Redis安装包

    下载redis安装包到/opt/目录下 + 安装到/usr/local/下

    cd /opt
    wget http://download.redis.io/releases/redis-5.0.5.tar.gz
    
    cp /opt/redis-5.0.5.tar.gz /usr/local
    tar -zxvf /usr/local/redis-5.0.5.tar.gz -C /usr/local/ #解压后的存放目录: /usr/local/redis-5.0.5/
          -C : 指定解压后存放的路径
    
    • 编译&安装
    [编译]
    cd /usr/local/redis-5.0.5/deps
    make hiredis jemalloc linenoise lua geohash-int
    
    cd /usr/local/redis-5.0.5
    make && make install
          注: make              #等待编译完成即可,未报错即进行下一步
          注: make install      #安装,执行make install的时候会将src下面的几个命令复制到/usr/local/bin/下  
    
    [安装]
    cd /usr/local/redis-5.0.5/utils
    ./install_server.sh
    
    • 初步配置
    redis_port=6379
    	设置临时变量,以便后续调用其值
    cp /usr/local/redis-5.0.5/redis.conf /etc/redis/redis_port.conf
    sed -i "s/127.0.0.1/0.0.0.0/g" /etc/redis/$redis_port.conf
    	注: sed 's/要被取代的字串/新的字串/g' ; bind 0.0.0.0 : 允许与任何主机通信
    vi /etc/redis/redis_port.conf
    	bind 192.168.0.101/102/103 # 本机ip
    	daemonize yes # 后台运行
    	cluster-enabled yes # 启用集群
    
    service redis_$redis_port restart
    
    安装压缩包存放路径: /opt/redis-5.0.5.tar.gz
    安装路径        : /usr/local/redis-5.0.5/*
    
    Config file    : /etc/redis/6379.conf
    Log file       : /var/log/redis_6379.log
    Data dir       : /var/lib/redis/6379
    Executable     : /usr/local/bin/redis-server
    Cli Executable : /usr/local/bin/redis-cli
    Copied /tmp/6379.conf => /etc/init.d/redis_6379
    
    • 运行redis

    因为redis-server命令在/usr/local/bin目录里面,而该目录又配置在PATH中,所以可以像执行ls、mkdir等命令的方式去执行redis-cli或者redis-server等命令。

    redis-server                        # 在任意目录下运行 (方式1)
    
    /usr/local/redis-5.0.5/src/redis-server   # 在redis目录下运行 (方式2) 
    
    redis-server /etc/redis/6379.conf  # 指定redis.conf文件启动 (方式3)
    
    service redis_6379 restart
    
    

    2 配置Redis集群

    3台机器均需如下操作

    • 配置防火墙
    systemctl start firewalld
    firewall-cmd --permanent --zone=public --add-port=6379/tcp
    	ss -lntp
    		"master"
    	关闭端口
    		firewall-cmd --permanent --zone=public --remove-port=6379/tcp
    firewall-cmd --permanent --zone=public --add-port=16379/tcp
    	Redis集群通信时用
    systemctl restart firewalld
    
    • 安装集群相关依赖
    yum -y install ruby rubygems
    gem install redis
          安装Redis客户端 for ruby
    

    [若执行"gem install redis"出现如下信息时,可通过如下4步来尝试解决]

    ERROR: Could not find a valid gem 'redis' (>= 0), here is why:Unable to download data from https://rubygems.org/ - no such name (https://rubygems.org/latest_specs.4.8.gz)

    1 下载、上传到/opt/dataeye/redis/redis-3.3.0.gem (下载地址: http://rubygems.org/downloads/redis-3.3.0.gem)
    
    2 cd  /opt/dataeye/redis/
    3 yum -y install zlib-devel
    	gem的依赖
    4 gem install redis-3.3.0.gem
    
    • 创建集群
    [推荐]
    redis-cli --cluster create 192.168.1.101:6379 192.168.1.102:6379 192.168.1.103:6379
    
    [不被推荐/已废弃]X
    cd /usr/local/redis-5.0.5/src/
    ./redis-trib.rb create 192.168.1.101:6379 192.168.1.102:6379 192.168.1.103:6379
    

    3 验证集群可用性

    3.1 查看集群节点信息及状态

    cluster info
    cluster nodes
    

    3.2 数据同步一致性验证

    [node102]

    [root@node102 src]# redis-cli -h 192.168.1.101 -c
    192.168.1.101:6379> set name sdc_test
    OK
    192.168.1.101:6379> get name
    "sdc_test"
    192.168.1.101:6379> exit
    

    [node101]

    [root@node101 src]# redis-cli -h 192.168.1.102 -c
    192.168.1.102:6379> get name
    -> Redirected to slot [5798] located at 192.168.1.101:6379
    "sdc_test"
    192.168.1.101:6379> set name "sdc_test2~~~~~~~~"
    -> Redirected to slot [5798] located at 192.168.1.101:6379
    OK
    192.168.1.101:6379> exit
    

    [node103]

    [root@node103 redis]# redis-cli -h 192.168.1.102 -c
    192.168.1.102:6379> get name
    -> Redirected to slot [5798] located at 192.168.1.101:6379
    "sdc_test2~~~~~~~~"
    192.168.1.102:6379> exit
    

    4 参考文献

  • 相关阅读:
    django--模型层(orm)-查询补充及cookie
    django-模型层(model)-多表相关操作(图书管理练习)
    Linux-正则表达式与三剑客
    网络配置命令优先级和元字符
    Linux-Nginx和NFS
    Linux-内存进程和软件安装
    乌龟棋
    倍增 Tarjan 求LCA
    切蛋糕
    HDU1505 City Game/玉蟾宫/全0子矩阵 悬线法
  • 原文地址:https://www.cnblogs.com/johnnyzen/p/13394720.html
Copyright © 2011-2022 走看看