基于docker实现Redis集群(3主3从)

  • 按照ruby,根据不同系统选择安装

sudo apt-get install ruby
sudo gem install redis

yum install ruby
yum install rubygems
  • 下载redis源码,要用到redis-trib.rb这个工具,在源码的src下面 wget http://download.redis.io/releases/redis-4.0.7.tar.gz

  • 启动三台redis服务器

  • 为了方便修改,贴一个简介版的配置文件cluster1.conf,修改一下端口和日志文件

bind 0.0.0.0
protected-mode no
port 36371
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile "/var/log/redis/redis_36371.log"

# 开启集群模式,把注释#去掉
cluster-enabled yes
# 集群的配置,配置文件首次启动自动生成
cluster-config-file /conf/conf/36371.conf
# 请求超时,设置10秒
cluster-node-timeout 10000

databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-del-sync-files no
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread yes


  • 依次启动三台redis

guofu@guofu-Inspiron-3558 src $  docker run --rm -d -p 36371:36371 --name c1  \
> -v /home/guofu/redis:/conf  \
> -v  /home/guofu/redis/log/redis:/var/log/redis  \
> -v /home/guofu/redis/data36371:/data \
> redis  
2856bd87484507735896cb7b98031394b1bdc01a1b1c48e0699736d46e522fe3
guofu@guofu-Inspiron-3558 src $ docker exec -it c1 bash 
root@2856bd874845:/data# /usr/local/bin/redis-server /conf/cluster1.conf 
root@2856bd874845:/data# 

  • 贴一下配置文件cluster1 copy.conf,其他文件对照修改

bind 0.0.0.0
protected-mode no
port 46371
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile "/var/log/redis/redis_46371.log"

# 开启集群模式,把注释#去掉
cluster-enabled yes
# 集群的配置,配置文件首次启动自动生成
cluster-config-file /conf/conf/46371.conf
# 请求超时,设置10秒
cluster-node-timeout 10000
#replicaof 172.17.0.2 36371
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-del-sync-files no
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread yes

  • 查看一下dokcer容器的情况

guofu@guofu-Inspiron-3558 conf $ docker ps  -a
CONTAINER ID        IMAGE                    COMMAND                  CREATED              STATUS                      PORTS                                                                                                    NAMES
be0f766e78dc        redis                    "docker-entrypoint.s…"   About a minute ago   Up About a minute           6379/tcp, 0.0.0.0:46372->46372/tcp                                                                       s2
e9ca614bd84d        redis                    "docker-entrypoint.s…"   3 minutes ago        Up 3 minutes                6379/tcp, 0.0.0.0:46371->46371/tcp                                                                       s1
4b8d13a1f0f5        redis                    "docker-entrypoint.s…"   4 minutes ago        Up 4 minutes                6379/tcp, 0.0.0.0:46373->46373/tcp                                                                       s3
23be782c35ac        redis                    "docker-entrypoint.s…"   39 minutes ago       Up 39 minutes               6379/tcp, 0.0.0.0:36373->36373/tcp                                                                       c3
6ca8afa0c4c7        redis                    "docker-entrypoint.s…"   40 minutes ago       Up 40 minutes               6379/tcp, 0.0.0.0:36372->36372/tcp                                                                       c2
3ef311bfb9bd        redis                    "docker-entrypoint.s…"   42 minutes ago       Up 42 minutes               6379/tcp, 0.0.0.0:36371->36371/tcp                                                                       c1

  • 查看容器ip


guofu@guofu-Inspiron-3558 conf $ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
3ee830381e47        artipub_default     bridge              local
b1cdce82cf33        bridge              bridge              local
dd70fc583dd8        docker_default      bridge              local
883be7758b3b        host                host                local
258fabed914d        none                null                local
guofu@guofu-Inspiron-3558 conf $  docker network inspect b1cdce82cf33

基于docker实现Redis集群(3主3从)_第1张图片

