Concepts & Overview
Kubernetes Objects
We use Kubernetes API objectsto describe our cluster'sdesired state: what applications or other workloads you want to run, what container images they use, the number of replicas, what network and disk resources you want to make available, and more.
The basic Kubernetes objects include:
In addition, Kubernetes contains a number of higher-level abstractions called Controllers. Controllers build upon the basic objects, and provide additional functionality and convenience features. They include:
Kubernetes Objectsare persistent entities in the Kubernetes system. Kubernetes uses these entities to represent the state of your cluster. Specifically, they can describe:
- What containerized applications are running (and on which nodes)
- The resources available to those applications
- The policies around how those applications behave, such as restart policies, upgrades, and fault-tolerance
A Kubernetes object is a "record of intent"--once you create the object, the Kubernetes system will constantly work to ensure that object exists. By creating an object, you're effectively telling the Kubernetes system what you want your cluster's workload to look like; this is your cluster'sdesired state.
Labels and Selectors
-
Key/value pairs associated with Kubernetes objects
-
Used to organize and select subsets of objects
-
Attached to objects at creation time but modified at any time
-
Labels are the essential glue to associate one API object with other
- Replication Controller -> Pods
Replication Controller means that I tell kubernetes that I want this container and I want 4 copies of it.
- Service -> Pods
- Pods -> Nodes
-
First metadata in deployment along with labels describes the deployment itself. It gives a labelfor that actual deployment
-
the second selector, is actually a selector for the deployment to apply to the pod that the deployment is describing. Therefore pod labels and selector must match each other
-
Thetemplateis actually apodTemplate. It describes a pod that is launched.
Object Spec and Status
Every Kubernetes object includes two nested object fields that govern the object's configuration: the object spec and the object status. Thespec, which you must provide, describes your desired state for the object--the characteristics that you want the object to have. The status describes the actual state of the object, and is supplied and updated by the Kubernetes system. At any given time, the Kubernetes Control Plane actively manages an object's actual state to match the desired state you supplied.
Names - All objects in the Kubernetes REST API are unambiguously identified by a Name and a UID
Namespaces
Kubernetes supports multiple virtual clusters backed by the same physical cluster. These virtual clusters are called namespaces
Hierarchical Namespaces
Kubernetes has introduced the feature of the hierarchical namespace.!!!
Till now we have the namespaces for logical grouping of components in Kubernetes but it was not flexible enough to meet common use cases.
That's why they have introduced the hierarchical namespaces which will work on policy inheritance and delegated access control which means now you don't need cluster admin permission to create namespaces. The namespace owner can create the hierarchical namespace with his/her own access.
This concept of ownership enables two additional types of behaviours:
- Policy inheritance: if one namespace is a child of another, policy objects such as RBAC RoleBindings are copied from the parent to the child.
- Delegated creation: you usually need cluster-level privileges to create a namespace, but hierarchical namespaces adds an alternative: subnamespaces, which can be manipulated using only limited permissions in the parent namespace.
https://kubernetes.io/blog/2020/08/14/introducing-hierarchical-namespaces
Labels - Labelsare key/value pairs that are attached to objects, such as pods. Labels are intended to be used to specify identifying attributes of objects that are meaningful and relevant to users, but do not directly imply semantics to the core system. Labels can be used to organize and to select subsets of objects. Labels can be attached to objects at creation time and subsequently added and modified at any time. Each object can have a set of key/value labels defined. Each Key must be unique for a given object
Label Selectors - Via a label selector, the client/user can identify a set of objects.The API currently supports two types of selectors: equality-based and set-based
Equality-based requirement
Equality or inequality based requirements allow filtering by label keys and values. Matching objects must satisfy all of the specified label constraints, though they may have additional labels as well. Three kinds of operators are admitted =
,==
,!=
. The first two represent equality (and are simply synonyms), while the latter represents inequality.
For example:
environment = production
tier != frontend
Set-based requirement
Set-based label requirements allow filtering keys according to a set of values. Three kinds of operators are supported: in, notin and exists(only the key identifier).
For example:
environment in (production, qa)
tier notin (frontend, backend)
partition
!partition
Annotations
You can use Kubernetes annotations to attach arbitrary non-identifying metadata to objects. Clients such as tools and libraries can retrieve this metadata.
kubectl annotate po nginx1 nginx2 nginx3 description='my description'
https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations
Field Selectors
Field selectors let you select Kubernetes resources based on the value of one or more resource fields.
kubectl get pods --field-selector status.phase=Running
Kubernetes Object Management
The kubectl command-line tool supports several different ways to create and manage Kubernetes objects.
Management Techniques
Management technique | Operates on | Recommended environment | Supported writers | Learning curve |
---|---|---|---|---|
Imperative commands | Live objects | Development projects | 1+ | Lowest |
Imperative object configuration | Individual files | Production projects | 1 | Moderate |
Declarative object configuration | Directories of files | Production projects | 1+ | Highest |