Skip to main content
In this lesson, we walk you through a series of hands-on tasks using imperative commands in Kubernetes. These exercises reinforce concepts and help you prepare for your exam by showcasing how to create and manage Kubernetes objects on the fly using the kubectl command-line tool.

Deploying an Nginx Pod

Start by deploying a pod named “nginx-pod” using the nginx Alpine image with the following command:
If successful, your terminal will output something similar to:

Deploying a Redis Pod with Labels

Next, create a Redis pod using the Redis Alpine image and assign it a label (tier=db) to facilitate later operations like service selection:
This command creates the Redis pod with the specified label.
The image shows a Kubernetes task to deploy a Redis pod using the redis:alpine image with specific labels, alongside command-line options for kubectl.

Creating a Redis Service

Expose the Redis pod within the cluster by creating a service called “redis-service” on port 6379. We recommend using the kubectl expose command as it automatically detects pod labels to configure selectors. Run the following command to expose the pod:
This command creates a ClusterIP service that routes traffic to your Redis pod. To verify the service, execute:
The image shows a Kubernetes command-line interface with instructions to create a Redis service using imperative commands, specifying service name, port, and type.

Creating a Deployment for a Web Application

Now, create a deployment named “webapp” with a specified image and scale it to three replicas using the following command:
After deploying, verify that all replicas are running correctly by checking the deployment status.
Ensure you use the correct number of dashes when specifying replica count to avoid syntax errors.

Creating a Custom Nginx Pod with a Specific Port

Create a pod named “custom-nginx” with the nginx image and set up the container to listen on port 8080:
This command configures the container’s port to 8080. Check the pod’s configuration to confirm it is running with the correct port settings.

Creating a New Namespace

To isolate your resources, create a new namespace called “dev-ns”:
You should see a confirmation message similar to:

Creating a Redis Deployment in the “dev-ns” Namespace

Within the “dev-ns” namespace, deploy a Redis application with two replicas by running:
Verify the deployment with:
Expected output:

Creating a Pod and Exposing It as a Service in a Single Command

For the final task, create a pod named “httpd” using the httpd Alpine image and simultaneously expose it as a ClusterIP service on port 80. You can accomplish this in a single step:
This command does the following:
  • Creates a pod named “httpd”.
  • Exposes the pod by creating a corresponding ClusterIP service on port 80.
Verify both the pod and service with:
For a detailed view of the service configuration, run:
The output will include details such as the selector (e.g., “run=httpd”) and the endpoint configurations, ensuring that the service is properly set up to expose the pod on port 80.

Verification Commands

After completing the above tasks, use the following commands to check the status of your pods and services:
  1. To list all pods:
    An example output might look like:
  2. To list all services:
    Sample output:
  3. To describe the httpd service in detail:
    You should see output detailing the service configuration, such as:
This confirms that the “httpd” service is correctly exposing the corresponding pod on port 80.
This concludes the lab on imperative commands for deploying and exposing Kubernetes objects. Each command illustrates a practical approach to managing pods, deployments, and services, providing valuable hands-on experience for your journey toward becoming a certified Kubernetes professional. Happy practicing!

Watch Video