docker redis4.0 集群(cluster)搭建

文章来源:https://www.cnblogs.com/hello-tl/p/9244550.html 

原理
1.集群管理器将16384个哈希槽点平均分配到三台master上,只有master会有槽点。具体算法是对key进行哈希求余,得到的余数落在那个节点的哈希槽点范围内,就分到那台master上
2.只有master才有槽点
3.一个从节点只对应一个master节点,即只会复制那个master节点的数据。如果master节点坏了,其中一个slave节点会升级为master。如果master和从节点都坏了,那么master对应的数据就丢了。
4.只有主节点可以被写入,从节点只是同步。
5.AOF同步保存,RDB有保存的规则。

0.准备六个虚拟机 / 六台服务器

  集群中有三个主节点,三个从节点,一共六个结点。因此要构建六个redis的docker容器。

  在宿主机中将这六个独立的redis结点关联成一个redis集群。需要用到官方提供的ruby脚本。

  安装好之后必须步骤

  1.开启22 / 6379 / 16379 端口  注意:每一台虚拟机上都是如此执行

# systemctl start firewalld  
# firewall-cmd --zone=public --add-port=22/tcp --permanent 
# firewall-cmd --zone=public --add-port=6379/tcp --permanent 
# firewall-cmd --zone=public --add-port=16379/tcp --permanent 
# systemctl restart firewalld.service
# yum install net-tools

  2.更新 yum 原   # yum update    (属实个人习惯   跟redis集群搭建并没任何软用)

  3.安装 wget # yum install wget   (属实个人习惯   跟redis集群搭建并没任何软用)

  4.安装 vim   # yum install vim     (属实个人习惯   跟redis集群搭建并没任何软用)

  5.ContOS 7.4 64 (mysql 优化工具 pt-query-digest 学习) root 123456:mysql 123456      跟咱们没关系

   docker redis4.0 集群(cluster)搭建_第1张图片

  虚拟机明明上的ip地址已经标明

2.我用的linux远程工具是xshell 4 链接这六台虚拟机

  docker redis4.0 集群(cluster)搭建_第2张图片

3.安装docker(来自小田吃饺子-博客园docker 安装https://www.cnblogs.com/hello-tl/p/8901132.html

  安装完并且启动docker

5.拉取redis4.0镜像并且启动

  注意:每一个虚拟机上都是如此配置

  docker安装redis详情步骤 (来自小田吃饺子-博客园docker 安装redishttps://www.cnblogs.com/hello-tl/p/9239474.html

1.添加镜像
    # docker pull redis:4.0
2.创建conf文件目录
    # mkdir /data
    # mkdir /data/redis
    # mkdir /data/redis/conf4.0
3.创建redis.conf并且写入内容
    # vim /data/redis/conf4.0/redis.conf
    内容如下
        # 持久化
        appendonly yes

        # 端口
        port 6379

        # 密码
        # requirepass password
        # masterauth password
        # 集群配置
        cluster-enabled yes
        cluster-config-file nodes.conf
        cluster-node-timeout 5000
        cluster-announce-ip xxx.xxx.x.xxx [自己电脑的ip]
        cluster-announce-port 6379
        cluster-announce-bus-port 16379
4.创建自定义network
    # docker network create redis-net
5.执行命令启动
    # docker run -d -ti -p 6379:6379 -p 16379:16379 \
      -v /data/redis/conf4.0/redis.conf:/usr/local/etc/redis/redis.conf \
      -v /data/redis/data:/data \
      --restart always --name redis4.0 --net redis-net \
      --sysctl net.core.somaxconn=1024 redis redis-server /usr/local/etc/redis/redis.conf

6.拉取ruby

# docker pull ruby:2.4

 7.通过启动ruby来实现集群

注:随意一台服务器上运行即可

echo yes | docker run -i --rm --net=host  ruby sh -c '\
gem install redis \
&& wget http://download.redis.io/redis-stable/src/redis-trib.rb \
&& ruby redis-trib.rb create --replicas 1 \
192.168.1.152:6379 192.168.1.177:6379 192.168.1.187:6379 192.168.1.197:6379 192.168.1.105:6379 192.168.1.138:6379 '

注:进入集群

# docker run -it redis:4.0 redis-cli -c -h 172.17.0.1 -p 6379

8.以上命令显示 (表示成功)

Successfully installed redis-4.0.1
1 gem installed
--2018-07-04 04:50:10--  http://download.redis.io/redis-stable/src/redis-trib.rb
Resolving download.redis.io (download.redis.io)... 109.74.203.151
Connecting to download.redis.io (download.redis.io)|109.74.203.151|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 65991 (64K) [text/plain]
Saving to: 'redis-trib.rb'

     0K .......... .......... .......... .......... .......... 77%  107K 0s
    50K .......... ....                                       100% 20.3M=0.5s

2018-07-04 04:50:11 (138 KB/s) - 'redis-trib.rb' saved [65991/65991]

>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.1.152:6379
192.168.1.177:6379
192.168.1.187:6379
Adding replica 192.168.1.105:6379 to 192.168.1.152:6379
Adding replica 192.168.1.138:6379 to 192.168.1.177:6379
Adding replica 192.168.1.197:6379 to 192.168.1.187:6379
M: ef114f1afe3b19247bd2855d6c8b06558061df28 192.168.1.152:6379
   slots:0-5460 (5461 slots) master
M: 73e5be149d2ae5c73025da0cc3f5badc21e57c58 192.168.1.177:6379
   slots:5461-10922 (5462 slots) master
M: 99d6ecfa496726f50d5e24cebaa94648cde54b0a 192.168.1.187:6379
   slots:10923-16383 (5461 slots) master
S: 52c058d234b23db447fc9d5761d32af1cb60d911 192.168.1.197:6379
   replicates 99d6ecfa496726f50d5e24cebaa94648cde54b0a
S: e7af3586b42dbfe3bf312bd6b46b99ded953e5a1 192.168.1.105:6379
   replicates ef114f1afe3b19247bd2855d6c8b06558061df28
S: a6f9990f84f5cef22d2293c320720f7b0d818dea 192.168.1.138:6379
   replicates 73e5be149d2ae5c73025da0cc3f5badc21e57c58
Can I set the above configuration? (type 'yes' to accept): >>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join...
>>> Performing Cluster Check (using node 192.168.1.151:6379)
M: ef114f1afe3b19247bd2855d6c8b06558061df28 192.168.1.152:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 73e5be149d2ae5c73025da0cc3f5badc21e57c58 192.168.1.177:6379
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: a6f9990f84f5cef22d2293c320720f7b0d818dea 192.168.1.138:6379
   slots: (0 slots) slave
   replicates 73e5be149d2ae5c73025da0cc3f5badc21e57c58
S: 52c058d234b23db447fc9d5761d32af1cb60d911 192.168.1.197:6379
   slots: (0 slots) slave
   replicates 99d6ecfa496726f50d5e24cebaa94648cde54b0a
S: e7af3586b42dbfe3bf312bd6b46b99ded953e5a1 192.168.1.105:6379
   slots: (0 slots) slave
   replicates ef114f1afe3b19247bd2855d6c8b06558061df28
M: 99d6ecfa496726f50d5e24cebaa94648cde54b0a 192.168.1.187:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

文章来源:https://www.cnblogs.com/hello-tl/p/9244550.html 

转载于:https://www.cnblogs.com/hello-tl/p/9244550.html

你可能感兴趣的:(docker redis4.0 集群(cluster)搭建)