To build a simple CaaS system _docker

Source: Internet
Author: User

In the CAAS system before the enterprise application architecture is basically iaas/saas/paas and other patterns monopoly, until Docker appeared for us to open another door, nonsense do not say, we go straight to the theme.

Let's begin by understanding how the next simple CaaS system provides services to users:

    1. The enterprise user uploads its application code or other code hosting way, we generate the user application The mirror, or the user uploads the mirror directly, or the user directly uses our provision the basic service mirror
    2. The user deploys his mirror application and launches its mirrored container
    3. User access to his application services

OK, the demand is OK, it's time to move the bricks.

User image making

Since it is a simple caas system, we will not allow users to upload code or the use of Third-party code hosted, directly let them make a mirror to submit to us, so we need to build a docker to allow users to upload a mirror, assuming that the user uploaded mirrors follow this format:d ocker address/{ Appid}:{version}, this to the user has certain requirements, after all, some users may even Docker is not know what is more do not expect them to write Dockerfile production mirror delivered to us. Of course, if we provide some basic services mirroring (such as MySQL services, Redis services, etc.) to the user that is the best.

Start User Mirroring

With a user-made mirror, it's time to start it.

Docker pull Docker address/{appid}:{version} Docker run-d docker

servers/{appid}:{version} 

Startup is simple, but this is not what we want, after all, we are to enable users to access the services he deployed, if the user's service is a Web service, then you have to expose the user's Web service port, which requires us to determine the container communication scheme:

    1. Share a network space with host hosts
    2. Publish a container port to allow Docker to randomly select an unused high port
    3. Publishes a container port and maps the specified port on the host to the external routing service
    4. Use the Docker ' links ' to allow communication between containers. If a new container is linked to an existing container, the new container will obtain the link information for the existing container through the environment variable, and an associated container will obtain its corresponding connection information, allowing it to connect automatically after it processes those variables. This allows the container on the same host computer to communicate directly without knowing the port and address of the corresponding service

Our simple CaaS system also temporarily does not use the communication between the containers, if the host with a network space that is--net= "host" mode, then if a number of users upload the mirror, their Web service port is 8080, apparently the host can only start a 8080 port, Only one user's container starts successfully, others because the port is already occupied, causing the startup to fail, here we choose a third mode, select the specified port mapping to publish the container, which is also convenient for us to manage the host port resources. OK, change the startup mode to the following:

Docker run-d-P 25701:8080 Docker address/{appid}:{version} 

In order not to allow a user's application to consume too much resources to affect other applications across the host, we have slightly limited the user's resources, such as limiting the user's use of the container's memory and CPU weight:

Docker run-d-P 25701:8080-m 512m-c 1024 docker servers address/{appid}:{version} 

In order to be able to achieve horizontal expansion, container services are best to be stateless, so as to better achieve load balancing and horizontal expansion.

The application started successfully, and we can access the container's 8080 port service by accessing 25701 on the host.

In writing code, we use the Docker Remote API client libraries to launch the Unload container, the specific code implementation is not much to say.

Service discovery

After the container starts successfully, how does the user access to his container service, the total cannot provide the host IP to the user direct access, this needs us to construct a service discovery component.

How Service Discovery Works

When each service is launched online, they register their information by discovering tools
The consumer of the service is able to query the information about the service at a preset terminal, and then it can interact with the components it needs based on the information that is found
For simplicity, we use zookeeper as our service Discovery tool.

First, we register the service into zookeeper after the container has started successfully, and store the path path as follows:/caas/service/address/{appid}/{version}, the storage Service child node is {containerid}->{ Host ip}:{Service Port}.

For example, users appId01 and APPID02 deployed their respective application versions of the container containerId01 and CONTAINERID02, respectively, the corresponding service ports are 25701 and 25702, then ZK stored the registry information as follows:

/CAAS/SERVICE/ADDRESS/APPID01/APP01VERSION/CONTAINERID01-> {host ip}:25701

/caas/service/address/appid02/ APP02VERSION/CONTAINERID02-> {host ip}:25702 

If a user deploys multiple container instances, the corresponding ZK registry information is similar to the following:

/CAAS/SERVICE/ADDRESS/{APPID}/{VERSION}/CONTAINERID01-> {host ip}:25701

/caas/service/address/{appid}/{ VERSION}/CONTAINERID02-> {host ip}:25702

/caas/service/address/{appid}/{version}/containerid03-> {host IP} : 25703

/caas/service/address/{appid}/{version}/containerid04-> {host ip}:25704 

Fault detection

Above we completed the service registration, after registering the service in order to realize the application high availability, we should also need to carry on the fault detection to the container, the fault detection plan usually has below 2 kinds:

    • Component Active request Service Discovery Heartbeat mode: component can set a timeout time, and can periodically go to request service discovery to reset timeout time, timeout time to reach threshold update registry
    • Service discovery Active Request Component Heartbeat mode: Service Discovery periodic Health Check component and update registry when component fails

