{"id":782,"date":"2024-03-08T01:09:00","date_gmt":"2024-03-08T01:09:00","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=782"},"modified":"2024-03-08T01:09:00","modified_gmt":"2024-03-08T01:09:00","slug":"kubernetes-objects-resources","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=782","title":{"rendered":"Kubernetes Objects Vs Resources Vs Custom Resource"},"content":{"rendered":"<p>In this blog you will learn about Kubernetes objects, resources, custom resources and their differences in detail.<\/p>\n<p>While learning Kubernetes, you will quite often come across the names &#8220;<strong>Kubernetes Object <\/strong>, &#8220;<strong>Kubernetes resources<\/strong>&#8221; and &#8220;<strong>Custom Resources<\/strong>&#8220;. These are fundamental concepts in Kubernetes that are crucial to understand<\/p>\n<p>Let&#8217;s understand what they really mean.<\/p>\n<h2 id=\"what-is-kubernetes-object\">What is Kubernetes Object?<\/h2>\n<p>In Kubernetes, anything a user creates and retains is referred to as an &#8220;<strong>Object<\/strong>.&#8221; These can include various items such as namespaces, <a href=\"https:\/\/devopscube.com\/kubernetes-pod\/\" rel=\"noreferrer noopener\">pods<\/a>, <a href=\"https:\/\/devopscube.com\/kubernetes-deployment-tutorial\/\" rel=\"noreferrer noopener\">deployments<\/a>, <a href=\"https:\/\/devopscube.com\/kubernetes-daemonset\/\" rel=\"noreferrer noopener\">DaemonSet<\/a>, volumes, or secrets.<\/p>\n<p>If you look at the <a href=\"https:\/\/devopscube.com\/kubernetes-architecture-explained\/\">Kubernetes architecture<\/a>, the etcd component plays an important role in persisting cluster objects amongst other things. Any object you create, gets stored in etcd.<\/p>\n<p><a href=\"https:\/\/devopscube.com\/backup-etcd-restore-kubernetes\/\">etcd<\/a> stores all objects under the key named <strong><code>\/registry<\/code><\/strong> directory. For instance, details regarding a pod called Nginx in the default namespace can be retrieved by searching for <strong><code>\/registry\/pods\/default\/nginx<\/code><\/strong>. The following image shows the registry paths where different objects are stored.<\/p>\n<figure class=\"kg-card kg-image-card kg-card-hascaption\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-21-21.png\" class=\"kg-image\" alt=\"Kubernetes objects stored in ecd.\" loading=\"lazy\" width=\"760\" height=\"704\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-21-21.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-21-21.png 760w\" sizes=\"auto, (min-width: 720px) 720px\"><figcaption><span style=\"white-space: pre-wrap;\">Click to view in HD<\/span><\/figcaption><\/figure>\n<p>To create these objects, you describe what you want (desired state) in a file using either YAML or JSON. It is called a Object Specification.<\/p>\n<p>Here is an example of a Pod Object Specification (highlighted in bold) where we describe the desired stated of the pod.<\/p>\n<pre><code># Pod spec\napiVersion: v1\nkind: Pod\nmetadata:\n  name: webserver-pod\nspec:\n  containers:\n  - name: webserver\n    image: nginx:latest\n    ports:\n    - containerPort: 80<\/code><\/pre>\n<p>Once you&#8217;ve created an object, you can ask Kubernetes for information about it using Kubectl or API calls to the API server.<\/p>\n<p>The following table shows the important native Kubernetes object types organized in categories.<\/p>\n<p><!--kg-card-begin: html--><\/p>\n<table>\n<thead>\n<tr>\n<th>Category<\/th>\n<th>Kubernetes Objects<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>Workload<\/strong><\/td>\n<td>1. Pods <br \/>2. ReplicaSets <br \/>3. Deployments <br \/>4. StatefulSets <br \/>5. DaemonSets <br \/>6. Jobs <br \/>7. CronJobs<br \/>8. Horizontal Pod Autoscaler<br \/>9. Vertical Pod Autoscaler<\/td>\n<\/tr>\n<tr>\n<td><strong>Service &amp; Networking<\/strong><\/td>\n<td>1. Services <br \/>2. Ingress <br \/>3. IngressClasses<br \/>4. Network Policies <br \/>5. Endpoints<br \/>6. EndpointSlices<\/td>\n<\/tr>\n<tr>\n<td><strong>Storage<\/strong><\/td>\n<td>1. PersistentVolumes <br \/>2. PersistentVolumeClaims <br \/>3. StorageClasses<\/td>\n<\/tr>\n<tr>\n<td><strong>Configuration &amp; Management<\/strong><\/td>\n<td>1. ConfigMaps <br \/>2. Namespaces <br \/>3. ResourceQuotas <br \/>4. LimitRanges<br \/>5. Pod Disruption Budgets (PDB)<br \/>6. Pod Priority and Preemption<\/td>\n<\/tr>\n<tr>\n<td><strong>Security<\/strong><\/td>\n<td>1. Secrets <br \/>2. ServiceAccounts (sa)<br \/>3. Roles <br \/>4. RoleBindings <br \/>5. ClusterRoles <br \/>6. ClusterRoleBindings<\/td>\n<\/tr>\n<tr>\n<td><strong>Metadata<\/strong><\/td>\n<td>1. Labels and Selectors <br \/>2. Annotations<br \/>3. Finalizers<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><!--kg-card-end: html--><\/p>\n<h2 id=\"common-object-parameters\">Common Object Parameters<\/h2>\n<p>There are many object types in Kubernetes, however, there are common parameters for every object.<\/p>\n<p>Following are the common set of parameters shared by Kubernetes objects<\/p>\n<p><!--kg-card-begin: html--><\/p>\n<table>\n<thead>\n<tr>\n<th>Parameter<\/th>\n<th>Description<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>apiVersion<\/strong><\/td>\n<td>The Kubernetes API version for the object.<br \/>&#8211; Alpha<br \/>&#8211; Beta<br \/>&#8211; Stable<\/td>\n<\/tr>\n<tr>\n<td><strong>kind<\/strong><\/td>\n<td>The type of object being defined <br \/>&#8211; Pod<br \/>&#8211; Deployment<br \/>&#8211; Service<br \/>&#8211; Configmap, etc.<\/td>\n<\/tr>\n<tr>\n<td><strong>metadata<\/strong><\/td>\n<td>metadata is used to uniquely identify and describe a Kubernetes object. Here are some of the common key metadata that can be added to an object<br \/>&#8211; labels <br \/>&#8211; name <br \/>&#8211; namespace <br \/>&#8211; annotations<br \/>&#8211; finalizers etc<\/td>\n<\/tr>\n<tr>\n<td><strong>spec<\/strong><\/td>\n<td>Under the &#8216;spec&#8217; section of a Kubernetes object definition, we declare the desired state and characteristics of the object that we want to create.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><!--kg-card-end: html--><\/p>\n<h2 id=\"object-uid\">Object UID<\/h2>\n<p>Every object created by Kubernetes has a unique id associated with it. Universally Unique Identifier (UUID) or UID.<\/p>\n<p>You cant have two object with same name. Meaning, you cant have two pods with same name <strong><code>webserver<\/code><\/strong>. However, you can have a pod named <strong><code>webserver<\/code><\/strong> and a Deployment named <strong><code>webserver<\/code><\/strong>.<\/p>\n<p>You can find this UID by describing the pod. It will be present under the metadata section.<\/p>\n<figure class=\"kg-card kg-image-card kg-card-hascaption\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-30-19.png\" class=\"kg-image\" alt=\"Kubernetes Object UUID\" loading=\"lazy\" width=\"582\" height=\"505\"><figcaption><span style=\"white-space: pre-wrap;\">Click to view in HD<\/span><\/figcaption><\/figure>\n<p>If you want a non-unique identified for your object, you can use <strong><code>labels<\/code><\/strong> and <strong><code>annotations<\/code><\/strong>.<\/p>\n<h2 id=\"what-are-kubernetes-resources\">What are Kubernetes Resources?<\/h2>\n<p>In Kubernetes, everything is accessed through APIs.<\/p>\n<p>To create different types of objects such as pods, namespaces, configmaps etc, Kubernetes API server provides API endpoints.<\/p>\n<p>These object-specific endpoints are called <strong>API resources or resources<\/strong>. For example, the API endpoint used to create a pod is referred to as a <strong>Pod resource<\/strong>.<\/p>\n<p>In simpler terms, a resource is a <strong>specific API URL<\/strong> used to access an object, and they can be accessed through HTTP verbs such as GET, POST, and DELETE.<\/p>\n<p>For instance, the <strong><code>\/api\/v1\/pods<\/code><\/strong> resource can be used to retrieve a list of Pod objects, and an individual Pod object can be obtained from the <strong><code>\/api\/v1\/namespaces\/pods\/<\/code><\/strong> resource.<\/p>\n<p>Here are some examples of API resource endpoints in Kubernetes:<\/p>\n<ol>\n<li><code>\/api\/v1\/namespaces<\/code>: This API resource retrieves a list of all namespaces in the cluster.<\/li>\n<li><code>\/api\/v1\/pods<\/code>: This API resource retrieves a list of all pods in the cluster.<\/li>\n<li><code>\/api\/v1\/pods\/{pod-name}<\/code>: This API resource retrieves information about a specific pod, where <code>{pod-name}<\/code> is the name of the pod.<\/li>\n<li><code>\/api\/v1\/namespaces\/{namespace-name}\/pods<\/code>: This API resource retrieves a list of all pods in a specific namespace, where <code>{namespace-name}<\/code> is the name of the namespace.<\/li>\n<li><code>\/api\/v1\/namespaces\/{namespace-name}\/pods\/{pod-name}<\/code>: This API resource retrieves information about a specific pod in a specific namespace.<\/li>\n<\/ol>\n<p>These endpoints are just a few examples of the many API resources available.<\/p>\n<p>When creating a Kubernetes object using kubectl, the<strong> YAML specification is converted to JSON format<\/strong> and sent to the Pod resource (Pod API endpoint).<\/p>\n<p>It&#8217;s important to note that the terms pods, volumes, and other similar terms are referred to as generic &#8220;resources,&#8221; not API resources, so it&#8217;s important not to confuse the two.<\/p>\n<p>The following image shows how a Kubernetes object spec is send to the API servers pod API resource via kubectl to be persisted in etcd.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-20-24.png\" class=\"kg-image\" alt=\"Kubernetes Objects Vs API Resources\" loading=\"lazy\" width=\"775\" height=\"888\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-20-24.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-20-24.png 775w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<p>Also, when using Kubernetes RBAC, you will comes across objects concepts.<\/p>\n<h2 id=\"what-are-kubernetes-custom-resources\">What are Kubernetes Custom Resources?<\/h2>\n<p>Kubernetes comes with a native list of API resources such as pods, deployments, secrets, configmaps, and more.<\/p>\n<p>For example, if you deploy a pod object, it creates a pod with specified containers.<\/p>\n<p>But what if you want to <strong>create your own API resource?<\/strong><\/p>\n<p>Let&#8217;s say you need a resource named &#8220;<strong>backup<\/strong>&#8221;  and if you deploy the backup object, it should  perform a etcd backup and uploads it to S3.<\/p>\n<p>Luckily, Kubernetes is <strong>highly extensible<\/strong> and allows users to create their own resources and integrate them with core Kubernetes APIs. If you develop your own custom API in Kubernetes to perform a specific action, you <strong>can call it a custom resource<\/strong>.<\/p>\n<div class=\"kg-card kg-callout-card kg-callout-card-grey\">\n<div class=\"kg-callout-text\"><b><strong style=\"white-space: pre-wrap;\">Note<\/strong><\/b>: To create a custom resources, you need to implement a controller that handles the custom resource.<\/div>\n<\/div>\n<p>Here is how a custom object specification for backup would look like. When you deploy this object, the custom controller the user developed performs the desired action at the backend.<\/p>\n<pre><code>apiVersion: devopscube.com\/v1\nkind: Backup\nmetadata:\n  name: my-backup\nspec:\n  etcdEndpoint: http:\/\/etcd:2379\n  s3Bucket: my-bucket\n  s3Region: us-west-2\n<\/code><\/pre>\n<h2 id=\"kubenetes-manifests\">Kubenetes Manifests<\/h2>\n<p>Normally we call <a href=\"https:\/\/devopscube.com\/create-kubernetes-yaml\/\">Kubernetes YAML <\/a>files as manifests. It includes specifications for one or more Kubernetes objects, such as Deployments, Services, ConfigMaps,  Secrets etc.<\/p>\n<p>The following image shows an example of a Kubernetes manifest file where we have a Pod and Sevice object defined. If you run this manifest it will create both the pod and service object.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-14-34.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"933\" height=\"1208\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-14-34.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-14-34.png 933w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<p>Is there a maximum limit for the number of objects in a manifest file?<\/p>\n<p>No, but there is a default size limit of 1MB. However, it is recommended to keep the number of objects per file to a manageable level for ease of maintenance and readability if you choose to use manifest files.<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>In this blog, we looked at Kubernetes object, resources, custom resource&#8217;s and its differences. If you want to master Kubernetes, these basics are very much important.<\/p>\n<p>If you are learning Kubernetes, you can checkout the 40+ <a href=\"https:\/\/devopscube.com\/kubernetes-tutorials-beginners\/\">comprehensive Kubernetes tutorials.<\/a><\/p>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/kubernetes-objects-resources\/\" target=\"_blank\" rel=\"noopener noreferrer\">Kubernetes Objects Vs Resources Vs Custom Resource \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/kubernetes-objects-resources\/<\/p>\n","protected":false},"author":1,"featured_media":783,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-782","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/782","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=782"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/782\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/783"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=782"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=782"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=782"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}