docker swarm快速部署redis分布式集群

环境准备四台虚拟机

  • 192.168.2.38(管理节点)
  • 192.168.2.81(工作节点)
  • 192.168.2.100(工作节点)
  • 192.168.2.102(工作节点)
时间同步每台机器都执行
yum install -y ntpcat <<EOF>>/var/spool/cron/root00 12 * * * /usr/sbin/ntpdate -u ntp1.aliyun.com && /usr/sbin/hwclock -wEOF##查看计划任务crontab -l##手动执行/usr/sbin/ntpdate -u ntp1.aliyun.com && /usr/sbin/hwclock -wDocker安装Dockercurl -sSL https://get.daocloud.io/docker | sh启动dockersudo systemctl start docker搭建Swarm集群打开防火墙(Swarm需要)
  • 管理节点打开2377
    # managerfirewall-cmd --zone=public --add-port=2377/tcp --permanent
  • 所有节点打开以下端口
    # 所有nodefirewall-cmd --zone=public --add-port=7946/tcp --permanentfirewall-cmd --zone=public --add-port=7946/udp --permanentfirewall-cmd --zone=public --add-port=4789/tcp --permanentfirewall-cmd --zone=public --add-port=4789/udp --permanent
  • 所有节点重启防火墙
    【docker swarm快速部署redis分布式集群】# 所有nodefirewall-cmd --reloadsystemctl restart docker
  • 图个方便可以直接关闭防火墙
创建Swarmdocker swarm init --advertise-addr your_manager_ip查看join-token
[root@manager ~]# docker swarm join-token workerTo add a worker to this swarm, run the following command:docker swarm join --token SWMTKN-1-51b7t8whxn8j6mdjt5perjmec9u8qguxq8tern9nill737pra2-ejc5nw5f90oz6xldcbmrl2ztu 192.168.2.61:2377[root@manager ~]#加入Swarmdocker swarm join --token SWMTKN-1-51b7t8whxn8j6mdjt5perjmec9u8qguxq8tern9nill737pra2-ejc5nw5f90oz6xldcbmrl2ztu192.168.2.38:2377#查看节点docker node ls服务约束添加label
sudo docker node update --label-add redis1=true 管理节点名称sudo docker node update --label-add redis2=true 工作节点名称sudo docker node update --label-add redis3=true 工作节点名称sudo docker node update --label-add redis4=true 工作节点名称单机集群
弊端:容器都部署在一个机器上,机器挂了,就全挂了 。
创建容器Tips:这里可以写个脚本启动,因为这种方式不常用,这里就不写那个脚本了
docker create --name redis-node1 --net host -v /data/redis-data/node1:/data redis --cluster-enabled yes --cluster-config-file nodes-node-1.conf --port 6379docker create --name redis-node2 --net host -v /data/redis-data/node2:/data redis --cluster-enabled yes --cluster-config-file nodes-node-2.conf --port 6380docker create --name redis-node3 --net host -v /data/redis-data/node3:/data redis --cluster-enabled yes --cluster-config-file nodes-node-3.conf --port 6381docker create --name redis-node4 --net host -v /data/redis-data/node4:/data redis --cluster-enabled yes --cluster-config-file nodes-node-4.conf --port 6382docker create --name redis-node5 --net host -v /data/redis-data/node5:/data redis --cluster-enabled yes --cluster-config-file nodes-node-5.conf --port 6383docker create --name redis-node6 --net host -v /data/redis-data/node6:/data redis --cluster-enabled yes --cluster-config-file nodes-node-6.conf --port 6384启动容器docker start redis-node1 redis-node2 redis-node3 redis-node4 redis-node5 redis-node6进入容器启动集群# 进入其中一个节点docker exec -it redis-node1 /bin/bash# 创建集群redis-cli --cluster create 192.168.2.38:6379 192.168.2.38:6380 192.168.2.38:6381 192.168.2.38:6382 192.168.2.38:6383 192.168.2.38:6384 --cluster-replicas 1# --cluster-replicas 1 一比一,一主一从分布式集群
redis集群至少需要3个主节点,所以这里搭建三主三从的集群,由于只有4台机器 , 所以在脚本中把前三个节点放到一台机器上了 。
部署在swarm集群的Manager节点中创建
mkdir /root/redis-swarmcd /root/redis-swarmvi docker-compose.ymldocker compose.yml说明:
  1. 前6个服务为redis节点,最后一个redis-start是用于创建集群,利用redis-cli客户端搭建集群 , 该服务搭建完redis集群后会自动停止运行 。
  2. redis-start需要等待前6个redis节点的执行完毕才能创建集群,因此需要用到脚本wait-for-it.sh
  3. 由于redis-cli --cluster create不支持网络别名,所以另写脚本redis-start.sh