Typically, internal services can use the first way to get components to actively request service discovery, users write their own services generally can not struggle to achieve the heartbeat to access the service discovery component, so often require users to implement a service discovery component can access the heartbeat interface, so that the service Discovery component to actively request the user's application, Once the access fails after a certain number of retries to assume that the application has failed to continue providing the service, you can choose to stop deleting the user container directly or reboot according to the policy.

For example, the Health Check component of service discovery can access the user's heartbeat interface at a certain time, similar to the host ip}:25701/_ping.

Registry security Access

Based on security considerations, we usually need to make the appropriate access control for the service discovery, in order to secure access to the information stored in the registry, there may be several scenarios for reference:

    1. The Service Discovery tool can use SSL/TLS encrypted links
    2. To encrypt the write data, the information used by the user must be obtained from the service discovery with the corresponding key decoding
    3. Service discovery realizes access control, splits different key values into different groupings, and makes different secret keys according to the needs of the visit to access the corresponding groupings.

Here we do not say the specific security aspects of the implementation, who let us is a simple version of the CAAS system.

Distributed Configuration Storage and load balancing

In fact, the service found that the registry storage access address is only one aspect, you can use it to save other information, such as the storage application configuration, you can configure dynamic adjustment applications, but also to save the relevant indicators of the container, load balancing is a good example, It can be found through the query service to the various backend nodes to withstand the number of traffic, and then based on this information to adjust the configuration. The specific load balancing algorithm can be selected according to the requirements, we use the simplest round bobin algorithm, that is, polling way to access. This implementation involves another component of the CAAS system: Routing gateways, which are described later.

Above we have been using zookeeper as a Service discovery tool, in addition to zookeeper, we can also use other service discovery tools: Etcd, Consul, crypt, CONFD, you are interested to understand, The most important thing is to ensure the data consistency of the registry information.

Dispatch choreography

Your CaaS system is basically small by the steps above, but it's not enough. We need to implement a scheduler component in our production environment as the number of user application containers increases to support the need to increase host hosts to avoid resource shortages, or to deploy individual instances of some users to the specified host host.

Host Selection

CaaS system is a distributed system, in the environment of multiple host hosts, we need to know the user's application in which host, if the single words that do not need to choose, directly designated on the good. The following points need to be considered in the specific scheduling:

    1. A default scheduling policy is required, such as selecting the host Deployment service with the most available memory or selecting the most idle host deployment service for the CPU
    2. The scheduler needs to provide an overlay mechanism, such as 2 containers must be deployed on the same host as a unit to run, more than 2 instance containers of a service must be deployed on different machines to achieve high availability
    3. The scheduler needs to meet the constraints, such as labeling specific host hosts, such as services that need to be deployed on every host in the cluster
    4. Multi-Container deployment scheduling

As the business expands, we may need to provide packet container management, which will be handled as a separate application of the container of a collection (usually a component with a tightly interdependent relationship), such as a Web service container plus a backend database service container combined into a project to publish. There is no more discussion here, and our simplified version of the system has not yet taken this step.

Supply

Provisioning is the process of bringing a new host online and completing basic configuration so that they can work, usually in cluster management to automatically extend the host, management tools to define the requirements for additional hosts, and automatically trigger conditions, for example, if your application is heavily loaded, You may want to allow your system to add additional machines and extend the container horizontally to ease the load, as we do not implement it, simply manually adding the host to the simple version is good.

Here's a relatively simple scenario for implementing a scheduler:

The main use of relational database such as MySQL to store host information, dispatcher query host related index information according to the scheduling algorithm to select the appropriate host to deploy, the use of optimistic locks to ensure the concurrency of data consistency, the use of transactions to ensure the deployment and unloading operations, such as atomic nature. Here may be a lot of pits, you can also use the current more popular scheduler, commonly used dispatchers are: Fleet, Marathon, Swarm, Mesos, Kubernetes, Compose, we are interested to understand.

Gateway

Above we introduce the gateway in the load balancing aspect of service discovery, we take it as an important component in the CAAS system, he is mainly responsible for the forwarding of the user request, for example the user deploys the container service that the container wants to access it, When this request arrives at the gateway, the gateway selects the appropriate backend container service based on the policy and forwards the request. According to the user's setting, the dynamic routing request to the corresponding container instance, which is equivalent to a proxy server. How to select the container instance service forwarding needs to implement the load balancer, we can do it by querying the service Discovery component to obtain the corresponding container information. Since it is a proxy service, we can in the middle of the user's request to do other processing, such as blacklist filtering, do traffic statistics, do cnames Routing and so on

Suppose our CaaS gateway access domain name is mycaas.gateway.cn, the user deploys a Web application container instance in our backstage, The dispatcher deploys him on the 10.10.10.101 host, the container service port is mapped to 25701, the user requests the mycaas.gateway.cn to arrive at the gateway, the gateway according to the request information to identify the user to query all of the user's application container information, get all the container service address, according to the load Balancing Rule agent forwarding to the target Label Container Service. This query service discovery process is best to implement a local cache, such as using zookeeper cache to reduce and avoid each request to access the service Discovery component, while agent forwarding to maximize the use of connection pooling to reduce overhead.

Summarize

At this point our simple CAAS system has been designed, in the entire system has a service discovery/Dispatcher/gateway and other components coordinated.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.