Docker-Compose如何搭建Redis集群
1. 集群配置
3主+3从
由于仅用于测试,故我这里只用1台服务器进行模拟
redis列表
2.编写redis.conf
在server上创建一个目录用于存放redis集群部署文件。这里我放的路径为/root/redis-cluster
在/opt/docker/redis-cluster目录下创建redis-1,redis-2,redis-3,redis-4,redis-5,redis-6文件夹
mkdir -p /opt/docker/redis-cluster/{redis-1,redis-2,redis-3,redis-4,redis-5,redis-6} #创建持久化目录 mkdir -p /opt/docker/redis-cluster/redis-1/data mkdir -p /opt/docker/redis-cluster/redis-2/data mkdir -p /opt/docker/redis-cluster/redis-3/data mkdir -p /opt/docker/redis-cluster/redis-4/data mkdir -p /opt/docker/redis-cluster/redis-5/data mkdir -p /opt/docker/redis-cluster/redis-6/data登录后复制
3. 在每个redis-*文件夹下创建redis.conf文件,并写入如下内容:
cluster-enabled yes # 开启集群 cluster-config-file nodes.conf # 集群配置文件 cluster-node-timeout 5000 # 集群节点多少时间未响应视为该节点丢失 appendonly yes port 6379 # redis监听端口 masterauth passwd123 #设置master节点密码 requirepass passwd123 #设置密码登录后复制
注意:port值不能都为6379,根据上面redis列表设置的端口号,依次给redis-1 ~ redis-6设置6379~6384端口号
4.编写docker-compose.yml文件
这里直接使用daocloud提供的redis镜像地址daocloud.io/library/redis:6.0.4
在/root/redis-cluster文件夹下创建docker-compose.yml文件。
docker-compose.yml文件内容如下:
version: '3.1' services: # redis1配置 redis1: image: daocloud.io/library/redis:6.0.4 container_name: redis-1 restart: always network_mode: "host" volumes: - /opt/docker/redis-cluster/redis-1/data:/data - /opt/docker/redis-cluster/redis-1/redis.conf:/usr/local/etc/redis/redis.conf command: ["redis-server", "/usr/local/etc/redis/redis.conf"] # redis2配置 redis2: image: daocloud.io/library/redis:6.0.4 container_name: redis-2 restart: always network_mode: "host" volumes: - /opt/docker/redis-cluster/redis-2/data:/data - /opt/docker/redis-cluster/redis-2/redis.conf:/usr/local/etc/redis/redis.conf command: ["redis-server", "/usr/local/etc/redis/redis.conf"] # redis3配置 redis3: image: daocloud.io/library/redis:6.0.4 container_name: redis-3 restart: always network_mode: "host" volumes: - /opt/docker/redis-cluster/redis-3/data:/data - /opt/docker/redis-cluster/redis-3/redis.conf:/usr/local/etc/redis/redis.conf command: ["redis-server", "/usr/local/etc/redis/redis.conf"] # redis4配置 redis4: image: daocloud.io/library/redis:6.0.4 container_name: redis-4 restart: always network_mode: "host" volumes: - /opt/docker/redis-cluster/redis-4/data:/data - /opt/docker/redis-cluster/redis-4/redis.conf:/usr/local/etc/redis/redis.conf command: ["redis-server", "/usr/local/etc/redis/redis.conf"] # redis5配置 redis5: image: daocloud.io/library/redis:6.0.4 container_name: redis-5 restart: always network_mode: "host" volumes: - /opt/docker/redis-cluster/redis-5/data:/data - /opt/docker/redis-cluster/redis-5/redis.conf:/usr/local/etc/redis/redis.conf command: ["redis-server", "/usr/local/etc/redis/redis.conf"] # redis6配置 redis6: image: daocloud.io/library/redis:6.0.4 container_name: redis-6 restart: always network_mode: "host" volumes: - /opt/docker/redis-cluster/redis-6/data:/data - /opt/docker/redis-cluster/redis-6/redis.conf:/usr/local/etc/redis/redis.conf command: ["redis-server", "/usr/local/etc/redis/redis.conf"]登录后复制
启动容器,执行命令:
#启动容器 docker-compose -f xxx.yaml up -d登录后复制
5.开启集群
随便找一个容器进入,这里我选择redis-1进入。 在进入容器后,输入如下命令开启集群:
redis-cli --cluster create 192.168.66.180:6379 \ 192.168.66.180:6380 \ 192.168.66.180:6381 \ 192.168.66.180:6382 \ 192.168.66.180:6383 \ 192.168.66.180:6384 \ --cluster-replicas 1登录后复制
出现下图即为成功。
6.测试
使用redis-cli命令,连接集群中任意节点。我随便在一台能够ping通集群所在IP的电脑上进行了测试,这里我使用了本机
6.1 查看节点属性
redis-cli -c -h 192.168.66.180 -p 6379 192.168.66.180:6379> cluster info登录后复制
显示为如下图,即为集群健康状态
6.2 查看节点信息
cluster nodes登录后复制
注意看图中的slave,master,myself等关键字。
6.3 插入一个值
192.168.66.180:6379> set test 'hello world' -> Redirected to slot [6918] located at 192.168.66.180:6380 OK 192.168.66.180:6380>登录后复制
注意:这里根据切片自动切换到了该数据分片所在的节点上,所以下面可以看到连接的节点变为了192.168.1.4:6380
get test
168.66.180:6379> set test 'hello world' -> Redirected to slot [6918] located at 192.168.66.180:6380 OK 192.168.66.180:6380> get test "hello world" 192.168.66.180:6380>登录后复制
这里因为就是在该数据所在分片上进行的查询,所以连接节点没有改变。
7.添加密码:
7.1 编辑redis.conf文件
#为redis.conf文件添加如下配置。这里设置密码为123456 masterauth 123456 requirepass 123456登录后复制
7.2 编辑docker-compose.yml文件
#为docker-compose.yml中每个容器添加如下配置: environment: - REDISCLI_AUTH=123456登录后复制
7.3 连接集群
redis-cli -c -h 192.168.66.180 -p 6379 -a 123456登录后复制【文章原创作者:cc防御 http://www.558idc.com/gfip.html提供,感恩】