Kubernetes: Deployments

Claire Lee
3 min readMar 3, 2023

--

In Kubernetes, Deployments enable the creation and management of multiple versions of an application, which can be seamlessly updated and rolled back without disruption to the end-users. Although both ReplicaSets and Deployments can managing pod replicas, Deployments build on top of ReplicaSets and are recommended for managing complex applications that require updates and rollbacks.

Kubernetes: Deployments

Deployments(Menu Curation)

Deployment in Kubernetes can be compared to the planning process of a restaurant menu.

In Kubernetes, Deployment allows for the creation and management of multiple versions of an application. Deployment can perform a rollout by gradually updating the pods in a ReplicaSet to a new version, while ensuring that the new version meets the desired state. If any issues arise during the rollout, the Deployment can quickly perform a rollback to the previous version.

In a restaurant, a menu is carefully curated to provide a diverse range of dishes that cater to different tastes and preferences. A new dish is introduced to the menu and replaces the old one. However, remove the new dish from the menu and revert back to the old one if it is not popular or does not meet customers’ expectations.

Short Name: deploy

$ kubectl api-resources
NAME SHORTNAMES APIVERSION NAMESPACED KIND
deployments deploy apps/v1 true Deployment

Deployment with YAML

The configuration file for a Deployment is similar to that of a ReplicaSet, with the key difference being the specification of the “kind” attribute as “Deployment” instead of “ReplicaSet”.

Deployments with YAML

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
name: <deployment_name>
labels:
<key1>: <value1>
<key2>: <value2>
:
:
<keyN>: <valueN>
spec:
replicas: <number_of_replicas>
selector:
matchLabels:
<key1>: <value1>
<key2>: <value2>
:
:
<keyN>: <valueN>
template:
# pod definition
metadata:
name: <pod_name>
labels:
<key1>: <value1>
<key2>: <value2>
:
:
<keyN>: <valueN>
spec:
containers:
- name: <container1_name>
image: <image>
- name: <container2_name>
image: <image>

ReplicaSets vs. Deployments

In Kubernetes, both ReplicaSets and Deployments are used to manage and ensure the desired number of replicas of a pod are running in a cluster. However, there are some differences between them.

ReplicaSets can’t perform rolling updates or rollbacks. Deployments, on the other hand, are a higher-level abstraction that build on top of ReplicaSets and provide advanced functionality and automation, including seamless updates to applications without downtime and declarative configuration of the desired state of the application. In addition, Deployments can create and manage ReplicaSets.

In general, it is recommended to use Deployments for managing and deploying applications in Kubernetes, especially for complex applications that require updates and rollbacks. ReplicaSets are typically used for simple stateless applications or when fine-grained control over pod creation and deletion is necessary.

Commands

commands
  1. Create a new deployment based on the YAML file
$ kubectl create -f <deployment>.yaml

analogy: Create a new menu item in the restaurant

2. Create a new deployment with a specified name, image and replicas

$ kubectl create deployment <deployment-name> --image=<image> 
--replicas=<number_of_replicas>

analogy: Add a new dish to the menu with its name, image and serving size

3. Retrieve information about deployment objects

$ kubectl get deployment

analogy: Check the list of items on the menu

4. Retrieve information about all Kubernetes objects

$ kubectl get all

analogy: Check the status of all restaurant operations

--

--