Bite-Sized Kubernetes Part 4 - Deployments
Tags: kubernetes, devops, containers
Published: 2023-03-08 19:43:09

## Blimey!☸️

In Part 3 we looked at ReplicaSets in kubernetes.
Moving right along to **Deployments**!🌊

<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>

## Deployments:
- K8s deployments are a higher-level abstraction that are built on top of ReplicaSets.
- K8s deployments help in rolling updates and rollbacks.
- K8s deployments are one level above ReplicaSets in the k8s hierarchy.
- Creating deployments:
- The .yml definition of a deployment is exactly similar to that of a ReplicaSet except for the `kind` property.
  - Example `deployment-definition.yml`:
        ```yaml
        apiVersion: apps/v1
        kind: Deployment

        # all else is same as ReplicaSet
        ```
  - Once the .yml file is created, it can be used to create a deployment:
        ```bash
        $ kubectl create -f <file name>.yml
        ``` 
  - See list of deployments:
        ```bash
        $ kubectl get deployment
        ``` 
  - The deployment will automatically create a ReplicaSet and the ReplicaSet will create the pods.
  - See list of ReplicaSets:
        ```bash
        $ kubectl get replicaset
        ```
  - See list of pods:
        ```bash
        $ kubectl get pods  
        ``` 
  - See detailed info about a deployment:
        ```bash
        $ kubectl describe deployment <deployment name>
        ```
  - Scale up or down:
      - We can scale up the number of pods by updating the `replicas` property in the .yml file and then run the `kubectl replace` command:
            ```bash
            $ kubectl replace -f <file name>.yml
            ```
      - Or we can use the `kubectl scale` command:
            ```bash
            $ kubectl scale deployment <deployment name> --replicas=<number of replicas>
            ```
- To see all k8s objects created i.e. pods, ReplicaSets, deployments, etc.:
      ```bash
      $ kubectl get all
      ```
- Rollouts (updates) and rollbacks in deployments:
  - To see rollout status:
        ```bash
        $ kubectl rollout status deployment <deployment name>
        ``` 
  - To see rollout history:
        ```bash
        $ kubectl rollout history deployment <deployment name>
        ```
  - Deployment strategies:
    - `Recreate`: the default strategy
      - this will delete all the pods and then create new pods
      - this is not recommended for production
    - `RollingUpdate`: the recommended strategy
      - this will update the pods one by one
      - this is the recommended strategy and the default strategy for `kubectl apply` command
  - To update a deployment: 
        ```bash
        $ kubectl edit deployment <deployment name>
        ```
  - After updating the .yml file e.g. updating the app version (image tag), run the `kubectl apply` command to trigger a new rollout:
        ```bash
        $ kubectl apply -f <file name>.yml
        ```
  - The k8s deployment will create a new ReplicaSet and the new ReplicaSet will create new pods following wither of the deployment strategies.
  - To rollback a deployment:
        ```bash
        $ kubectl rollout undo deployment <deployment name>
        ```

You're unstoppable!
Let's look at "Services" next. See ya in Part 5!

**Flank Speed!!!**🚢