Article directory
- 1. Docker builds a redis cluster with three masters and three slaves
- 2. Master-slave fault-tolerant switching migration of redis cluster (based on redis cluster)
- 3. Master-slave expansion of redis cluster (based on redis cluster)
- 4. Master-slave scaling of redis cluster (based on redis cluster)
- 5. Introduction to DockerFile
- 6. DockerFile build process analysis
- 7. Introduction to DockerFile reserved words
- 8. DockerFile reserved word case demonstration
- 9. Dangling Mirror
- 10. Docker microservices in action
1. Docker builds a redis cluster with three masters and three slaves
Architecture diagram:
Step 1: Start 6 redis nodes:
docker run -d --name redis-node-1 --net host --privileged=true \
-v /data/redis/share/redis-node-1:/data redis:6.0.8 \
--cluster-enabled yes --appendonly yes --port 6381
docker run -d --name redis-node-2 --net host --privileged=true \
-v /data/redis/share/redis-node-2:/data redis:6.0.8 \
--cluster-enabled yes --appendonly yes --port 6382
...
# 开上6台redis节点。
Parameter command explanation:
Started container:
Step 2: Enter one of the nodes and build a master-slave relationship.
# 进入redis节点
docker exec -it redis-node-1 /bin/bash
# 构建主从关系
redis-cli --cluster create 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 127.0.0.1:6385 127.0.0.1:6386 --cluster-replicas 1
--cluster-replicas 1 表示为每个master创建一个salve节点,就是1:1的主从比例。
Step 3: Enter a node to view the redis cluster status.
- cluster info command: print information about the cluster.
- cluster nodes command: View cluster nodes.
到了这,一个3主3从的redis集群就算搭建成功!
Note the error:
- Because it is a cluster, you cannot use the stand-alone command to enter the redis client. You need to add the -c parameter to start in cluster mode.
- After adding -c, there will be an effect of read and write routing switching when data is read and written.
其实就是通过哈希槽分区算法,算出要插入的槽位,进而切换到槽位所在的节点而已。
Step 4: Run the redis-cli --cluster check 127.0.0.1:6381
command to view cluster information.
2. Master-slave fault-tolerant switching migration of redis cluster (based on redis cluster)
Fault tolerance is what happens if a redis node goes down.
The master node is down, and the corresponding slave node is detected by heartbeat. If it is not detected, it will go up.
When the downed node recovers, it will become the corresponding slave.
3. Master-slave expansion of redis cluster (based on redis cluster)
Expansion is a case of adding nodes.
Step 1: Create two redis nodes 6387 and 6388.
Step 2: Add the newly added 6387 nodes to the cluster as master nodes.
redis-cli --cluster add-node 自己实际IP地址:6387 自己实际IP地址:6381
# 6387就是将要作为master新增节点。
# 6381就是原来集群节点里面的领路人,相当于找到组织加入集群。
Step 3: Although the node has joined the cluster, there is no slot. So the slot number needs to be reassigned.
redis-cli --cluster reshard IP地址:端口号
After executing the reshard command, some configuration needs to be filled in:
Step 4: Slot number assignment is to allocate some slot numbers from the previous nodes to the new node.
Step 5: Allocate the slave node 6388 to the master node 6387.
# 从节点绑定
redis-cli --cluster add-node 从节点IP:从节点端口 主节点IP:主节点端口 --cluster-slave --cluster-master-id 主节点ID
This expands the effect of 3 masters and 3 slaves to 4 masters and 4 slaves.
4. Master-slave scaling of redis cluster (based on redis cluster)
Step 1: First delete the slave node corresponding to the master node.
# 删除从节点
redis-cli --cluster del-node ip:从机端口 从机6388节点ID
# 检查从节点是否被删除
redis-cli --cluster check 127.0.0.1:6382
Step 2: Clear the slot number of the master node redis and reassign it. In this example, the cleared slot numbers are given to the 6381 node.
# 分配槽号给6381节点
redis-cli --cluster reshard 127.0.0.1:6381
The allocation process is as follows:
Step 3: Delete the master node 6387.
redis-cli --cluster del-node ip:从机端口 6387节点ID
5. Introduction to DockerFile
DockerFile is a text file used to build a Docker image. It is a script composed of instructions and parameters required to build an image.
The docker commit command was used before to save the container as an image to achieve an enhanced version of the image. And Dockerfile is even simpler than this way.
Official address of Dockerfile documentation: https://docs.docker.com/engine/reference/builder/
6. DockerFile build process analysis
DockerFile content basics:
- Each reserved word instruction must be in uppercase and be followed by at least one parameter.
- Instructions are executed sequentially from top to bottom.
- # indicates a comment.
- Each instruction creates a new image layer and commits the image.
The general process of Docker executing DockerFile:
DockerFile就类似配置文件一样。
7. Introduction to DockerFile reserved words
You can refer to the dockerfile of tomcat8: https://github.com/docker-library/tomcat
from Reserved word:
- It's based on which mirror it comes from.
maintainer reserved word:
run reserved words:
- Two formats: shell format and exec format.
- run is run during docker build.
expose reserved words:
workdir reserved words:
user reserved word:
env reserved words:
volume reserved words:
add reserved words:
add和copy保留字功能差不多,只不过add要更强大一点,会自动处理URL和解压压缩包。
copy reserved words:
cmd reserved word:
Note: There can be multiple CMD commands in the Dockerfile, but only the last one takes effect, and the CMD will be replaced by the parameters after docker run.
The difference between cmd and run:
entrypoint reserved word:
Check out the case in the picture:
Summary of reserved words:
8. DockerFile reserved word case demonstration
Scenario requirements:
- The centos mirror has vim + ifconfig + jdk8.
jdk8 download address: https://mirrors.yangxingzhen.com/jdk/
Because, install a jdk8 environment. Therefore, download the compressed package for jdk8, decompress it through ADD reserved words, and configure environment variables through ENV.
The first step: write the Dockerfile file, note that the beginning must be a capital D.
Dockerfile file writing:
FROM centos
MAINTAINER zzyy<[email protected]>
ENV MYPATH /usr/local
WORKDIR $MYPATH
#安装vim编辑器
RUN yum -y install vim
#安装ifconfig命令查看网络IP
RUN yum -y install net-tools
#安装java8及lib库
RUN yum -y install glibc.i686
RUN mkdir /usr/local/java
#ADD 是相对路径jar,把jdk-8u171-linux-x64.tar.gz添加到容器中,安装包必须要和Dockerfile文件在同一位置
ADD jdk-8u171-linux-x64.tar.gz /usr/local/java/
#配置java环境变量
ENV JAVA_HOME /usr/local/java/jdk1.8.0_171
ENV JRE_HOME $JAVA_HOME/jre
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib:$CLASSPATH
ENV PATH $JAVA_HOME/bin:$PATH
EXPOSE 80
CMD echo $MYPATH
Step 2: Build a new image via Dockerfile.
- Format: docker build -t new image name: TAG . Don't forget there is a dot after it.
When building, there are the following problems to solve! ! !
Step 3: Run the container of the new image.
- Format: docker run -it new scene name: TAG
9. Dangling Mirror
Write a dangling image via Dockerfile.
The first step: vim Dockerfile .
from ubuntu
CMD echo 'action is success'
Step 2: docker build . Don't forget the dots that follow.
In this way, the dangling image is created successfully.
Be sure to delete the dangling mirror!
Step 3: You can view all dangling images of the current image through the docker image ls -f dangling=true command.
Step 4: Use the docker image prune command to remove the dangling image.
10. Docker microservices in action
Purpose: Deploy a microservice to a docker container.
Step 1: Build a springboot microservice project. Upload it to the linux server.
Step 2: Write the Dockerfile file.
# 基础镜像使用java
FROM java:8
# 作者
MAINTAINER itholmes
# VOLUME 指定临时文件目录为/tmp,在主机/var/lib/docker目录下创建了一个临时文件并链接到容器的/tmp
VOLUME /tmp
# 将jar包添加到容器中并更名为zzyy_docker.jar
ADD SSMProject-1.0-SNAPSHOT.jar itholmes_docker.jar
# 运行jar包
RUN bash -c 'touch /itholmes_docker.jar'
ENTRYPOINT ["java","-jar","/itholmes_docker.jar"]
#暴露6001端口作为微服务
EXPOSE 6001
Step 3: Execute the build command: docker build -t itholmes_docker:1.6 . (Don't forget the dots behind)
Step 4: Run the container image just created, docker run -d -p 6001:6001 image ID.
- The test port can be accessed using the curl command.