Kubernetes pods are the basic building blocks of a Kubernetes application. A pod contains one or more containers, and all containers within a pod are always scheduled in the same host. Usually, there is a single container within a pod, but there are use cases where you need to schedule multiple containers in a single pod.

It takes a while to digest why Kubernetes started with the concept of pods in the first place instead of using containers, but there are reasons for that, and you will appreciate this as you gain more experience with the tool. For now, let’s look at a simple example of a pod and how to schedule it in Kubernetes.

Running a pod

We will start by running an NGINX container in a pod using simple imperative commands. We will then look at how we can do this declaratively.

To access the resources for this section, cd into the following directory:

$ cd ~/modern-devops/ch5/pod/

To run a pod with a single NGINX container, execute the following command:

$ kubectl run nginx –image=nginx

To check whether the pod is running, run the following command:

$ kubectl get pod

NAME       READY   STATUS       RESTARTS   AGE

nginx     1/1         Running   0                  26s

And that’s it! As we can see, the pod is now running.

To delete the pod, you can run the following command:

$ kubectl delete pod nginx

The kubectl run command was the imperative way of creating pods, but there’s another way of

interacting with Kubernetes – by using declarative manifests. Kubernetes manifests are YAML or JSON files you can use to declare the desired configuration instead of telling Kubernetes everything through a command line. This method is similar todocker compose.

Tip

Always use the declarative method to create Kubernetes resources in staging and production environments. They allow you to store and version your Kubernetes configuration in a source code management tool such as Git and enable GitOps. You can use imperative methods during development because commands have a quicker turnaround than YAML files.

Let’s look at an example pod manifest, nginx-pod.yaml:

apiVersion: v1

kind: Pod

metadata:

labels:

run: nginx

name: nginx

spec:

containers:

  • image: nginx

imagePullPolicy: Always

name: nginx resources: limits:

memory: “200Mi”

cpu: “200m” requests:

memory: “100Mi”

cpu: “100m”

restartPolicy: Always

Leave a Reply

Your email address will not be published. Required fields are marked *