Understanding Kubernetes Controllers: Managing State and Reacting to Changes

 
Extending Kubernetes
with Custom Resources
 
$> whoami
 
Co-founder & CTO @ LeanNet Ltd.
Consulting, training, implementing
Cloud Native, Kubernetes, Microservices, DevOps
 
PhD in Telecommunications @ Budapest University of Technology
Worked with 5G technology
SDN & NFV 
 Cloud Native Network Functions
Graduated in the EIT Digital Doctoral School
 
megyesi@leannet.eu
 
twitter.com/M3gy0
 
linkedin.com/in/M3gy0
 
Kubernetes Architecture
API server
etcd
Controller
Manager
Cloud
Controller
Scheduler
kube-proxy
kubelet
cont.runtime
kube-proxy
kubelet
cont.runtime
kube-proxy
kubelet
cont.runtime
Cloud
Provider
 
Control Plane
 
Worker Nodes
Control Loops
 
Temp
 
Time
 
22.5
Control Loops in Kubernetes
API server
etcd
Controller
Manager
Scheduler
kube-proxy
kubelet
cont.runtime
Control Plane
Worker Node 1
kubectl create –f job.yaml
1
2
Job
Controller
3
 
c
r
e
a
t
e
 
a
 
j
o
b
 
v
i
a
 
a
n
 
A
P
I
 
c
a
l
l
 
p
e
r
s
i
s
t
 
s
t
a
t
e
 
t
o
 
e
t
c
d
 
t
h
e
 
j
o
b
 
c
o
n
t
r
o
l
l
e
r
 
s
e
e
s
 
t
h
e
 
n
e
w
l
y
 
c
r
a
t
e
d
 
j
o
b
a
n
d
 
c
r
e
a
t
e
s
 
a
 
p
o
d
 
b
a
s
e
d
 
o
n
 
t
h
e
 
t
e
m
p
l
a
t
e
4
 
t
h
e
 
s
c
h
e
d
u
l
e
r
 
s
e
e
s
 
t
h
a
t
 
t
h
e
r
e
 
i
s
 
a
 
p
o
d
 
w
i
t
h
o
u
t
a
n
 
a
s
s
i
g
n
e
d
 
n
o
d
e
,
 
s
o
 
i
t
 
d
o
e
s
 
t
h
e
 
a
s
s
i
g
n
m
e
n
t
 
t
h
e
 
k
u
b
e
l
e
t
 
o
n
 
t
h
e
 
n
o
d
e
 
s
e
e
s
 
t
h
a
t
 
t
h
e
r
e
 
i
s
 
a
p
o
d
 
a
s
s
i
g
n
e
d
 
t
o
 
i
t
,
 
a
n
d
 
s
t
a
r
t
s
 
i
t
 
v
i
a
 
t
h
e
 
C
R
I
5
 
a
f
t
e
r
 
t
h
e
 
c
o
n
t
a
i
n
e
r
 
e
x
i
t
s
,
 
k
u
b
e
l
e
t
 
r
e
p
o
r
t
s
 
b
a
c
k
t
h
e
 
e
x
i
t
 
c
o
d
e
 
t
o
 
t
h
e
 
p
o
d
s
 
s
t
a
t
e
6
 
t
h
e
 
j
o
b
 
c
o
n
t
r
o
l
l
e
r
 
n
o
t
i
c
e
s
 
t
h
e
 
c
h
a
n
g
e
 
i
n
 
t
h
e
p
o
d
s
 
s
t
a
t
e
 
a
n
d
 
m
a
k
e
s
 
a
 
d
e
c
i
s
i
o
n
e
x
i
t
 
c
o
d
e
 
=
 
0
 
 
m
a
r
k
s
 
t
h
e
 
j
o
b
 
a
s
 
c
o
m
p
l
e
t
e
e
x
i
t
 
c
o
d
e
 
 
0
 
a
n
d
 
r
e
a
c
h
e
d
 
t
h
e
 
b
a
c
k
o
f
f
 
l
i
m
i
t
:
 
m
a
r
k
s
 
t
h
e
 
j
o
b
 
a
s
 
f
a
i
l
e
d
e
l
s
e
 
 
i
n
c
r
e
a
s
e
s
 
t
h
e
 
c
o
u
n
t
 
a
n
d
s
t
a
r
t
s
 
a
 
n
e
w
 
p
o
d
 
(
b
a
c
k
 
t
o
 
s
t
e
p
 
4
)
7
1
2
3
4
5
6
7
Kubernetes Controllers
 
