In this lesson, we will explore how to create, manage, and scale pods within a Kubernetes cluster. Pods are the smallest deployable units in Kubernetes and can hold one or more containers.
A Pod is a logical host for one or more containers, which share the same network namespace, IP address, and storage. Pods are designed to run a single instance of a given application.
You can create a Pod using a YAML configuration file or directly via kubectl commands.
Create a file named my-pod.yaml with the following content:
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: nginx
ports:
- containerPort: 80
To create the Pod, run the following command:
kubectl apply -f my-pod.yaml
To check if the Pod is running, use:
kubectl get pods
Managing Pods involves operations like viewing logs, deleting, and updating Pods.
You can view the logs of a specific container in a Pod:
kubectl logs my-pod -c my-container
To delete a Pod, use:
kubectl delete pod my-pod
Kubernetes allows you to scale Pods easily using Deployments.
Create a file named my-deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: nginx
ports:
- containerPort: 80
To create the Deployment, run:
kubectl apply -f my-deployment.yaml
To scale the number of replicas:
kubectl scale deployment my-deployment --replicas=5
Note: Always use Deployments for managing Pods instead of creating Pods directly. This allows for easier scaling and updates.
Common Mistake: Not specifying resource limits for containers can lead to resource exhaustion on nodes.
| Best Practice | Description |
|---|---|
| Use Deployments | For managing Pods and scaling. |
| Set Resource Limits | To avoid resource starvation. |
| Use Labels and Selectors | For better organization and management of Pods. |
kubectl commands.test-pod.yaml that defines a Pod running an httpd container.kubectl apply -f test-pod.yaml.kubectl get pods to list all Pods.kubectl logs <pod-name> to view the logs of your created Pod.kubectl delete pod <pod-name>.test-deployment.yaml with 2 replicas of an nginx container.kubectl scale deployment <deployment-name> --replicas=4.kubectl commands.