Imperative vs Declarative Kubernetes Commands

The Backend Grip
3 min readJun 23, 2024

--

The Kubernetes command-line tool, kubectl, allows you to run commands against Kubernetes clusters. You can use kubectl to deploy applications, inspect and manage cluster resources, and view logs.

The kubectl tool supports three kinds of object management

  1. Imperative commands
  2. Imperative object configuration
  3. Declarative object configuration
photo credit: kubernetes website

Imperative Commands

Imperative commands directly operates on objects on the cluster, these commands change the state of the object(s) immediately.

Examples of these commands:

  • kubectl create — This command is used to create an object e.g Deployment, ReplicaSet etc.
  • kubectl run — This command is used to create a Pod
  • kubectl expose — This command is used to create a Service for a Deployment or ReplicaSet
  • kubectl scale — This command is used to scale up or down the number of replicas in a Deployment or ReplicaSet
  • kubectl delete — This command is used to delete an object e.g Deployment, Pod etc.

Examples

The first command creates a Pod object running the nginx container, the second command

Create a Pod object running the nginx container:

kubectl run nginx --image=nginx

Create a Deployment object with ReplicaSets running the nginx container:

kubectl create deployment nginx --image nginx

Imperative commands are generally easy to use, they are great for learning and for test projects but should generally be avoided for production systems because the changes to the system cannot be tracked in a version control system like Git

Imperative Object Configuration

In imperative object configuration, the kubectlcommand specifies the operation (create, replace, etc.), optional flags and at least one file name.

Examples

Create the objects defined in the configuration file:

kubectl create -f config.yaml

Delete the objects defined in two configuration files:

kubectl delete -f config1.yaml -f config2.yaml

Declarative Object Configuration

In contrast to Imperative commands where the exact steps needs to be carried out in the right order to operate on objects, the declarative approach declares the desired state of the objects in declarative manifest files and Kubernetes takes care of achieving the desired state of the objects by using the kubectl applycommand

Examples

Process all object configuration files in the configs directory, and create or patch the live objects. You can first diff to see what changes are going to be made, and then apply:

kubectl diff -f configs/
kubectl apply -f configs/

Recursively process directories:

kubectl diff -R -f configs/
kubectl apply -R -f configs/

Declarative manifests files are written in YAML or JSON and define the desired states of Kubernetes objects. When a declarative manifest is applied on a cluster, Kubernetes compares the desired state against the current state of the objects and makes necessary changes to attain the desired state

Here is an example of a declarative manifest — configs/nginx-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx-container
image: nginx:latest
ports:
- containerPort: 8080

This manifest describes a deployment that should run two replicas of a container based on the nginx:latest Docker image.

This way, we can add many more declarative manifests files in the configs directly and apply all of them with a single kubectl apply -f configs/ command.

The declarative approach in general enables our changes to be tracked in a version control system, makes code review possible and applying the changes automatically in the CI/CD pipeline

please hit the follow button if you like this article and want to get more articles like this

--

--

The Backend Grip

TheBackendGrip is a community for backend programmers, our goal is to share useful content and create networking opportunities for our members