How to create Kubernetes Deployments step by step

Creating a successful Kubernetes Deployment is crucial for the efficient management of container applications. In this tutorial, you’ll gain insights into the basic steps and best practices to create a robust deployment in a Kubernetes environment.

What is a Kubernetes Deployment?

A Kubernetes Deployment is an essential concept within the Kubernetes platform that simplifies the deployment and management of applications in containers. This control mechanism acts as an intermediary between the configuration and its actual execution in the Kubernetes cluster. You define the desired state of your applications, including replication details, container images and configuration settings.

The Kubernetes Deployment then takes over the automated deployment, organises containers in Kubernetes pods and distributes them to available resources in the cluster. It also allows rolling updates to perform updates in stages and minimise downtime. Auto-scaling functions ensure dynamic adjustment of the number of running instances based on the current load.

Tip

With managed Kubernetes from IONOS, you can enjoy all the benefits of container orchestration without having to worry about time-consuming administrative tasks such as monitoring, updates and maintenance routines. Thanks to the free management service from IONOS Cloud, you can increase your productivity and save valuable resources.

Creating a Kubernetes Deployment can be done using one of two basic methods:

  • Imperative method
  • Declarative method

How to create a Kubernetes Deployment using the imperative method

With the imperative method, you enter specific commands to directly perform the desired actions. This can include creating resources, such as pods and services, or updating configurations. The imperative method is well suited to situational or experimental requirements.

The following command creates a Kubernetes Deployment called nginx-deployment and configures it to deploy an Nginx web server in a container from the official Nginx image on port 80.

$ kubectl create deployment nginx-deployment --image nginx --port=80
shell

Although the imperative approach is quick to implement, it has some drawbacks. The steps in the imperative method are directly linked to the current situation, which can make it difficult to repeat actions. This means that re-executing the same command may not always produce the same result, especially if the initial situation has changed.

How to create a Kubernetes Deployment using the declarative method

Compared to the imperative method, the declarative method follows a more abstract approach. You define the desired state of the application in a configuration file and Kubernetes ensures that the cluster corresponds to this. You describe what you want to achieve and Kubernetes takes over the implementation and maintenance.

Step 1: Create a YAML configuration file

First, open a text editor and create the YAML file nginx-deployment.yaml to define the configuration for the Kubernetes Deployment:

apiVersion: apps/v1 
kind: Deployment 
metadata: 
    name: nginx-deployment 
spec: 
    replicas: 3 
    selector: 
        matchLabels: 
            app: nginx 
    template: 
        metadata: 
            labels: 
                app: nginx 
        spec: 
            containers: 
            - name: nginx-container 
                image: nginx 
                ports: 
                - containerPort: 80
yaml

In the spec section, specify the desired configuration. With replicas: 3 we specify that the deployment should create three replications (pods) of the application. The selector section defines how the pods are selected, with app=nginx serving as the selection criterion. The template is the template for the pods to be created, including labels. Within containers, the container nginx-container is configured, which uses the Nginx image and listens on port 80.

Step 2: Applying the configuration

Enter the following in the command line to apply the configuration to the cluster:

kubectl apply -f nginx-deployment.yaml
shell

The -f option specifies the path to the YAML or JSON file that contains the configuration for the Kubernetes resources.

Step 3: Check the status of the deployment

This step provides information on whether the Kubernetes Deployment has been successfully created and how many pods are currently available.

kubectl get deployments nginx-deployment
shell

The output shows us:

NAME                   READY   UP-TO-DATE   AVAILABLE   AGE 
nginx-deployment         3/3            3           3    2m
shell
  • READY: Shows the number of pods currently running in relation to the desired number.
  • UP-TO-DATE: Shows the number of pods that have been updated with the latest configuration.
  • AVAILABLE: Lists the number of pods that are available and can accept requests.
  • AGE: Indicates how long the deployment has been running.

Step 4: Display additional information

The following command provides you with detailed information about your Kubernetes Deployment, the strategies used, the current and desired replications and the selector labels.

kubectl describe deployment nginx-deployment
shell

The output reads:

Name:                               nginx-deployment 
Namespace:                    default 
CreationTimestamp:      Thu, 22 Feb 2024 12:34:56 +0000 
Labels:                              <none> 
Annotations:                   deployment.kubernetes.io/revision: 1 
Selector:                           app=nginx 
Replicas:                           3 desired | 3 updated | 3 total | 3 available | 0 unavailable 
StrategyType:                  RollingUpdate 
MinReadySeconds:         0 
RollingUpdateStrategy:  25% max unavailable, 25% max surge
shell

The update strategy is configured as Rolling Update, with a maximum of 25% unavailability and an increase of up to 25% additional pods during an update.

Step 5: Scaling the deployment

To adjust the number of pods in a Kubernetes Deployment, use the kubectl scale command.

kubectl scale deployment nginx-deployment --replicas=2
shell

After executing this command, Kubernetes will update the deployment and ensure that the desired number of replications is available. In our example, we reduce the number of running pods to 2.

Kubernetes Deployment strategies

The deployment strategies in Kubernetes define how changes in an application are implemented in a cluster.

  • Rolling Deployment (default): The rolling strategy is the default method for deployments in Kubernetes. New pods are deployed in stages, while old pods are gradually removed. This enables continuous availability during the update process.
  • Recreate Deployment: With this strategy, all existing pods are removed first, and then the updated pods are started. This method can lead to a temporary service outage because no pods are available during the recreate process.
  • Blue/Green Deployment: Here, two sets of pods (blue and green) are created, one representing the current version of the application and the other the new version. By assigning specific labels to these pods, traffic can be seamlessly redirected between the two versions. This allows you to quickly switch between the two versions and easily perform rollbacks.

Kubernetes Deployment rollback

If an error occurs in the latest version of your application, it’s crucial to roll back as quickly as possible. In Kubernetes, you can initiate the rollback process by checking the revision history of your deployment and reverting to a previous version if necessary.

Check the revision history

To display the revision history of your deployment, use the following command:

kubectl rollout history deployment/nginx-deployment
shell

Rollback to previous status

This command performs a rollback to the previous revision, which is considered stable:

kubectl rollout undo deployment/nginx-deployment
shell

Rollback to a specific revision

If you want to return to a specific revision, enter the following command and the desired revision number:

kubectl rollout undo deployment/nginx-deployment --to-revision=1
shell
Tip

If you are new to Kubernetes, we recommend the Kubernetes tutorial from our Digital Guide.

IONOS Cloud Managed Kubernetes
Container workloads in expert hands

The ideal platform for demanding, highly scalable container applications. Managed Kubernetes works with many cloud-native solutions and includes 24/7 expert support.

Was this article helpful?
Page top