SpringCloud Microservice [Practical] | Docker Revelations

Table of contents

1: Docker Apocalypse

1. Docker Apocalypse

2. The difference between Docker and virtual machines

3. Docker architecture

4. Install Docker on Centos7

4.1. Uninstall

4.2. Install docker

4.3. Start docker

4.4. Configure image acceleration


A few days ago, I suddenly discovered a giant artificial intelligence learning website. It is easy to understand and humorous. I can’t help but share it with everyone. Interested students can learn it Artificial Intelligence Learning

1: Docker Apocalypse

After studying the previous microservices, we found a very troublesome problem: the deployment of services. There are many microservices, and it is very troublesome to deploy! Learn Docker today to solve this microservice deployment problem!

1. Docker Apocalypse

Project deployment issues

Large-scale projects have many components and the operating environment is relatively complex. You will encounter some problems during deployment: Complex dependencies and compatibility issues are prone to occur; Development, testing, and production environments are different!

The front-end part of various microservices relies on Node and JS. The server side requires database MySQL, cache system Redis, asynchronous communication MQ, etc. These need to be deployed on the server (Linux operating system); each application requires dependencies and function libraries, but the dependencies and function libraries of each application are different (The dependencies are complex It is easy to produce compatibility). After setting up the development environment, there will be testing and production environments later (The corresponding operating system environment may be different: Centos, Ubuntu)

How does Docker solve dependency compatibility issues?

Since each application has its own dependencies and function libraries, Docker can package the Libs (function libraries), Deps (dependencies), and configuration of the application together with the application ! And puteach application into an isolated container to run to avoid mutual interference

How does Docker solve operating systems in different environments?

The operating systems of different environments are different (the native function libraries of Linux and Ubuntu are different). How does Docker solve it? Let’s first understand the operating system structure:

All Linux operating systems can be divided into two layers: Linux kernel (all the same) and system applications (the difference is that the upper layer applications are different). The kernel is responsible for communicating with the computer’s hardware and providing instructions for operating the hardware. System application encapsulates kernel instructions into functions for easy calling by programmers; user programs implement functions based on the system function library. The program calls the function library---》The function library calls the kernel instruction---》Specifies the call to the computer hardware, thereby realizing the execution of the application.

Ubuntu and CentOS are both based on the Linux kernel, but the system applications are different and the function libraries provided are different; therefore, according to the packaging program of the Ubuntu environment, it is possible to put it on Centos Doesn't work!

Docker packages the user program together with the required system (such as Ubuntu and Centos) function library. When Docker runs on different operating systems, it is directly based on the packaged library functions and runs with the help of the Linux kernel of the operating system.

1. How does Docker solve the compatibility issues of complex dependencies and dependencies on different components in large projects?

①Docker allows applications, dependencies, function libraries, and configurations to be packaged together during development to form a portable image;

②Docker applications run in containers, using the sandbox mechanism to isolate each other;

2. How does Docker solve the problem of differences between development, testing and production environments?

①The Docker image contains a complete operating environment, including system function libraries, and only relies on the system’s Linux kernel, so it can run on any Linux operating system;

Summary: Docker is a technology for quickly delivering and running applications

① The program, its dependencies, and the operating environment can be packaged into an image, which can be migrated to any Linux operating system;

② Use the sandbox mechanism to form an isolated container during runtime, so that each application does not interfere with each other;

③Startup and removal can be completed with one line of commands, which is convenient and fast;

2. The difference between Docker and virtual machines

Docker and virtual machines

Docker implementation principle:Docker allows an application to run on different Linux environments by integrating the application and its dependent function libraries, and even the function libraries of the operating system. Pack. In this way, when the application is running, it can directly call the local function library and then interact with the kernel of the operating system. In this way, there is no need to care about what kind of system it is, and cross-system operation can be achieved! (Directly calls the kernel of the operating system, the performance is better)

Virtual machine implementation principle:Use Hypervisor technology to install another operating system on top of one operating system, simulating various computer hardware, and you can install any arbitrary computer hardware on the simulated computer. operating system, and then you can install applications, dependencies, etc. Install another system on one system, so when the application is executed, the application will think that it is running on a real computer: it will first call the built-in operating system---"Interact with the Hypevisor---"Transmit the information to the external real computer The operating system----》The external operating system then calls the computer hardware. (Transmitted layer by layer, poor performance)

, 

Comparison between the two

 Differences between Docker and virtual machines:

①Docker is a system process; the virtual machine is the operating system in the operating system;

②Docker is small in size, fast in startup, and has good performance; virtual machines are large in size, slow in startup, and have average performance;

3. Docker architecture

Images and containers

Image: Docker packages the application and its required dependencies, function libraries, environment, configuration and other files together, called an image.

Container (Container):The process formed after the application in the image is run is the container, It’s just that Docker will isolate the container and make it invisible to the outside world.

