kubectl commands to create and inspect Kubernetes objects. These commands are useful for quick tasks and are commonly asked about in certification exams. Work through each step in order, run the commands shown, and use the verification steps to confirm resources are created and configured correctly.
Helpful links and references:
1) Deploy a Pod named nginx-pod using the nginx:alpine image
Create a simple standalone Pod with kubectl run:
2) Deploy a Redis Pod using the redis:alpine image with label tier=db
Add labels during creation with --labels:
3) Create a ClusterIP Service named redis-service to expose Redis on port 6379
There are two primary imperative approaches for creating Services:
kubectl create service clusterip— useful when you need explicit settings like--tcpmappings or a custom--clusterip. It does not infer selectors from pods automatically.kubectl expose— creates a Service based on an existing resource (pod, deployment, etc.) and infers selectors from that object’s labels. This is usually the preferred approach when exposing an existing labeled object.
Use
kubectl expose when you want the Service to automatically use the source object’s labels as the selector. Use kubectl create service when you need explicit control (for example, custom --clusterip or --node-port).redis Pod has the label tier=db, expose it as a ClusterIP Service named redis-service on port 6379:
kubectl get svc output:
kubectl describe svc redis-service (relevant fields):
4) Create a Deployment named webapp with 3 replicas
Use kubectl create deployment and set the number of replicas:
kubectl get deployment output:
kubectl get pods to see the rollout progress.
5) Create a Pod custom-nginx using the nginx image and set the container port to 8080
Set the container’s port with --port (this populates containerPort in the Pod spec):
6) Create a new namespace devns
Create and verify the namespace:
When running commands in a specific namespace, either include
-n <namespace> or set the namespace in your current context with kubectl config set-context --current --namespace=<namespace>.7) Create a Deployment redis-deploy in namespace devns with 2 replicas
Create the Deployment in the devns namespace and set replicas to 2:
8) Create a Pod httpd (image: httpd:alpine) and a ClusterIP Service of the same name targeting port 80, using the fewest steps
Create both the Pod and a ClusterIP Service in one command by using --expose with kubectl run. --expose creates a ClusterIP service by default and requires --port:
kubectl describe svc httpd (relevant fields):
Summary command reference
That completes the imperative tasks. Use
kubectl get, kubectl describe, and kubectl get -o yaml to inspect resources and confirm labels, selectors, ports, and endpoints for each created object. If a pod or deployment takes time to become ready, wait a few moments and re-run the verification commands.