Kubernetes CLI Commands

kubectl is a command-line tool used to interact with Kubernetes clusters. It allows you to deploy, manage, and troubleshoot applications running in Kubernetes. You can use kubectl to:

  • Create and manage resources: Create, update, and delete resources like pods, services, deployments, and more.
  • View and inspect resources: List resources, describe their details, and see their logs.
  • Scale applications: Adjust the number of replicas for a deployment or stateful set.
  • Execute commands: Run commands inside containers and debug issues.
  • Apply configurations: Apply configuration files to create or update resources.

kubectl is essential for anyone working with Kubernetes, as it provides a way to control and monitor your containerized applications and services.

Here’s a cheat sheet of essential Kubernetes CLI commands (kubectl) for managing Kubernetes clusters, pods, services, and more.

Commonly used Kubernetes CLI Commands

1. Basic Commands

Check cluster version and info:

kubectl version

kubectl cluster-info

View configuration settings:

kubectl config view

Related: What is Kubernetes? Uses, Features, Architecture & Working

2. Working with Contexts

List available contexts:

kubectl config get-contexts

Set a default context:

kubectl config use-context <context-name>

Display the current context:

kubectl config current-context

3. Namespace Management

View all namespaces:

kubectl get namespaces

Switch namespace:

kubectl config set-context --current --namespace=<namespace>

Create a new namespace:

kubectl create namespace <namespace-name>

Delete a namespace:

kubectl delete namespace <namespace-name>

4. Working with Pods

View all pods in a namespace:

kubectl get pods --namespace=<namespace>

Get details of a specific pod:

kubectl describe pod <pod-name>

Watch pods in real-time:

kubectl get pods --watch

Delete a pod:

kubectl delete pod <pod-name>

Execute a command inside a pod:

kubectl exec -it <pod-name> -- <command>

View logs from a pod:

kubectl logs <pod-name>

Related: Top 6 Kubernetes Deployment Strategies

5. Managing Deployments

View deployments:

kubectl get deployments

Create a deployment:

kubectl create deployment <deployment-name> --image=<image-name>

Scale a deployment:

kubectl scale deployment <deployment-name> --replicas=<number>

Update a deployment (e.g., change image):

kubectl set image deployment/<deployment-name> <container-name>=<new-image>

Rollback a deployment:

kubectl rollout undo deployment/<deployment-name>

6. Services and Networking

View services:

kubectl get services

Expose a deployment as a service:

kubectl expose deployment <deployment-name> --type=<service-type> --port=<port>

(Service types: ClusterIP, NodePort, LoadBalancer)

Describe a service:

kubectl describe service <service-name>

Delete a service:

kubectl delete service <service-name>

7. Persistent Volumes (PV) & Persistent Volume Claims (PVC)

View all persistent volumes (PVs):

kubectl get pv

View all persistent volume claims (PVCs):

kubectl get pvc

Describe a persistent volume:

kubectl describe pv <pv-name>

8. ConfigMaps and Secrets

View ConfigMaps:

kubectl get configmaps

Create a ConfigMap from a file:

kubectl create configmap <configmap-name> --from-file=<file-path>

View Secrets:

kubectl get secrets

Create a secret from a file:

kubectl create secret generic <secret-name> --from-file=<file-path>

9. DaemonSets and StatefulSets

View DaemonSets:

kubectl get daemonsets

View StatefulSets:

kubectl get statefulsets

10. Jobs and CronJobs

View Jobs:

kubectl get jobs

View CronJobs:

kubectl get cronjobs

11. Node Management

View nodes in the cluster:

kubectl get nodes

Drain a node for maintenance:

kubectl drain <node-name>

Cordon a node to prevent scheduling new pods:

kubectl cordon <node-name>

Uncordon a node to allow scheduling:

kubectl uncordon <node-name>

12. Troubleshooting

Check cluster events:

kubectl get events

Get cluster node status:

kubectl describe nodes

Debug a pod interactively:

kubectl debug pod/<pod-name> -it --image=<debug-image>

13. Other Useful Commands

Apply changes from a YAML file:

kubectl apply -f <file.yaml>

Delete resources defined in a YAML file:

kubectl delete -f <file.yaml>

Dry-run (check without applying changes):

kubectl apply -f <file.yaml> --dry-run=client

These commands will help you manage your Kubernetes cluster efficiently, troubleshoot issues, and deploy applications.

Leave a Comment

Select your currency
USD United States (US) dollar