Technical Dry Goods | Use Kubernetes to manage containerized stateful applications

About the Author:
Josh Berkus works at Red Hat's Project Atomic, which requires close contact with Kubernetes, AtomicHost, Docker, Fedora and other projects. He has over ten years of experience at the PostgreSQL core team and contributes to the popular open source database.
At present, almost all of the talk about containers and micro services are linked to "stateless" applications. This is perfectly understandable because stateless applications are simpler. And the extent to which the container and the arrangement technique are mature enough to be able to carry the work load: stateful application. Kubernetes is an open source system for automating the deployment, expansion and management of containerized applications. I have done two previous speeches on Kubernetes, databases and containers.

【Description】 Kubernetes overview and architecture, deployment and core mechanism analysis, advanced articles – kubernetes tone work principle and source code analysis.

What is a "stateless" application?

Stateless services, such as Web servers, proxies, and application code, that can handle data but do not store. During the choreography, developers prefer to use them because they are easy to deploy and easy to scale. If the traffic rises, you only need to add more load balancing. More importantly, they are "unchanging"; the upstream container mirrors and the containers that are running in the infrastructure are virtually indistinguishable. This means that they can be replaced at any time, and there is little need to "switch costs" in the container instance switching process.

What is a stateful application?

Stateful services, namely, routers, CDN (content delivery network), streaming server and authentication server. From the beginning of the deployment, these containers began to mirror with the upstream, the longer the difference between them the greater the difference. This difference is called "state". In fact, each running application has at least one small state (difference), but for a "stateless" application, the state (difference) is small and can be quickly replaced.
For the state, it is not the case. Although states can be synchronized or replicated through state nodes, this must be done outside the business process system through some of the application-specific methods. In short, it is very troublesome.

Database and stateful applications <br /> Of course, given that I have worked in PostgreSQL for 18 years, I really care about stateful applications that are transactional databases. The database is critical to most application stacks and is a good test case for state support because they can be stateful in an efficient way, including:

Cluster Role

For example, PostgreSQL needs to store data and transactions in each PostgreSQL container (storage) persistent and unique file. Each container needs to be identified as a specific database node, and we need to be able to route traffic to it by name or address. Database client connections or sessions also have a state, breaking them need to pay a certain price, so we do not want to move the database node. Finally, each database node has a role in its database cluster, cluster roles such as master, replica, shard, and so on. These cluster roles will persist until the database-specific event changes them.
Implementing these types of states on popular container cloud stacks has been challenging. Docker and Arrangement Frames treat most types of states as occurring outside the container stack, forcing database architects to manage storage, identity, routing, and many other things. You have no way to move the database to help your container. As a result, while many web applications are already containerized, few databases or other stateful applications can be containerized. So where do we store the data? In general, you can use Amazon RDS.

Use Kubernetes StatefulSet's database
The Kubernetes project has been working on an object and a set of features last year called StatefulSet to handle databases and other state services. Developers originally released this feature with the name "PetSet", but they made changes and then posted them to version 1.5 and have a more appropriate name: StatefulSet. At this point, StatefulSet implements Storage and Identity with state quality. The other two can use Kubernetes as the resource implementation with minimal bonding code. In other words, you do not have to wait to deploy a coordinated containerized database.
Now you can run the database in the container on Kubernetes, but why should you do that? The answer is nothing to do with the container, but it is related to the benefits of the arrangement.
We expect high availability (HA) in the modern database platform, hoping it will stand out, but it's not enough to rely on database software. Introducing a cluster, replacing a database node on a failed machine, rerouting the application traffic to the migrated node, and other HA considerations requires a lot of code and many of the database's external and its internal utilities.
This code is hard to write because it needs to implement a distributed system first.
Like other business process systems, Kubernetes provides an easy-to-use distributed system. Database (such as PostgreSQL and MySQL) If there is no built-in HA, it is easy to become such, already HA database (such as Cassandra and RethinkDB), it can be achieved automatically. In terms of experience, it is easier than ever to start from scratch.
I created some examples of how to use StatefulSet to deploy PostgreSQL on my atomicdb demo repo. Above these are to illustrate how to use the function, not a complete production deployment. Zalando is the leader of the Patroni cluster management project, which publishes the Helm Chart, which is based on Kubernetes' cluster PostgreSQL.
(Small note: For more information on the author's speech, please click "read the original"!)

This article by the only cloud editing translation, if reproduced, must be marked reproduced from "only Caicloud", see the original, please click: … tions

Heads up! This alert needs your attention, but it's not super important.