Kubernetes (K8s) is a powerful platform for managing containerized applications. Familiarity with essential Kubernetes commands can significantly improve your productivity and help you troubleshoot issues more effectively. Below, we’ll cover 20 of the most important Kubernetes commands, along with simple explanations.
1.kubectl get
Use this command to list resources. You can specify the resource type (like pods, services, deployments) to get a concise view of what's running.
kubectl get pods
2.kubectl describe
This command provides detailed information about a specific resource, including events, status, and configuration.
kubectl describe pod <pod-name>
3.kubectl create
Use this command to create a resource from a file or directly in the command line.
kubectl create -f <file.yaml>
4.kubectl apply
This command updates a resource by applying changes from a file. It can create the resource if it doesn't exist.
kubectl apply -f <file.yaml>
5.kubectl delete
Use this command to delete a resource. You can specify the resource type and name.
kubectl delete pod <pod-name>
6.kubectl logs
This command retrieves logs from a specified pod, which is useful for debugging.
kubectl logs <pod-name>
7.kubectl exec
Use this command to execute a command inside a running container within a pod.
kubectl exec -it <pod-name> -- /bin/bash
8.kubectl port-forward
This command allows you to forward a local port to a port on a pod, which is useful for accessing services locally.
kubectl port-forward <pod-name> <local-port>:<pod-port>
9.kubectl scale
Use this command to scale a deployment up or down by specifying the desired number of replicas.
kubectl scale deployment <deployment-name> --replicas=<number>
10.kubectl rollout
This command manages the rollout of a resource, such as deploying a new version or rolling back to a previous version.
kubectl rollout status deployment <deployment-name>
11.kubectl get nodes
This command lists all the nodes in your Kubernetes cluster, showing their status and roles.
kubectl get nodes
12.kubectl get services
Use this command to list all services in the cluster, which helps you understand how different parts of your application communicate.
kubectl get services
13.kubectl get deployments
This command lists all deployments, providing an overview of your application’s desired state.
kubectl get deployments
14.kubectl top
This command shows resource usage (CPU and memory) of nodes or pods, which is helpful for monitoring performance.
kubectl top pods
15.kubectl config
This command allows you to modify kubeconfig settings, such as changing the current context.
kubectl config current-context
16.kubectl namespace
Use this command to manage namespaces in your Kubernetes cluster, which helps organize resource
kubectl create namespace <namespace-name>
17.kubectl cp
This command copies files between your local filesystem and a pod.
kubectl cp <local-file-path> <pod-name>:<remote-file-path>
18.kubectl get events
This command lists events in your cluster, which can help you diagnose issues by showing what’s happening behind the scenes.
kubectl get events
19.kubectl edit
Use this command to edit a resource in your default editor, allowing for quick changes without creating a new file.
kubectl edit deployment <deployment-name>
20.kubectl api-resources
This command lists all API resources available in your Kubernetes cluster, helping you discover new types you might want to use.
kubectl api-resources
Conclusion
Understanding these Kubernetes commands will enhance your ability to manage and troubleshoot your applications in a Kubernetes environment. Whether you’re a beginner or an experienced user, these commands are vital for navigating the complexities of container orchestration. Happy K8s-ing!