Kube builder practice 4: operator requirements description and design

Time:2022-5-24

Welcome to my GitHub

https://github.com/zq2599/blog_demos

Content: classification and summary of all original articles and supporting source code, involving Java, docker, kubernetes, Devops, etc;

Links to series articles

  1. One of kubebuilder’s actual combat: preparation
  2. Kubebuilder practice 2: experience kubebuilder for the first time
  3. Kubebuilder practice 3: a quick overview of basic knowledge
  4. Kube builder practice 4: operator requirements description and design
  5. Kubebuilder practice 5: operator coding
  6. Kubebuilder practice 6: build, deploy and run
  7. Kubebuilder practical battle 7: webhook
  8. Kubebuilder practice 8: Notes on knowledge points

Overview of this article

  • As the fourth part of “Kube builder actual combat” series, we have experienced full preparation. Starting from this part, let’s develop a practical operator namedelasticweb, i.e. elastic web service;
  • This will be a complete actual combat of operator development, involving design, coding, deployment and other linksKubebuilder II: first experience of kubebuilderThe difference is that elastic web has clear business meaning from CRD design to controller function, and can execute business logic, while kubebuilder Practice II is only a development process experience;
  • In order to do a good job of this operator, this article is not in a hurry to code, but to do a good job in the design. What functions, problems and core contents of our operator will be sorted out in this article. With such preparation, we can write code that meets the requirements in the next chapter;
  • Next, let’s talk about some background knowledge to better get to the point;

Demand background

  • QPS: queries per second, that is, the query rate per second, that is, how many requests the server processed in one second;
  • Background: students who have done website development should all know about horizontal capacity expansion. In short, suppose that the QPS limit of a Tomcat is 500. If the QPS of external access reaches 600, in order to ensure the service quality of the whole website, we must start another Tomcat to jointly share the request, as shown in the figure below (for simplicity, suppose that our background service is stateless, that is, it does not depend on the IP and local disk of the host):

在这里插入图片描述

  • The above is the conventional practice of horizontal expansion. In kubernetes environment, if the external request exceeds the processing limit of a single pod, we can increase the number of pods to achieve the purpose of horizontal expansion, as shown in the following figure:

在这里插入图片描述

  • The above is the background information. Next, let’s talk about the specific functions of the elastic web operator;

Requirement description

  • In order to clarify the requirements, here is a fictional scene: Xiao Xin is a java developer, which is the sister in the figure below:

在这里插入图片描述

  • Now Xiaoxin is going to deploy the springboot application to kubernetes. Her current situation and problems are as follows:
  1. Springboot application has been made into docker image;
  2. The QPS of a single pod is 500 through pressure measurement;
  3. It is estimated that the total QPS after going online will be about 800;
  4. With the change of operation strategy, QPS will be adjusted;
  5. Generally speaking, Xiaoxin has only three data in her hand: docker image, QPS of a single pod and total QPS. She doesn’t know kubernetes. She needs a scheme to help her deploy the service and support external high concurrent access during operation;

These are Xiaoxin’s needs. Let’s summarize:

  1. Let’s develop an operator for Xiaoxinelasticweb), for Xiaoxin, she only needs to tell elasticweb the three parameters in her hand (docker image, QPS of a single pod and total QPS);
  2. Elasticweb creates pods in kubernetes. Of course, the number of pods is calculated automatically. To ensure that it can meet the requirements of QPS, take the previous case as an example, it needs two pods to meet the QPS of 800;
  3. The QPS of a single pod and the total QPS may change at any time. Once there is a change, elastic web should automatically adjust the number of pods to ensure the quality of service;
  4. In order to ensure that the service can be called externally, let’s help Xiaoxin create a service (she doesn’t know much about kubernetes, so let’s do it easily);

Self insurance statement

  • After reading the above requirements, smart people will despise me. In fact, kubernetes already has ready-made QPS adjustment schemes, such as modifying the number of copies of deployment, vertical expansion of a single pod, autoscale, etc. this time, the use of operator is only to show the development process of operator, not to say that custom operator is the only solution;

  • Therefore, if you think my method of capacity expansion with operator is very low,Please don’t scold me too badly, I just want to show the development process of the operator. Besides, our operator is not good for nothing. With this operator, you don’t have to pay attention to the number of pods. Just focus on single instance QPS and total QPS. These two parameters are closer to the business;

  • In order not to complicate things,It is assumed that the CPU and memory required for each pod are fixed, write it directly in the operator code. In fact, you can also change the code yourself to be configured externally, just like the image name parameter;

  • Explain the requirements clearly, and then enter the design link. First design the CRD, which is the core data structure;

Spec part of CRD design

Spec is used to save the user’s expectations, that is, the three parameters in Xiaoxin’s hand (docker image, QPS of a single pod and total QPS), plus the port number:

  1. Image: the image corresponding to the business service
  2. Port: the host port occupied by the service. External requests access the service of the pod through this port
  3. Singlepodqps: QPS upper limit of a single pod
  4. Totalqps: the total QPS of the current business
  • For Xiaoxin, entering these four parameters is the end;

