Kubernetes objects: Imperative vs declarative creation explained
Learn how to create and manage Kubernetes objects using both imperative kubectl commands and declarative YAML files. Covers object structure, kubectl apply, dry-run, and api-resources.
4 lessons · 11 min · Intermediate
Written by
Marketing Team at Civo
Written by
Marketing Team at Civo
Everything in Kubernetes happens through objects. Whether you are deploying an application, scaling it, or exposing it to traffic, you are creating and managing Kubernetes objects. These objects represent the desired state of your cluster, and Kubernetes continuously works to ensure the actual state matches what you have defined.
Two ways to create objects
There are two approaches to creating Kubernetes objects: imperative and declarative.
- Imperative means telling Kubernetes exactly what to do right now using a direct command. It is fast for experimentation and debugging but not repeatable or version-controllable.
- Declarative means defining the desired state in a YAML file and applying it. Kubernetes figures out how to get from the current state to the desired state. This is the recommended approach for production because it works with version control and GitOps workflows.
Imperative creation
Create a deployment directly with a command:
kubectl create deploy nginx --image=nginx --port=80
Verify the deployment was created:
kubectl get deploy
Expected output:
NAME READY UP-TO-DATE AVAILABLE AGEnginx 1/1 1 1 10s
Delete the deployment:
kubectl delete deploy nginx
Declarative creation
The declarative approach uses YAML files applied with kubectl apply. Rather than writing YAML from scratch, use the --dry-run=client -o yaml flag to generate a starting template.
Generating a YAML template without creating the resource
This is one of the most useful kubectl techniques. The --dry-run=client flag runs the command locally without sending anything to the API server, so nothing gets created. Combined with -o yaml it outputs a valid manifest you can save and edit:
kubectl create deploy nginx --image=nginx --port=80 --dry-run=client -o yaml > deploy.yaml
This generates a complete Deployment YAML file saved to deploy.yaml. Open it, make any changes you need, then apply it:
kubectl apply -f deploy.yaml
Expected output:
deployment.apps/nginx created
To update the deployment later, edit the YAML file and run kubectl apply -f deploy.yaml again. Unlike kubectl create, kubectl apply is idempotent — it creates the resource if it does not exist, or updates it if it does.
Structure of a Kubernetes object
Every Kubernetes object is defined by four fields:
apiVersion: apps/v1 # The Kubernetes API version for this resourcekind: Deployment # The type of object to createmetadata: # Data that identifies the objectname: nginxlabels:app: nginxspec: # The desired state of the objectreplicas: 1selector:matchLabels:app: nginxtemplate:metadata:labels:app: nginxspec:containers:- name: nginximage: nginxports:- containerPort: 80
apiVersionspecifies which version of the Kubernetes API to use. Different object types use different API versions — for example Deployments useapps/v1while Pods usev1.- kind specifies the type of object. Common values include
Pod,Deployment,Service,ConfigMap, andNamespace. metadatacontains identifying information including the object name, namespace, and labels.specdefines the desired state. The contents ofspecvary by object type — a Deployment spec defines replicas and pod templates, a Service spec defines ports and selectors.
Viewing available API resources
To see every resource type available in your cluster including short names, API groups, and whether they are namespaced:
kubectl api-resources
Expected output (abbreviated):
NAME SHORTNAMES APIVERSION NAMESPACED KINDpods po v1 true Poddeployments deploy apps/v1 true Deploymentservices svc v1 true Servicenamespaces ns v1 false Namespacenodes no v1 false Node
This is useful when you cannot remember the exact resource name or want to check what custom resource definitions (CRDs) are installed in the cluster.
View all namespaces:
kubectl get ns

Marketing Team at Civo
Civo is the Sovereign Cloud and AI platform designed to help developers and enterprises build without limits. We bridge the gap between the openness of the public cloud and the rigorous security of private environments, delivering full cloud parity across every deployment. As a team, we are dedicated to providing scalable compute, lightning-fast Kubernetes, and managed services that are ready in minutes. Through CivoStack Enterprise and our FlexCore appliance, we empower organizations to maintain total data sovereignty on their own hardware.
Our mission is to make the cloud faster, simpler, and fairer. By providing enterprise-grade NVIDIA GPUs and streamlined model management, we ensure that high-performance AI and machine learning are accessible to everyone. Built for transparency and performance, the Civo Team is here to give you total control over your infrastructure, your data, and your spend.
Share this lesson