The detailed process of deploying Redis cluster and deploying microservice projects in Docker

Table of contents
  • 1. The benefits of using Docker deployment
  • 2. Comparison between Docker and Kubernetes
  • Three, Redis cluster deployment actual combat
  • 4. Spring Boot project packaging image
  • ?summary

1. The benefits of using Docker deployment

The advantage of Docker is: running the same container on different instances

Five advantages of Docker:  持续部署与测试, 多云服务平台支持, 环境标准化和版本控制, 隔离,安全

2. Comparison between Docker and Kubernetes

Docker is suitable for relatively small applications. When the amount of concurrency is not large, there are more than 10 microservices . It is recommended to apply Docker deployment, which also saves resources and reduces development costs.

K8S is suitable for large clusters, with high concurrency and more than 10 microservices. At the same time, the performance is also very good. Under the premise of good performance, the development cost has also increased a lot !

Three, Redis cluster deployment actual combat

The following deploys a Redis cluster with three masters and three slaves as shown in the figure

Stop all containers and delete all records

?

1

docker rm $(docker ps -a -q)

Create a custom network redis

?

1

docker network create redis --subnet 172.38.0.0/16

Create six redis basic information through scripts

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

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

Start 6 redis containers and mount data

?

1

2

3

4

5

6

7

8

# 通过脚本一次性启动

for port in $(seq 1 6); \

do

docker run -p 637${port}:6379 -p 1637${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

Enter the settings in redis-1 to create a cluster

?

1

2

3

4

5

# 进入redis-1

docker exec -it redis-1 /bin/sh

# 创建集群

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

into the container

?

1

2

3

4

redis-cli -c

# 查看详细信息

CLUSTER INFO

view nodes

?

1

CLUSTER NODES

Settings

?

1

set k1 v1

We can see that the value is set on node 13, we stop the node, and get the value again to view

?

1

2

# 新开窗口停止redis-3容器

docker stop redis -3

Get k1 in the original window

?

1

2

# 需要重新进入再次获取

get k1

It can be seen that the value has been synchronized to other nodes. After the master node hangs up, we can still get the value!

4. Spring Boot project packaging image

Create a SpringBoot project

TestController

?

1

2

3

4

5

6

7

8

@RestController

public class TestController {

    @GetMapping("/hello")

    public String hello() {

        return "Hello World!!!";

    }

}

The local test is successfully accessed, and then the project is compiled as a jar package

?

1

2

// 进入项目目录

mvn clean package

Download the Docker image

Write Dockerfile

?

1

2

3

4

5

FROM java:8

COPY *.jar /app.jar

CMD ["--server.port=8080"]

EXPOSE 8080

ENTRYPOINT ["java", "-jar", "/app.jar"]

Upload the jar package and Dockerfile to the server

server file

compile Dockerfile

?

1

docker build -t xiaowang .

test access

?

1

curl localhost:49153

The image uploaded successfully!

?summary

The above is [ Bug Terminator ]  a brief introduction to  Docker's actual combat - deploying Redis clusters and deploying microservice projects . Using Docker to deploy our applications is very convenient and fast , but if the architecture is huge, it is not recommended to use Docker. You can use our k8s cluster deployment is the most popular technology at present!

Guess you like

Origin blog.csdn.net/qq_15509251/article/details/131608108