Learn about Deployment in Kubernetes

In Kubernetes, the Deployment object is the main object responsible for deploying and managing our application. It allows us to deploy Pods, update Pods, rollback Pods, and ReplicaSets. How is it in detail? We will find out together in this tutorial!

First thing, to create a new Deployment object, we need to define information for it using a YAML file.

You can define the information of a Deployment using a YAML file with the following basic content:

With:

  • <name> is the name of the Deployment
  • <replicas-number> is the number of Pods that we want the Deployment to guarantee.
  • <label> defines the label for the Pod, you can define the label as you like.
  • <container_name> is the name of the container in the Pod after initialization
  • <container_image> is the name of the Image used to create the container.

We define the name of the Deployment using the field metadata.name.

To define the number of replicas, the number of Pods that this Deployment manages, we will use the spec.replicas field.

The field spec.selector is used to define how the Deployment finds Pods to manage. Here, we are using the matchLabels property to do this. Any Pod with a label defined as “<label>” will be selected and managed by Deployment. We can also use the matchExpressions property to do this.

The field spec.template is used to define information about the Pod we want to run. You can use the metadata.labels attribute in this spec.template field to label the Pod.

We use the spec.containers property to define all the containers that the Pod will have. Each container will have at least the container name and the Image used to create the container.

For example, now I need to run a Deployment to deploy the node-hello application from the image “gcr.io/google–samples/node–hello:1.0”, I can define the content of the YAML file with the following content:

Run the command “apply” with this configuration file:

You will see the following result:

You can check all the Deployments in the Kubernetes cluster using the command:

My results are as follows:

Check the Pod, you will see the following results:

In general, the Deployment will manage the ReplicaSet and the ReplicaSet will manage the Pods to run the applications. The Deployment will create ReplicaSets to ensure the number of Pods for our application.

When our application is deployed with the Deployment object, it is only accessible within the cluster.

So if you want to check the results of this deployment, you need to login to one of the Pods that have deployed the application. For example, to check the results of the above example, first we need to know the Container ID of the Pod with the command:

then access this Container to check the results:

As you can see, my example application has been deployed on port 8080 and the result when accessing this application is “Hello Kubernetes!”.

To delete a Deployment, you can use the command:

For example, I can delete my Deployment node-hello as follows:

Result:

Add Comment