653 words
3 minutes
Creating Kubernetes Objects - Imperative vs Declarative Approaches

Creating Kubernetes Objects: Imperative vs Declarative Approaches#

In Kubernetes, there are two primary ways to create and manage objects: the imperative way and the declarative way. Each approach has its use cases and benefits. In this blog post, we’ll explore both methods and work through some practical examples.

Imperative vs Declarative: An Overview#

  1. Imperative Approach:

    • Uses commands or API calls to directly create or modify Kubernetes objects
    • Ideal for quick, one-off tasks or when learning Kubernetes
    • Example: kubectl create pod nginx --image=nginx
  2. Declarative Approach:

    • Uses YAML or JSON manifest files to describe the desired state of objects
    • Better for version control and maintaining complex configurations
    • Example: kubectl apply -f nginx-pod.yaml

Now, let’s dive into some practical tasks to illustrate these approaches.

Task 1: Creating a Pod Imperatively#

Let’s create a pod using the imperative command with nginx as the image:

Terminal window
kubectl run nginx-pod --image=nginx --port=80

This command creates a pod named “nginx-pod” using the nginx image and exposing port 80.

To verify the pod creation:

Terminal window
kubectl get pods

You should see your nginx-pod listed in the output.

Task 2: From Imperative to Declarative#

Now, let’s convert our imperatively created pod into a YAML file:

  1. Generate the YAML from the existing pod:
Terminal window
kubectl get pod nginx-pod -o yaml > nginx-pod.yaml
  1. Edit the YAML file to update the pod name:
Terminal window
sed -i 's/name: nginx-pod/name: nginx-new/' nginx-pod.yaml
  1. Create a new pod using this YAML file:
Terminal window
kubectl apply -f nginx-pod.yaml

Verify the new pod creation:

Terminal window
kubectl get pods

You should now see both nginx-pod and nginx-new in the list of pods.

Task 3: Troubleshooting a Pod YAML#

Let’s apply the following YAML and fix any errors:

apiVersion: v1
kind: Pod
metadata:
labels:
app: test
name: redis
spec:
containers:
- image: rediss
name: redis

Applying this YAML:

Terminal window
kubectl apply -f redis-pod.yaml

You might encounter an error like this:

Error: ImagePullBackOff: Back-off pulling image "rediss"

This error suggests that Kubernetes couldn’t pull the specified image. Let’s troubleshoot:

  1. Check the pod status:
Terminal window
kubectl describe pod redis

In the events section, you’ll likely see an error indicating that the image “rediss” couldn’t be found.

  1. The issue is a typo in the image name. Let’s correct it:
Terminal window
sed -i 's/image: rediss/image: redis/' redis-pod.yaml
  1. Apply the corrected YAML:
Terminal window
kubectl apply -f redis-pod.yaml
  1. Verify the pod is now running:
Terminal window
kubectl get pods

You should now see the redis pod in a “Running” state.

Conclusion#

We’ve explored both imperative and declarative approaches to creating Kubernetes objects. While the imperative method is quick for simple tasks, the declarative approach with YAML files offers better version control and is more suitable for complex configurations.

Remember, when troubleshooting Kubernetes issues:

  1. Always check the pod status using kubectl get pods
  2. Use kubectl describe pod <pod-name> for detailed information
  3. Look for common issues like typos in image names or misconfigured resources

By mastering both approaches and understanding how to troubleshoot common issues, you’ll be well-equipped to manage Kubernetes objects effectively.

References#

  1. Kubernetes Official Documentation: Imperative Commands
  2. Kubernetes Official Documentation: Declarative Management
  3. Kubernetes Official Documentation: Troubleshooting Applications
Imperative kubectl create pod nginx --image=nginx kubectl expose pod nginx --port=80 kubectl scale deployment nginx --replicas=3 Declarative apiVersion: v1 kind: Pod metadata: name: nginx spec: containers: - name: nginx image: nginx

kubectl apply -f nginx-pod.yaml

Both create Kubernetes objects

Creating Kubernetes Objects - Imperative vs Declarative Approaches
https://mranv.pages.dev/posts/kubernetes-objects-imperative-declarative/
Author
Anubhav Gain
Published at
2024-09-22
License
CC BY-NC-SA 4.0