Kubectl Cheatsheet - Most commonly used kubectl commands

Kubectl Cheatsheet - Most commonly used kubectl commands

Hey Folks, In this blog we are gonna talk about some Kubectl commands that are extensively used while working with Kubernetes. This is a sort of Cheat Sheet for anybody who just wants to revise Kubernetes components.


What is Kubectl?

Before we jump into what commands we use, first we need to know what Kubectl is exactly.

Kubectl is a command-line tool used to interact with Kubernetes clusters. It provides a wide range of commands to manage various aspects of the cluster, such as deploying applications, scaling resources, inspecting cluster state, and more.

In layman's terms, Kubectl is a command-line tool used to manage Kubernetes clusters.


Commands

Since we now know what Kubectl is, let's see what commands are used in managing Kubernetes clusters.

1. kubectl get

Used to retrieve information about various Kubernetes resources.

Examples:

  • kubectl get pods: Lists all the pods in the cluster.

  • kubectl get deployments: Lists all the deployments in the cluster.

2. kubectl describe

Describes a specific resource in detail.

Examples:

  • kubectl describe pod <pod_name>: Displays detailed information about a specific pod.

  • kubectl describe service <service_name>: Shows Detailed information about a specific service in the cluster.

3. kubectl create

Creates a resource from a YAML or JSON file.

Example:

  • kubectl create -f deployment.yaml: Creates a deployment using the configuration specified in the deployment.yaml file.

4. kubectl apply

Applies changes to a resource or creates a new resource from a YAML or JSON file.

Example:

  • kubectl apply -f service.yaml: Applies changes or creates a deployment based on the configuration in the service.yaml file.

5. kubectl delete

Deletes a Resource [Crazy I know🤯]

Examples:

  • kubectl delete pod <pod_name>: Deletes a specific pod.

  • kubectl delete deployment <deployment_name>: Deletes a deployment.

6. kubectl scale

Changes the number of replicas for a deployment, replicaset, or statefulset.

Example:

  • kubectl scale deployment <deployment_name> --replicas=3: Scales a deployment to have three replicas.

  • kubectl scale deployment <deployment_name> --replicas=5: Scales a deployment to have 5 replicas.

7. kubectl logs

Prints all the logs of a specific container within a pod.

Example:

  • kubectl logs <pod_name>: Displays the logs of the first container in a pod.

  • kubectl logs nginx: Return snapshot logs from pod nginx with only one container.

8. kubectl exec

Executes a command within a pod.

Example:

  • kubectl exec <pod_name> -- <command>: Executes the specified command within a container of the given pod.

  • kubectl exec 123456-7890 -c ruby-container date: Get output from running 'date' in ruby-container from pod 123456-7890.

9. kubectl port-forward

Forward one or more local ports to a pod.

Example:

  • kubectl port-forward <pod_name> <local_port>:<pod_port>: Forwards a local port to the specified pod and port.

  • kubectl port-forward mypod 8888:5000: Listen on port 8888 locally, forwarding to 5000 in the pod.

10. kubectl run

Used to run a particular image on the cluster.

Example:

  • kubectl run nginx --image=nginx: Start a single instance of nginx.

  • kubectl run hazelcast --image=hazelcast --port=5701: Start a single instance of hazelcast and let the container expose port 5701.


More Resources

If you want to learn more about Kubernetes, you can check these resources:

  1. Kubernetes Blog

    %[hashnode.com/post/clclprz44000108lg7v2tdvdg]

  2. Video Explanation on Kubernetes by TechWorld with Nana.

    %[youtube.com/watch?v=X48VuDVv0do&pp=ygUK..


Final words

So this was it for this blog everyone, this was a super quick blog that will help you whenever you want to learn more about Kubectl or just for a quick revision before an interview.

If you liked this blog, A Like would be appreciated and if you want me to cover any other topic related to DevOps you can let me know in the comments or you can reach out to me on Twitter.

See you on the next blog.

Did you find this article valuable?

Support Varchasv Hoon by becoming a sponsor. Any amount is appreciated!