docker redis4.0 集羣(cluster)搭建

文章來源:http://www.javashuo.com/article/p-hgdndssx-cs.html html

原理
1.集羣管理器將16384個哈希槽點平均分配到三臺master上,只有master會有槽點。具體算法是對key進行哈希求餘,獲得的餘數落在那個節點的哈希槽點範圍內,就分到那臺master上
2.只有master纔有槽點
3.一個從節點只對應一個master節點,即只會複製那個master節點的數據。若是master節點壞了,其中一個slave節點會升級爲master。若是master和從節點都壞了,那麼master對應的數據就丟了。
4.只有主節點能夠被寫入,從節點只是同步。
5.AOF同步保存,RDB有保存的規則。

0.準備六個虛擬機 / 六臺服務器node

  集羣中有三個主節點,三個從節點,一共六個結點。所以要構建六個redis的docker容器。mysql

  在宿主機中將這六個獨立的redis結點關聯成一個redis集羣。須要用到官方提供的ruby腳本。linux

  安裝好以後必須步驟redis

  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集羣搭建並沒任何軟用)sql

  3.安裝 wget # yum install wget   (屬實我的習慣   跟redis集羣搭建並沒任何軟用)docker

  4.安裝 vim   # yum install vim     (屬實我的習慣   跟redis集羣搭建並沒任何軟用)shell

  5.ContOS 7.4 64 (mysql 優化工具 pt-query-digest 學習) root 123456:mysql 123456      跟我們不要緊vim

   

  虛擬機明明上的ip地址已經標明

2.我用的linux遠程工具是xshell 4 連接這六臺虛擬機

  

3.安裝docker(來自小田吃餃子-博客園docker 安裝http://www.javashuo.com/article/p-vaxwojtw-dy.html

  安裝完而且啓動docker

5.拉取redis4.0鏡像而且啓動

  注意:每個虛擬機上都是如此配置

  docker安裝redis詳情步驟 (來自小田吃餃子-博客園docker 安裝redishttp://www.javashuo.com/article/p-unjfqrdv-cd.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.

文章來源:http://www.javashuo.com/article/p-hgdndssx-cs.html 

相關文章
相關標籤/搜索