docker pull redis:6.2
[root@tsxdev1 /]# clear [root@tsxdev1 /]# mkdir dockerdata [root@tsxdev1 /]# cd dockerdata/ [root@tsxdev1 dockerdata]# mkdir redis-cluster [root@tsxdev1 dockerdata]# cd redis-cluster/ [root@tsxdev1 redis-cluster]# mkdir conf [root@tsxdev1 redis-cluster]# mkdir data [root@tsxdev1 redis-cluster]# ls conf data [root@tsxdev1 redis-cluster]#
(1)# bind 127.0.0.1 -::1 注释掉,不注释只能本机访问 (2)port 7001 根据文件后缀端口修改,如果docker网络采用桥接方式,此处也可以都使用默认的6379端口,每个docker容器的网络都是独立的 (3)daemonize no ,此处必须设置成no,不然docker容器一启动就停掉。设置成yes,redis进程就以守护进程运行,docker容器检测到没有前台进程,所以容器直接停掉 (4)pidfile "/var/run/redis_7001.pid" 根据端口号修改 (5)logfile "/data/redis7001.log" 此处将log文件存放的路径设置到容器的/data路口,刚开始设置了一个其他路径,容器启动的时候提示权限不够 (6)dbfilename "dump7001.rdb" rdb数据文件的名称,按照端口号修改 (7)dir "/data" 数据文件存放的路径 (8)requirepass "123456" redis的密码,根据实际情况进行设置 (9)appendonly yes 是否开启aof持久化配置,根据实际情况配置 (10)appendfilename "appendonly7001.aof" aof文件的名称 (11)appendfsync everysec aof持久化策略 ,always 、no (12)cluster-enabled yes 开启集群模式,设置成yes (13)cluster-config-file /data/nodes-7001.conf 集群配置文件存放位置 (14)cluster-node-timeout 15000 节点通信超时时间
docker run -it -d -v /dockerdata/redis-cluster/conf:/usr/local/etc/redis -v /dockerdata/redis-cluster/data:/data --network=host --name redis-7001 redis:6.2 redis-server /usr/local/etc/redis/redis7001.conf docker run -it -d -v /dockerdata/redis-cluster/conf:/usr/local/etc/redis -v /dockerdata/redis-cluster/data:/data --network=host --name redis-7002 redis:6.2 redis-server /usr/local/etc/redis/redis7002.conf docker run -it -d -v /dockerdata/redis-cluster/conf:/usr/local/etc/redis -v /dockerdata/redis-cluster/data:/data --network=host --name redis-7003 redis:6.2 redis-server /usr/local/etc/redis/redis7003.conf docker run -it -d -v /dockerdata/redis-cluster/conf:/usr/local/etc/redis -v /dockerdata/redis-cluster/data:/data --network=host --name redis-7004 redis:6.2 redis-server /usr/local/etc/redis/redis7004.conf docker run -it -d -v /dockerdata/redis-cluster/conf:/usr/local/etc/redis -v /dockerdata/redis-cluster/data:/data --network=host --name redis-7005 redis:6.2 redis-server /usr/local/etc/redis/redis7005.conf docker run -it -d -v /dockerdata/redis-cluster/conf:/usr/local/etc/redis -v /dockerdata/redis-cluster/data:/data --network=host --name redis-7006 redis:6.2 redis-server /usr/local/etc/redis/redis7006.conf 命令说明: -it 以交互模式运行 -d 后台运行 -v /dockerdata/redis-cluster/conf:/usr/local/etc/redis -v 挂载卷,将redis配置文件挂载到宿主机,方便在宿主机更改redis的配置 -v /dockerdata/redis-cluster/data:/data 挂载卷,容器中的产生的文件跟宿主机是互相隔离的,如果容器没了,则对应的数据也会丢失,因此需要将重要的数据挂载到宿主机上面,即使容器没了,数据依然还在,如 mysql、redis的数据文件,
冒号前面表示宿主机的路径,冒号后面表示容器的路径,/data是容器默认的工作路径。 --network=host docker的网络模式,此处选用host模式 --name redis-7001 docker容器的名字 redis:6.2 docker镜像的名字 redis-server /usr/local/etc/redis/redis7003.conf 以指定的配置文件启动redis
[root@tsxdev1 redis-cluster]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6ddf0e2aec52 redis:6.2 "docker-entrypoint.s…" 8 minutes ago Up 8 minutes redis-7006 96d7e78bdf55 redis:6.2 "docker-entrypoint.s…" 9 minutes ago Up 9 minutes redis-7005 826add755d6d redis:6.2 "docker-entrypoint.s…" 9 minutes ago Up 9 minutes redis-7004 7f54a84b7cb1 redis:6.2 "docker-entrypoint.s…" 9 minutes ago Up 9 minutes redis-7003 090082e5c588 redis:6.2 "docker-entrypoint.s…" 9 minutes ago Up 9 minutes redis-7002 249ca5f31550 redis:6.2 "docker-entrypoint.s…" 9 minutes ago Up 9 minutes redis-7001 0434baecf5fe nginx "/docker-entrypoint.…" 7 weeks ago Up 7 weeks 0.0.0.0:9091->80/tcp, :::9091->80/tcp nginx01 2aa0dd8342b0 centos "/bin/bash" 7 weeks ago Up 7 weeks blissful_meitner [root@tsxdev1 redis-cluster]#
[root@tsxdev1 redis-cluster]# docker exec -it redis-7001 /bin/bash root@tsxdev1:/data# redis-cli -h 127.0.0.1 -p 7001 127.0.0.1:7001> auth 123456 OK root@tsxdev1:/data# /usr/local/bin/redis-cli --cluster create 39.96.74.211:7001 39.96.74.211:7002 39.96.74.211:7003 39.96.74.211:7004 39.96.74.211:7005 39.96.74.211:7006 --cluster-replicas 1 -a 123456 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 39.96.74.211:7005 to 39.96.74.211:7001 Adding replica 39.96.74.211:7006 to 39.96.74.211:7002 Adding replica 39.96.74.211:7004 to 39.96.74.211:7003 >>> Trying to optimize slaves allocation for anti-affinity [WARNING] Some slaves are in the same host as their master M: 8919ea1c3b3e420ef34bacd80436872aa1e53b00 39.96.74.211:7001 slots:[0-5460] (5461 slots) master M: 2de28ef683b7f0986bc92ddd1bc9b5604ec6c91d 39.96.74.211:7002 slots:[5461-10922] (5462 slots) master M: 675db2b2ccb0e4670ccc014ca407cf9c5d51cda9 39.96.74.211:7003 slots:[10923-16383] (5461 slots) master S: 1412fd367a501e31891386856ffe651879a62bac 39.96.74.211:7004 replicates 2de28ef683b7f0986bc92ddd1bc9b5604ec6c91d S: 45a2000a947c8ffb9c7e26168c7c78cd357aa239 39.96.74.211:7005 replicates 675db2b2ccb0e4670ccc014ca407cf9c5d51cda9 S: 0b3022182812d1d14d37a4d89c7116721df92500 39.96.74.211:7006 replicates 8919ea1c3b3e420ef34bacd80436872aa1e53b00 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 39.96.74.211:7001) M: 8919ea1c3b3e420ef34bacd80436872aa1e53b00 39.96.74.211:7001 slots:[0-5460] (5461 slots) master 1 additional replica(s) M: 2de28ef683b7f0986bc92ddd1bc9b5604ec6c91d 39.96.74.211:7002 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: 45a2000a947c8ffb9c7e26168c7c78cd357aa239 39.96.74.211:7005 slots: (0 slots) slave replicates 675db2b2ccb0e4670ccc014ca407cf9c5d51cda9 M: 675db2b2ccb0e4670ccc014ca407cf9c5d51cda9 39.96.74.211:7003 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: 1412fd367a501e31891386856ffe651879a62bac 39.96.74.211:7004 slots: (0 slots) slave replicates 2de28ef683b7f0986bc92ddd1bc9b5604ec6c91d S: 0b3022182812d1d14d37a4d89c7116721df92500 39.96.74.211:7006 slots: (0 slots) slave replicates 8919ea1c3b3e420ef34bacd80436872aa1e53b00 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. root@tsxdev1:/data# redis-cli -h 127.0.0.1 -p 7001 127.0.0.1:7001> auth 123456 OK
docker run -it -d -p 7001:6379 -v /dockerdata/redis-cluster/conf:/usr/local/etc/redis -v /dockerdata/redis-cluster/data:/data --network=host --name redis-7001 redis:6.2 redis-server /usr/local/etc/redis/redis7001.conf docker run -it -d -p 7002:6379 -v /dockerdata/redis-cluster/conf:/usr/local/etc/redis -v /dockerdata/redis-cluster/data:/data --network=host --name redis-7002 redis:6.2 redis-server /usr/local/etc/redis/redis7002.conf docker run -it -d -p 7003:6379 -v /dockerdata/redis-cluster/conf:/usr/local/etc/redis -v /dockerdata/redis-cluster/data:/data --network=host --name redis-7003 redis:6.2 redis-server /usr/local/etc/redis/redis7003.conf docker run -it -d -p 7004:6379 -v /dockerdata/redis-cluster/conf:/usr/local/etc/redis -v /dockerdata/redis-cluster/data:/data --network=host --name redis-7004 redis:6.2 redis-server /usr/local/etc/redis/redis7004.conf docker run -it -d -p 7005:6379 -v /dockerdata/redis-cluster/conf:/usr/local/etc/redis -v /dockerdata/redis-cluster/data:/data --network=host --name redis-7005 redis:6.2 redis-server /usr/local/etc/redis/redis7005.conf docker run -it -d -p 7006:6379 -v /dockerdata/redis-cluster/conf:/usr/local/etc/redis -v /dockerdata/redis-cluster/data:/data --network=host --name redis-7006 redis:6.2 redis-server /usr/local/etc/redis/redis7006.conf
原文:https://www.cnblogs.com/tengsongxu/p/15054622.html