20 K8s Commands For DevOps Engineers & SysAdmins Beginners
Kubernetes, often abbreviated as K8s, is a powerful open-source platform for automating the deployment, scaling, and management of containerized applications. It simplifies the complexity of running applications at scale by providing features like load balancing, self-healing, and rolling updates. For developers and system administrators, Kubernetes is an essential tool for managing cloud-native applications.
To get started with Kubernetes, you need to familiarize yourself with the command-line tool kubectl
, which allows you to interact with your Kubernetes clusters. Kubernetes commands are crucial for deploying applications, managing resources, and debugging issues. In this guide, we’ll explore some beginner-friendly Kubernetes commands and their practical uses.
1. Check Cluster Information
- Displays the cluster’s API server details and other essential services.
kubectl cluster-info
Kubernetes control plane is running at https://127.0.0.1:6443
2. View Nodes in the Cluster
- Lists all the nodes in the Kubernetes cluster.
kubectl get nodes
NAME STATUS ROLES AGE VERSION node-1 Ready master 10d v1.26.0
3. Deploy an Application
- Creates a deployment for the
nginx
container.
kubectl create deployment nginx-deployment --image=nginx
deployment.apps/nginx-deployment created
4. Check Pods in the Cluster
- Lists all the pods running in the cluster.
kubectl get pods
NAME READY STATUS RESTARTS AGE nginx-deployment-5d9f98745f-k9xzq 1/1 Running 0 5m
5. Get Deployment Details
- Displays detailed information about the specified deployment.
kubectl describe deployment nginx-deployment
6. Expose a Deployment as a Service
- Creates a service to expose the deployment to external traffic.
kubectl expose deployment nginx-deployment --type=NodePort --port=80
7. Scale a Deployment
- Increases the number of pod replicas for the deployment.
kubectl scale deployment nginx-deployment --replicas=3
deployment.apps/nginx-deployment scaled
8. View Logs of a Pod
- Fetches the logs of a specific pod.
- Replace
<pod-name>
with the actual name of the pod.
kubectl logs <pod-name>
9. Run a Pod Temporarily
- Runs a temporary pod using the
busybox
image.
kubectl run busybox --image=busybox --restart=Never -- sleep 3600
10. Delete Resources
- Deletes the specified deployment and its associated pods.
kubectl delete deployment nginx-deployment
deployment.apps "nginx-deployment" deleted
11. Get Services
- Lists all the services in the cluster.
kubectl get services
12. Get Resource YAML/JSON
- Outputs the YAML configuration of a deployment.
kubectl get deployment nginx-deployment -o yaml
13. Execute a Command Inside a Pod
- Opens an interactive shell in the specified pod.
- Replace
<pod-name>
with the actual name of the pod.
kubectl exec -it <pod-name> -- /bin/bash
14. Apply a YAML File
- Applies the configurations specified in a YAML file.
kubectl apply -f deployment.yaml
15. Check Kubernetes Version
- Displays the client and server versions of Kubernetes.
kubectl version --short
16. View Events in the Cluster
- Lists recent events in the cluster, such as pod creation, scaling, or errors.
kubectl get events
LAST SEEN TYPE REASON OBJECT MESSAGE 30s Normal Scheduled pod/nginx-deployment-5d9xz Successfully assigned pod to node-1 10s Normal Pulling pod/nginx-deployment-5d9xz Pulling image "nginx"
17. Roll Out Updates to a Deployment
- Updates the container image in a deployment to a new version.
kubectl set image deployment/nginx-deployment nginx=nginx:1.21
deployment.apps/nginx-deployment image updated
18. Roll Back a Deployment
- Rolls back the deployment to the previous version in case of issues.
kubectl rollout undo deployment/nginx-deployment
deployment.apps/nginx-deployment rolled back
19. Check the Status of a Rollout
- Displays the status of the deployment rollout.
kubectl rollout status deployment/nginx-deployment
Waiting for deployment "nginx-deployment" to roll out... deployment "nginx-deployment" successfully rolled out
20. Debug a Pod with Port Forwarding
- Forwards a port from the local machine to the pod for testing.
- Replace
<pod-name>
with the actual pod name. - Example: Access the application running in the pod by visiting
http://localhost:8080
in a web browser.
kubectl port-forward <pod-name> 8080:80
Conclusion
Kubernetes is an essential tool for managing containerized applications at scale, and mastering its basic commands is the first step toward leveraging its full potential. The commands provided above are beginner-friendly and help you interact with your cluster effectively, from deploying applications to managing resources and debugging issues.
As you grow more comfortable with these commands, you can explore advanced features like custom resource definitions, Helm charts, and networking configurations.