This document demonstrates the implementation of both Imperative and Declarative approaches in Kubernetes using an NGINX deployment. The objective is to understand how Kubernetes manages desired state through different operational methodologies and how declarative configurations enable reproducibility and scalability.
To implement and compare Imperative and Declarative deployment approaches in Kubernetes by deploying an NGINX application, modifying configuration using YAML, and validating deployment behavior.
Kubernetes operates on a declarative model where the user defines the desired state of the system. The Kubernetes control plane continuously monitors the actual state and reconciles it with the desired state.
The imperative approach involves issuing direct commands to Kubernetes to create or manage resources.
Example: kubectl create deployment web –image=nginx
Characteristics:
The declarative approach uses configuration files (YAML or JSON) to define the desired system state.
Example: kubectl apply -f deployment.yaml
Characteristics:
The NGINX deployment is created using a direct command:
kubectl create deployment web –image=nginx
Deployment and pod status are verified using:
kubectl get deployments kubectl get pods
A YAML configuration file is generated from the existing deployment:
kubectl create deployment web –image=nginx –dry-run=client -o yaml > deployment.yaml
The generated YAML file is modified to include multiple replicas and structured metadata.
Configuration: apiVersion: apps/v1 kind: Deployment metadata: name: web labels: app: web spec: replicas: 3 selector: matchLabels: app: web template: metadata: labels: app: web spec: containers: - name: nginx image: nginx
The existing deployment is removed:
kubectl delete deployment web
The YAML configuration is applied:
kubectl apply -f deployment.yaml
Deployment status and pod replicas are verified:
kubectl get pods kubectl describe deployment web
The application is accessed using port forwarding:
kubectl port-forward deployment/web 8080:80
The application is available at: http://localhost:8080
The deployment is scaled dynamically:
kubectl scale deployment web –replicas=5
Resources are cleaned up:
kubectl delete deployment web
The declarative approach is preferred in Kubernetes environments as it provides a robust and scalable way to manage infrastructure. It aligns with modern DevOps practices by enabling version control, reproducibility, and automation. Kubernetes ensures system consistency through its reconciliation loop, maintaining the desired state at all times.