使用这套脚本同样可以单机部署集群 , 只需要在启动时不使用swarm启动就可以了 , 然后把docker-compose.yml中的网络模式driver: overlay给注释掉即可
version: '3.7'services:redis-node1:image: redishostname: redis-node1ports:- 6379:6379networks:- redis-swarmvolumes:- "node1:/data"command: redis-server --cluster-enabled yes --cluster-config-file nodes-node-1.confdeploy:mode: replicatedreplicas: 1resources:limits:# cpus: '0.001'memory: 5120Mreservations:# cpus: '0.001'memory: 512Mplacement:constraints:- node.role==managerredis-node2:image: redishostname: redis-node2ports:- 6380:6379networks:- redis-swarmvolumes:- "node2:/data"command: redis-server --cluster-enabled yes --cluster-config-file nodes-node-2.confdeploy:mode: replicatedreplicas: 1resources:limits:# cpus: '0.001'memory: 5120Mreservations:# cpus: '0.001'memory: 512Mplacement:constraints:- node.role==managerredis-node3:image: redishostname: redis-node3ports:- 6381:6379networks:- redis-swarmvolumes:- "node3:/data"command: redis-server --cluster-enabled yes --cluster-config-file nodes-node-3.confdeploy:mode: replicatedresources:limits:# cpus: '0.001'memory: 5120Mreservations:# cpus: '0.001'memory: 512Mreplicas: 1placement:constraints:- node.role==managerredis-node4:image: redishostname: redis-node4ports:- 6382:6379networks:- redis-swarmvolumes:- "node4:/data"command: redis-server --cluster-enabled yes --cluster-config-file nodes-node-4.confdeploy:mode: replicatedreplicas: 1resources:limits:# cpus: '0.001'memory: 5120Mreservations:# cpus: '0.001'memory: 512Mplacement:constraints:- node.labels.redis2==trueredis-node5:image: redishostname: redis-node5ports:- 6383:6379networks:- redis-swarmvolumes:- "node5:/data"command: redis-server --cluster-enabled yes --cluster-config-file nodes-node-5.confdeploy:mode: replicatedreplicas: 1resources:limits:# cpus: '0.001'memory: 5120Mreservations:# cpus: '0.001'memory: 512Mplacement:constraints:- node.labels.redis3==trueredis-node6:image: redishostname: redis-node6ports:- 6384:6379networks:- redis-swarmvolumes:- "node6:/data"command: redis-server --cluster-enabled yes --cluster-config-file nodes-node-6.confdeploy:mode: replicatedreplicas: 1resources:limits:# cpus: '0.001'memory: 5120Mreservations:# cpus: '0.001'memory: 512Mplacement:constraints:- node.labels.redis4==trueredis-start:image: redishostname: redis-startnetworks:- redis-swarmvolumes:- "$PWD/start:/redis-start"depends_on:- redis-node1- redis-node2- redis-node3- redis-node4- redis-node5- redis-node6command: /bin/bash -c "chmod 777 /redis-start/redis-start.sh && chmod 777 /redis-start/wait-for-it.sh && /redis-start/redis-start.sh"deploy:restart_policy:condition: on-failuredelay: 5smax_attempts: 5placement:constraints:- node.role==managernetworks:redis-swarm:driver: overlayvolumes:node1:node2:node3:node4:node5:node6:

推荐阅读