Watch the changes 
in the API server and compares the 
desired state 
with the 
current state
If these states 
differ
 they 
carry out actions 
to bring the current state closer to the desired state
 
The 
spec
 field describes
the desired state
 
The 
status 
field describes
the current state
 
Kubernetes Controllers
 
Watch the changes 
in the API server and compares the 
desired state 
with the 
current state
If these states 
differ
 they 
carry out actions 
to bring the current state closer to the desired state
 
The 
spec
 field describes
the desired state
 
The 
status 
field describes
the current state
Kubernetes Controllers
Watch the changes 
in the API server and compares the 
desired state 
with the 
current state
If these states 
differ
 they 
carry out actions 
to bring the current state closer to the desired state
 
working with desired states is also referred as
declarative infrastructure
 
acting on the deference between the desired state and the current state
is also referred as
reconcile pattern
 
foundation of every 
cloud native infrastructure
Custom Resources
Custom resources
 are extensions of the Kubernetes API
Built-in object
: e.g. pod, job, deployment, service, ingress, persistent volume
Custom resources 
simply
 let you 
store
 and 
retrieve
 
structured data
 
kubectl create –f Megyo.yaml
kubectl get developers
 
kubectl get developer Megyo
kubectl get devs
Custom Resources + Custom Controllers = Operators
 
You can write your own controller logic
Works the same way as built in controllers:
watches resources in the Kubernetes API
carry out action if there are changes in either the 
spec
 (desired state) or the 
status
 (current state)
watch build-in resources and config your own software
e.g. Ingress controllers (e.g. Nginx, Traefik, Envoy), KubeDNS/CoreDNS, Prometheus SD
change the behavior of your software based on a CRD 
 use the Kubernetes API as your API
e.g. Calico, OpenShift, Istio
interact with external systems / APIs
e.g. kubelet, kube-proxy, 
DynamoDB
 operator
create other Kubernetes resources (e.g. pods, deployments, statefulsets, services, ingresses)
e.g. MySQL operator, GitLab operator
 
A custom controller working over a custom resource is usually referred as an 
Operator
Example: Zalando Postgres Operator
Postgres Operator
Secrets
Users
 
kubectl
 create
Robots
Standby
StatefulSet
Services
Primary
Replica
Endpoints
Primary
Replicas
Pod Template
Pods
Primary
Replica
Replica
Persistent Volume Claims
Primary
Replica
Replica
Pods
Primary
Replica
Example: Zalando Postgres Operator
Postgres Operator
Secrets
Users
Robots
Standby
StatefulSet
Services
Primary
Replica
Endpoints
Primary
Replicas
Pod Template
Replica
Replica
Persistent Volume Claims
Primary
Replica
Replica
Primary
kubectl
 create
 
Where to Find Operators
Write Your Own Operator
 
K
OPF
Kubernetes Operator Pythonic Framework
https://github.com/zalando-incubator/kopf
 
java-operator-sdk
https://github.com/ContainerSolutions/java-operator-sdk
Remember: Custom Resources + Custom Controllers = Operators
 
Custom resources
 are extensions of the Kubernetes API
Built-in object 
with
 controllers
: e.g. pod, job, deployment, service, ingress, persistent volume
Custom resources 
simply
 let you 
store
 and 
retrieve
 
structured data
Schema is defined with 
OpenAPI
 spec
But you can write 
custom controllers 
over 
custom resources
: 
operator pattern
 
Example operators:
 
MySQL
 
PostgreSQL
 
Kafka
 
Virtual Machines
 
Machine Learning
 
CI/CD
 
Serverless
 
Cassandra
Slide Note
Embed
Share

Kubernetes controllers play a crucial role in managing the state of resources within a Kubernetes cluster by watching for changes in the API server and ensuring the desired state matches the current state. They take actions to bring the system into alignment, helping to maintain stability and efficiency. This involves creating, updating, deleting resources as needed based on the defined specifications. Controllers ensure that the cluster operates as intended, reacting to changes proactively.


