Skip to main content
In this lesson, we will create a Kubernetes deployment by leveraging an existing ReplicaSet definition. This approach helps streamline the process by reusing a familiar structure.

Step 1: Set Up the Deployment Directory

First, navigate to your project directory and create a new folder called deployments. Inside this folder, create a file named deployment.yaml.

Step 2: Review the ReplicaSet Definition

For reference, open the existing ReplicaSet definition on the right side of your split editor:
Review this ReplicaSet definition carefully, as you will reuse its structure when defining your new deployment.

Step 3: Create the Deployment Configuration

In your new deployment.yaml file, start with the apiVersion (apps/v1) as used in the ReplicaSet. Set the kind to Deployment, and then define the metadata with a unique name and appropriate labels. In our example, we use the name myapp-deployment with labels like tier: frontend and app: nginx. For the spec section, copy the structure from the ReplicaSet but modify the configuration to meet the deployment requirements. In this case, we reduce the number of replicas from four to three. Below is the final configuration for your deployment:
The deployment uses the same selector as the ReplicaSet, matching on app: myapp. This ensures that the deployment’s pods are correctly managed.

Step 4: Reference the Original ReplicaSet Definition

For comparison, here is the unchanged ReplicaSet definition saved in replicaset.yaml:

Step 5: Deploy the Configuration to Your Cluster

Save your deployment configuration and create the deployment by running the following command in your terminal:
After creating the deployment, verify it with:
The output should resemble:

Step 6: Validate the Pods

To inspect the pods created by the deployment, execute:
Expected output:
For more detailed information about your deployment, use:
This command provides comprehensive details on metadata, pod specifications, and events. You will see that the deployment uses the same selector (app: myapp), ensuring three desired and three available pods are running.

Step 7: Review All Cluster Objects

Finally, run the following command to list all objects created in the cluster:
A sample output might look like this:
This output confirms that both the deployment and its corresponding ReplicaSet have been successfully created, with the expected pods up and running.
You have now successfully deployed your application using Kubernetes Deployments. Experiment with these configurations in your practice environment for a deeper understanding of Kubernetes object management.

Additional Resources

Watch Video

Practice Lab