
** Definition of container **: Container is used to solve the problem of "how to ensure that software can run normally when switching operating environments".
目前,容器和 Docker 依旧是技术领域最热门的词语,无状态的服务容器化已经是大势所趋,同时也带来了一个热点问题被大家所争论不以:数据库 MySQL 是否需要容器化?
认真分析大家的各种观点,发现赞同者仅仅是从容器优势的角度来阐述 MySQL 需要容器化,几乎没有什么业务场景进行验证自己的观点;反过来再看反对者,他们从性能、数据安全等多个因素进行阐述 MySQL 不需要容器化,也举证了一些不适合的业务场景。下面,我们就聊一下 Docker 不适合跑 MySQL 的 N 个原因!
data security issues
Don't store data in containers, which is one of Docker's official container tips. Containers can be stopped or deleted at any time. When a container is rm dropped, the data in the container will be lost. To avoid data loss, users can use data volume mounts to store data. However, the container's Volumes design provides persistent storage around the Union FS mirroring layer, and data security is not guaranteed. If the container suddenly crashes and the database is not closed properly, data may be damaged. In addition, sharing data volume groups in containers also causes greater damage to physical machine hardware.

performance issues
As everyone knows, MySQL is a relational database and has high IO requirements. When a physical machine runs multiple, IO will accumulate, causing IO bottlenecks and greatly reducing MySQL's read and write performance.
In a special session on the top ten difficulties in Docker applications, an architect from a state-owned bank also proposed: "The performance bottleneck of the database generally appears on IO. If Docker follows Docker's thinking, then multiple final IO requests from Docker will appear on storage again. Nowadays, most databases on the Internet are based on share nothing architecture. Perhaps this is also a factor that does not consider migrating to Docker."

In fact, there are some corresponding strategies to solve this problem, such as:
- Separation of database program and data
If you use Docker to run MySQL, the database program and the data need to be separated, the data should be stored in shared storage, and the program should be placed in a container. If the container has exceptions or MySQL service exceptions, a brand new container is automatically launched. In addition, it is recommended not to store data in the host machine. The host machine and container share a volume group, which will have a greater impact on host machine damage.
- Run lightweight or distributed databases
When Docker deploys lightweight or distributed databases, Docker itself recommends that the service be suspended and new containers be automatically started, rather than continuing to restart the Kubernetes Engine.
- Reasonable layout and application
For applications or services with high IO requirements, it is more appropriate to deploy the database in a physical machine or KVM. Currently, Tencent Cloud's TDSQL and Ali's Oceanbase are deployed directly on physical machines rather than Docker.
status issues
Horizontal scaling in Docker can only be used for stateless computing services, not databases.
An important feature of Docker's rapid expansion is that it is stateless. Anything with data state is not suitable for being placed directly in Docker. If a database is installed in Docker, storage services need to be provided separately.
Currently, Tencent Cloud's TDSQL (Financial Distributed Database) and Alibaba Cloud's Oceanbase (Distributed Database System) are running directly on physical machines, not on Docker, which is easy to use.

Resource isolation
In terms of resource isolation, Docker is indeed not as good as virtual machine KVM. Docker uses Cgroup to achieve resource restriction. It can only limit the maximum resource consumption, but cannot isolate other programs from occupying their own resources. If other applications excessively occupy physical machine resources, it will affect the read and write efficiency of MySQL in the container.
The more isolation levels you need, the more resource overhead you will gain. Compared to dedicated environments, ease of horizontal scaling is a major advantage of Docker. However, in Docker, horizontal scaling can only be used for stateless computing services, and databases are not applicable.

Can't MySQL run in a container?
MySQL is not completely containerized.
Services that are not sensitive to data loss (such as users searching for goods) can be digitized, using database fragmentation to increase the number of instances, thereby increasing throughput.
Docker is suitable for running lightweight or distributed databases. When the docker service hangs, it will automatically start a new container instead of continuing to restart the Kubernetes Engine.
The database can automatically scale, disaster tolerance, switch, and bring multiple nodes with it by using middleware and containerized systems. It can also be containerized.
Typical cases: Database containerization in Tongcheng Travel, Jingdong, and Ali are all good cases. You can check it yourself.
Original author: Lao Wang talks about operation and maintenance
Original link: www.toutiao.com/i6675622107390411276? wid=1640184427889