Uploaded on Sep 21, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Extending Kubernetes with Custom Resources https://leannet.eu

  2. $> whoami Co-founder & CTO @ LeanNet Ltd. Consulting, training, implementing Cloud Native, Kubernetes, Microservices, DevOps PhD in Telecommunications @ Budapest University of Technology Worked with 5G technology SDN & NFV Cloud Native Network Functions Graduated in the EIT Digital Doctoral School megyesi@leannet.eu twitter.com/M3gy0 linkedin.com/in/M3gy0 https://leannet.eu

  3. Kubernetes Architecture Cloud Provider Controller Manager Cloud Controller API server kube-proxy kube-proxy kube-proxy kubelet kubelet kubelet Scheduler cont.runtime cont.runtime cont.runtime etcd Control Plane Worker Nodes https://leannet.eu

  4. Control Loops Temp 22.5 Time https://leannet.eu

  5. Control Loops in Kubernetes Job create a job via an API call 1 Controller Controller Manager persist state to etcd 2 the job controller sees the newly crated job and creates a pod based on the template 1 3 3 7 the scheduler sees that there is a pod without an assigned node, so it does the assignment the kubelet on the node sees that there is a pod assigned to it, and starts it via the CRI after the container exits, kubelet reports back the exit code to the pod s state 4 API server 5 kube-proxy 2 4 6 kubelet 6 5 Scheduler the job controller notices the change in the pod s state and makes a decision exit code = 0 marks the job as complete exit code 0 and reached the backoff limit: marks the job as failed else increases the count and starts a new pod (back to step 4) 7 cont.runtime etcd Control Plane Worker Node 1 https://leannet.eu

  6. Kubernetes Controllers Watch the changes in the API server and compares the desired state with the current state If these states differ they carry out actions to bring the current state closer to the desired state The spec field describes the desired state The status field describes the current state https://leannet.eu

  7. Kubernetes Controllers Watch the changes in the API server and compares the desired state with the current state If these states differ they carry out actions to bring the current state closer to the desired state The spec field describes the desired state The status field describes the current state https://leannet.eu

  8. Kubernetes Controllers Watch the changes in the API server and compares the desired state with the current state If these states differ they carry out actions to bring the current state closer to the desired state working with desired states is also referred as declarative infrastructure acting on the deference between the desired state and the current state is also referred as reconcile pattern foundation of every cloud native infrastructure https://leannet.eu

  9. Custom Resources Custom resources are extensions of the Kubernetes API Built-in object: e.g. pod, job, deployment, service, ingress, persistent volume Custom resources simply let you store and retrieve structured data kubectl create f Megyo.yaml kubectl get developers kubectl get developer Megyo kubectl get devs https://leannet.eu

  10. Custom Resources + Custom Controllers = Operators You can write your own controller logic Works the same way as built in controllers: watches resources in the Kubernetes API carry out action if there are changes in either the spec (desired state) or the status (current state) watch build-in resources and config your own software e.g. Ingress controllers (e.g. Nginx, Traefik, Envoy), KubeDNS/CoreDNS, Prometheus SD change the behavior of your software based on a CRD use the Kubernetes API as your API e.g. Calico, OpenShift, Istio interact with external systems / APIs e.g. kubelet, kube-proxy, DynamoDB operator create other Kubernetes resources (e.g. pods, deployments, statefulsets, services, ingresses) e.g. MySQL operator, GitLab operator A custom controller working over a custom resource is usually referred as an Operator https://leannet.eu

  11. Example: Zalando Postgres Operator Postgres Operator Secrets kubectl create Kubernetes API Users Robots Standby StatefulSet Services Primary Replica Pod Template Pods Endpoints Primary Replica Replica Primary Replicas Persistent Volume Claims Primary Replica Replica https://leannet.eu

  12. Example: Zalando Postgres Operator Postgres Operator Secrets kubectl create Kubernetes API Users Robots Standby StatefulSet Services Primary Replica Pod Template Pods Endpoints Replica Primary Primary Replica Replica Primary Replicas Persistent Volume Claims Primary Replica Replica https://leannet.eu

  13. Where to Find Operators https://leannet.eu

  14. Write Your Own Operator KOPF Kubernetes Operator Pythonic Framework https://github.com/zalando-incubator/kopf java-operator-sdk https://github.com/ContainerSolutions/java-operator-sdk https://leannet.eu

  15. Remember: Custom Resources + Custom Controllers = Operators Custom resources are extensions of the Kubernetes API Built-in object with controllers: e.g. pod, job, deployment, service, ingress, persistent volume Custom resources simply let you store and retrieve structured data Schema is defined with OpenAPI spec But you can write custom controllers over custom resources: operator pattern Example operators: Serverless CI/CD Machine Learning Virtual Machines MySQL PostgreSQL Kafka Cassandra https://leannet.eu

Related


More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#