实战:部署Redis集群
# 创建网卡
docker network create redis --subnet 172.38.0.0/16
# 通过脚本创建六个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
# 通过脚本运行六个redis
for port in $(seq 1 6);\
do \
docker run -p 637${port}:6379 -p 1667${port}:16379 --name redis-${port} \
-v /mydata/redis/node-${port}/data:/data \
-v /mydata/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.1${port} redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf \
done
docker run -p 6371:6379 -p 16671: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
..... 中间省略2-5,采用同样方法设置即可。
docker run -p 6376:6379 -p 16676:16379 --name redis-1 \
-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
docker exec -it redis-1 /bin/sh #redis默认没有bash
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
docker搭建redis集群完成!
测试redis集群
/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:594
cluster_stats_messages_pong_sent:601
cluster_stats_messages_sent:1195
cluster_stats_messages_ping_received:596
cluster_stats_messages_pong_received:594
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:1195
查看集群节点信息
在redis集群中,set 一个值:set name zhangsan
172.38.0.14:6379> set name zhangsan
-> Redirected to slot [5798] located at 172.38.0.12:6379
OK
关闭redis-2的容器
docker stop redis-1
退出redis-cli终端,重新连接
重新连接一个存活的容器docker exec -it redis-2 /bin/sh # 以redis-2为例,因为redis-1容器被我们stop了
[root@mil redis]# docker exec -it redis-2 /bin/sh
/data # redis-cli -c
127.0.0.1:6379> get a
-> Redirected to slot [15495] located at 172.38.0.14:6379
"b"
172.38.0.14:6379> get age
-> Redirected to slot [741] located at 172.38.0.15:6379
"10"
172.38.0.15:6379> get name
-> Redirected to slot [5798] located at 172.38.0.16:6379
"zhangsan"
###可以看出集群工作是正常的。
我们使用docker之后,所有的技术都会慢慢变得简单起来!
SpringBoot微服务打包Docker镜像
1、构建SpringBoot项目
2、打包运行 mvn package
3、编写dockerfile
FROM java:8
COPY *.jar /app.jar
CMD ["--server.port=8080"]
EXPOSE 8080
ENTRYPOINT ["java","-jar","app.jar"]
4、构建镜像
# 1.复制jar和DockerFIle到服务器
# 2.构建镜像
$ docker build -t xxxxx:xx .
5、发布运行
以后我们使用了Docker之后,给别人交付就是一个镜像即可!
还没有任何评论,你来说两句吧