目錄
- 環(huán)境配置
- 修改hostname
- 創(chuàng)建目錄
- 創(chuàng)建網(wǎng)絡(luò)
- 編寫compose.yml模版文件
- 啟動(dòng)服務(wù)
- 驗(yàn)證測(cè)試
- 集群
- 集群測(cè)試驗(yàn)證
環(huán)境配置
1.三臺(tái)虛擬機(jī),VM16
操作系統(tǒng) | IP | 備注 |
---|---|---|
centos7 | 192.168.2.131 | |
centos7 | 192.168.2.132 | |
centos7 | 192.168.2.133 |
2.redis配置
hostname | IP | 端口 | 備注 | |
---|---|---|---|---|
manager | 192.168.2.131 | 7001 | 17001 | |
manager | 192.168.2.131 | 7002 | 17002 | |
worker01 | 192.168.2.132 | 7003 | 17003 | |
worker01 | 192.168.2.132 | 7004 | 17004 | |
worker02 | 192.168.2.133 | 7005 | 17005 | |
worker02 | 192.168.2.133 | 7006 | 17006 |
修改hostname
192.168.2.131
sudo hostnamectl set-hostname manager
重啟后永久生效,或者執(zhí)行 exec bash 使立即生效
192.168.2.132
sudo hostnamectl set-hostname worker01
重啟后永久生效,或者執(zhí)行 exec bash 使立即生效
192.168.2.133
sudo hostnamectl set-hostname worker02
重啟后永久生效,或者執(zhí)行 exec bash 使立即生效
創(chuàng)建目錄
分別在集群的各服務(wù)器(131/132/133)上創(chuàng)建目錄:
sudo rm -rf /home/data/redis/ && sudo mkdir -p /home/data/redis/{7001,7002,7003,7004,7005,7006}/{data,conf} && chmod 777 -R /home/data/
創(chuàng)建網(wǎng)絡(luò)
docker network create --driver overlay mynetwork
這里創(chuàng)建網(wǎng)絡(luò)類型為overlay,網(wǎng)絡(luò)類型主要使用比較多的是bridge、overlay ,由于這里使用的是swarm集群部署,要讓所有服務(wù)在同一個(gè)網(wǎng)絡(luò)中,則需要使用overlay 。
[root@worker2 ~]# docker network ls NETWORK ID NAME DRIVER SCOPE 147f2ede7454 bridge bridge local e29b8451a401 docker_gwbridge bridge local 4b767db33495 host host local splhg7ef8xyc ingress overlay swarm b037f0b632fa mynet bridge local 6zxw8ah74hor mynetwork overlay swarm b6f757b6cadd none null local 063d396e139c somenetwork bridge local
編寫compose.yml模版文件
version: "3.8" services: ? redis7001: ? ? image: redis:alpine ? ? container_name: redis7001 ? ? #設(shè)置主機(jī)名 ? ? hostname: redis7001 ? ? restart: always ? ? #privileged: true ? ? #掛載目錄,相當(dāng)于 docker run -v 主機(jī)目錄:容器目錄 ? ? volumes: ? ? ? - /home/data/redis/7001/data:/data ? ? ? - /home/data/redis/7001/conf:/conf ? ? #啟動(dòng)容器執(zhí)行命令,相當(dāng)于docker run [鏡像:tag] ?[命令], 登錄redis: redis-cli -h 192.168.0.80 -p 6379 -a Dszn@2020 ? ? command: redis-server --appendonly yes --cluster-enabled yes --cluster-config-file /conf/nodes.conf --cluster-announce-ip 192.168.2.131 --cluster-announce-port 7001 --cluster-announce-bus-port 17001 ? ? ports: ? ? ? - "7001:6379" ? ? ? - "17001:16379" ? ? #指定環(huán)境變量,相當(dāng)于docker run -e 參數(shù), 登錄mysql: mysql -h192.168.3.80 -P3306 -uroot -pDs20Pwd@ ? ? environment: ? ? ? - TZ=Asia/Shanghai ? ? networks: ? ? ? - mynetwork ? ? deploy: ? ? ? placement: ? ? ? ? constraints: ? ? ? ? ? - node.hostname == manager ? ? ? ? ? - node.role == manager ? redis7002: ? ? image: redis:alpine ? ? container_name: redis7002 ? ? #設(shè)置主機(jī)名 ? ? hostname: redis7002 ? ? restart: always ? ? #privileged: true ? ? #掛載目錄,相當(dāng)于 docker run -v 主機(jī)目錄:容器目錄 ? ? volumes: ? ? ? - /home/data/redis/7002/data:/data ? ? ? - /home/data/redis/7002/conf:/conf ? ? #啟動(dòng)容器執(zhí)行命令,相當(dāng)于docker run [鏡像:tag] ?[命令], 登錄redis: redis-cli -h 192.168.0.80 -p 6379 -a Dszn@2020 ? ? command: redis-server --appendonly yes --cluster-enabled yes --cluster-config-file /conf/nodes.conf --cluster-announce-ip 192.168.2.131 --cluster-announce-port 7002 --cluster-announce-bus-port 17002 ? ? ports: ? ? ? - "7002:6379" ? ? ? - "17002:16379" ? ? #指定環(huán)境變量,相當(dāng)于docker run -e 參數(shù), 登錄mysql: mysql -h192.168.3.80 -P3306 -uroot -pDs20Pwd@ ? ? environment: ? ? ? - TZ=Asia/Shanghai ? ? networks: ? ? ? - mynetwork ? ? deploy: ? ? ? placement: ? ? ? ? constraints: ? ? ? ? ? - node.hostname == manager ? ? ? ? ? - node.role == manager ? redis7003: ? ? image: redis:alpine ? ? container_name: redis7003 ? ? #設(shè)置主機(jī)名 ? ? hostname: redis7003 ? ? restart: always ? ? volumes: ? ? ? - /home/data/redis/7003/data:/data ? ? ? - /home/data/redis/7003/conf:/conf ? ? command: redis-server --appendonly yes --cluster-enabled yes --cluster-config-file /conf/nodes.conf --cluster-announce-ip 192.168.2.132 --cluster-announce-port 7003 --cluster-announce-bus-port 17003 ? ? ports: ? ? ? - "7003:6379" ? ? ? - "17003:16379" ? ? environment: ? ? ? - TZ=Asia/Shanghai ? ? networks: ? ? ? - mynetwork ? ? deploy: ? ? ? placement: ? ? ? ? constraints: ? ? ? ? ? - node.hostname == worker1 ? redis7004: ? ? image: redis:alpine ? ? container_name: redis7004 ? ? #設(shè)置主機(jī)名 ? ? hostname: redis7004 ? ? restart: always ? ? volumes: ? ? ? - /home/data/redis/7004/data:/data ? ? ? - /home/data/redis/7004/conf:/conf ? ? command: redis-server --appendonly yes --cluster-enabled yes --cluster-config-file /conf/nodes.conf --cluster-announce-ip 192.168.2.132 --cluster-announce-port 7004 --cluster-announce-bus-port 17004 ? ? ports: ? ? ? - "7004:6379" ? ? ? - "17004:16379" ? ? environment: ? ? ? - TZ=Asia/Shanghai ? ? networks: ? ? ? - mynetwork ? ? deploy: ? ? ? placement: ? ? ? ? constraints: ? ? ? ? ? - node.hostname == worker1 ? redis7005: ? ? image: redis:alpine ? ? container_name: redis7005 ? ? #設(shè)置主機(jī)名 ? ? hostname: redis7005 ? ? restart: always ? ? volumes: ? ? ? - /home/data/redis/7005/data:/data ? ? ? - /home/data/redis/7005/conf:/conf ? ? command: redis-server --appendonly yes --cluster-enabled yes --cluster-config-file /conf/nodes.conf --cluster-announce-ip 192.168.2.132 --cluster-announce-port 7005 --cluster-announce-bus-port 17005 ? ? ports: ? ? ? - "7005:6379" ? ? ? - "17005:16379" ? ? environment: ? ? ? - TZ=Asia/Shanghai ? ? networks: ? ? ? - mynetwork ? ? deploy: ? ? ? placement: ? ? ? ? constraints: ? ? ? ? ? - node.hostname == worker2 ? redis7006: ? ? image: redis:alpine ? ? container_name: redis7006 ? ? #設(shè)置主機(jī)名 ? ? hostname: redis7006 ? ? restart: always ? ? volumes: ? ? ? - /home/data/redis/7006/data:/data ? ? ? - /home/data/redis/7006/conf:/conf ? ? command: redis-server --appendonly yes --cluster-enabled yes --cluster-config-file /conf/nodes.conf --cluster-announce-ip 192.168.2.133 --cluster-announce-port 7006 --cluster-announce-bus-port 17006 ? ? ports: ? ? ? - "7006:6379" ? ? ? - "17006:16379" ? ? environment: ? ? ? - TZ=Asia/Shanghai ? ? networks: ? ? ? - mynetwork ? ? deploy: ? ? ? placement: ? ? ? ? constraints: ? ? ? ? ? - node.hostname == worker2 #聲明網(wǎng)橋 networks: ? #定義服務(wù)網(wǎng)橋名稱 ? mynetwork: ? ? #指定網(wǎng)橋驅(qū)動(dòng),有bridge/overlay,默認(rèn)是bridge ? ? driver: overlay ? ? #false-統(tǒng)自動(dòng)創(chuàng)建網(wǎng)橋名,格式為: 目錄名_網(wǎng)橋名,默認(rèn)為false; true-使用外部創(chuàng)建的網(wǎng)橋,需要自己手動(dòng)創(chuàng)建 ? ? external: true #掛載目錄,聲明服務(wù)使用的創(chuàng)建卷名 volumes: ? mysqldata: ? ? #false-系統(tǒng)自動(dòng)創(chuàng)建的卷名,格式為: 目錄名_卷名,默認(rèn)為false; true-使用外部創(chuàng)建的卷面,需要自己手動(dòng)創(chuàng)建 ? ? external: false
配置說(shuō)明:
這里使用了6個(gè)節(jié)點(diǎn),3主3從,分別在manager/worker1/worker2上創(chuàng)建2個(gè)副本。
注意:掛載目錄,如果不使用宿主機(jī)掛載目錄,可以使用卷的方式掛載,卷掛載會(huì)自動(dòng)創(chuàng)建,可以不用提前手動(dòng)創(chuàng)建。
啟動(dòng)服務(wù)
sudo docker stack deploy -c redis-stack.yml redis
1.啟動(dòng)服務(wù)
[root@manager redis]# sudo docker stack deploy -c redis-stack.yml redis Ignoring unsupported options: restart Ignoring deprecated options: container_name: Setting the container name is not supported. Creating service redis_redis7001 Creating service redis_redis7002 Creating service redis_redis7003 Creating service redis_redis7004 Creating service redis_redis7005 Creating service redis_redis7006
2.查看啟動(dòng)節(jié)點(diǎn)
[root@manager redis]# sudo docker stack ls NAME SERVICES ORCHESTRATOR redis 6 Swarm
3.查看啟動(dòng)的堆棧
[root@worker2 redis_cluster]# docker stack ps redis ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS bdofotp2fx91 redis_redis7001.1 redis:alpine manager Running Running 2 minutes ago 81kmx5o61zn0 redis_redis7002.1 redis:alpine manager Running Running about a minute ago wku68rakslzj redis_redis7003.1 redis:alpine worker1 Running Running 49 seconds ago oo7obcm33guk redis_redis7004.1 redis:alpine worker1 Running Running about a minute ago xrt14qitd7ar redis_redis7005.1 redis:alpine worker2 Running Running about a minute ago jc1xxv66fskg redis_redis7006.1 redis:alpine worker2 Running Running 2 minutes ago [root@worker2 redis_cluster]#
驗(yàn)證測(cè)試
本地連接成功
集群
進(jìn)入manager(192.168.2.131)服務(wù)設(shè)置集群
1.登錄manager節(jié)點(diǎn),執(zhí)行如下命令集群:
[root@manager ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 71092cdaaf55 redis:alpine "docker-entrypoint.s…" 2 minutes ago Up About a minute 6379/tcp redis_redis7001.1.j84ta2mmwttpl2lp6eoyksz71 695c0fc4545b redis:alpine "docker-entrypoint.s…" 2 minutes ago Up About a minute 6379/tcp redis_redis7002.1.8jt2yodv9ju8k4saumd7n5rif [root@manager ~]# docker exec -it 710 /bin/sh /data # redis-cli -h 192.168.2.131 -p 7001 --cluster create 192.168.2.131:7001 192.168.2.131:7002 192.168.2.132:7003 192.168.2.132:7004 192.168.2.133:7005 192.168.2 .133:7006 --cluster-replicas 1 --cluster-yes
2.查看集群
可以看到集群后,redis有3主3從,分別在manager、worker1、worker2節(jié)點(diǎn)上創(chuàng)建了2個(gè)服務(wù)。
[root@manager ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 71092cdaaf55 redis:alpine "docker-entrypoint.s…" 28 minutes ago Up 28 minutes 6379/tcp redis_redis7001.1.j84ta2mmwttpl2lp6eoyksz71 695c0fc4545b redis:alpine "docker-entrypoint.s…" 28 minutes ago Up 28 minutes 6379/tcp redis_redis7002.1.8jt2yodv9ju8k4saumd7n5rif [root@manager ~]# docker exec -it 710 /bin/sh /data # redis-cli -c 127.0.0.1:6379> cluster nodes 01011fe3d1e19db0bfab5d736f7fcb85f84e5a2b 192.168.2.131:7002@17002 slave 9fece13f2f1ff237b0e450cb5485c27cd4cc1522 0 1648894677000 5 connected 9a68f8ad7688f3c7f3a5474d64ffc5adc7448288 192.168.2.132:7004@17004 slave c1a9795720eb270d6bc77632236d30bfb1fe9728 0 1648894678000 1 connected c1a9795720eb270d6bc77632236d30bfb1fe9728 192.168.2.131:7001@17001 myself,master - 0 1648894677000 1 connected 0-5460 9fece13f2f1ff237b0e450cb5485c27cd4cc1522 192.168.2.132:7005@17005 master - 0 1648894679923 5 connected 10923-16383 4b6cafd544d4353249eb7f7d58bed7bf0b1caeb1 192.168.2.132:7003@17003 slave 65c23a5ae11b985b270a5e34d7526648a3c826b8 0 1648894679000 7 connected 65c23a5ae11b985b270a5e34d7526648a3c826b8 192.168.2.133:7006@17006 master - 0 1648894678000 7 connected 5461-10922 127.0.0.1:6379>
集群測(cè)試驗(yàn)證
1.Manager節(jié)點(diǎn)192.168.2.131:7001@17001 myself,master 登錄redis:
redis-cli -c set username ‘zxq’ get username
[root@manager ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 71092cdaaf55 redis:alpine "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 6379/tcp redis_redis7001.1.j84ta2mmwttpl2lp6eoyksz71 695c0fc4545b redis:alpine "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 6379/tcp redis_redis7002.1.8jt2yodv9ju8k4saumd7n5rif [root@manager ~]# docker exec -it 710 /bin/sh /data # redis-cli -c 127.0.0.1:6379> set username "zxq" -> Redirected to slot [14315] located at 192.168.2.132:7005 OK
2.Worker1節(jié)點(diǎn)192.168.2.132:7003@17003 master 登錄redis:
[root@worker1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 73da00eeb366 redis:alpine "docker-entrypoint.s…" 24 minutes ago Up 23 minutes 6379/tcp redis_redis7004.1.mofccxkrtzb0erc36fwaj9l3b 8e4fed3c8855 redis:alpine "docker-entrypoint.s…" 24 minutes ago Up 23 minutes 6379/tcp redis_redis7003.1.5qmpefwnglv6jvd62206owq3h [root@worker1 ~]# docker exec -it 73d /bin/sh /data # redis-cli -c 127.0.0.1:6379> get username -> Redirected to slot [14315] located at 192.168.2.132:7005 "zxq" 192.168.2.132:7005> set nodes5 "7005" OK
3.Worker1節(jié)點(diǎn)192.168.2.132:7004@17004 slave登錄redis:
[root@worker1 ~]# docker exec -it 8e4 /bin/sh /data # redis-cli -c 127.0.0.1:6379> get nodes5 -> Redirected to slot [15322] located at 192.168.2.132:7005 "7005" 192.168.2.132:7005>
4.Worker2節(jié)點(diǎn)192.168.2.133:7006@17006 slave 登錄redis:
[root@worker2 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7505c9fde687 redis:alpine "docker-entrypoint.s…" 47 minutes ago Up 47 minutes 6379/tcp redis_redis7006.1.o6yly8n6h352g034jhx7nbvaf 2da9dc998c69 redis:alpine "docker-entrypoint.s…" 47 minutes ago Up 47 minutes 6379/tcp redis_redis7005.1.s6lfflko40afutyz9a5b77tib [root@worker2 ~]# docker exec -it 750 /bin/sh /data # redis-cli -c 127.0.0.1:6379> get username -> Redirected to slot [14315] located at 192.168.2.132:7005 "zxq" 192.168.2.132:7005> get name -> Redirected to slot [5798] located at 192.168.2.133:7006 "zxq" 192.168.2.133:7006> get nodes5 -> Redirected to slot [15322] located at 192.168.2.132:7005 "7005" 192.168.2.132:7005>
redis集群完成
參考原文章地址:
docker swarm redis集群_starsky20的博客-CSDN博客_docker swarm搭建redis集群