基于docker-compose的redis cluster集群——Composing a Redis Cluster on Docker Containers + Redis集群密码设置

做好的实例:

git clone https://github.com/yangyuanpeng525/redis_cluster-docker-compose
其中包含redis版本:4.0.10 5.0.9 6.0.7

详细步骤:

目标:

3主3从
master---->slave 一 一对应

1修改docker-compose.yaml文件

进入工作目录:
cd redis-cluster

version: "3.7"
services:
  redis6-master1:
    image: redis:4.0.10
    restart: always
    container_name: redis6-master1
    network_mode: host
    environment:
      - TZ=Asia/Shanghai
    command: /usr/local/bin/redis-server  /usr/local/etc/redis/redis.conf
    volumes:
      - ./conf1/redis.conf:/usr/local/etc/redis/redis.conf
      - ./data1:/data
      - ./log1:/var/log/redis


  redis6-master2:
    image: redis:4.0.10
    restart: always
    container_name: redis6-master2
    network_mode: host
    environment:
      - TZ=Asia/Shanghai
    command: /usr/local/bin/redis-server  /usr/local/etc/redis/redis.conf
    volumes:
      - ./conf2/redis.conf:/usr/local/etc/redis/redis.conf
      - ./data2:/data
      - ./log2:/var/log/redis



  redis6-master3:
    image: redis:4.0.10
    restart: always
    container_name: redis6-master3
    network_mode: host
    environment:
      - TZ=Asia/Shanghai
    command: /usr/local/bin/redis-server  /usr/local/etc/redis/redis.conf
    volumes:
      - ./conf3/redis.conf:/usr/local/etc/redis/redis.conf
      - ./data3:/data
      - ./log3:/var/log/redis



  redis6-slave1:
    image: redis:4.0.10
    restart: always
    container_name: redis6-slave1
    network_mode: host
    environment:
      - TZ=Asia/Shanghai
    command: /usr/local/bin/redis-server  /usr/local/etc/redis/redis.conf
    volumes:
      - ./conf4/redis.conf:/usr/local/etc/redis/redis.conf
      - ./data4:/data
      - ./log4:/var/log/redis



  redis6-slave2:
    image: redis:4.0.10
    restart: always
    container_name: redis6-slave2
    network_mode: host
    environment:
      - TZ=Asia/Shanghai
    command: /usr/local/bin/redis-server  /usr/local/etc/redis/redis.conf
    volumes:
      - ./conf5/redis.conf:/usr/local/etc/redis/redis.conf
      - ./data5:/data
      - ./log5:/var/log/redis





  redis6-slave3:
    image: redis:4.0.10
    restart: always
    container_name: redis6-slave3
    network_mode: host
    environment:
      - TZ=Asia/Shanghai
    command: /usr/local/bin/redis-server  /usr/local/etc/redis/redis.conf
    volumes:
      - ./conf6/redis.conf:/usr/local/etc/redis/redis.conf
      - ./data6:/data
      - ./log6:/var/log/redis
2添加配置文件

#在工作目录下创建conf{1…6}
mkdir conf{1…6}

#由于redis不支持NAT网络模式,docker-compose不能采用常用的网桥+端口映射,应采用network_mode: host模式,上文docker-compose.yaml文件中有所体现。

#容器的端口直接在主机上。所以6个容器不能采用相同端口,这是下面的redis.conf里唯一的不同,我采用的6318/6328/6338/6348/6358/6368这六个端口。

#下面只给一个redis.conf的模板,修改对应容器端口即可。

#不使用host模式:创建集群的时候会遇到

Waiting for the cluster to join....................

一直没有成功

#解决放方案:
网络模式改成host,docker run 的方式加上–net host,docker-compose方式加上network_mode: host

#第一次创建集群时不要设置密码,不然后面添加节点到集群会报错,集群搭建成功再设置密码。

bind 0.0.0.0 ::1
protected-mode yes
port 6318   #修改
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile /var/log/redis/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
#requirepass admin123
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
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
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000

redis.conf放在各自的conf{1…6}文件夹下。
完成这个就不需要再进行任何配置了。

核对目录结构:

[root@docker3 redis4-cluster]# ll
drwxr-xr-x 2 root root   24 9月   2 15:19 conf1
drwxr-xr-x 2 root root   24 9月   2 15:19 conf2
drwxr-xr-x 2 root root   24 9月   2 15:19 conf3
drwxr-xr-x 2 root root   24 9月   2 15:19 conf4
drwxr-xr-x 2 root root   24 9月   2 15:19 conf5
drwxr-xr-x 2 root root   24 9月   2 15:19 conf6
-rw-r--r-- 1 root root 2730 9月   2 15:50 docker-compose.yaml



3启动

在工作目录下执行

