Skip to main content
In this lesson, we guide you through imperative commands in Kubernetes. Gain practical experience creating pods, services, deployments, and namespaces imperatively—an invaluable exercise for exam preparation and day-to-day operations. ─────────────────────────────

Deploying Pods Imperatively

Deploying an Nginx Pod

Start by deploying an Nginx pod named “nginx-pod” using the nginx:alpine image. Run the following command:
You should see output similar to:
─────────────────────────────

Deploying a Redis Pod with Labels

Next, deploy a Redis pod using the redis:alpine image and assign it the label tier=db. The --labels option accepts key-value pairs and supports multiple labels separated by commas. The image below illustrates deploying a Redis pod with the specified label:
The image shows a Kubernetes task to deploy a Redis pod using the redis:alpine image with the label tier=db, alongside kubectl command examples.
Execute this command:
─────────────────────────────

Creating Services

Exposing the Redis Application

To expose the Redis pod on port 6379, create a ClusterIP service named “redis-service”. Although the command kubectl create service clusterip exists, it does not support specifying selectors. Instead, use the kubectl expose command, which automatically uses the pod’s labels as selectors. For example, run:
Afterwards, verify the service with:
Expected output:
Below are additional examples showcasing various usages of the kubectl expose command:

Understanding kubectl create service clusterip

Running the following command without providing a name will result in an error:
Output:
To create a ClusterIP service named “my-cs” mapping port 5678 to target port 8080, use:
For a headless service, specify:
Since the kubectl create service clusterip command does not allow specifying selectors, using kubectl expose is the recommended approach.
For more details on exposing the Redis application, refer to the image below:
The image shows a Kubernetes command-line interface with instructions to create a service named "redis-service" to expose a Redis application on port 6379 using imperative commands.
─────────────────────────────

Creating a Deployment

Create a deployment named “webapp” using the kodekloud/webapp-color image and then scale it to three replicas. Execute the following commands:
You should see:
Use the command below to check that all three replicas are running:
─────────────────────────────

Creating a Pod with a Specific Container Port

Next, create a pod called “custom-nginx” using the nginx image and configure it to expose container port 8080:
This configures the pod so that the container listens on port 8080. ─────────────────────────────

Managing Namespaces and Deployments

Creating a Namespace

To organize your resources, create a new namespace called “dev-ns”:

Deploying in a Specific Namespace

Within the “dev-ns” namespace, deploy a new deployment named “redis-deploy” using the Redis image, scaled to two replicas:
Verify the deployment using:
Expected output:
─────────────────────────────

Creating a Pod and Exposing It as a Service in One Step

In this task, create a pod named “httpd” using the httpd:alpine image in the default namespace and simultaneously expose it as a ClusterIP service on port 80. The kubectl run command supports the --expose option to automatically create a service. Run the command below:
You should see:
Then, verify that both the pod and its corresponding service have been created:
To view detailed service information, run:
This confirms that the service has the correct selector (e.g., run=httpd), target port (80/TCP), and that endpoints are automatically discovered. For a visual outline of the creation process, refer to the diagram below:
The image shows a Kubernetes task to create a pod and service, with command options and a checklist for verification.
─────────────────────────────

Final Verification

Verify your Kubernetes resources by listing all pods:
Sample output:
Next, verify your services:
Expected output:
Finally, describe the “httpd” service for full configuration details:
This command confirms the service’s selector, ClusterIP, exposed ports, and endpoints. ─────────────────────────────
This concludes our lesson on imperative commands in Kubernetes. By following these steps, you have practiced creating and managing pods, services, deployments, and namespaces using imperative commands. For more detailed information, consider exploring the Kubernetes Documentation to expand your knowledge further.
Imperative commands are great for quick testing and learning; however, for production deployments, consider using declarative configurations for better maintainability.

Watch Video