|
| 1 | +--- |
| 2 | +title: Controllers |
| 3 | +content_template: templates/concept |
| 4 | +weight: 30 |
| 5 | +--- |
| 6 | + |
| 7 | +{{% capture overview %}} |
| 8 | + |
| 9 | +In robotics and automation, a _control loop_ is |
| 10 | +a non-terminating loop that regulates the state of a system. |
| 11 | + |
| 12 | +Here is one example of a control loop: a thermostat in a room. |
| 13 | + |
| 14 | +When you set the temperature, that's telling the thermostat |
| 15 | +about your *desired state*. The actual room temperature is the |
| 16 | +*current state*. The thermostat acts to bring the current state |
| 17 | +closer to the desired state, by turning equipment on or off. |
| 18 | + |
| 19 | +In Kubernetes, a _controller_ is a control loop that watches the |
| 20 | +state of your |
| 21 | +{{< glossary_tooltip term_id="cluster" text="cluster">}}, then |
| 22 | +makes or requests changes. Each controller tries to move the current |
| 23 | +cluster state closer to the desired state. |
| 24 | + |
| 25 | +{{% /capture %}} |
| 26 | + |
| 27 | + |
| 28 | +{{% capture body %}} |
| 29 | + |
| 30 | +## Controller pattern |
| 31 | + |
| 32 | +A controller tracks at least one Kubernetes resource type. |
| 33 | +These [objects](/docs/concepts/overview/working-with-objects/kubernetes-objects/) |
| 34 | +and their associated data are the desired state, so the |
| 35 | +controller(s) for that resource are responsible for making the current |
| 36 | +state come closer to that desired state. |
| 37 | + |
| 38 | +The controller might carry the action out itself; more commonly, in Kubernetes, |
| 39 | +a controller will send messages to the |
| 40 | +{{< glossary_tooltip text="API server" term_id="kube-apiserver" >}} that have |
| 41 | +useful side effects. |
| 42 | + |
| 43 | +### Control via API server |
| 44 | + |
| 45 | +The controller for Job is an example of a built-in controller that |
| 46 | +makes all of its changes by interacting with your cluster's API server. |
| 47 | + |
| 48 | +{{< glossary_tooltip term_id="job" >}} is a Kubernetes resource that |
| 49 | +runs a {{< glossary_tooltip term_id="pod" >}}, or perhaps several Pods, |
| 50 | +to carry out a task and then stop. |
| 51 | + |
| 52 | +(Once [scheduled](/docs/concepts/scheduling/), Pod objects become part |
| 53 | +of the desired state for a kubelet). |
| 54 | + |
| 55 | +When the Job controller sees a new task it makes sure that, somewhere |
| 56 | +in your cluster, the kubelets on a set of Nodes are running the right |
| 57 | +number of Pods to get the work done. |
| 58 | +The Job controller does not run any Pods or containers |
| 59 | +itself. Instead, the Job controller tells the API server to create or remove |
| 60 | +Pod [objects](/docs/concepts/overview/working-with-objects/kubernetes-objects/). |
| 61 | +Other components in the |
| 62 | +{{< glossary_tooltip text="control plane" term_id="control-plane" >}} |
| 63 | +act on the new information (there are new Pods to schedule and run), |
| 64 | +and eventually the work is done. |
| 65 | + |
| 66 | +After you create a new Job, the desired state is for that Job to |
| 67 | +be completed. The Job controller makes the current state be nearer |
| 68 | +to the desired state, by creating Pods that do the work you |
| 69 | +wanted. |
| 70 | + |
| 71 | +Controllers also update the objects that configure them |
| 72 | +For example: once the work is done for a Job, the Job controller |
| 73 | +updates that Job object to mark it `Finished`. |
| 74 | + |
| 75 | +(This is a bit like how some thermostats turn a light off to |
| 76 | +indicate that the your room is now at the temperature you set). |
| 77 | + |
| 78 | +### Direct control |
| 79 | + |
| 80 | +By contrast with Job, some controllers need to make changes to |
| 81 | +things outside of your cluster. |
| 82 | + |
| 83 | +For example, if you use a control loop to make sure there |
| 84 | +are enough {{< glossary_tooltip text="Nodes" term_id="node" >}} |
| 85 | +in your cluster, then that controller needs something outside the |
| 86 | +current cluster to set up new Nodes when needed. |
| 87 | + |
| 88 | +This kind of controller finds its desired state from the API |
| 89 | +server and then communicates directly with an external system |
| 90 | +to bring the current state closer in line. |
| 91 | + |
| 92 | +(There actually is a controller that horizontally scales the |
| 93 | +nodes in your cluster. See |
| 94 | +[Cluster autoscaling](https://kubernetes.io/docs/tasks/administer-cluster/cluster-management/#cluster-autoscaling)). |
| 95 | + |
| 96 | +## Desired versus current state {#desired-vs-current} |
| 97 | + |
| 98 | +Kubernetes takes a cloud-native view of systems, and is able to handle |
| 99 | +constant change. |
| 100 | + |
| 101 | +Your cluster could be changing at any point as work happens and |
| 102 | +control loops automatically fix failures. This means that, |
| 103 | +potentially, your cluster never reaches a stable state. |
| 104 | + |
| 105 | +So long as the controllers for your cluster are making useful changes, |
| 106 | +it's OK for the current state to be different from the desired state |
| 107 | +at a particular moment. |
| 108 | + |
| 109 | +## Design |
| 110 | + |
| 111 | +As a tenet of its design, Kubernetes uses lots of controllers that each manage |
| 112 | +a particular aspect of cluster state. Most commonly, a particular control loop |
| 113 | +(controller) uses one kind of resource as its desired state, and has a different |
| 114 | +kind of resource that it manages to make that desired state happen. |
| 115 | + |
| 116 | +It's useful to have simple controllers rather than one, monolithic set of control |
| 117 | +loops that are interlinked. Controllers can fail, so Kubernetes is designed to |
| 118 | +allow for that. |
| 119 | + |
| 120 | +For example: a controller for Jobs tracks Job objects (to discover |
| 121 | +new work) and Pod object (to run the Jobs, and then to see when the work is |
| 122 | +finished). In this case something else creates the Job objects, whereas the Job |
| 123 | +controller creates some Pod objects. |
| 124 | + |
| 125 | +{{< note >}} |
| 126 | +There can be several controllers that create or update the same kind of object. |
| 127 | +Behind the scenes, Kubernetes controllers make sure that they only pay attention |
| 128 | +to the resources linked to their controlling resource. |
| 129 | + |
| 130 | +For example, you can have Deployments and Jobs; these both create Pods. |
| 131 | +The Job controller does not delete the Pods that your Deployment created, |
| 132 | +because there is information ({{< glossary_tooltip term_id="label" text="labels" >}}) |
| 133 | +the controllers can use to tell those Pods apart. |
| 134 | +{{< /note >}} |
| 135 | + |
| 136 | +## Ways of running controllers {#running-controllers} |
| 137 | + |
| 138 | +Kubernetes comes with a set of built-in controllers that run inside |
| 139 | +the {{< glossary_tooltip term_id="kube-controller-manager" >}}. These |
| 140 | +built-in controllers provide important core behaviors. |
| 141 | + |
| 142 | +The Deployment controller and Job controller are examples of controllers that |
| 143 | +come as part of Kubernetes itself (“built-in” controllers). |
| 144 | +Kubernetes lets you run a resilient control plane, so that if any of the built-in |
| 145 | +controllers were to fail, another part of the control plane will take over the work. |
| 146 | + |
| 147 | +You can find controllers that run outside the control plane, to extend Kubernetes. |
| 148 | +Or, if you want, you can write a new controller yourself. |
| 149 | +You can run your own controller as a set of Pods, |
| 150 | +or externally to Kubernetes. What fits best will depend on what that particular |
| 151 | +controller does. |
| 152 | + |
| 153 | +{{% /capture %}} |
| 154 | + |
| 155 | +{{% capture whatsnext %}} |
| 156 | +* Read about the [Kubernetes control plane](https://kubernetes.io/docs/concepts/#kubernetes-control-plane) |
| 157 | +* Discover some of the basic [Kubernetes objects](https://kubernetes.io/docs/concepts/#kubernetes-objects) |
| 158 | +* Learn more about the [Kubernetes API](/docs/concepts/overview/kubernetes-api/) |
| 159 | +* If you want to write your own controller, see [Extension Patterns](/docs/concepts/extend-kubernetes/extend-cluster/#extension-patterns) in Extending Kubernetes. |
| 160 | +{{% /capture %}} |
0 commit comments