这里用docker-compose在一台机器搭建三主三从,生产环境肯定是在多台机器搭建,否则一旦这台宿主机挂了,redis集群全挂了,依然是单点故障。同时,受机器性能极限影响,其并发也上不去,算不上高并发。
单机存在的问题:
主从存在的问题:
哨兵模式存在的问题:
集群存在问题:
技术都有优点和缺点,适合才是最好
服务器宿主机IP
192.168.123.182
创建redis
集群配置基本目录
mkdir -p /root/redis-cluster
切换至redis集群配置基本目录
cd /root/redis-cluster
编写redis.conf
配置模板文件redis-cluster.tmpl
# 端口号
port ${PORT}
# 绑定ip
bind 0.0.0.0
# redis-cli访问密码
requirepass 123456i
# 访问主节点密码
masterauth 123456i
# 保护模式,默认是yes,开启保护模式
protected-mode no
# 是否守护模式,docker方式不需要
daemonize no
# 是否开启AOF持久化模式
appendonly yes
# 是否开启集群模式
cluster-enabled yes
# 集群节点信息文件
cluster-config-file nodes.conf
# 集群节点连接超时时间
cluster-node-timeout 15000
# 集群节点宿主机IP
cluster-announce-ip 192.168.123.182
# 集群节点映射端口
cluster-announce-port ${PORT}
# 集群节点总线端口
cluster-announce-bus-port 1${PORT}
改配置模板,前面是redis.conf单机时常规配置,只是最后六条配置是redis集群配置
下面for循环实现6个redis配置文件生成:
for port in `seq 6380 6385`
do
mkdir -p ${port}/{conf,data} \
&& PORT=${port} envsubst < redis-cluster.tmpl > ${port}/conf/redis.conf
done
docker-compose.yml
文件编写:
version: "3.3"
# 定义服务
services:
redis-6380: # 服务名称
image: redis # 容器镜像
container_name: redis-6380 # 容器名称
restart: always # 容器总是重启
network_mode: "host" # host 网络模式
ports: # 映射端口
- 6380:6380
volumes: # 目录挂载
- /root/redis-cluster/6380/conf/redis.conf:/usr/local/redis/redis.conf
- /root/redis-cluster/6380/data:/data
command: redis-server /usr/local/redis/redis.conf # 服务启动命令
redis-6381:
image: redis
container_name: redis-6381
network_mode: "host"
ports:
- 6381:6381
volumes:
- /root/redis-cluster/6381/conf/redis.conf:/usr/local/redis/redis.conf
- /root/redis-cluster/6381/data:/data
command: redis-server /usr/local/redis/redis.conf
redis-6382:
image: redis
container_name: redis-6382
network_mode: "host"
ports:
- 6382:6382
volumes:
- /root/redis-cluster/6382/conf/redis.conf:/usr/local/redis/redis.conf
- /root/redis-cluster/6382/data:/data
command: redis-server /usr/local/redis/redis.conf
redis-6383:
image: redis
container_name: redis-6383
network_mode: "host"
ports:
- 6383:6383
volumes:
- /root/redis-cluster/6383/conf/redis.conf:/usr/local/redis/redis.conf
- /root/redis-cluster/6383/data:/data
command: redis-server /usr/local/redis/redis.conf
redis-6384:
image: redis
container_name: redis-6384
network_mode: "host"
ports:
- 6384:6384
volumes:
- /root/redis-cluster/6384/conf/redis.conf:/usr/local/redis/redis.conf
- /root/redis-cluster/6384/data:/data
command: redis-server /usr/local/redis/redis.conf
redis-6385:
image: redis
container_name: redis-6385
network_mode: "host"
ports:
- 6385:6385
volumes:
- /root/redis-cluster/6385/conf/redis.conf:/usr/local/redis/redis.conf
- /root/redis-cluster/6385/data:/data
command: redis-server /usr/local/redis/redis.conf
编写完毕,整体情况:
根据docker-compose.yml
文件启动服务:
服务创建后,通过如下操作实现redis集群创建
# 进入其中一个redis容器内
docker exec -it redis-6380 /bin/bash
# 用redis-cli创建整个redis集群
# -a 表示连接的密码
# --cluster create 表示需要集群的redis主机和端口
# --cluster-replicas 1 其中的1表示一主一从
redis-cli -a 123456i --cluster create 192.168.123.182:6380 192.168.123.182:6381 192.168.123.182:6382 192.168.123.182:6383 192.168.123.182:6384 192.168.123.182:6385 --cluster-replicas 1
查看集群状态:
redis-cli -a 123456i --cluster check 192.168.123.182:6380
redis-cli -p 6380 -a 123456i
# 查看集群信息
cluster info
# 查看集群节点
cluster nodes
这里的镜像是:
redis-7.2.4