Kubernetes APIs Under the Hood
@pwittrock
Kubernetes APIs Under the Hood @pwittrock Who Am I? Phillip - - PowerPoint PPT Presentation
Kubernetes APIs Under the Hood @pwittrock Who Am I? Phillip Wittrock (@pwittrock) Software Engineer at Google working on GKE and OSS Kubernetes My mission is to make using Kubernetes simple and enjoyable You might have come across me
@pwittrock
Phillip Wittrock (@pwittrock) Software Engineer at Google working on GKE and OSS Kubernetes My mission is to make using Kubernetes simple and enjoyable You might have come across me through…
@pwittrock
ReplicaSets
updated with `kubectl apply`
== Resources @pwittrock
@pwittrock
Create or update the resource in the cluster by run kubectl apply on a file or directory
kubectl apply -f deploy.yaml
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment labels: {app: nginx} spec: replicas: 3 selector: matchLabels: {app: nginx} template: metadata: labels: {app: nginx} spec: containers: [ {name: nginx, image: 'nginx:1.7.9'}] Configuration for a Deployment that manages 3 Pods each running an nginx container
@pwittrock
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment labels: {app: nginx} spec: replicas: 3 selector: matchLabels: {app: nginx} template: metadata: labels: {app: nginx} spec: containers: [ {name: nginx, image: 'nginx:1.7.9'}] deploy.yaml Nodes apiserver kubectl apply -f deploy.yaml 3. ??? 2. HTTP POST Pod 1. Discover EndPoints Pod Pod @pwittrock
@pwittrock
Foo Resources stored in etcd by the apiserver
Foo objects Foo objects Foo objects Foo Resources
Resource: Stores Stuff Controller: Does Stuff
Loose coupling between Controller and API endpoints: Storage doesn’t know about Controllers API endpoints (CRUD storage) Asynchronous watch notification
Foo Controller @pwittrock
default/baz Watch events:
Reconcile Queue Reconcile Function default / baz
Reconcile on namespace/name
Batch events together into single Reconcile call
Tips:
finalizers Create Update Update @pwittrock
Controller apiserver
Controller
Objects (e.g. Deployment + ReplicaSets)
updates object status Watch Event Reconcile: Read Cluster State Reconcile: Update Cluster Objects
@pwittrock
apiserver Deployment Controller deploy. yaml ReplicaSet Controller
Node(s)
(Pod) Scheduler
apply create watch evt create ReplicaSet create Pod(s) update Pod(s) update Pod(s) @pwittrock
apiserver Deployment Controller deploy. yaml ReplicaSet Controller
Node(s)
(Pod) Scheduler
apply patch watch evt Scale up new ReplicaSet / Scale down old ReplicaSet create Pod(s) update Pod(s) update Pod(s) delete Pod(s)
@pwittrock
schema
tooling - e.g. kubectl apply
spec: containers:
image: gcr.io/some-project/udptest imagePullPolicy: Always name: client … dnsPolicy: ClusterFirst … apiVersion: v1 kind: Pod metadata: name: my-app namespace: default … spec status metadata status: podIP: 10.8.3.11 …
@pwittrock
○ Name of the API (e.g. Deployment)
○ Like a package in go, java, etc (e.g. apps)
○ Ensures backwards compatibility of: Defaulted Fields & Schema apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment labels: {app: nginx} spec: replicas: 3 selector: ... template: ... @pwittrock
that cannot be queried
queried (selected)
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment namespace: default labels: {app: nginx} spec: replicas: 3 selector: ... template: ... @pwittrock
○ Object Desired State (e.g. how many replicas to run, template for Pods, etc)
○ Defines the observed state for an
running)
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment labels: {app: nginx} spec: replicas: 3 selector: matchLabels: {...} template: metadata: labels: {...} spec: ... @pwittrock
Deployment name:nginx labels:run=nginx selector:run=nginx ReplicaSet name:nginx-65899c769f labels:run=nginx selector:run=nginx
Pod name:nginx-65899c769f-6slpx labels:run=nginx selector:run=nginx Pod name:nginx-65899c769f-fbgcv labels: run=nginx
Tip: Objects with owner references are automatically garbage collected when all of their owners have been deleted @pwittrock
defaulted by the apiserver before the
through OpenAPI
apiserver before the object is stored
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment labels: {app: nginx} spec: # server defaults this value # to 1 if unset replicas: 1 # make sure these match the # template labels selector: matchLabels: {...} template: metadata: labels: {...} @pwittrock
Foo Resource Foo Controller Foo Admission
Mutating Webhook + Service + Deployment (or Pod) CustomResourceDefinition (CRD) Deployment (or Pod)
Defaulting, Validation, Version Conversion Storage, Schema, Display, etc Actuation: level-triggered, asynchronous Tip: build your own APIs in go using kubebuilder @pwittrock
with new fields which must be retained across updates to the object
managing Resources using config
apiVersion: v1 kind: Service metadata: name: nginx labels: {app: nginx} spec: selector: app: nginx ports:
port: 80 # not set by owner! # don’t overwrite! clusterIp: 10.0.171.239 # not set by owner! # don’t overwrite! loadBalancerIp: 78.1124.19 type: LoadBalancer
@pwittrock
controllers through Status field
status (current) to other tools (part of Status field)
past events to users
Kind: Pod ... spec: readinessGates:
"www.example.com/feature-1" status: conditions:
status: "False" lastProbeTime: null lastTransitionTime: ...
status: "False" lastProbeTime: null lastTransitionTime: ... containerStatuses:
ready: true
@pwittrock
representations ○ Changing default values and field names / field types requires a new version
version -- the underlying object remains the same -- but the endpoints are different. v1beta1 v1 v1beta2 default/baz API Endpoints Foo Object @pwittrock
Spark, Airflow
Tekton, Knative
Autoscalers, Resource Tuners @pwittrock
@pwittrock
@pwittrock
Session page on conference website O’Reilly Events App
@pwittrock