Kubernetes Controllers - The Kubernetes Workshop
Kubernetes Controllers - The Kubernetes Workshop
7. Kubernetes Controllers
Overview
Introduction
In previous chapters, we created different Pods,
managed their life cycle manually, and added
metadata (labels or annotations) to them to help
organize and identify various Pods. In this
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 1/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 2/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
ReplicaSets
As discussed earlier, having multiple replicas of
our application ensures that it is still available
even if a few replicas fail. This also makes it easy
for us to scale our application to balance the load
to serve more traffic. For example, if we are
building a web application that's exposed to
users, we'd want to have at least two replicas of
the application in case one of them fails or dies
unexpectedly. We would also want the failed
replica to recover on its own. In addition to that,
if our traffic starts growing, we would want to
increase the number of Pods (replicas) running
our application. A ReplicaSet is a Kubernetes
controller that keeps a certain number of Pods
running at any given time.
ReplicaSet Configuration
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx-replicaset
labels:
app: nginx
spec:
replicas: 2
selector:
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 4/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
matchLabels:
environment: production
template:
metadata:
labels:
environment: production
spec:
containers:
- name: nginx-container
image: nginx
As with Pod configuration, a ReplicaSet also
needs fields such as apiVersion, kind, and
metadata. For a ReplicaSet, the API version,
apps/v1, is the current version and the kind
field will always be ReplicaSet. One field that
is different from what we have seen in Pod
configuration so far is the spec.
Replicas
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 5/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
replicas: 2
The ReplicaSet will create or delete Pods in order
to match this number. The default value for this
field, if not specified, is 1.
Pod Template
template:
metadata:
labels:
environment: production
spec:
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 6/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
containers:
- name: nginx-container
image: nginx
Pod Selector
selector:
matchLabels:
environment: production
The preceding example ensures that our
controller will only manage Pods with an
environment: production label.
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 7/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 8/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 9/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 10/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 11/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 12/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
Labels: app=nginx
It shows that, as desired, the ReplicaSet was
created with a label key called app with a value
of nginx.
Selector:
environment=production
This shows that the ReplicaSet is configured with
an environment=production Pod selector. This
means that this ReplicaSet will try to acquire
Pods that have this label.
Replicas
Replicas: 2 current / 2
desired
We can see that the ReplicaSet has the desired
count of 2 for the Pods, and it also shows that
there are currently two replicas present.
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 13/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
Pods Status
Pods Template
Events
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 14/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
Note
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 15/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
nginx-replicaset-9tgb9 1/1
Running 0 103s
nginx-replicaset-zdjb5 1/1
Running 0 103s
2. Delete the first Pod to replicate Pod failure
during runtime by using the
following command:
kubectl delete pod <pod_name>
You should see a response similar to the
following:
pod "nginx-replicaset-9tgb9"
deleted
3. Describe the ReplicaSet and check the events:
kubectl describe rs nginx-
replicaset
You should see output similar to the following:
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 16/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 17/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
replicaset.apps "nginx-
replicaset" deleted
As shown in the preceding output, the nginx-
replicaset ReplicaSet was deleted.
5. Run the following command to verify that the
Pods managed by the ReplicaSet were also
deleted:
kubectl get pods
You should get the following response:
No resources found in default
namespace
As you can see from this output, we can verify
that the Pods were deleted.
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 18/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 19/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 20/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 21/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 22/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 23/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 24/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 25/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 26/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 27/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 28/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
Deployment
A Deployment is a Kubernetes object that acts as
a wrapper around a ReplicaSet and makes it
easier to use. In general, in order to manage
replicated services, it's recommended that you
use Deployments that, in turn, manage the
ReplicaSet and the Pods created by the
ReplicaSet.
Deployment Configuration
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 30/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
app: nginx
spec:
replicas: 3
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
maxSurge: 1
selector:
matchLabels:
app: nginx
environment: production
template:
metadata:
labels:
app: nginx
environment: production
spec:
containers:
- name: nginx-container
image: nginx
The value for the kind field is Deployment. The
rest of the configuration remains the same as
that for ReplicaSets. Deployments also have the
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 31/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
Strategy
RollingUpdate
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 32/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
maxSurge: 1
maxUnavailable is the maximum number of
Pods that can be unavailable during the update.
This field can be specified as either an integer
representing the maximum number of
unavailable Pods or a string representing the
percentage of total replicas that can be
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 33/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 34/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
Recreate
strategy:
type: Recreate
A good use case for using the Recreate update
strategy is if we need to run some data migration
or data processing before the new code can be
used. In this case, we will need to use the
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 35/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 36/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
environment: production
template:
metadata:
labels:
app: nginx
environment: production
spec:
containers:
- name: nginx-container
image: nginx
In this configuration, we can see that the
Deployment will have three replicas of Pods
running with the app: nginx and
environment: production labels.
2. Run the following command to create the
Deployment defined in the previous step:
kubectl apply -f nginx-
deployment.yaml
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 37/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 39/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 40/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
Labels: app=nginx
This indicates that the Deployment was created
with an app=nginx label. Now, let's consider the
next field in the output:
Annotations: deployment.kuberne
tes.io/revision: 1
kubectl.kubernetes
.io/last-applied-configuration:
{"apiVersion":"apps/v1","kind":"De
ployment","metadata":
{"annotations":{},"labels":
{"app":"nginx"},"name":"nginx-
deployment","namespace":"d...
There are two annotations added to the
Deployment automatically.
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 41/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
Selector: app=nginx,environment
=production
This shows which Pod selectors the Deployment
is configured with. So, this Deployment will try
to acquire the Pods that have both of these
labels.
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 42/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
Replicas
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 43/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 44/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
name: app-deployment
labels:
environment: production
spec:
replicas: 3
selector:
matchLabels:
app: nginx
environment: production
template:
metadata:
labels:
app: nginx
environment: production
spec:
containers:
- name: nginx-container
image: nginx
2. Run the following command to create the
Deployment:
kubectl apply -f app-
deployment.yaml
You should see the following response:
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 45/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
deployment.apps/app-deployment
created
3. Run the following command to check the
rollout history of the newly
created Deployment:
kubectl rollout history
deployment app-deployment
You should see the following response:
deployment.apps/app-deployment
REVISION CHANGE-CAUSE
1 <none>
This output shows that the Deployment has no
rollout history as of now.
4. For the first update, let's change the name of
the container to nginx instead of nginx-
container. Update the content of the app-
deployment.yaml file with the following:
apiVersion: apps/v1
kind: Deployment
metadata:
name: app-deployment
labels:
environment: production
spec:
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 46/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
replicas: 3
selector:
matchLabels:
app: nginx
environment: production
template:
metadata:
labels:
app: nginx
environment: production
spec:
containers:
- name: nginx
image: nginx
As you can see, the only thing that has changed
in this template is the container name.
5. Apply the changed configuration using the
kubectl apply command with the --record
flag. The --record flag ensures that the
update to the Deployment is recorded in the
rollout history of the Deployment:
kubectl apply -f app-
deployment.yaml --record
You should see the following response:
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 47/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
deployment.apps/app-deployment
configured
Note that the rollout history maintained by the
--record flag is different from the past
configs stored in the annotations, which we
saw in the Labels and Annotations on the
Deployment subsection.
6. Wait for a few seconds to allow the
Deployment to recreate the Pods with the
updated Pod configuration, and then run the
following command to check the rollout
history of the Deployment:
kubectl rollout history
deployment app-deployment
You should see the following response:
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 48/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 49/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 50/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 51/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
StatefulSets
StatefulSets are used to manage stateful replicas.
Similar to a Deployment, a StatefulSet creates
and manages the specified number of Pod
replicas from an identical Pod template.
However, where StatefulSets differ from
Deployments is that they maintain a unique
identity for each of their Pods. So, even if all the
Pods are of identical specs, they are not
interchangeable. Each of the Pods has a sticky
identity that can be used by the application code
to manage the state of the application on a
particular Pod. For a StatefulSet with n replicas,
each Pod is assigned a unique integer ordinal
between 0 and n – 1. The names of the Pods
reflect the integer identity assigned to them.
When a StatefulSet is created, all the Pods are
created in the order of their integer ordinal.
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 53/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
StatefulSet Configuration
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: example-statefulset
spec:
replicas: 3
selector:
matchLabels:
environment: production
template:
metadata:
labels:
environment: production
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 54/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
spec:
containers:
- name: name-container
image: image_name
As we can see in the preceding configuration,
apiVersion for a StatefulSet is apps/v1 and
kind is StatefulSet. The rest of the fields are
used in the same way as for ReplicaSets.
Note
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 55/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
DaemonSets
DaemonSets are used to manage the creation of a
particular Pod on all or a selected set of nodes in
a cluster. If we configure a DaemonSet to create
Pods on all nodes, then if new nodes are added to
the cluster, new pods will be created to run on
these new nodes. Similarly, if some nodes are
removed from the cluster, the Pods running on
these nodes will be destroyed.
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 56/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
DaemonSet Configuration
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: daemonset-example
labels:
app: daemonset-example
spec:
selector:
matchLabels:
app: daemonset-example
template:
metadata:
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 57/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
labels:
app: daemonset-example
spec:
containers:
- name: busybox-container
image: busybox
args:
- /bin/sh
- -c
- sleep 10000
As we can see in the preceding configuration,
apiVersion for a DaemonSet is set to apps/v1
and kind is set to DaemonSet. The rest of the
fields are used in the same way as for
ReplicaSets.
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 58/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
Jobs
A Job is a supervisor in Kubernetes that can be
used to manage Pods that are supposed to run a
determined task and then terminate gracefully. A
Job creates the specified number of Pods and
ensures that they successfully complete their
workloads or tasks. When a Job is created, it
creates and tracks the Pods that were specified
in its configuration. When a specified number of
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 59/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
Job Configuration
apiVersion: batch/v1
kind: Job
metadata:
name: one-time-job
spec:
template:
spec:
containers:
- name: busybox-container
image: busybox
args:
- /bin/sh
- -c
- date
restartPolicy: OnFailure
The apiVersion field for a Job object is set to
batch/v1. The batch API group contains objects
relating to batch processing jobs. The kind field
is set to Job.
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 61/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 63/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
job.batch/one-time-job created
3. Run the following command to check the
status of the Job:
kubectl get jobs
You should see a response similar to this:
NAME COMPLETIONS DURATION AGE
one-time-job 0/1 3s 3s
We can see that the Job requires one
completion and is not yet completed.
4. Run the following command to check the Pod
running the Job:
kubectl get pods
Note that you should run this before the Job is
complete to see the response shown here:
NAME READY STATUS RESTARTS AGE
one-time-job-bzz8l 1/1 Running 0
7s
We can see that the Job has created a Pod
named one-time-job-bzz8l to run the task
specified in the Job template.
5. Next, run the following command to check the
logs for the Pod created by the Job:
kubectl logs -f <pod_name>
You should see logs similar to the following:
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 64/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 65/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 66/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 67/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 68/70
10/16/22, 8:59 AM 7. Kubernetes Controllers | The Kubernetes Workshop
Note
Summary
Kubernetes treats Pods as ephemeral entities,
and ideally you would not deploy any
application or a microservice in an individual
Pod. Kubernetes offers various controllers to
leverage various benefits, including automatic
replication, health monitoring, and automatic
scaling.
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_07_Final_SMP_ePub.xhtml 70/70