docker-compose部署Redis-Cluster集群

小柒助手 Docker评论6731字数 1812阅读6分2秒阅读模式

一、Redis简介

Redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。

Redis 是一个高性能的key-value数据库。 redis的出现,很大程度补偿了memcached这类key/value存储的不足,在部 分场合可以对关系数据库起到很好的补充作用。它提供了Java,C/C++,C#,PHP,JavaScript,Perl,Object-C,Python,Ruby,Erlang等客户端,使用很方便。
文章源自小柒网-https://www.yangxingzhen.cn/8546.html

Redis支持主从同步。数据可以从主服务器向任意数量的从服务器上同步,从服务器可以是关联其他从服务器的主服务器。这使得Redis可执行单层树复制。存盘可以有意无意的对数据进行写操作。由于完全实现了发布/订阅机制,使得从数据库在任何地方同步树时,可订阅一个频道并接收主服务器完整的消息发布记录。同步对读取操作的可扩展性和数据冗余很有帮助。
文章源自小柒网-https://www.yangxingzhen.cn/8546.html

Redis的官网地址,非常好记,是redis.io。(域名后缀io属于国家域名,是british Indian Ocean territory,即英属印度洋领地),Vmware在资助着redis项目的开发和维护。
文章源自小柒网-https://www.yangxingzhen.cn/8546.html

从2010年3月15日起,Redis的开发工作由VMware主持。从2013年5月开始,Redis的开发由Pivotal赞助。
文章源自小柒网-https://www.yangxingzhen.cn/8546.html

二、安装docker-compose

Linux上我们可以从Github上下载它的二进制包来使用,最新发行的版本地址:https://github.com/docker/compose/releases
文章源自小柒网-https://www.yangxingzhen.cn/8546.html

1、运行以下命令以下载Docker Compose的v2.11.0版本
文章源自小柒网-https://www.yangxingzhen.cn/8546.html

[root@k8s-master ~]# wget -O /usr/bin/docker-compose https://github.com/docker/compose/releases/download/v2.11.0/docker-compose-linux-x86_64
文章源自小柒网-https://www.yangxingzhen.cn/8546.html

2、授权可执行权限
文章源自小柒网-https://www.yangxingzhen.cn/8546.html

[root@k8s-master ~]# chmod +x /usr/bin/docker-compose
文章源自小柒网-https://www.yangxingzhen.cn/8546.html

3、测试是否安装成功
文章源自小柒网-https://www.yangxingzhen.cn/8546.html

[root@k8s-master ~]# docker-compose -v

Docker Compose version v2.11.0

三、部署Redis-Cluster

1、创建Redis目录

[root@k8s-master ~]# mkdir -p /opt/redis-cluster

2、创建docker-compose配置文件

[root@k8s-master ~]# cd /opt/redis-cluster

[root@k8s-master redis-cluster]# vim docker-compose.yml

version: '3.3'
services:
  redis-cluster-master-1:
    image: redis:6.2.7
    container_name: redis-cluster-master-1
    ports:
      - 6379:6379
      - 16379:16379
    command:
      - "redis-server"
      - "/etc/redis/redis.conf"
      - "--protected-mode"
      - "no"
      - "--cluster-announce-ip 10.10.50.24"
      - "--cluster-announce-port"
      - "6379"
      - "--cluster-announce-bus-port"
      - "16379"
    volumes:
      - ./redis.conf:/etc/redis/redis.conf
      - ./master1:/data
      - /etc/localtime:/etc/localtime
  redis-cluster-master-2:
    image: redis:6.2.7
    container_name: redis-cluster-master-2
    ports:
      - 6380:6379
      - 16380:16379
    command:
      - "redis-server"
      - "/etc/redis/redis.conf"
      - "--protected-mode"
      - "no"
      - "--cluster-announce-ip 10.10.50.24"
      - "--cluster-announce-port"
      - "6380"
      - "--cluster-announce-bus-port"
      - "16380"
    volumes:
      - ./redis.conf:/etc/redis/redis.conf
      - ./master2:/data
      - /etc/localtime:/etc/localtime
  redis-cluster-master-3:
    image: redis:6.2.7
    container_name: redis-cluster-master-3
    ports:
      - 6381:6379
      - 16381:16379
    command:
      - "redis-server"
      - "/etc/redis/redis.conf"
      - "--protected-mode"
      - "no"
      - "--cluster-announce-ip 10.10.50.24"
      - "--cluster-announce-port"
      - "6381"
      - "--cluster-announce-bus-port"
      - "16381"
    volumes:
      - ./redis.conf:/etc/redis/redis.conf
      - ./master3:/data
      - /etc/localtime:/etc/localtime
  redis-cluster-slave-1:
    image: redis:6.2.7
    container_name: redis-cluster-slave-1
    ports:
      - 6382:6379
      - 16382:16379
    command:
      - "redis-server"
      - "/etc/redis/redis.conf"
      - "--protected-mode"
      - "no"
      - "--cluster-announce-ip 10.10.50.24"
      - "--cluster-announce-port"
      - "6382"
      - "--cluster-announce-bus-port"
      - "16382"
    volumes:
      - ./redis.conf:/etc/redis/redis.conf
      - ./slave1:/data
      - /etc/localtime:/etc/localtime
  redis-cluster-slave-2:
    image: redis:6.2.7
    container_name: redis-cluster-slave-2
    ports:
      - 6383:6379
      - 16383:16379
    command:
      - "redis-server"
      - "/etc/redis/redis.conf"
      - "--protected-mode"
      - "no"
      - "--cluster-announce-ip 10.10.50.24"
      - "--cluster-announce-port"
      - "6383"
      - "--cluster-announce-bus-port"
      - "16383"
    volumes:
      - ./redis.conf:/etc/redis/redis.conf
      - ./slave2:/data
      - /etc/localtime:/etc/localtime
  redis-cluster-slave-3:
    image: redis:6.2.7
    container_name: redis-cluster-slave-3
    ports:
      - 6384:6379
      - 16384:16379
    command:
      - "redis-server"
      - "/etc/redis/redis.conf"
      - "--protected-mode"
      - "no"
      - "--cluster-announce-ip 10.10.50.24"
      - "--cluster-announce-port"
      - "6384"
      - "--cluster-announce-bus-port"
      - "16384"
    volumes:
      - ./redis.conf:/etc/redis/redis.conf
      - ./slave3:/data
      - /etc/localtime:/etc/localtime

