Bite-Sized Kubernetes Part 2 - Pods
Tags: kubernetes, devops, containers
Published: 2023-03-06 22:30:44

## Arr!☸️

In Part 1 we were introduced to the basics of kubernetes and took a brief look at the k8s "objects".
Onward now with our expedition with **Pods**, the smallest object in k8s.

<table>
<tr><th>OBJECT</th><th>DESCRIPTION</th></tr>
<tr><td>👉 Pods</td><td>The lowest level, single instance of an application</td></tr>
<tr><td>ReplicaSets</td><td>The next level, manages a set of pods</td></tr>
<tr><td>Deployments</td><td>The next level, manages a set of ReplicaSets</td></tr>
<tr><td>Services</td><td>The next level, manages a set of deployments</td></tr>
<tr><td>Ingress</td><td>The next level, manages a set of services</td></tr>
<tr><td>Volumes</td><td>The next level, manages a set of ingress</td></tr>
<tr><td>Namespaces</td><td>The next level, manages a set of volumes</td></tr>
<tr><td>Cluster</td><td>The highest level, manages a set of namespaces</td></tr>
</table>

## Pods: 
- K8s does not deploy the containers directly on the worker nodes.
- The containers are encapsulated into a kubernetes object, known as 'pod'.
- A pod is a single instance of an application.
- Usually a pod contains a single container, but it can contain multiple containers, and these containers are not the same containers.
- For example, a pod can contain a container running the application, and another container running a sidecar process or helper application.
- Containers inside the pod share the same network namespace, and can communicate with each other using localhost, and can share the same storage space.
- Commands:
      ```bash
      # deploy a container by creating a pod
      $ kubectl run <pod name> --image=<image name>

      # list all pods
      $ kubectl get pods

      # list detailed info about all pods
      $ kubectl get pods -o wide

      # describe a pod
      $ kubectl describe pod <pod name>  

      # delete a pod
      $ kubectl delete pod <pod name>
      ```
- Creating pods:
  - The .yml file must contain 4 top-level fields/properties i.e. `apiVersion`, `kind`, `metadata` and `spec`.
    - `apiVersion`: the version of the k8s API that is being used e.g. `v1`, `apps/v1`
    - `kind`: the type of object that is being created e.g. `Pod`, `Service`, `Deployment`, `ReplicaSet`
    - `metadata`: data that helps to uniquely identify the object e.g. `name`, `labels`
      - `name` can only have a string value
      - `labels` can have any kind of key-value pairs
    - `spec`: the specification of the object
      - `containers`: the list of containers that will be launched in the pod
        - `name`: the name of the container
        - `image`: the image that will be used to launch the container
  - Example `pod-definition.yml`:
        ```yaml
        apiVersion: v1
        kind: Pod
        metadata:
          name: my-pod
          labels:
            app: my-app
            type: my-type
        spec:
          containers:
          - name: my-container
            image: my-image-name:my-image-tag
        ```
  - Once the .yml file is created, it can be used to create a pod using the `kubectl create` command:
        ```bash
        $ kubectl create -f <file name>.yml
        ```
  - View detailed info about the pod:
        ```bash
        $ kubectl describe pod <pod name>
        ```
  - Delete the pod:
        ```bash
        $ kubectl delete pod <pod name>
        ```
  - Update the pod  with an updated .yml file e.g. with a new image, etc.:
        ```bash
        $ kubectl apply -f <file name>.yml
        ```

Great job sailor!
You now know what pods are, hopefully you found this post an interesting and useful read.
Onwards now to "Controllers and ReplicaSets" in Part 3 which will be out soon! 

**Haul Wind!!!**⛵