Spring Cloud + kubernetes Micro Service Framework principle and practice

Source: Internet
Author: User
Tags docker run k8s aliyun

As early as half a year ago, the company began to implement the container deployment scheme Appos, although the publishing interface is too geek, very obscure, but carefully studied really feel very powerful, after the implementation of the container, computing resources (CPU, memory) utilization can be greatly improved, reduce the number of servers, thereby saving technical costs.

Coincidentally, a number of friends in the start-up companies are also recently trying to do micro-services, container. Architecture abandons the Dubbo of SOA, joins the spring cloud camp, deploys directly from past cloud servers and upgrades to containerized deployments based on kubernetes clusters. Spring Cloud

The concept of micro-service from a developer's perspective and the difference between SOA is small, according to the business domain fine-grained split system for a number of services, services only access to the corresponding database, according to the project team, the service independent development, deployment and iteration. The call between services is completed through RPC.

Use a few diagrams to illustrate the concepts associated with spring cloud in a straightforward and concise way.

The above illustration shows a simple system that has several components. The service provider, Demo Service, from the developer's perspective, is a stand-alone project (or subproject) that only provides an interface declaration to the outside, and it runs as a stand-alone process, like a Web server, waiting for a remote call. The figure depicts three of the same to describe its deployment, three service processes are located on three hosts, high availability (one hangs, does not affect all), scalable (can be increased to 10), it accesses its own corresponding database. Service consumer, Demo Consumer, in order to simplify only one instance, and as a service provider, it can also be highly available and scalable. Because the service provider and the consumer are deployed on different hosts, the calls between them use the RPC (remote Service invocation) registry, Eureka-server, since there are service providers and service consumers, and they are all running on different hosts, then how to let service consumers discover, And the service provider is invoked according to the corresponding protocol, which introduces the concept of the registry. If the reader has Dubbo experience, it is easy to think of the zookeeper cluster, they provide a similar function.
Of course, its deployment is also to support high availability (multiple instance registration composition cluster), three core components have surfaced, routing, Zuul, on the top side of the graph. is also the overall framework for the development of external network portal. The URL rule configuration allows the request to be forwarded to the appropriate service, such as request get API.DUMMY.COM/DEMO_CONSUMER/USER/1 via Zuul, which can forward the request to DEMO-CONSUMER:GET/USER/1. Of course Zuul can also support more, including universal authentication, filters and so on.

The core components involved the service consumer and service provider are implemented through RPC calls, through the registry, the service consumer to discover the service provider, along with its natural introduction of client load balancing and fuse-related concepts. The easiest way for consumers to hold examples of several providers is by taking turns, which is the client load balancing; If a service provider has reached a threshold for a period of time in the past, it can be temporarily set to unavailable, which is the fuse. The associated built-in components, ribbon, and Hystrix are available in spring cloud.

Of course, everything is not absolute, one of the advantages of spring cloud is that there are many good compatibility options in the community, as mentioned in Musical.ly's Spring Cloud architecture Practice: The team has made more changes to the framework itself, replacing the more friendly registry consul, Using the GRPC as a remote call framework, using PROTOBUF as a serialization framework, replacing the fuse and current limiting scheme, integrating the fault diagnosis and tracking functions, etc., these modifications are transparent to the business. deployment of micro-services

With spring cloud, different businesses can be split into different projects and can be deployed separately. You can use Jenkins to build a simple continuous integration and continuous delivery solution. After the developer pushes the code into the GIT repository, it triggers the Jenkins build action, and further can use Jenkins to execute the publishing scripts in different environments, and of course the scripts can perform backups and rollback actions.

Implementation here, the System program can support a company to go very far, that kubernetes and what a chivalrous place.

Assuming the company's further development, flow and business are extremely high, there will be two more common problem expansion action is still a bit of trouble, you can through the prepared operating system mirroring (including various online operating environment), the new instance quickly ready, but still need to update the publishing script. Of course, if there is a strong operational team, it can be almost automated. A lot of waste of resources, because there are a lot of services, access is very small, a large number of machines may be less than 5% CPU utilization of the case occurs (from Tencent colleagues share that their optimization goal is CPU utilization average 30%), resulting in high technology costs.

The ideal situation, if the operation of the machine, all through a portal, unified management, unified grasp of the use of cluster resources, the cluster needs to expand or shrink capacity, as long as the increase or recovery of the server, the need for a service expansion capacity, as long as a simple set of replicas quantity. That would be nice. (Of course kubernetes much more than this function) kubernetes (k8s)

If you have Docker experience, it is easy to understand k8s, the original use of Docker may only be used to build CI/CD, a command to start Gitlab, and then a command to start a Jenkins, all super simple. In many tutorials, a number of micro-services will be placed in a server in the Docker run, you will find service registration, service discovery is very simple.

However, when the container runs on different servers, the problem comes, and you even find that there is no communication between the containers across the host.

