22 Deploying Pods from a manifest file
22.1 What is a Pod?
In Kubernetes, the basic building block is a Pod. A Pod can contain one or more ‘containers’ (usually Docker containers), which are like mini-programs, and all of these get sent to a single computer in the group (or ‘cluster’) at the same time.
We use special instruction files (YAML manifest files) to tell Kubernetes how and what to deploy, and it’s common to use tools called ‘Deployments’ and ‘DaemonSets’ to manage these Pods.
If a Pod is set up without using a controller, like a Deployment or DaemonSet, it’s known as a ‘static’ Pod. To set up these Pods, we use a command kubectl apply
to send the instruction files to the Kubernetes control center (the ‘API server’), and then Kubernetes decides which computer in the cluster will run the Pod.
A special program on the chosen computer, called the ‘kubelet daemon’, takes care of starting the Pod and keeping an eye on it, and tries to fix problems locally. But if the computer running a static Pod has a problem and stops working, the Pod won’t be automatically replaced.
22.2 Deploying Pods from a manifest file
- Change directory into the pods folder:
cd /mnt/vdb/github/TheK8sBook/pods
- Create your Pod with the manifest file
pod.yml
:
sudo kubectl apply -f pod.yml
Here is the content of pod.yml
:
apiVersion: v1
kind: Pod
metadata:
name: hello-pod
labels:
zone: prod
version: v1
spec:
containers:
- name: hello-ctr
image: nigelpoulton/k8sbook:1.0
ports:
- containerPort: 8080
In essence, this YAML file creates a Pod named hello-pod
with a single container called hello-ctr
. The container is created from the nigelpoulton/k8sbook:1.0 Docker image and exposes port 8080 for network communication. The Pod is labelled with zone: prod and version: v1 to help manage and organize it better.
Here’s a breakdown of what each part of the YAML file means:
apiVersion: v1
: This indicates the version of the Kubernetes API you’re using to create this object.
kind: Pod
: This line indicates that the type of the Kubernetes object you’re creating is a Pod.
metadata:
This section provides data that helps uniquely identify the Pod.
`name:` hello-pod: This is the name of the Pod.
`labels:` These are key-value pairs that are used to organize and categorize Pods. Here, `zone`: prod and `version`: v1 are used as labels.
spec:
This section defines the desired state for this Pod, meaning what containers should be running.
`containers:` This is a list of containers that should be launched inside this Pod.
`name: hello-ctr`: This is the name of the container.
`image: nigelpoulton/k8sbook:1.0`: This indicates the Docker image to be used for this container. The image is taken from a public Docker image repository.
`ports:` This lists the network ports the container should expose. containerPort: 8080 means that the container is listening on port 8080.
22.3 Check the status of Pods
- Show status of your Pod with:
sudo kubectl get Pods
This sould output something like:
NAME READY STATUS RESTARTS AGE
hello-pod 1/1 Running 0 2m48s
The Pod is running on a node and is being monitored by the local kubelet process.
- To get a more detailed overview use:
sudo kubectl get pods -o yaml
- Another option is:
sudo kubectl describe pods hello-pod
22.4 Running commands in Pods
Next, we will execute a command from inside the Pod.
- Use
kubectl exec -it
to open an interactive session:
sudo kubectl exec -it hello-pod -- sh
- For example, let’s show the Pod hostname inside the interactive session:
env | grep HOSTNAME
The hostname of the container is set to the Pod’s name. Note that you should always set Pod names as valid DNS names (a-z and 0-9, the minus sign and the period sign).
- Type exit to get back to the terminal
exit
- Let’s delete this Pod:
sudo kubectl delete pod hello-pod