[root@localhost ~]# docker network create redis --subnet 172.38.0.0/16 d87c5b1c0a4283c83c485782823e815b176106800482bdc90ac685b750548316
可以查看下这和网卡的信息
[root@localhost ~]# docker network inspect redis [ { "Name": "redis", "Id": "d87c5b1c0a4283c83c485782823e815b176106800482bdc90ac685b750548316", "Created": "2022-01-08T18:13:28.534719317+08:00", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": {}, "Config": [ { "Subnet": "172.38.0.0/16" } ] }, "Internal": false, "Attachable": false, "Ingress": false, "ConfigFrom": { "Network": "" }, "ConfigOnly": false, "Containers": {}, "Options": {}, "Labels": {} } ]
然后通过脚本创建6个redis配置
for port in $(seq 1 6); \
do \
mkdir -p /mydata/redis/node-${port}/conf
touch /mydata/redis/node-${port}/conf/redis.conf
cat << EOF >/mydata/redis/node-${port}/conf/redis.conf
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.38.0.1${port}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
EOF
done
[root@localhost ~]# cd /mydata/ [root@localhost mydata]# ls redis [root@localhost mydata]# cd redis [root@localhost redis]# [root@localhost redis]# ls node-1 node-2 node-3 node-4 node-5 node-6
查看下redis中的配置
[root@localhost redis]# cd node-1 [root@localhost node-1]# ls conf [root@localhost node-1]# cd conf [root@localhost conf]# ls redis.conf [root@localhost conf]# cat redis.conf port 6379 bind 0.0.0.0 cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 clister-announce-ip 172.38.0.11 cluster-announce-port 6379 cluster-announce-bus-port 16379 appendonly yes
运行容器redis1-6:
[root@localhost conf]# docker run -p 6371:6379 -p 16371:16379 --name redis-1 \ -v /mydata/redis/node-1/data:/data \ -v /mydata/redis/node-1/conf/redis.conf:/etc/redis/redis.conf \ -d --net redis --ip 172.38.0.11 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf [root@localhost conf]# docker run -p 6372:6379 -p 16372:16379 --name redis-2 \ -v /mydata/redis/node-2/data:/data \ -v /mydata/redis/node-2/conf/redis.conf:/etc/redis/redis.conf \ -d --net redis --ip 172.38.0.12 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf docker run -p 6373:6379 -p 16373:16379 --name redis-3 \ -v /mydata/redis/node-3/data:/data \ -v /mydata/redis/node-3/conf/redis.conf:/etc/redis/redis.conf \ -d --net redis --ip 172.38.0.13 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf [root@localhost conf]# docker run -p 6374:6379 -p 16374:16379 --name redis-4 \ -v /mydata/redis/node-4/data:/data \ -v /mydata/redis/node-4/conf/redis.conf:/etc/redis/redis.conf \ -d --net redis --ip 172.38.0.14 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf [root@localhost conf]# docker run -p 6375:6379 -p 16375:16379 --name redis-5 \ -v /mydata/redis/node-5/data:/data \ -v /mydata/redis/node-5/conf/redis.conf:/etc/redis/redis.conf \ -d --net redis --ip 172.38.0.15 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf [root@localhost conf]# docker run -p 6376:6379 -p 16376:16379 --name redis-6 \ -v /mydata/redis/node-6/data:/data \ -v /mydata/redis/node-6/conf/redis.conf:/etc/redis/redis.conf \ -d --net redis --ip 172.38.0.16 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
查看下启动的6个容器
全部启动完成
进入redis
[root@localhost conf]# docker exec -it redis-1 /bin/sh /data #
创建集群:
redis-cli --cluster create 172.38.0.11:6379 172.38.0.12:6379 172.38.0.13:6379 172.38.0.14:6379 172.38.0.15
/data # redis-cli --cluster create 172.38.0.11:6379 172.38.0.12:6379 172.38.0.13:6379 172.38.0.14:6379 172.38.0.15 :6379 172.38.0.16:6379 --cluster-replicas 1 >>> Performing hash slots allocation on 6 nodes... Master[0] -> Slots 0 - 5460 # 下面是三个主机 Master[1] -> Slots 5461 - 10922 Master[2] -> Slots 10923 - 16383 Adding replica 172.38.0.15:6379 to 172.38.0.11:6379 Adding replica 172.38.0.16:6379 to 172.38.0.12:6379 Adding replica 172.38.0.14:6379 to 172.38.0.13:6379 M: 9a543e84f3959ba0f6b82b5eff337eae5444a0e0 172.38.0.11:6379 slots:[0-5460] (5461 slots) master M: 657f713302814023da9db31eec19494a3a689866 172.38.0.12:6379 slots:[5461-10922] (5462 slots) master M: 437a3a60270854fd2ea4404e75b210989273f0c0 172.38.0.13:6379 slots:[10923-16383] (5461 slots) master S: e601c4c666b179deca7e142dbae18cf79fc3238a 172.38.0.14:6379 replicates 437a3a60270854fd2ea4404e75b210989273f0c0 S: 62c5ca960b44f64cbcc34ad06ad47f306bc26ace 172.38.0.15:6379 replicates 9a543e84f3959ba0f6b82b5eff337eae5444a0e0 S: 45a8083c5f9836765afad6c5cd679d26caeb7d08 172.38.0.16:6379 replicates 657f713302814023da9db31eec19494a3a689866
yes
启动集群
/data # redis-cli -c #启动集群 127.0.0.1:6379> cluster info #查看集群信息 cluster_state:ok cluster_slots_assigned:16384 cluster_slots_ok:16384 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:3 cluster_current_epoch:6 cluster_my_epoch:1 cluster_stats_messages_ping_sent:120 cluster_stats_messages_pong_sent:127 cluster_stats_messages_sent:247 cluster_stats_messages_ping_received:122 cluster_stats_messages_pong_received:120 cluster_stats_messages_meet_received:5 cluster_stats_messages_received:247
查看下集群的状态 都是可以连上的
测试集群:
127.0.0.1:6379> set a b -> Redirected to slot [15495] located at 172.38.0.13:6379 OK
存入数据成功 此时断开节点3试一试
发现是从节点4获取到的a的值
redis集群搭建完成!!!
写一个springboot项目打包为jar包
安装一个docker高亮的插件
写一个dockerfile
然后将打包好的jar包和dockerfile上传到linux中
打包为镜像
容器运行成功