K8s from Google, Gaofu origin, decided to debut on their own with a variety of halo. The market share has already surpassed 70%, has become the container management mainstream tool. In practice, because a lot of data practice background is GPE on the k8s cluster, network, storage and other infrastructure are provided by the platform, all feel easy, but once you try to build a private k8s cluster, but found that the world is hostile, even the basic network plug-ins need to install themselves.

Google is good, and provide $300 vouchers and a one-year probation is very kind, but Google is not you want to visit you can access. Fortunately Aliyun also offers kubernetes cluster services, which cost more than ECS, but are also cost-effective compared to the cost of an operational team and a variety of constant potholes.

This article first introduces some basic concepts, and then describes the practice of deploying the Aliyun of Spring cloud on the k8s cluster of the group. Cluster

A cluster is a set of nodes, which can be physical servers or virtual machines, with the Kubernetes platform installed on top. The following illustration shows such a cluster. Note that the figure is simplified to emphasize the core concept. Here you can see a typical kubernetes architecture diagram. Pod

The most basic scheduling unit in k8s is pod, which has a network and is stored. The pod runs one or several Docker containers. Containers in the same pod share the same network namespace, and you can use localhost to communicate with each other. It can be understood that pod is a host, and the Docker container is the process that runs on the host computer. Replication Controller

We don't normally create pods ourselves, which is difficult to manage. Using replication Controller, you can define the contents of POD operations, the number of replicas, and other information, and its upgraded version is Replicaset. Now that you have created some copies of the pod, how do you balance the load on these replicas? What we need is a service. Service

A set of pods can be composed of services Service,service has a virtual Clusterip, service access can be clusterip as a unified request portal, because a service corresponding to a set of pod, so you can do load balancing. Services can expose external services through a nodeport,loadbalancer approach. Note that type = LoadBalancer requires a cloud service platform to provide the basis for the service, the k8s cluster by default is not this thing. If you define the service type = LoadBalancer on the Ali Cloud, you will find that the load Balancing page in the admin backend will add a load balancer Kubectl get service execution results, and note that External-ip automatically creates a load balancer to provide a unified entrance to the external , backend corresponding container pod practice

To reduce costs, the author purchased the lowest-configured k8s cluster from Aliyun, comprising 3 master nodes and 2 node nodes. Basically is the lowest configuration, cost 30 dollars per day. Prepared a hand-and-foot rack code, including several basic projects Demo-service service provider demo-provider service consumer Eureka-server Registry Api-gateway Gateway

You need to deploy the registry Eureka-server first, then deploy the service provider Demo-service and consumer Demo-provider, and finally deploy Api-gateway.

Then the hand is the code, the opposite is k8s cluster, how to deploy it, the answer is mirroring services.
Ali's Mirror service is a choice, of course, can also choose other, through the CI/CD scheme, automatically after the construction of the mirror, tag, pushed to the mirror service provided by the registry, and then you can use.

For example, in a mirrored warehouse, there are the following mirrors: Http://registry.cn-beijing.aliyuncs.com/tianming/demo-service:latest, by writing the Yaml file, define the RC

APIVERSION:V1
kind:replicationcontroller
metadata:
 name:demo-service
Spec:
 replicas:2
 Selector:
  app:demo-service
 Template:
  metadata:
   Labels:
    app:demo-service
  Spec:
   Containers:
    -Name:demo-service
      image:registry.cn-beijing.aliyuncs.com/tianming/demo-service:latest
      ports:
      -containerport:8081

In the Black Font section, the image of the service will be published, where the number of replicas is set to 2, and the RC can be created by executing the bash command below

Kubectl create-f Demo-service-rc.yaml

You can then perform

Kubectl Get Pods

To see if the container was created correctly, if the pod has a state exception, such as Error can see the cause of the creation failure through the describe command, this command is useful and can help us to handle many problems.

Kubectl describe pod demo-service-xxx

Of course, this is not enough, we also need to define services, and services exposed to the interface:

APIVERSION:V1
kind:service
metadata:
 name:demo-service
Spec:
 type:loadbalancer
 ports:
  -port:8081
 selector:
  app:demo-service

The service is established because the LoadBalancer is set up so that it can be accessed via external IP on the external network. In a Prod environment, we do not do this, and generally only the Api-gateway project exposes the access port.

In this way, the deployment of other services in turn, if there is a feasible CI/CD solution, then the subsequent release, expansion capacity, will be a breeze. Broken Read

If you're a start-up CTO, you don't have the resources to build a cluster and you don't have the energy to learn k8s. In architecture selection, you can use only the cloud components of spring then deploy on the cloud host; If you have the ability to learn k8s, but do not have the energy and manpower to build a k8s cluster, you can buy cloud vendor cluster services, with this set of things, at least no longer worry about the future expansion of the pain, And as the relative ultimate form of the development of the architecture, there will be no need for reconstruction in the short term, so the future has the manpower and financial resources, and then moved back to the k8s cluster, it is easy.

Overall, the article is still somewhat shallow, practice in the Pits and optimization options, or far more than this article.

Transmission door: https://zhuanlan.zhihu.com/p/31670782

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.