docker-compose up -d
docker-compose ps
ll
#会生成这些目录
drwxr-xr-x 2 root root   24 9月   2 15:19 conf1
drwxr-xr-x 2 root root   24 9月   2 15:19 conf2
drwxr-xr-x 2 root root   24 9月   2 15:19 conf3
drwxr-xr-x 2 root root   24 9月   2 15:19 conf4
drwxr-xr-x 2 root root   24 9月   2 15:19 conf5
drwxr-xr-x 2 root root   24 9月   2 15:19 conf6
drwxr-xr-x 2 root root   46 9月   2 16:47 data1
drwxr-xr-x 2 root root   46 9月   2 16:47 data2
drwxr-xr-x 2 root root   46 9月   2 16:47 data3
drwxr-xr-x 2 root root   46 9月   2 16:47 data4
drwxr-xr-x 2 root root   46 9月   2 16:47 data5
drwxr-xr-x 2 root root   46 9月   2 16:47 data6
-rw-r--r-- 1 root root 2730 9月   2 15:50 docker-compose.yaml
drwxr-xr-x 2 root root   23 9月   2 16:47 log1
drwxr-xr-x 2 root root   23 9月   2 16:47 log2
drwxr-xr-x 2 root root   23 9月   2 16:47 log3
drwxr-xr-x 2 root root   23 9月   2 16:47 log4
drwxr-xr-x 2 root root   23 9月   2 16:47 log5
drwxr-xr-x 2 root root   23 9月   2 16:47 log6

选择任意端口检测cluster是否开启:

echo 'info cluster' |  redis-cli  -p 6318
#结果为1表示开启成功
# Cluster
cluster_enabled:1

4 使用redis-trib将节点加入集群

在工作目录下:

ll add_replica_to_master.sh

-rwxr-xr-x 1 root root 197 9月   2 15:19 add_replica_to_master.sh

cat add_replica_to_master.sh 


#!/bin/bash
docker run --rm -it zvelo/redis-trib create --replicas 1  192.168.200.113:6318  192.168.200.113:6328 192.168.200.113:6338 192.168.200.113:6348 192.168.200.113:6358 192.168.200.113:6368

#zvelo/redis-trib这个镜像没有会自己从公网拉
#1表示一个master只有一个slave
#六个redis的顺序为:master1  master2 master3  slave1  slave2  slave3

#会命令行交互,输入 ‘yes’继续。

在这里插入图片描述

下图为加入集群正常的状态:
在这里插入图片描述
到数据库内查询:
info
在这里插入图片描述
#集群连接方式:
-c

redis-cli -c -h 127.0.0.1 -p 6318

Redis集群密码设置

1,如果是使用redis-trib.rb工具构建集群,集群构建完成前不要配置密码,集群构建完毕再通过config set + config rewrite命令逐个机器设置密码

2,如果对集群设置密码,那么requirepass和masterauth都需要设置,否则发生主从切换时,就会遇到授权问题,可以模拟并观察日志

3,各个节点的密码都必须一致,否则Redirected就会失败

一 密码设置
方式一:修改所有Redis集群中的redis.conf文件

masterauth 1234
requirepass 1234
注意:所有节点的密码都必须一致,masterauth也要加的。

说明:这种方式需要重新启动各节点

方式二:进入各个实例通过config set设置
127.0.0.1:6318> config set masterauth 1234
OK 
127.0.0.1:6318> config set requirepass 1234
OK 
127.0.0.1:6318> auth 1234
OK 
127.0.0.1:6318> config rewrite 
OK 
127.0.0.1:6318> exit 

之后分别使用./redis-cli -c -p 6328,./redis-cli -c -p 6338……命令给各节点设置上密码。

注意:各个节点密码都必须一致,否则Redirected就会失败, 推荐这种方式,这种方式会把密码写入到redis.conf里面去,且不用重启。

二 通过指令找到安装的redis在ruby环境中的配置client.rb(传统部署)

用方式二修改密码,./redis-trib.rb check 10.104.111.174:6379执行时可能会报[ERR] Sorry, can’t connect to node 10.104.111.174:6379,因为6379的redis.conf没找到密码配置。
设置密码之后如果需要使用redis-trib.rb的各种命令
如:./redis-trib.rb check 127.0.0.1:6318,则会报错ERR] Sorry, can’t connect to node 127.0.0.1:6318
解决办法:vim /usr/local/rvm/gems/ruby-2.3.3/gems/redis-4.0.0/lib/redis/client.rb,然后修改passord

find / -name “client.rb”


编辑该文件

vim /usr/local/rvm/gems/ruby-2.4.1/gems/redis-4.0.1/lib/redis/client.rb
#目录有差异


#之后重启Redis。
三 带密码访问集群

./redis-cli -c -p 6318 -a 1234

#参考文章
https://www.cnblogs.com/brithToSpring/p/13187206.html
https://www.cnblogs.com/chenchuxin/p/8404699.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值