This article provides hands-on tasks using imperative commands in Kubernetes to create and manage objects with the kubectl command-line tool.
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.
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:
Copy
Ask AI
kubectl expose pod redis --port=6379 --name=redis-service
This command creates a ClusterIP service that routes traffic to your Redis pod. To verify the service, execute:
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:
Copy
Ask AI
kubectl run httpd --image=httpd:alpine --port=80 --expose
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:
Copy
Ask AI
kubectl get podkubectl get svc
For a detailed view of the service configuration, run:
Copy
Ask AI
kubectl describe svc httpd
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.
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!