DEV Community

Cover image for Mastering kubectl: Your Gateway to Kubernetes
Shiva Shanmugam
Shiva Shanmugam

Posted on

Mastering kubectl: Your Gateway to Kubernetes

When working with Kubernetes, kubectl is your essential command-line tool. It helps you deploy applications, inspect cluster resources, debug issues, and more.

What is kubectl?

kubectl is the CLI tool for interacting with Kubernetes clusters. It allows you to deploy applications, manage resources, and view logs efficiently.

Basic Structure

kubectl <command> <type> <name> [flags]
Enter fullscreen mode Exit fullscreen mode
  • command: The operation to perform (get, describe, create, delete, apply, etc.)
  • type: Resource type (pods, deployments, services, etc.)
  • name: Resource name (optional)

Common Resource Types

  • pods
  • deployments
  • services
  • nodes
  • configmaps
  • secrets
  • namespaces
  • ingress

Essential kubectl Commands & Use Cases

1. Check Cluster Info

kubectl cluster-info
Enter fullscreen mode Exit fullscreen mode

Use Case: Verify cluster connection and endpoints.

2. List All Resources

kubectl get all
Enter fullscreen mode Exit fullscreen mode

Use Case: Get a quick overview of all resources in the current namespace.

3. Get Specific Resources

kubectl get pods
kubectl get deployments
kubectl get services
Enter fullscreen mode Exit fullscreen mode

Use Case: Monitor resource status.

4. View Detailed Resource Info

kubectl describe pod <pod-name>
Enter fullscreen mode Exit fullscreen mode

Use Case: Debug failed pods and view event logs.

5. Apply Configuration (YAML)

kubectl apply -f <file>.yaml
Enter fullscreen mode Exit fullscreen mode

Use Case: Deploy apps or update configurations using manifest files.

6. Delete Resources

kubectl delete pod <pod-name>
kubectl delete -f <file>.yaml
Enter fullscreen mode Exit fullscreen mode

Use Case: Remove unwanted or failed resources.

7. Logs and Debugging

kubectl logs <pod-name>
kubectl logs <pod-name> -c <container-name>
Enter fullscreen mode Exit fullscreen mode

Use Case: Debug container issues.

8. Execute Commands in a Pod

kubectl exec -it <pod-name> -- /bin/bash
Enter fullscreen mode Exit fullscreen mode

Use Case: Access a container shell for troubleshooting.

9. Port Forwarding

kubectl port-forward svc/my-service 8080:80
Enter fullscreen mode Exit fullscreen mode

Use Case: Access internal services locally.

10. Scaling Deployments

kubectl scale deployment <deployment-name> --replicas=3
Enter fullscreen mode Exit fullscreen mode

Use Case: Adjust the number of replicas based on load.

11. View Events

kubectl get events
Enter fullscreen mode Exit fullscreen mode

Use Case: Monitor recent cluster activities.

12. Switch Context / Namespace

kubectl config use-context <context-name>
kubectl config set-context --current --namespace=<namespace>
Enter fullscreen mode Exit fullscreen mode

Use Case: Manage multiple clusters or focus on a specific namespace.

Bonus Tips

Shortcuts:

  • po = pods
  • svc = services
  • dep = deployments
  • ns = namespaces

Example:

kubectl get po
kubectl get svc
Enter fullscreen mode Exit fullscreen mode

Watch Mode:

kubectl get pods -w
Enter fullscreen mode Exit fullscreen mode

Use Case: Continuously monitor status changes in real-time.


kubectl is a powerful tool that gives you control over your Kubernetes cluster. Mastering it will make managing containerized applications much more efficient.

HappyReading!

Top comments (0)