Hello everyone, welcome back to my blog series on CKA 2024 in this seventh entry of the series, we'll be diving deep into Kubernetes Pods and the basics of YAML, which is the primary language used in Kubernetes configurations. We will also explore the concept of Pods in Kubernetes, setting the stage for the hands-on exercises that follow.
Introduction to Kubernetes Architecture
To begin, let's revisit a key diagram from our blog on Kubernetes Architecture. This diagram shows how a user interacts with the Kubernetes API server, which resides in a control plane node (or master node). The interaction happens through client utilities like kubectl or cloud consoles in managed services. Using kubectl, users can provision, update, delete, or retrieve details from the cluster. For instance, when a user runs the kubectl run command to create a Pod, the Pod is scheduled on one of the worker nodes in the cluster.
Creating Pods: Imperative vs. Declarative
There are two main approaches to creating Pods in Kubernetes: imperative and declarative.
1. Imperative Approach
In the imperative approach, you run simple commands like kubectl run nginx-pod --image=nginx. This method involves directly instructing the API server or the kubectl utility to perform actions. It's quick and useful for troubleshooting or local deployments, but not ideal for production environments.
2. Declarative Approach
The declarative approach involves creating a configuration file (in JSON or YAML format) that specifies the desired state of the object. This file includes details like API version, kind, metadata, and spec. You then use commands like kubectl create -f or kubectl apply -f to create or update resources based on this configuration. YAML is preferred due to its readability and ease of use, and it is widely adopted in Kubernetes configurations.
Understanding YAML Syntax
YAML (YAML Ain't Markup Language) is a human-readable data serialization standard. Here are some basics:
- Comments: Start with #
- Data Types: Supports lists, dictionaries, strings, integers, etc.
- Indentation: Uses spaces for indentation (usually 2 spaces)
Example of YAML Structure
# This is a comment
spec:
name: John Doe
age: 30
address:
- old_address: "123 Old St"
- new_address: "456 New St"
In Kubernetes, a typical YAML file for a Pod looks like this:
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
environment: demo
type: frontend
spec:
containers:
- name: nginx-container
image: nginx
ports:
- containerPort: 80
Hands-On: Creating and Managing Pods
Creating a Pod Imperatively
To create an Nginx Pod using the imperative approach, run the following command in your terminal:
kubectl run nginx-pod --image=nginx
Check the Pod status with:
kubectl get pods
Creating a Pod Declaratively
Create a YAML file (pod.yaml) with the following content:
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
Apply this configuration with:
kubectl apply -f pod.yaml
Verify the Pod creation:
kubectl get pods
Editing a Pod
If you need to update the Pod, you can either edit the YAML file and reapply it or use the kubectl edit command to make changes directly:
kubectl edit pod nginx-pod
Troubleshooting
If a Pod is not running correctly, use the kubectl describe pod command to see detailed information and troubleshoot issues.
Thank you for watching, and I hope you found this video valuable. Happy learning!
For further reference, check out the detailed YouTube video here:
Top comments (0)