kubectl

Commands change the cluster. Files describe it

IMPERATIVE: YOU GIVE EACH COMMANDkubectl create deploy web --image=nginxkubectl scale deploy web --replicas=3kubectl delete deploy webquick, but only the cluster rememberswhat you didDECLARATIVE: YOU HAND OVER THE FILE# web.yaml says: replicas: 3kubectl apply -f web.yaml# edit the file to 5, then again:kubectl apply -f web.yamlthe file is the record: review it, version itand apply it again anywherekubectl is a client of the API server, and "apply" is how you work declaratively.Every kubectl command becomes a request to the API server. Imperative commands are fine for lookingaround and for emergencies. For anything that must last, keep the manifests in files and apply them:Kubernetes compares the file with what exists and changes only the difference.

Imperative commands act once. kubectl apply makes the cluster match a file: declarative.

kubectl · Going deeper

The kubectl verbs

The kubectl verbs worth knowinggetList objects of a kind, brieflydescribeShow one object in full, with its recent eventslogsPrint what a container wroteexecRun a command inside a running containerapply -fCreate or update objects to match a filedeleteRemove objectsexplainShow the documentation for a kind and its fieldsconfig use-contextSwitch cluster, user and namespace, as set in the kubeconfig filerolloutWatch, pause or undo the update of a DeploymentA kubeconfig file holds clusters, users and contexts. A context is one cluster, one user, one namespace.

get, describe, logs, exec, apply, delete, explain and rollout.

Namespaces

A namespace divides the cluster, and a quota limits each part

ONE CLUSTERKUBE-SYSTEMcorednskube-proxythe cluster's own podsTEAM-AwebwebapiCPU quota: 6 of 8 usedTEAM-BwebdbCPU quota: 2 of 4 usedA namespace divides one cluster into named parts, and a quota limits what each part may use.Both teams have something called "web": a name only has to be unique inside its namespace. Accessrules, quotas and network policies are all set per namespace. A namespace does not isolate by itself:pods in different namespaces can still talk unless a NetworkPolicy forbids it.

A namespace scopes names, access and ResourceQuota. It does not isolate the network.

Namespaces · Going deeper

Built-in namespaces, quota and limit range

The four namespaces every cluster starts withdefaultWhere objects go when you name no namespacekube-systemThe control plane and add-ons: DNS, kube-proxy, the network pluginkube-publicReadable by everyone, even without signing in; rarely usedkube-node-leaseHeartbeat objects that show each node is aliveTwo objects that limit a namespaceResourceQuotaA ceiling for the whole namespace: total CPU, memory, number of podsLimitRangeDefaults and bounds for each single pod or container in itQuota is the budget for the namespace. LimitRange is the rule for each pod inside it.Nodes, PersistentVolumes, StorageClasses and ClusterRoles belong to the cluster, not to any namespace.

default, kube-system, kube-public and kube-node-lease.

Check yourself

NAMESPACE: TEAM-ADeployment: webNAMESPACE: TEAM-BDeployment: web

Two Deployments have the same name, as shown. Is this allowed?

Requests and limits

A request reserves room. A limit caps use

A node with 4 CPUspod A requests 1 CPUpod B requests 1.5 CPUpod C requests 1 CPU0.5 freepod D requests 1 CPU: it does not fit here, so it stays PendingA limit is different: it is the most a container may use once it is running.Over its memory limit, it is killed. Over its CPU limit, it is slowed down.A request reserves room on a node. A limit caps what the container may take.The scheduler adds up requests, not real usage. Here 3.5 of 4 CPUs are reserved, so a pod asking for1 cannot be placed, even if the node is idle. A pod with no request reserves nothing, and is thefirst to be evicted when the node runs short.

The scheduler counts requests. With 3.5 of 4 CPUs reserved, a pod asking for 1 stays Pending.

Requests and limits · Going deeper

Quality of service classes

Three quality of service classesGiven whenUnder memory pressureGuaranteedevery container has requests equal to limitsevicted lastBurstablesome request or limit is set, but they differevicted in betweenBestEffortno requests and no limits at allevicted firstYou do not set the class. Kubernetes derives it from the requests and limits you wrote.CPU is measured in cores or millicores: 500m is half a core. Memory is in bytes: 256Mi, 1Gi. A containerkilled for exceeding its memory limit shows the reason OOMKilled.

