# Pods

Pods are Kubernetes Objects that are the basic unit for running our containers inside our Kubernetes cluster. In fact, Pods are the smallest object of the Kubernetes Model.

Kubernetes uses pods to run an instance of our application and a single pod represents a single instance of that application. We can scale out our application horizontally by adding more Pod replicas.

Pods can contain a single or multiple containers as a group, that share the same resources within that Pod (storage, network resources, namespaces).

Pods are ephemeral resources, meaning that Pods can be terminated at any point and then restarted on another node within our Kubernetes cluster.

<figure><img src="https://921544542-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FiRQTWHUNVZ5tIxQJXclF%2Fuploads%2FGfchJsImxnE2pmb7RQui%2F0_up5b1m30jjhKLuOY.webp?alt=media&#x26;token=81a4d398-31cb-4486-ba7c-2f4242de477d" alt=""><figcaption><p>Pods</p></figcaption></figure>

### What’s the difference between single container and multi-container Pods? <a href="#bcaf" id="bcaf"></a>

Running a **single container in a Pod** is a common use case. Here, the Pod acts as a wrapper around the single container and Kubernetes manages the Pods rather than the containers directly.

We can also run **multiple containers in a Pod**. Here, the Pod wraps around an application that’s composed of multiple containers and share resources.

If we need to run multiple containers within a single Pod, it’s recommended that we only do this in cases where the containers are tightly coupled.

### How can we create Pods in Kubernetes? <a href="#id-0c0f" id="id-0c0f"></a>

```yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-2
  labels:
    name: nginx-2
    env: production
spec:
  containers:
  - name: nginx
    image: nginx
```

**apiVersion** — This defines the Kubernetes API version that we want to use in this YAML file. You can read more about API versioning in Kubernetes [here](https://kubernetes.io/docs/reference/using-api/#api-versioning).

**kind** — This defines what kind of Kubernetes object we want to create.

**metadata** — This is data that helps us uniquely identify the object that we want to create. Here we can provide a name for our app, as well as apply labels to our object.

**spec** — This defines the state that we want or our object. The format that we use for spec. For our Pod file, we have provided information about the containers that we want to host on our Pod.

### How can we ensure that our Pods in Kubernetes are healthy? <a href="#id-420c" id="id-420c"></a>

Kubernetes relies on Probes to determine whether or not a Pod is healthy. Probes are diagnostic operations that are performed periodically by the Kubelet on containers.

There are three types of Probes:

* **Liveness Probes** — These are used to determine if a Pod is healthy and running as expected. If the liveness probe fails, kubelet will kill the container and the container will then restart according to its defined policy (We can define these as either Always, OnFailure and Never)
* **Readiness Probes** — These are use to determine whether or not a pod should receive requests. If the probe fails, the endpoints controller removes the IP address of the Pod from the endpoints of all Services that match the Pod.
* **Startup Probe** — This is used to determine whether the container has started. All other probes are disabled if a startup probe is provided until it succeeds.
