1. The controller is the "brain" of the k8s cluster
The controller itself is not new to us. The washing machines, refrigerators, air conditioners, etc. we use every day rely on the controller to work normally
The core components of the K8S cluster can be logically divided into three parts: the core component etc database, the entry component API Server that directly operates etcd, and other components scheduler scheduler
2. "Simple Refrigerator" of Analogy Learning Method
Understand the principle of the K8S cluster controller by analyzing the design process of a simple refrigerator
The refrigerator consists of five components: the cabinet, the cooling system, the lighting system, the thermostat, and the door.
The refrigerator has only two functions: when someone opens the refrigerator door, the light inside the refrigerator will automatically turn on; when someone presses the thermostat, the refrigeration system will adjust the temperature inside the refrigerator according to the temperature setting.
3. Unified entrance
This entrance provides users with two interfaces: opening and closing the door and adjusting the thermostat, so how does the entrance specifically adjust the status of the refrigerator door and the thermostat? This is a problem.
4. Controller
The controller was created to solve the above problems. The controller is a bridge between the user's operation and the correct state of each component of the refrigerator: when the user opens the door, the controller observes the change of the door, and it
Turn on the light in the refrigerator for the user; when the user presses the thermostat, the controller observes the temperature set by the user, and it manages the refrigeration system for the user to adjust the temperature in the refrigerator.
5. Controller manager
A refrigerator has a lighting system and a cooling system. Obviously, it is more reasonable for us to implement a controller for each component than a controller to manage two components. At the same time we implement a controller manager to unify
Maintain all these controllers to ensure that the controllers are working properly.
6、SharedInformer
We hand over the task of monitoring the state changes of refrigerator components to a new module SharedInformer. SharedInformer acts as a proxy for the controller, monitoring the refrigerator for the controller
The state of the component changes, and according to the preferences of the controller, the change of the state of different components is notified to the corresponding controller. Through optimization, such a SharedInformer can greatly relieve the pressure on the refrigerator entrance.
7、ListWatcher
Assuming that SharedInformer communicates with the refrigerator entrance through http protocol, then http chunked transfer encoding is a good choice to implement ListWatcher.
The controller sends a query to the refrigerator portal through the ListWatcher and then waits. When there is a change in the refrigerator components, the portal notifies the controller through a chunked http response.
When the controller sees the chunked response, it will think that the response data has not been sent, so it will continue to wait.
8. Example
From the evolution process of a simple refrigerator above, we understand the significance of the controller, its role, and the way of realization. Now let's go back to the K8S cluster. The K8S cluster implements a large number of controllers, and the
In the foreseeable future, controllers with new functions will continue to appear, and some old controllers will be gradually eliminated. Currently, we use more commonly used controllers, such as pod controllers, deployment controllers,
service controller, replicaset controller, etc. Some of these controllers are implemented and managed by the kube controller manager, while the route controller and service controller are implemented by the cloud controller manager.
8.1 Service Controller
First, the user requests the API Server to create a service of type LoadBalancer, API
Server receives the request and writes the details of the service into the etcd database. And this change, controlled by the service
The controller observed. The service controller understands services of type LoadBalancer, except for services stored in etcd
In addition to the internal service records, an SLB is required as the service entry, and several endpoints as the service
service backend. So the service controller requests SLB's cloud openapi and API Server respectively to create
SLB resources, and endpoints resources in the cluster.
8.2 Routing Controller
In the chapter on cluster network, we mentioned that when a node joins a K8S cluster, the cluster
A route needs to be added to the VPC routing table to build the trunk road from the newly added node to the pod network.
And this thing is done by the routing controller. The route controller completes the process of this matter, and the above service
The processing flow of the controller is very similar and will not be repeated here.
conclusion
Basically, the controller of the K8S cluster actually acts as the brain of the cluster. With the controller, the K8S cluster has the opportunity to get rid of mechanical and passive, and become an automatic, intelligent, and useful system.