• Home
  • LLMs
  • Python
  • Docker
  • Kubernetes
  • Java
  • All
  • About
Kubernetes | Deployments (deployments|deploy)
  1. Notes
  2. Deployments (deployments|deploy)
  3. Manage Deployments
  4. Performing Rolling Updates
  5. Record the command that used to perform the update (--record)
  6. Performing Rollbacks
  7. Performing Rollbacks ("--to-revision")
  8. Delete Deployments

  1. Notes
    See these pages for more details about Deployments:
    https://kubernetes.io/docs/concepts/workloads/controllers/deployment/
    https://kubernetes.io/docs/concepts/security/controlling-access/
    https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers/
  2. Deployments (deployments|deploy)
    A Deployment provides declarative updates for Pods and ReplicaSets.

    Deployments add additional capabilities to Pods: rolling updates, rollbacks, ...

    Deployments leverage the ReplicaSet objects. They are automatically created when Deployments are created.

    ReplicaSet objects provide additional features to Deployments: self-healing and scaling capabilities.

    • Rolling updates (zero-downtime):
      When you update Deployment and you post the changes to the API server, Kubernetes creates a new ReplicSet for the Pods (with all the new changes).

      While the Deployment is applied, you will notice that two ReplicSets exist for the Deployment: One with the original changes and a second that reflect the new changes.

      For each Pod that is successfully created for the new ReplicaSet, a corresponding one from the old ReplicaSet will be terminated. If the update is successful, the old ReplicaSet will have no Pod. The old ReplicaSet itself won't be deleted.

      Deployments provides settings to control the sequence of time by which Pods will be created. A wait time can be added between Pods creation/termincation.

    • Rollbacks:
      If after you update a Deployment, you decide to rollback the changes of the new Deployment, the old ReplicaSet will be there and you can perform "easily" the rollback.

    • Self healing:
      Self healing is the ability to re-create a Pod if it fails.

    • Scaling:
      Scaling is the ability to add new Pods or delete existing Pods. Scaling is used to adjust the number of the Pods when the load on the system increases or decreases.

    To create/deploy a Deployment, you need to create a manifest YAML file. The manifest file describe the Deployment and its components (name, Pods, ...).

    Here's a sample file ("hello-nginx-deployment.yaml") that provides a declarative configuration of a Deployment:

    You can use "kubectl" (or any rest client tool) to post the manifest file to the API server If applicable, the API server verifies that the request is authenticated, validates it's authorized, and runs the admission controllers on it. The API server verifies the manifest file and, if no issues, it writes a record for that manifest in the cluster store (etcd). The scheduler will then read the record and deploy the Pod of the Deployment to a healthy worker node with enough available resources.

    The same behaviour, described above, applies if you use the imperative command ("kubectl create") to create the Deployment.

    To apply the deployment:
    The "hello-nginx-deployment.yaml" file defines the following fields:
    • The apiVersion field defines the Kubernetes API group and the Kubernetes API version.
      Its value is written as following: <api-group>/<api-version>
      For Deployments: apps/v1

    • The kind field defines the type of the Kubernetes object to be created (Deployment).

    • The metadata field defines the Deployment metadata (name, labels, namespace, ...).

    • The spec field defines the Pod information.
      • The spec.replicas field defines the number of instances to be created for the Pod.

      • The spec.selector field defines a list of labels that Pods must have in order for the Deployment to manage them.

      • The spec.minReadySeconds field defines the minimum number of seconds for which a newly created Pod should be ready without any of its containers crashing, for it to be considered available. This defaults to 0 (the Pod will be considered available as soon as it is ready).

      • The spec.template field defines the Pod information (metadata, containers, ...).

    The spec.strategy field can be used to define how to perform updates to the Pods managed by the Deployment.


    • The spec.strategy.type field defines the rolling update strategy:
      RollingUpdate: gradually terminating old Pods as soon as the new ones are created.
      Recreate: terminating old Pods first, then create the new ones.
      Custom: allows you to customize the deployment behavior.

    • The spec.strategy.rollingUpdate.maxUnavailable field defines the number of Pods that can be unavailable during the update, without impacting the desired state (desired number of Pods: replicas).

    • The spec.strategy.rollingUpdate.maxSurge field defines the number of new Pods that can be created during the update, where the total number of Pods is above desired state (desired number of Pods: replicas).

    For example, if the desired state of the Pod is 10 instances (replicas: 10), then:
    • Setting maxUnavailable to 2, means, minimum 8 Pods must be available during the update..

    • Setting maxSurge to 2, means, maximum 12 Pods can be available during the update.

    It means, for this case, the rolling update strategy can manage maximum four (the sum of maxUnavailable and maxSurge) Pods at a time (the update will be creating 0-2 Pods or/and terminating 0-2 Pods).

    Note: Managing Deployments using the Manifest Yam file (declarative) is the recommended way, but in some cases you might want to use the imperative command "kubectl create" to create a Deployment.

    You can use the "--dry-run" flag to print the YAML file:
  3. Manage Deployments
    • To deploy the hello-nginx-deployment.yaml file:

    • To monitor/list all Deployments (kubectl get deployments):



      Notice the value of the READY column is transiting from 0 to 1: that indicate when the Pod is ready.

      Notice also the value of the AVAILABLE column is transiting from 0 to 1: that indicate when the Pod is available (see spec.minReadySeconds field).

      If we set the Deployment replicas to 10 (replicas: 1), we should see that the values of the READY and AVAILABLE columns are transiting from 0 to 10.




    • To list all ReplicaSet (kubectl get replicasets):

    • To see the info of a specific ReplicaSet (kubectl get replicaset <REPLICASET-NAME>):

    • To print the manifest file of a specific Deployment as stored in the cluster store (etcd):

      The printed yaml object contains more settings than what it was defined in the original manifest file.
      Some of the settings are the default values that Kubernets gives to the missing field in the manifest file.
      Some of the settings are runtime information set by Kubernets (node IP address, Pod IP address, container info, status, ...).

      The status field provides detailed information about the different transitional states of the Deployment.

    • To describe a Deployment (kubectl describe deployment).

      The command provides information about the Deployment (Replicas, Pods, ...) and its current state.
      It also provide the list of the events occurred while applying the Deployment.

  4. Performing Rolling Updates
    • Let's update the hello-nginx-deployment.yaml file and set the container port to 8080.
      Apply the changes:

    • Monitor the changes:

    • To monitor the rolling update progress:

    • To check the rollout history.

      Revision 1 is referring to the initial deployment and the revision 2 is the current rolling update.

    • List Deployments:

      As mentioned before, if we set replicas to 10, we should see that the value of the READY column is transiting between 8/10 to 12/10, and the value of the AVAILABLE column is transiting between 8 to 10 (see the fields spec.strategy.rollingUpdate.maxUnavailable and spec.strategy.rollingUpdate.maxSurge).

    • List Pods:

    • List ReplicaSets:

      After applying the changes, there are now two ReplicaSets.

      As mentioned, when performing a rolling update, a new ReplicaSet is created and the old one is kept (but all its Pods are terminated).

      Note that the name of the ReplicaSet is composed of the deployment name plus a hash calculated from the spec.template field in the manifest YAML file.

    • Validate that the container port was changed (should be 8080):

      Note: to get the correct json path you can print the pod information in json format kubectl get pods -o json.
  5. Record the command that used to perform the update (--record)
    Previously when checked the rollout history, the CHANGE-CAUSE was showing <none>.

    You can use the --record flag to save the current kubectl command in the resource annotation. This can be useful to keep track of the commands that triggered to changes.

    Let's repeat the initial deployment and to apply the changes using the --record flag.

    First, let's delete the Deployment:

    Apply the hello-nginx-deployment.yaml file:

    Change the container port to 8080 and apply the hello-nginx-deployment.yaml file:

    If we check the rollout history, we should see now the command that was used to apply the Deployment:

  6. Performing Rollbacks
    Note that the rollback follow the same rolling update strategy behavior defined in the manifest YAML file see (spec.strategy.rollingUpdate.maxUnavailable spec.strategy.rollingUpdate.maxSurge).

    • Let's rollback the latest changes:

    • Validate that the container port was changed (should be 8080):

    • List ReplicaSets:

      Notice that the "old" ReplicaSet was not deleted. Which mean that you can do another rollback to get the container port set again to 8080!

      As mentioned before, if we set the replicas to 10, then when we run the command kubectl get deployment hello-nginx we should see that the value of the READY column is transiting between 8/10 to 12/10, and the value of the AVAILABLE column is transiting between 8 to 10 (see the fields spec.strategy.rollingUpdate.maxUnavailable and spec.strategy.rollingUpdate.maxSurge).
  7. Performing Rollbacks ("--to-revision")
    • To rollback to a specific revision your can use the --to-revision flag:

    • Check the rolling update history:
  8. Delete Deployments
    To delete a Deployment using its manifest file:

    To delete a Deployment using its name:
© 2025  mtitek