3、创建Redis配置文件

[root@k8s-master redis-cluster]# vim redis.conf

bind 0.0.0.0
port 6379
appendonly yes
masterauth Aa123456
requirepass Aa123456
io-threads-do-reads yes
io-threads 3
maxmemory 1G
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
  • cluster-announce-ip 10.10.50.24 # 指定集群访问ip
  • cluster-announce-port 6379 # 指定集群每个容器对外端口
  • cluster-announce-bus-port 16379 # 指定集群每个容器对外端口

4、启动容器

[root@k8s-master redis-cluster]# docker-compose up -d

5、查看容器状态

[root@k8s-master redis-cluster]# docker-compose ps

NAME                            COMMAND                    SERVICE                       STATUS              PORTS

redis-cluster-master-1   "docker-entrypoint.s…"   redis-cluster-master-1   running             0.0.0.0:6379->6379/tcp, 0.0.0.0:16379->16379/tcp

redis-cluster-master-2   "docker-entrypoint.s…"   redis-cluster-master-2   running             0.0.0.0:6380->6379/tcp, 0.0.0.0:16380->16379/tcp

redis-cluster-master-3   "docker-entrypoint.s…"   redis-cluster-master-3   running             0.0.0.0:6381->6379/tcp, 0.0.0.0:16381->16379/tcp

redis-cluster-slave-1    "docker-entrypoint.s…"   redis-cluster-slave-1    running             0.0.0.0:6382->6379/tcp, 0.0.0.0:16382->16379/tcp

redis-cluster-slave-2    "docker-entrypoint.s…"   redis-cluster-slave-2    running             0.0.0.0:6383->6379/tcp, 0.0.0.0:16383->16379/tcp

redis-cluster-slave-3    "docker-entrypoint.s…"   redis-cluster-slave-3    running             0.0.0.0:6384->6379/tcp, 0.0.0.0:16384->16379/tcp

四、配置Redis-Cluster集群并自动分配slot

1、初始化集群

# 一共有6个节点,当选项--cluster-replicas指定为1时,表示为每个Master节点分配一个Slave节点,这样集群中刚好3个Master节点和3个Slave节点

[root@k8s-master redis-cluster]# docker exec -it redis-cluster-master-1 bash

root@57000c3be04f:/data# redis-cli -a Aa123456 --cluster create 10.10.50.24:6379 10.10.50.24:6380 10.10.50.24:6381 10.10.50.24:6382 10.10.50.24:6382 10.10.50.24:6383 --cluster-replicas 1

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

>>> Performing hash slots allocation on 6 nodes...

Master[0] -> Slots 0 - 5460

Master[1] -> Slots 5461 - 10922

Master[2] -> Slots 10923 - 16383

Adding replica 10.10.50.24:6382 to 10.10.50.24:6379

Adding replica 10.10.50.24:6383 to 10.10.50.24:6380

Adding replica 10.10.50.24:6382 to 10.10.50.24:6381

>>> Trying to optimize slaves allocation for anti-affinity

[WARNING] Some slaves are in the same host as their master

M: 23a71107dbcd84f83cb9370897f7e996eb931880 10.10.50.24:6379

   slots:[0-5460] (5461 slots) master

M: 73198acc4ea6cf00d93cfff762afb5d0506f9dba 10.10.50.24:6380

   slots:[5461-10922] (5462 slots) master

M: dce7173806bf271180102ad074760080122fbc5d 10.10.50.24:6381

   slots:[10923-16383] (5461 slots) master

S: 068ecdab974b8238c32fe14e2a99fde8a5c0504a 10.10.50.24:6382

   replicates 73198acc4ea6cf00d93cfff762afb5d0506f9dba

S: 068ecdab974b8238c32fe14e2a99fde8a5c0504a 10.10.50.24:6382

   replicates dce7173806bf271180102ad074760080122fbc5d

