1. 操作系统信息:

$ uname -a
  Linux iZbp11d57wmumnwuihb2czZ 3.10.-957.5..el7.x86_64 # SMP Fri Feb :: UTC x86_64 x86_64 x86_64 GNU/Linux
$ cat /proc/version
  Linux version 3.10.-957.5..el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8. (Red Hat 4.8.-) (GCC) ) # SMP Fri Feb :: UTC
$ cat /etc/redhat-release
  CentOS Linux release 7.6. (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..tar.gz
$ cd redis-4.0.
$ make

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

$ make distclean

172.16.214.160机器上创建redis目录

$ mkdir /opt/redis-4.0./redis-cluster
$ cd /opt/redis-4.0./redis-cluster
$ mkdir

172.16.214.161机器上创建redis目录

$ mkdir /opt/redis-4.0./redis-cluster
$ cd /opt/redis-4.0./redis-cluster
$ mkdir

172.16.214.163机器上创建redis目录

$ mkdir /opt/redis-4.0./redis-cluster
$ cd /opt/redis-4.0./redis-cluster
$ mkdir

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

#端口号
port #默认ip为127.0.0.,需要改为其他节点机器可访问的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 #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.
$ rvm use 2.3.
$ rvm use 2.3. --default
$ ruby --version
$ rvm remove 2.0.
$ gem install redis

设置ruby访问的密码

$  cd /usr/local/rvm/gems/ruby-2.3./gems/redis-4.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./src/redis-trib.rb create --replicas  172.16.214.160: 172.16.214.160: 172.16.214.160: 172.16.214.161: 172.16.214.161: 172.16.214.161: 172.16.214.163: 172.16.214.163: 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=;i<=;i++)); do /opt/redis-4.0./src/redis-cli -c -h 172.16.214.160 -p $i -a passwd123 shutdown; done

$ for((i=;i<=;i++)); do /opt/redis-4.0./src/redis-cli -c -h 172.16.214.161 -p $i -a passwd123 shutdown; done

$ for((i=;i<=;i++)); do /opt/redis-4.0./src/redis-cli -c -h 172.16.214.163 -p $i -a passwd123 shutdown; done

5.集群验证

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

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

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

检查集群状态

$ /opt/redis-4.0./src/redis-trib.rb check 172.16.214.160:
>>> Performing Cluster Check (using node 172.16.214.160:)
M: 8a78f3b5c1694caecfe9404f131234328481136f 172.16.214.160:
slots:- ( slots) master
additional replica(s)
M: d4f15c233b82ec71997f72bb9c04a4fb1f44eea8 172.16.214.161:
slots:- ( slots) master
additional replica(s)
M: 729f02e82887746b85d709e36f9b07148e6af442 172.16.214.163:
slots:- ( slots) master
additional replica(s)
S: 33a6c6f63ad565e6a9bc76712956d2cbc9819b93 172.16.214.160:
slots: ( slots) slave
replicates 729f02e82887746b85d709e36f9b07148e6af442
S: 0860ba2d9cadd23757b2052fe653857fd64e4363 172.16.214.163:
slots: ( slots) slave
replicates 8a78f3b5c1694caecfe9404f131234328481136f
S: e5c21b38ed2b6b4870df5f8438259dde0687b363 172.16.214.161:
slots: ( slots) slave
replicates 1ec686b61ce3b8a1161382503c62748fc1985553
M: 1ec686b61ce3b8a1161382503c62748fc1985553 172.16.214.160:
slots:- ( slots) master
additional replica(s)
S: 37cf628f031d47fc80dd02247d555eb6ab179686 172.16.214.161:
slots: ( slots) slave
replicates 8a78f3b5c1694caecfe9404f131234328481136f
S: b586d779805f2f75784bb526ae205cd672534b7a 172.16.214.163:
slots: ( slots) slave
replicates d4f15c233b82ec71997f72bb9c04a4fb1f44eea8
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All slots covered.

列出集群节点

$ /opt/redis-4.0./src/redis-cli -h 172.16.214.160 -c -p 7000 -a passwd123
172.16.214.160:> cluster nodes
d4f15c233b82ec71997f72bb9c04a4fb1f44eea8 172.16.214.161:@ master - connected -
729f02e82887746b85d709e36f9b07148e6af442 172.16.214.163:@ master - connected -
33a6c6f63ad565e6a9bc76712956d2cbc9819b93 172.16.214.160:@ slave 729f02e82887746b85d709e36f9b07148e6af442 connected
0860ba2d9cadd23757b2052fe653857fd64e4363 172.16.214.163:@ slave 8a78f3b5c1694caecfe9404f131234328481136f connected
e5c21b38ed2b6b4870df5f8438259dde0687b363 172.16.214.161:@ slave 1ec686b61ce3b8a1161382503c62748fc1985553 connected
1ec686b61ce3b8a1161382503c62748fc1985553 172.16.214.160:@ master - connected -
37cf628f031d47fc80dd02247d555eb6ab179686 172.16.214.161:@ slave 8a78f3b5c1694caecfe9404f131234328481136f connected
b586d779805f2f75784bb526ae205cd672534b7a 172.16.214.163:@ slave d4f15c233b82ec71997f72bb9c04a4fb1f44eea8 connected
8a78f3b5c1694caecfe9404f131234328481136f 172.16.214.160:@ myself,master - connected -

打印集群信息

