3 Master 3 Slave
Since it is only for testing, I only use 1 server for simulation here
redis list
Create a directory on the server for storage redis cluster deployment file. The path I put here is /root/redis-cluster
Create redis-1, redis-2, redis-3, redis-4, redis-5 in the /opt/docker/redis-cluster directory, redis-6 folder
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
cluster-enabled yes # 开启集群 cluster-config-file nodes.conf # 集群配置文件 cluster-node-timeout 5000 # 集群节点多少时间未响应视为该节点丢失 appendonly yes port 6379 # redis监听端口 masterauth passwd123 #设置master节点密码 requirepass passwd123 #设置密码
Note: The port value cannot all be 6379 , according to the port numbers set in the redis list above, set the port numbers 6379~6384 for redis-1 ~ redis-6 in sequence
Use daocloud directly here The provided redis image address is daocloud.io/library/redis:6.0.4
Create the docker-compose.yml file in the /root/redis-cluster folder.
docker-compose.yml file content is as follows:
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"]
Start the container and execute the command:
#启动容器 docker-compose -f xxx.yaml up -d
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
redis-cli -c -h 192.168.66.180 -p 6379 192.168.66.180:6379> cluster info
cluster nodes
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>
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>
#为redis.conf文件添加如下配置。这里设置密码为123456 masterauth 123456 requirepass 123456
#为docker-compose.yml中每个容器添加如下配置: environment: - REDISCLI_AUTH=123456
redis-cli -c -h 192.168.66.180 -p 6379 -a 123456
The above is the detailed content of How Docker-Compose builds a Redis cluster. For more information, please follow other related articles on the PHP Chinese website!