Photo by Growtika / Unsplash

Kubernetes - kubectl Commands

kubernetes Apr 4, 2023

Kubectl is a command-line tool used for managing Kubernetes clusters. It allows developers to deploy, inspect, and manage applications running in Kubernetes, as well as manage the Kubernetes cluster itself. Kubectl is a powerful tool that can help developers and system administrators streamline their workflow and automate common tasks.

In this blog post, we will explore some of the most commonly used kubectl commands and their functionality. Whether you are new to Kubernetes or a seasoned expert, understanding these commands can help you navigate and manage your Kubernetes clusters with ease. So, let's dive in!

Below listed are the commonly used commands with an example

  • kubectl get pods: This command lists all the running pods in the current namespace. Example: kubectl get pods
  • kubectl describe pod <pod_name>: This command provides detailed information about a specific pod, such as its status, containers, and events. Example: kubectl describe pod my-pod
  • kubectl logs <pod_name>: This command retrieves the logs of a specific pod. Example: kubectl logs my-pod
  • kubectl exec -it <pod_name> -- <command>: This command allows you to execute a command inside a running container in a pod. Example: kubectl exec -it my-pod -- bash
  • kubectl port-forward <pod_name> <local_port>:<container_port>: This command forwards traffic from a local port to a port on a container running in a pod. Example: kubectl port-forward my-pod 8080:80
  • kubectl create -f <file>: This command creates a resource from a YAML or JSON file. Example: kubectl create -f my-deployment.yaml
  • kubectl apply -f <file>: This command applies changes to a resource defined in a YAML or JSON file. Example: kubectl apply -f my-deployment.yaml
  • kubectl delete <resource_type> <resource_name>: This command deletes a resource by its type and name. Example: kubectl delete pod my-pod
  • kubectl scale <resource_type> <resource_name> --replicas=<replica_count>: This command scales a resource to the specified number of replicas. Example: kubectl scale deployment my-deployment --replicas=3
  • kubectl rollout <command>: This command manages the rollout of changes to a deployment. Example: kubectl rollout status deployment/my-deployment

Tags