Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.
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#
-
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
-
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:
1kubectl run nginx-pod --image=nginx --port=80This command creates a pod named “nginx-pod” using the nginx image and exposing port 80.
To verify the pod creation:
1kubectl get podsYou 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:
- Generate the YAML from the existing pod:
1kubectl get pod nginx-pod -o yaml > nginx-pod.yaml- Edit the YAML file to update the pod name:
1sed -i 's/name: nginx-pod/name: nginx-new/' nginx-pod.yaml- Create a new pod using this YAML file:
1kubectl apply -f nginx-pod.yamlVerify the new pod creation:
1kubectl get podsYou 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:
1apiVersion: v12kind: Pod3metadata:4 labels:5 app: test6 name: redis7spec:8 containers:9 - image: rediss10 name: redisApplying this YAML:
1kubectl apply -f redis-pod.yamlYou might encounter an error like this:
1Error: ImagePullBackOff: Back-off pulling image "rediss"This error suggests that Kubernetes couldn’t pull the specified image. Let’s troubleshoot:
- Check the pod status:
1kubectl describe pod redisIn the events section, you’ll likely see an error indicating that the image “rediss” couldn’t be found.
- The issue is a typo in the image name. Let’s correct it:
1sed -i 's/image: rediss/image: redis/' redis-pod.yaml- Apply the corrected YAML:
1kubectl apply -f redis-pod.yaml- Verify the pod is now running:
1kubectl get podsYou 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:
- Always check the pod status using
kubectl get pods - Use
kubectl describe pod <pod-name>for detailed information - 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#
- Kubernetes Official Documentation: Imperative Commands
- Kubernetes Official Documentation: Declarative Management
- Kubernetes Official Documentation: Troubleshooting Applications