Creating Deployments in Kubernetes Made Simple

Deployments in Kubernetes provide an essential framework for running multiple instances of an application, ensuring high availability and resilience. Managed by the Kubernetes Deployment controller, they allow for seamless updates and self-healing functionalities. By utilizing a Pod template, deployments facilitate automatic reconstruction of failed instances, maintaining consistency across the application lifecycle.

A Kubernetes deployment is a critical resource object that enables declarative updates to applications, ensuring stability and scalability. For additional information on Deployment specifications, refer to the Kubernetes API documentation.

In this article, you will learn how to create and manage a deployment in Kubernetes.

Pre-requisites

Ensure you have access to a Kubernetes Cluster with at least one worker node. If you need guidance on setting up a Kubernetes Cluster, click here for a detailed tutorial on setting up a Kubernetes cluster with one Master and two Nodes on AWS Ubuntu 18.04 EC2 Instances.

What We Will Do

Create and manage a Deployment in Kubernetes.

Create a Deployment

Create a YAML file with the following deployment configuration:

vim my-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: httpd-frontend
  name: httpd-frontend
spec:
  replicas: 1
  selector:
    matchLabels:
      app: httpd-frontend
  template:
    metadata:
      labels:
        app: httpd-frontend
    spec:
      containers:
      - image: httpd:2.4-alpine
        name: httpd

my-deployment.yaml

  • The Deployment is named httpd-frontend, as specified in the metadata: name field.
  • It will create one replica of the Pod, outlined in the replicas field.
  • The Pod template, defined in spec: template, is labeled as app: httpd-frontend.
  • The template specifies a single container running httpd using the httpd:2.4-alpine image.

Execute the following command to create the deployment:

kubectl create -f my-deployment.yaml

To retrieve details about the deployment, use the command below:

kubectl get deployment | grep httpd-frontend

get-deployment

To see the replica set created by the deployment, run the following:

kubectl get replicaset | grep httpd-frontend

get-replicaset

You can list the Pods created by the replica set using this command:

kubectl get pods | grep httpd-frontend

get-pod

To test the auto-recreation of a Pod upon failure, delete an existing Pod with the following commands:

kubectl get pods | grep httpd-frontend
kubectl delete pod httpd-frontend-74fd6fd8cd-8nj2s
kubectl get pods | grep httpd-frontend

delete-pod

Observe that the Pod is automatically recreated after deletion.

The deployment will recreate the replica set even if it is deleted. Test this by deleting the replica set:

kubectl get replicaset | grep httpd-frontend
kubectl delete replicaset httpd-frontend-74fd6fd8cd
kubectl get replicaset | grep httpd-frontend

delete-replicaset

After deletion, the replica set is automatically recreated.

To obtain more details about the Pod, use the following command:

kubectl get pods -o wide | grep httpd-frontend

get-pod-with-details

For comprehensive Pod information:

kubectl describe pod httpd-frontend

If you no longer need the application deployed, delete the deployment:

kubectl get deployment | grep httpd-frontend
kubectl delete deployment httpd-frontend
kubectl get deployment | grep httpd-frontend

delete-deployment

Observe that after deleting the deployment, it is not recreated. However, Pods or replica sets part of a deployment will be recreated if deleted separately.

Conclusion

This guide provided insights into creating and managing a deployment in Kubernetes, along with performing delete operations on Pods, ReplicaSets, and the Deployment itself.

FAQ

What is a Kubernetes Deployment?

A Kubernetes Deployment is a resource object that manages a set of identical Pods, ensuring high availability through declarative updates, self-healing, and scalability.

Why use a Deployment in Kubernetes?

Deployments automate the creation, scaling, and management of application instances, making your application highly available and resilient to failures.

How do I create a Deployment in Kubernetes?

To create a Deployment, define its configuration in a YAML file and apply it using kubectl create -f <deployment-file.yaml>.

What happens if a Pod fails?

The Deployment controller automatically replaces failed Pods to maintain the desired state of your application.

Can I delete a Deployment and what are the effects?

Yes, you can delete a Deployment using kubectl delete deployment <deployment-name>. This will remove the Deployment and its managed resources.