Guaranteed, Burstable and BestEffort, derived from requests and limits.

The scheduler

Filter, score, bind

a new podrequests 2 CPUsnode 1: 1 CPU freenode 2: taintednode 3: 3 CPUs freenode 4: 6 CPUs free1 Filter✗ too small✗ taint not tolerated✓ fits✓ fits2 Score40853 Bindthe pod goes hereThe scheduler filters out nodes that cannot run the pod, scores the rest, and binds to the best.Filtering is pass or fail: enough unreserved CPU and memory, taints tolerated, selectors matched.Scoring ranks what is left, by default preferring to spread load. Binding writes the chosen node intothe pod, and that node's kubelet takes over. If no node passes the filter, the pod stays Pending.

The scheduler filters out nodes that cannot run the pod, scores the rest and binds to the best.

The scheduler · Going deeper

Six placement controls

Six ways to influence where a pod landsnodeSelectorOnly nodes carrying this label. The simplest controlNode affinityThe same idea with richer rules, either required or merely preferredPod affinity and anti-affinityNear pods with a label, or away from themTaints and tolerationsA node repels pods; a toleration lets a pod throughTopology spread constraintsSpread copies evenly across zones or nodesPriority and preemptionAn important pod may evict less important ones to make roomAffinity attracts, a taint repels, spread constraints even out, and priority decides who waits."Required" rules filter nodes out. "Preferred" rules only change the score.

Affinity attracts, a taint repels, spread evens out, priority decides who waits.

Check yourself

pod Ano tolerationspod Btolerates team=mlnodetaint:team=ml:NoSchedule

Which pod can the scheduler place on this node?

Placement

A taint pushes pods away. Affinity pulls them in

TAINT: THE NODE REFUSESpod Ano tolerationpod Btolerates gpuGPU nodetaint:gpu=true:NoSchedule✗✓a taint keeps pods off unless they tolerate itAFFINITY: THE POD CHOOSESpod C requiresdisk=ssdnode Xdisk=ssdnode Ydisk=hdd✓✗affinity sends a pod to nodes with a labelA taint is set on a node and pushes pods away. Affinity is set on a pod and pulls it to nodes.They answer opposite questions. A toleration only permits: pod B may land on the GPU node, butnothing sends it there. To reserve nodes for one team you need both: a taint to keep others out,and affinity to bring that team's pods in.

A taint is on the node and repels. Affinity is on the pod and attracts.

Placement · Going deeper · 1 of 2

Maintenance, and extending the API

Looking after nodeskubectl cordonMark a node unschedulable. Pods already on it keep runningkubectl drainCordon it, then evict its pods so they restart elsewherekubectl uncordonLet the scheduler use the node againNoExecute taintEvicts pods already running that do not tolerate itExtending and versioning the APICustomResourceDefinitionAdds a new kind of object to the API, such as "Database"OperatorA custom controller that runs an application using such objectsalpha, beta, stablev1alpha1 may change or vanish; v1beta1 is well tested; v1 is stableCordon stops new pods arriving. Drain also moves the existing ones. Maintenance uses both, then uncordon.

Cordon stops new pods. Drain also moves the old ones. CRDs add new kinds.

Placement · Going deeper · 2 of 2

Clue words for administration and scheduling

When the question saysThe answer isBecause it is"keep manifests in Git and apply"Declarativedesired state in files"isolate teams in one cluster"Namespacenamed division"cap total CPU for a team"ResourceQuotabudget for a namespace"default limits for each container"LimitRangerule per pod"reserved for scheduling"Requestthe scheduler counts it"maximum a container may use"Limitenforced at run time"OOMKilled"Memory limit exceededthe container is killed"evicted first"BestEffortno requests or limits"dedicated nodes", "repel"Taints and tolerationsthe node refuses"run only on nodes labelled ..."nodeSelector or node affinitythe pod chooses"replicas in different zones"Topology spread, anti-affinityspread out"prepare a node for maintenance"kubectl drainevict, then repairAdministration and scheduling are about a fifth of the exam. These twelve are the core of it.

Requests schedule. Limits enforce. Quota budgets.

Check yourself

NODE 2 · READY, SCHEDULINGDISABLEDweb: Runningapi: Runningits pods were not movedno new podsare sent here

The node looks as shown. Which command was run on it?

Lesson complete

You finished “Administration and scheduling”

Answer the questions along the way to see your score here.

Open in a new tab