Status part of CRD design

  • Status is used to save the actual value. It is designed to have only one fieldrealQPS, which indicates the QPS that the whole operator can actually support. In this way, whenever Xiaoxin uses itkubectl describeCommand to know how many QPS the current system can actually support;

CRD source code

  • The best way to clarify the data structure is to look at the code:
package v1

import (
	"fmt"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"strconv"
)

//Expected state
type ElasticWebSpec struct {
	//The image corresponding to the business service, including the name tag
	Image string `json:"image"`
	//The host port occupied by the service. External requests access the service of the pod through this port
	Port *int32 `json:"port"`

	//QPS upper limit of a single pod
	SinglePodQPS *int32 `json:"singlePodQPS"`
	//Current total QPS of the whole business
	TotalQPS *int32 `json:"totalQPS"`
}

//In the actual state, the values in the data structure are calculated by the business code
type ElasticWebStatus struct {
	//Total QPS actually supported in current kubernetes
	RealQPS *int32 `json:"realQPS"`
}

// +kubebuilder:object:root=true

// ElasticWeb is the Schema for the elasticwebs API
type ElasticWeb struct {
	metav1.TypeMeta   `json:",inline"`
	metav1.ObjectMeta `json:"metadata,omitempty"`

	Spec   ElasticWebSpec   `json:"spec,omitempty"`
	Status ElasticWebStatus `json:"status,omitempty"`
}

func (in *ElasticWeb) String() string {
	var realQPS string

	if nil == in.Status.RealQPS {
		realQPS = "nil"
	} else {
		realQPS = strconv.Itoa(int(*(in.Status.RealQPS)))
	}

	return fmt.Sprintf("Image [%s], Port [%d], SinglePodQPS [%d], TotalQPS [%d], RealQPS [%s]",
		in.Spec.Image,
		*(in.Spec.Port),
		*(in.Spec.SinglePodQPS),
		*(in.Spec.TotalQPS),
		realQPS)
}

// +kubebuilder:object:root=true

// ElasticWebList contains a list of ElasticWeb
type ElasticWebList struct {
	metav1.TypeMeta `json:",inline"`
	metav1.ListMeta `json:"metadata,omitempty"`
	Items           []ElasticWeb `json:"items"`
}

func init() {
	SchemeBuilder.Register(&ElasticWeb{}, &ElasticWebList{})
}

Business logic design

  • The completion of CRD means that the core data structure has been determined. The next step is the design of business logic, which is mainly to clarify what is done in the reconcile method of controller. In fact, the core logic is very simple: calculate how many pods are needed, and then update the deployment to make the number of pods meet the requirements. On this core basis, do the trivial things such as creating deployment, service and updating status well, and you’re done;

  • Here, the flow chart of the whole business logic is given as follows to guide the development:

在这里插入图片描述

  • So far, we have completed the requirements and design of the entire elasticweb. You must be smart and can’t wait to start the development. OK, let’s officially start coding in the next article!

reference material

  • You may wonder that Xiao Xin doesn’t know about kubernetes. How can she know the production of docker image and how she measures the QPS of a single pod?
  • Actually, she isProgrammer XinchenFans of have read the following Blogs:
  1. Why do spring boot-2.3 image schemes have multiple layers
  2. Experience springboot (2.3) application making docker image (official scheme)
  3. Detailed explanation of making docker image with springboot (2.3) application (official scheme)
  4. Performance testing trilogy of Web services under kubernetes: preparation
  5. Web service performance testing trilogy II under kubernetes: vertical capacity expansion
  6. Performance testing trilogy of Web services under kubernetes: horizontal capacity expansion

You’re not alone. Xinchen’s original accompanies you all the way

  1. Java series
  2. Spring series
  3. Docker series
  4. Kubernetes series
  5. Database + middleware series
  6. Devops series

Welcome to official account: programmer Xinchen

Wechat search “programmer Xinchen”. I’m Xinchen. I look forward to traveling with you in the Java World
https://github.com/zq2599/blog_demos

Recommended Today

HTML webpack plugin package error

Package errorTypeError: Cannot read property ‘tap’ of undefined at HtmlWebpackPlugin.apply (D:\MyData\xiamy9\study\webpack-study\node_modules\html-webpack-plugin\index.js:40:31) at WebpackCLI.webpack (D:\MyData\xiamy9\study\webpack-study\node_modules\webpack\lib\webpack.js:51:13) at WebpackCLI.createCompiler (D:\MyData\xiamy9\study\webpack-study\node_modules\webpack-cli\lib\webpack-cli.js:1845:29) at async WebpackCLI.buildCommand (D:\MyData\xiamy9\study\webpack-study\node_modules\webpack-cli\lib\webpack-cli.js:1952:20) at async Command.<anonymous> (D:\MyData\xiamy9\study\webpack-study\node_modules\webpack-cli\lib\webpack-cli.js:742:25) at async Promise.all (index 1) at async Command.<anonymous> (D:\MyData\xiamy9\study\webpack-study\node_modules\webpack-cli\lib\webpack-cli.js:1289:13) It is found that there is a version problem: webpack 4 and html-webpack-plugin5 are used together. It is necessary to upgrade […]