$ 172.16.214.160:> cluster info
cluster_state:ok
cluster_slots_assigned:
cluster_slots_ok:
cluster_slots_pfail:
cluster_slots_fail:
cluster_known_nodes:
cluster_size:
cluster_current_epoch:
cluster_my_epoch:
cluster_stats_messages_ping_sent:
cluster_stats_messages_pong_sent:
cluster_stats_messages_sent:
cluster_stats_messages_ping_received:
cluster_stats_messages_pong_received:
cluster_stats_messages_meet_received:
cluster_stats_messages_publish_received:
cluster_stats_messages_received:

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

chsh admin -s /sbin/nologin

CentOS7.6搭建redis4.0.1 cluster集群的更多相关文章

  1. CentOs7.3 搭建 Redis-4.0.1 Cluster 集群服务

    环境 VMware版本号:12.0.0 CentOS版本:CentOS 7.3.1611 三台虚拟机(IP):192.168.252.101,192.168.102..102,192.168.252. ...

  2. (转)CentOs7.3 搭建 RabbitMQ 3.6 Cluster 集群服务与使用

    RabbitMQ是一个开源的AMQP实现,服务器端用Erlang语言编写,支持多种客户端,如:Python.Ruby..NET.Java.JMS.C.PHP.ActionScript.XMPP.STO ...

  3. Redis5.0.8 Cluster集群部署

    目录 一.Redis Cluster简介 二.部署 三.创建主库 一.Redis Cluster简介 Redis Cluster集群是一种去中心化的高可用服务,其内置的sentinel功能可以提供高可 ...

  4. Redis(二)CentOS7安装Redis4.0.10与集群搭建

    一 Redis单机安装 1 Redis下载安装 1.1 检查依赖环境(Redis是C语言开发,编译依赖gcc环境) [root@node21 redis-]$ gcc -v -bash: gcc: c ...

  5. Spark(二)CentOS7.5搭建Spark2.3.1分布式集群

    一 下载安装包 1 官方下载 官方下载地址:http://spark.apache.org/downloads.html 2  安装前提 Java8         安装成功 zookeeper  安 ...

  6. Redis3.0.7 cluster/集群 安装配置教程

    1.前言 环境:CentOS-6.7-i386-LiveDVD 安装的CentOs系统 节点: 6个节点,3个主节点.3个从节点(由于redis默认需要3个主节点,如果想每个主节点有一个从节点,这是最 ...

  7. CentOS7.4搭建kafka单结点和集群

    操作系统选择 CentOS7.4x86-64(操作系统的x86_64是跟CPU有关的,最早AMD公司开发出了一款向下兼容x86CPU,向上又扩充了指令集,具有了64位CPU的特性,这款CPU后来改名为 ...

  8. Linux Centos7 环境搭建Docker部署Zookeeper分布式集群服务实战

    Zookeeper完全分布式集群服务 准备好3台服务器: [x]A-> centos-helios:192.168.19.1 [x]B-> centos-hestia:192.168.19 ...

  9. Flink(二)CentOS7.5搭建Flink1.6.1分布式集群

    一. Flink的下载 安装包下载地址:http://flink.apache.org/downloads.html  ,选择对应Hadoop的Flink版本下载 [admin@node21 soft ...

随机推荐

  1. Springboot的static和templates区别

    static和templates部分参考博客:https://blog.csdn.net/wangb_java/article/details/71775637 热部署参考博客:https://www ...

  2. 【问题解决方案】AttributeError: module 'pygal' has no attribute 'Worldmap'

    <Python编程:从入门到实践>- 16章-16.2.5制作世界地图 import pygal 后报如标题的error 参考CSDN 解决:AttributeError: module ...

  3. .netcore2.0发送邮件

    SmtpClient smtpClient = new SmtpClient(); smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指 ...

  4. ansible 模块 分享

    A a10_server 管理A10 Networks AX / SoftAX / Thunder / vThunder设备 a10_service_group 管理A10网络设备的服务组 a10_v ...

  5. 实验一 windows基本网络命令

    一. 实验目的 1. 了解windows系统中网络命令的基本功能. 2. 掌握基本网络命令的使用方法. 3. 掌握使用网络命令观察网络状态的方法. 二.实验环境 1. 软件环境:Microsoft W ...

  6. list遍历时删除的坑

    1.测试代码如下: public class StrTest { public static void main(String[] args) { ArrayList<String> li ...

  7. Python基础:第一个Python程序(2)

    1.Python Shell 1.1 Windows命令 (1)[开始]|[运行],输入cmd回车,进入Windows命令界面. (2)输入python,回车,进入Python Shell. 1.2 ...

  8. Adding appsettings.json to a .NET Core console app

    This is something that strangely doesn’t seem to be that well documented and took me a while to figu ...

  9. $.ajax居然触发popstate事件?

    我使用$.ajax用来实现一个搜索效果 近段时间因为苹果上微信浏览器的不知明原因需要处理返回事件,因此加多了popstate事件监听用来分别处理苹果跟安卓的返回. 可是居然影响到了我前面的ajax搜索 ...

  10. [PL]如果天空是黑暗的,那就摸黑生存

    “如果天空是黑暗的,那就摸黑生存:如果发出声音是危险的,那就保持沉默:如果自觉无力发光的,那就蜷缩于墙角.但不要习惯了黑暗,就为黑暗辩护:不要为自己的苟且而得意:不要嘲讽那些比自己更勇敢更热情的人们. ...