S: cdccedafe50a4d0231e22c1ba7ef3175bf73b3fb 10.10.50.24:6383

   replicates 23a71107dbcd84f83cb9370897f7e996eb931880

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 10.10.50.24:6379)

M: 23a71107dbcd84f83cb9370897f7e996eb931880 10.10.50.24:6379

   slots:[0-5460] (5461 slots) master

   1 additional replica(s)

S: 068ecdab974b8238c32fe14e2a99fde8a5c0504a 10.10.50.24:6382

   slots: (0 slots) slave

   replicates dce7173806bf271180102ad074760080122fbc5d

M: 73198acc4ea6cf00d93cfff762afb5d0506f9dba 10.10.50.24:6380

   slots:[5461-10922] (5462 slots) master

S: cdccedafe50a4d0231e22c1ba7ef3175bf73b3fb 10.10.50.24:6383

   slots: (0 slots) slave

   replicates 23a71107dbcd84f83cb9370897f7e996eb931880

M: dce7173806bf271180102ad074760080122fbc5d 10.10.50.24:6381

   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.

# 可以看到16384个slot都已经创建完成,并且建立了3个master和对应的replica

2、验证集群状态

root@57000c3be04f:/data# redis-cli -p 6379

127.0.0.1:6379> auth Aa123456

OK

127.0.0.1:6379> cluster info

cluster_state:ok

cluster_slots_assigned:16384

cluster_slots_ok:16384

cluster_slots_pfail:0

cluster_slots_fail:0

cluster_known_nodes:5

cluster_size:3

cluster_current_epoch:6

cluster_my_epoch:1

cluster_stats_messages_ping_sent:355

cluster_stats_messages_pong_sent:379

cluster_stats_messages_sent:734

cluster_stats_messages_ping_received:375

cluster_stats_messages_pong_received:355

cluster_stats_messages_meet_received:4

cluster_stats_messages_received:734

127.0.0.1:6379> cluster nodes

068ecdab974b8238c32fe14e2a99fde8a5c0504a 10.10.50.24:6382@16382 slave dce7173806bf271180102ad074760080122fbc5d 0 1669447712242 3 connected

23a71107dbcd84f83cb9370897f7e996eb931880 10.10.50.24:6379@16379 myself,master - 0 1669447710000 1 connected 0-5460

73198acc4ea6cf00d93cfff762afb5d0506f9dba 10.10.50.24:6380@16380 master - 0 1669447709230 2 connected 5461-10922

cdccedafe50a4d0231e22c1ba7ef3175bf73b3fb 10.10.50.24:6383@16383 slave 23a71107dbcd84f83cb9370897f7e996eb931880 0 1669447711239 1 connected

dce7173806bf271180102ad074760080122fbc5d 10.10.50.24:6381@16381 master - 0 1669447711000 3 connected 10923-16383

3、测试Redis集群比较简单的办法就是使用redis-rb-cluster或者redis-cli,接下来我们将使用redis-cli为例来进行演示:

127.0.0.1:6379> set domain www.yangxingzhen.cn

OK

127.0.0.1:6379> keys *

1) "domain"

127.0.0.1:6379> get domain

"www.yangxingzhen.cn"

4、其他命令

1)查询槽位分配情况

127.0.0.1:6379> cluster slots

1) 1) (integer) 0

   2) (integer) 5460

   3) 1) "10.10.50.24"

      2) (integer) 6379

      3) "23a71107dbcd84f83cb9370897f7e996eb931880"

   4) 1) "10.10.50.24"

      2) (integer) 6383

      3) "cdccedafe50a4d0231e22c1ba7ef3175bf73b3fb"

2) 1) (integer) 5461

   2) (integer) 10922

   3) 1) "10.10.50.24"

      2) (integer) 6380

      3) "73198acc4ea6cf00d93cfff762afb5d0506f9dba"

3) 1) (integer) 10923

   2) (integer) 16383

   3) 1) "10.10.50.24"

      2) (integer) 6381

      3) "dce7173806bf271180102ad074760080122fbc5d"

   4) 1) "10.10.50.24"

      2) (integer) 6382

      3) "068ecdab974b8238c32fe14e2a99fde8a5c0504a"

2)查询某个key的槽位

127.0.0.1:6379> cluster keyslot domain

(integer) 5449

3)查询槽slot包含的键值对数量,需要去slot所在的容器里查询

127.0.0.1:6379> cluster countkeysinslot 5449

(integer) 1

若文章图片、下载链接等信息出错,请在评论区留言反馈,博主将第一时间更新!如本文“对您有用”,欢迎随意打赏,谢谢!

继续阅读
Wechat
微信扫一扫,加我!
weinxin
我的微信
微信号已复制
微信公众号
微信扫一扫,关注我!
weinxin
我的公众号
公众号已复制
小柒助手
  • 本文由 小柒助手 发表于 2022年11月26日 15:42:13
  • 声明:本站所有文章,如无特殊说明或标注,本站文章均为原创。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。转载请务必保留本文链接:https://www.yangxingzhen.cn/8546.html
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

拖动滑块以完成验证