image.png

  • 记住ip,我这里截取了一部分

  • 然后开始启动集群(按照 从主到从 的方式 从左到右 依次排列 6 个 redis 节点。)

guofu@guofu-Inspiron-3558 src $ sudo ./redis-trib.rb create --replicas 1 172.17.0.2:36371 172.17.0.11:46371 172.17.0.8:36372 172.17.0.12:46372 172.17.0.9:36373 172.17.0.10:46373
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
172.17.0.2:36371
172.17.0.11:46371
172.17.0.8:36372
Adding replica 172.17.0.9:36373 to 172.17.0.2:36371
Adding replica 172.17.0.10:46373 to 172.17.0.11:46371
Adding replica 172.17.0.12:46372 to 172.17.0.8:36372
M: 06cd500875a5919fa9e5d7346b4468c1edd6a602 172.17.0.2:36371
   slots:0-5460 (5461 slots) master
M: 24a835dfd385e5ab76a556f24866a2b045366a05 172.17.0.11:46371
   slots:5461-10922 (5462 slots) master
M: 7b77a53ce0ad22b90746f97bc7df16d02452c97b 172.17.0.8:36372
   slots:10923-16383 (5461 slots) master
S: f76149f3ad29a943f3171865c5d2ec07b5cdf4dc 172.17.0.12:46372
   replicates 7b77a53ce0ad22b90746f97bc7df16d02452c97b
S: 0786ff480d34c49c2140195a617c2985c2b41fa4 172.17.0.9:36373
   replicates 06cd500875a5919fa9e5d7346b4468c1edd6a602
S: 40a55f712045343afb3e12460b8bba43e839030f 172.17.0.10:46373
   replicates 24a835dfd385e5ab76a556f24866a2b045366a05
Can I set the above configuration? (type 'yes' to accept): yes
>>> 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 172.17.0.2:36371)
M: 06cd500875a5919fa9e5d7346b4468c1edd6a602 172.17.0.2:36371
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 7b77a53ce0ad22b90746f97bc7df16d02452c97b 172.17.0.8:36372
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: f76149f3ad29a943f3171865c5d2ec07b5cdf4dc 172.17.0.12:46372
   slots: (0 slots) slave
   replicates 7b77a53ce0ad22b90746f97bc7df16d02452c97b
S: 0786ff480d34c49c2140195a617c2985c2b41fa4 172.17.0.9:36373
   slots: (0 slots) slave
   replicates 06cd500875a5919fa9e5d7346b4468c1edd6a602
M: 24a835dfd385e5ab76a556f24866a2b045366a05 172.17.0.11:46371
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 40a55f712045343afb3e12460b8bba43e839030f 172.17.0.10:46373
   slots: (0 slots) slave
   replicates 24a835dfd385e5ab76a556f24866a2b045366a05
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
guofu@guofu-Inspiron-3558 src $ pwd
/home/guofu/redis/redis-4.0.7/src
guofu@guofu-Inspiron-3558 src $ 

  • 至此,集群创建完毕。里面的信息自己看一下就可以明白

  • 测试一下客户端连接

root@23be782c35ac:/data# /usr/local/bin/redis-cli -c  -h 127.0.0.1 -p 36373
127.0.0.1:36373> set ll hh
-> Redirected to slot [9069] located at 172.17.0.11:46371
OK
172.17.0.11:46371> get ll
"hh"
172.17.0.11:46371> 


root@f31d8b4844ee:/data# /usr/local/bin/redis-cli -c -h 127.0.0.1 -p 36371
127.0.0.1:36371> get ll
-> Redirected to slot [9069] located at 172.17.0.11:46371
"hh"
172.17.0.11:46371> 

  • 看一下相关日志: 基于docker实现Redis集群(3主3从)_第2张图片基于docker实现Redis集群(3主3从)_第3张图片

  • 小伙伴们,你们搭建成功了吗?遇到问题请给我留言吧,或者关注我的公众号 老a技术联盟 个人博客

你可能感兴趣的:(基于docker实现Redis集群(3主3从))