near 2 year Docker It's very hot , Developers would like to put all the applications 、 The software is deployed in Docker In the container , But are you sure you want to deploy the database in the container as well ？ This problem is not a myth , Because we can find many kinds of operation manuals and video tutorials on the Internet , Here we sort out some reasons why the database is not suitable for containerization for your reference , At the same time, I hope you can use it with a little caution . So far, it is unreasonable to container the database , But the advantages of containerization are believed to be enjoyed by all developers , I hope that with the development of technology, more perfect solutions will emerge .
Docker Not suitable for database deployment 7 The big reason
1、 Data security issues
Do not store data in containers , This is also Docker One of the official container tips . The container can stop at any time 、 Or delete . When the container is rm fall , The data in the container will be lost . To avoid data loss , Users can use data volume mount to store data . But the container Volumes Design is about Union FS The image layer provides persistent storage , Data security is not guaranteed . If the container suddenly collapses , The database is not shut down properly , Could damage data . in addition , Shared data volume group in container , The damage to physical machine hardware is also relatively large . Even if you want to put Docker Data is stored on the host , It's still not guaranteed to lose data . Use the current storage driver ,Docker There is still a risk of unreliability . If the container crashes and the database does not shut down properly , It can damage the data .
2、 Performance issues
Everybody knows ,MySQL Belongs to relational database , Yes IO The demand is higher . When a physical machine runs more than one ,IO It will add up , Lead to IO bottleneck , Greatly reduced MySQL Read and write performance of . In a Docker Ten difficulties in application , An architect of a state-owned bank once proposed ：“ The performance bottleneck of database usually appears in IO above , If the Docker The idea of , So many docker Final IO The request will appear on the store again . Now the Internet database is mostly share nothing The architecture of , Maybe it's not about moving to Docker A factor of ”.
Some students may have corresponding solutions for performance problems ：
(1) Database program is separated from data
If you use Docker run MySQL, Database program and data need to be separated , Store data in shared storage , Put the program in the container . If the container is abnormal or MySQL Service exception , Automatically start a new container . in addition , It is recommended not to store the data in the host computer , Host and container share volume group , It has a great impact on the damage of the host .
(2) Run lightweight or distributed databases
Docker Deployment of lightweight or distributed databases ,Docker Recommend the service itself and hang up , Start new container automatically , Instead of continuing to restart the container service .
(3) Reasonable layout and application
about IO Applications or services with high requirements , Deploy the database on a physical machine or KVM It's more suitable . at present TX The cloud TDSQL Ali Oceanbase They are all deployed directly on physical machines , Instead of Docker .
3、 Network problems
To understand Docker The Internet , You have to have an in-depth understanding of network virtualization . Databases need dedicated and persistent throughput , To achieve a higher load . Unresolved Docker The problem with the Internet is 1.9 The version is still unresolved . Put these questions together , Containerization makes database containers difficult to manage . How long will it take you to solve Docker Network problems ？ Wouldn't it be better to keep the database in a dedicated environment ？ Save time to focus on really important business goals .
stay Docker It's cool to pack stateless services in , It can be used to arrange containers and solve single point of failure . But the database ？ Put the database in the same environment , It will be stateful , And make the scope of system failure larger . Next time your application instance or application crashes , May affect the database . Knowledge point : stay Docker Medium level scaling can only be used for stateless computing services , Not a database .Docker An important feature of rapid expansion is statelessness , Those with data status are not suitable to be placed directly in Docker Inside , If Docker Install database in , Storage services need to be provided separately . at present ,TX The cloud TDSQL( Financial distributed database ) And Alibaba cloud Oceanbase( Distributed database system ) Are all running directly on the physical machine , It's not easy to manage Docker On .
5、 Resource isolation
In terms of resource isolation ,Docker It's not as good as a virtual machine KVM,Docker It's using Cgroup To achieve resource limitation , You can only limit the maximum consumption of resources , Instead of isolating other programs from using their own resources . If other applications take up physical machine resources , It will affect the container MySQL Reading and writing efficiency of . The more isolation levels you need , The more resources are spent . Compared to a dedicated environment , Easy horizontal expansion is Docker A big advantage of . However, in Docker Medium level scaling can only be used for stateless computing services , The database doesn't work . We don't see any isolation for the database , Then why should we put it in a container ？
6、 The inapplicability of cloud platform
Most people start projects through shared cloud . Cloud simplifies the complexity of virtual machine operation and replacement , So there is no need to test the new hardware environment at night or on weekends when no one is working . When we can start an instance quickly , Why do we need to worry about the running environment of this instance ？ That's why we pay a lot of fees to cloud providers . When we place the database container for the instance , These conveniences mentioned above do not exist . Because the data are inconsistent , The new instance will not be compatible with the honest one , If you want to restrict the use of stand-alone services by instances , Should let DB Use a non containerized environment , We just need to keep the elastic expansion capability for the computing service layer .
7、 Environment requirements for running database
Often seen DBMS Containers and other services run on the same host . However, these services have very different hardware requirements . database （ Especially relational databases ） Yes IO The requirements are high . In general, database engine uses special environment to avoid concurrent resource competition . If you put your database in a container , Then it will waste the resources of your project . Because you need to configure a lot of extra resources for this instance . In public , When you need 34G Memory time , The instance you started must be opened 64G Memory . In practice , These resources are not fully used . How to solve ？ You can design in layers , And use fixed resources to start multiple instances at different levels . Horizontal scaling is always better than vertical scaling .
In view of the above problems, does it mean that the database must not be deployed in the container ？ The answer is ： It's not that we can lose data to insensitive businesses （ Search for 、 Buried point ） It can be containerized , Use database fragmentation to increase the number of instances , Thus increasing throughput .docker Suitable for running lightweight or distributed databases , When docker Service hangs up , Will automatically start a new container , Instead of continuing to restart the container service . The database can automatically scale by using middleware and containerization system 、 disaster 、 Switch 、 With multiple nodes , It can also be containerized .
Link to the original text ：http://l.wz2.in/0AC
author ： Macy loves cycling
Focus on Brother Wu talks about programming
Make a little progress every day
Praise is the biggest support