Creating a suc­cess­ful Kuber­netes De­ploy­ment is crucial for the efficient man­age­ment of container ap­plic­a­tions. In this tutorial, you’ll gain insights into the basic steps and best practices to create a robust de­ploy­ment in a Kuber­netes en­vir­on­ment.

What is a Kuber­netes De­ploy­ment?

A Kuber­netes De­ploy­ment is an essential concept within the Kuber­netes platform that sim­pli­fies the de­ploy­ment and man­age­ment of ap­plic­a­tions in con­tain­ers. This control mechanism acts as an in­ter­me­di­ary between the con­fig­ur­a­tion and its actual execution in the Kuber­netes cluster. You define the desired state of your ap­plic­a­tions, including rep­lic­a­tion details, container images and con­fig­ur­a­tion settings.

The Kuber­netes De­ploy­ment then takes over the automated de­ploy­ment, organises con­tain­ers in Kuber­netes pods and dis­trib­utes 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 ad­just­ment of the number of running instances based on the current load.

Tip

With managed Kuber­netes from IONOS, you can enjoy all the benefits of container or­ches­tra­tion without having to worry about time-consuming ad­min­is­trat­ive tasks such as mon­it­or­ing, updates and main­ten­ance routines. Thanks to the free man­age­ment service from IONOS Cloud, you can increase your pro­ductiv­ity and save valuable resources.

Creating a Kuber­netes De­ploy­ment can be done using one of two basic methods:

  • Im­per­at­ive method
  • De­clar­at­ive method

How to create a Kuber­netes De­ploy­ment using the im­per­at­ive method

With the im­per­at­ive method, you enter specific commands to directly perform the desired actions. This can include creating resources, such as pods and services, or updating con­fig­ur­a­tions. The im­per­at­ive method is well suited to situ­ation­al or ex­per­i­ment­al re­quire­ments.

The following command creates a Kuber­netes De­ploy­ment called nginx-deployment and con­fig­ures 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 im­per­at­ive approach is quick to implement, it has some drawbacks. The steps in the im­per­at­ive 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, es­pe­cially if the initial situation has changed.

Cloud GPU VM
Maximum AI per­form­ance with your Cloud GPU VM
  • Exclusive NVIDIA H200 GPUs for maximum computing power
  • Guar­an­teed per­form­ance thanks to fully dedicated CPU cores
  • 100% European hosting for maximum data security and GDPR com­pli­ance
  • Simple, pre­dict­able pricing with fixed hourly rate

How to create a Kuber­netes De­ploy­ment using the de­clar­at­ive method

Compared to the im­per­at­ive method, the de­clar­at­ive method follows a more abstract approach. You define the desired state of the ap­plic­a­tion in a con­fig­ur­a­tion file and Kuber­netes ensures that the cluster cor­res­ponds to this. You describe what you want to achieve and Kuber­netes takes over the im­ple­ment­a­tion and main­ten­ance.

Step 1: Create a YAML con­fig­ur­a­tion file

First, open a text editor and create the YAML file nginx-deployment.yaml to define the con­fig­ur­a­tion for the Kuber­netes De­ploy­ment:

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 con­fig­ur­a­tion. With replicas: 3 we specify that the de­ploy­ment should create three rep­lic­a­tions (pods) of the ap­plic­a­tion. 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 con­figured, which uses the Nginx image and listens on port 80.

Step 2: Applying the con­fig­ur­a­tion

Enter the following in the command line to apply the con­fig­ur­a­tion 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 con­fig­ur­a­tion for the Kuber­netes resources.

Step 3: Check the status of the de­ploy­ment

This step provides in­form­a­tion on whether the Kuber­netes De­ploy­ment has been suc­cess­fully 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 con­fig­ur­a­tion.
  • AVAILABLE: Lists the number of pods that are available and can accept requests.
  • AGE: Indicates how long the de­ploy­ment has been running.

Step 4: Display ad­di­tion­al in­form­a­tion

The following command provides you with detailed in­form­a­tion about your Kuber­netes De­ploy­ment, the strategies used, the current and desired rep­lic­a­tions 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 con­figured as Rolling Update, with a maximum of 25% un­avail­ab­il­ity and an increase of up to 25% ad­di­tion­al pods during an update.

Step 5: Scaling the de­ploy­ment

To adjust the number of pods in a Kuber­netes De­ploy­ment, use the kubectl scale command.

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

After executing this command, Kuber­netes will update the de­ploy­ment and ensure that the desired number of rep­lic­a­tions is available. In our example, we reduce the number of running pods to 2.

Kuber­netes De­ploy­ment strategies

The de­ploy­ment strategies in Kuber­netes define how changes in an ap­plic­a­tion are im­ple­men­ted in a cluster.

  • Rolling De­ploy­ment (default): The rolling strategy is the default method for de­ploy­ments in Kuber­netes. New pods are deployed in stages, while old pods are gradually removed. This enables con­tinu­ous avail­ab­il­ity during the update process.
  • Recreate De­ploy­ment: 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 De­ploy­ment: Here, two sets of pods (blue and green) are created, one rep­res­ent­ing the current version of the ap­plic­a­tion and the other the new version. By assigning specific labels to these pods, traffic can be seam­lessly re­dir­ec­ted between the two versions. This allows you to quickly switch between the two versions and easily perform rollbacks.

Kuber­netes De­ploy­ment rollback

If an error occurs in the latest version of your ap­plic­a­tion, it’s crucial to roll back as quickly as possible. In Kuber­netes, you can initiate the rollback process by checking the revision history of your de­ploy­ment and reverting to a previous version if necessary.

Check the revision history

To display the revision history of your de­ploy­ment, 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 con­sidered 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 Kuber­netes, we recommend the Kuber­netes tutorial from our Digital Guide.

IONOS Cloud Managed Kuber­netes
Container workloads in expert hands

The ideal platform for demanding, highly scalable container ap­plic­a­tions. Managed Kuber­netes works with many cloud-native solutions and includes 24/7 expert support.

Go to Main Menu