For MySQL, it is a mirror, which contains many files that make up MySQL itself, so it can be said thatthe mirror is the file on the hard disk; a>The process in which MySQL runs is the container, but the container must be isolated in Docker! For containers that cannot write their own things into the image (causing image pollution), can create a new image based on the container, and the images are all read-only! How to write data to a container? Container writing data can copy a file to its own independent container!

DockerHub (image sharing)

 How to share the image with others? This requires DockerHub (image hosting) to be very similar to GitHub!

DockerHub: DockerHub is a hosting platform for Docker images; such a platform is called Docker Registry (mirror server). There are also public services similar to DockerHub in China, such as NetEase Cloud Mirror Service, Alibaba Cloud Mirror Library etc.

Programmers can use the commands provided by Docker to build images, such as MySQL and Nginx images; then upload these images to the DockerHub server (it will be more dangerous to make them public); they can also build a private cloud.

Docker architecture

How to use Docker to build, pull and run the image? This requires understanding the architecture of Docker!

Docker is a program with CS architecture and consists of two parts:

Server:Docker daemon, responsible for processing Docker instructions, managing images, building containers, etc.;

Client:via command (local) or RestAPI request (remote)Send instructions to the Docker server;Can send instructions to the server locally or remotely;

For example: To build an image locally, use thedocker build command. After this command reaches DockerServer, it will be provided by the daemon process docker daemon. The databuilds a mirror! You can also use DockerRegisterdocker pull to pull the image through DockerServer! Then you canrun the image and create the container. At this time, you needdocker run command, it will tell the docker daemon of DockerServer to complete the creation of the container; finally complete the deployment!

Mirror: Package the application and its dependencies, environment, and configuration together (can be viewed as a system file);

Container:When the image is running, it is a container. One image can run multiple containers (can be regarded as running processes);

To summarize the Docker structure:

Server: receives commands or remote requests, operates images or containers; 

Client: Send commands or requests to the Docker server;

DockerHub:A mirror hosting server, similar to Alibaba Cloud mirror service, collectively called DockerRegistry;

4. Install Docker on Centos7

Enterprise deployment generally uses the Linux operating system, among which CentOS distribution accounts for the largest proportion, so we install Docker under CentOS. Docker is divided into two major versions: CE and EE. CE is the community edition (free, support period is 7 months), EE is the enterprise edition, which emphasizes security, is paid for use, and has a support period of 24 months.

Note:Docker CE supports the 64-bit version of CentOS 7, and requires the kernel version to be no less than 3.10. CentOS 7 meets the minimum kernel requirements, so we install Docker on CentOS 7.

4.1. Uninstall

If you have installed an older version of Docker before, you can uninstall it using the following command:

Note: Backslash \ indicates the splicing of commands!

yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-selinux \
                  docker-engine-selinux \
                  docker-engine \
                  docker-ce

4.2. Install docker

First, you need to connect your virtual machine to the Internet and install the yum tool (download tool)

yum install -y yum-utils \
           device-mapper-persistent-data \
           lvm2 --skip-broken

Then update the local mirror source (the default is to connect to foreign countries, which is easy to disconnect and slow)

# 设置docker镜像源为阿里云
yum-config-manager \
    --add-repo \
    https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    
sed -i 's/download.docker.com/mirrors.aliyun.com\/docker-ce/g' /etc/yum.repos.d/docker-ce.repo

yum makecache fast

Install docker community edition

yum install -y docker-ce

4.3. Start docker

Docker applications need to use various ports, and modify the firewall settings one by one. It’s very troublesome, so I suggest you turn off the firewall directly! Be sure to turn off the firewall before starting docker! !

# 关闭防火墙
systemctl stop firewalld
# 禁止开机启动防火墙
systemctl disable firewalld
# 查看防火墙的状态
systemctl status firewalld

Start docker via command

systemctl status docker  # 查看当前docker的状态
systemctl start docker  # 启动docker服务
systemctl stop docker  # 停止docker服务
systemctl restart docker  # 重启docker服务

Then enter the command to check the docker version (you can check the version number only after the startup is successful)

docker -v

4.4. Configure image acceleration

Docker official image warehouse network speed is poor, we need to set up a domestic image service; refer to Alibaba Cloud's image acceleration document:

https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors

You can use the accelerator by modifying the daemon configuration file /etc/docker/daemon.json; configure the Alibaba Cloud mirror address in daemon.json!

# 创建一个路径
sudo mkdir -p /etc/docker
# 把阿里云的地址追加到daemon.json文件
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://6aqbcoj9.mirror.aliyuncs.com"]
}
EOF # 表示终止
# 重新加载上面的文件
sudo systemctl daemon-reload
# 重启docker
sudo systemctl restart docker

Guess you like

Origin blog.csdn.net/m0_61933976/article/details/133551797