Task 1 - Creating and Managing Pods
Objective: Learn Pod, container and init container
What is Pod
In Kubernetes, a Pod is the smallest and most basic deployable unit. It represents a single instance of a running process in your cluster. Pods are the building blocks of Kubernetes applications and encapsulate one or more containers, storage resources, a unique network IP, and options that govern how the containers should run. More info about Pods:
- The smallest, most basic unit in Kubernetes.
- Represents a single instance of a running process in your cluster.
- Can contain one or more containers that are tightly coupled and share resources, such as storage and networking.
- Often represents a single microservice, but can also encapsulate multiple closely related containers.
- Pods are ephemeral by nature, meaning they can be created, destroyed, and replaced dynamically based on the cluster’s needs.
- Managed by Kubernetes controllers like Deployments, ReplicaSets, or StatefulSets to ensure desired state and high availability.(We will learn about these in next chapters)
- Provides a unique IP address within the cluster, allowing communication between Pods.
- Can have one or more volumes attached to it for persistent storage.
- Logs and metrics of individual containers within a Pod can be accessed using kubectl.
Pods in a Kubernetes cluster are used in two main ways:
Pods that run a single container: The “one-container-per-Pod” model is the most common Kubernetes use case; in this case, you can think of a Pod as a wrapper around a single container; Kubernetes manages Pods rather than managing the containers directly.
Pods that run multiple containers: that need to work together. A Pod can encapsulate an application composed of multiple co-located containers that are tightly coupled and need to share resources. These co-located containers form a single cohesive unit.
Copy the below manifest file to create a Pod.
cat << EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
EOF
Init container
Init containers in Kubernetes are specialized containers that run before the main containers in a Pod, serving to prepare the environment or perform initialization tasks. They execute sequentially, ensuring that each init container completes successfully before the next one starts and before the main containers begin execution. Init containers share the same volume mounts as the main containers, facilitating the sharing of resources such as configuration files or secrets. They are transient in nature, running to completion once and then terminating, and are not restarted if they fail.
- To create Pod with init containers:
cat << EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod-manual
spec:
volumes:
- name: shared-data
emptyDir: {}
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
volumeMounts:
- name: shared-data
mountPath: /usr/share/nginx/html
initContainers:
- name: init-nginx
image: busybox
command: ['sh', '-c', 'echo "Hello, NGINX at $(date)!" >> /usr/share/nginx/html/index.html']
volumeMounts:
- name: shared-data
mountPath: /usr/share/nginx/html
EOF
To check Pods, run
kubectl get pods
To check more detail of Pod, use
kubectl describe pod
check the output difference between kubectl describe pod nginx
and kubectl describe pod nginx-pod-manual
.
- To access nginx Pod from contaier itself, use
kubectl exec -it po/nginx-pod-manual -- curl http://127.0.0.1
expected outcome
Hello, NGINX at Tue Feb 27 01:18:50 AM UTC 2024!
Review Questions
- What is the purpose of spec.containers.volumeMounts in a Pod definition?
- Which lines in the Pod nginx-pod-manual define a volume?
- Why can the init container access the shared-data folder?
- Can you restart a Pod created with kubectl create pod or a YAML file specifying kind: Pod?
- If you delete the Pod nginx-pod-manual and recreate it using the same YAML, will the previous data in the shared-data folder persist?"
Clean up
kubectl delete pod nginx-pod-manual