Essential kubectl commands for beginners
Are you new to Kubernetes and wondering how to start working with it using the command line interface? Then you've come to the right place! In this article, we'll go over some essential kubectl
commands that every beginner should know.
But before we start, let's briefly go over what kubectl
is and what it does.
What is kubectl?
kubectl
is a command line tool that is used to interact with Kubernetes clusters. It allows you to create, update, and delete various Kubernetes resources, such as pods, deployments, services, and more. kubectl
is an essential tool for anyone who wants to manage Kubernetes clusters effectively.
Now that we've got that out of the way, let's dive into some essential kubectl
commands.
1. kubectl create
The create
command is used to create various Kubernetes resources, such as pods, deployments, services, and more. When using the create
command, you typically provide a YAML or JSON configuration file that describes the resource you want to create. For example, the following command creates a nginx
deployment with three replicas:
$ kubectl create deployment nginx --image=nginx --replicas=3
2. kubectl get
The get
command is used to retrieve information about various Kubernetes resources. When using the get
command, you typically specify the kind of resource you want to retrieve, such as pods, deployments, services, and more. For example, the following command retrieves information about all pods in the current namespace:
$ kubectl get pods
3. kubectl describe
The describe
command is used to retrieve detailed information about a particular Kubernetes resource. When using the describe
command, you typically provide the name of the resource you want to describe. For example, the following command retrieves detailed information about a pod named my-pod
:
$ kubectl describe pod my-pod
4. kubectl apply
The apply
command is used to apply changes to various Kubernetes resources. When using the apply
command, you typically provide a YAML or JSON configuration file that describes the changes you want to make to the resource. For example, the following command applies changes to a deployment named nginx
:
$ kubectl apply -f nginx-deployment.yaml
5. kubectl delete
The delete
command is used to delete various Kubernetes resources. When using the delete
command, you typically provide the name of the resource you want to delete. For example, the following command deletes a deployment named nginx
:
$ kubectl delete deployment nginx
6. kubectl logs
The logs
command is used to retrieve the logs of a container running in a pod. When using the logs
command, you typically provide the name of the pod and the name of the container you want to retrieve logs for. For example, the following command retrieves the logs of a container named my-container
in a pod named my-pod
:
$ kubectl logs my-pod my-container
7. kubectl exec
The exec
command is used to execute a command in a container running in a pod. When using the exec
command, you typically provide the name of the pod and the name of the container you want to execute the command in, as well as the command itself. For example, the following command executes the ls
command in a container named my-container
in a pod named my-pod
:
$ kubectl exec my-pod -c my-container -- ls
8. kubectl port-forward
The port-forward
command is used to forward a port from a local machine to a pod running in a Kubernetes cluster. When using the port-forward
command, you typically provide the name of the pod and the local port and the port in the pod you want to forward. For example, the following command forwards port 8080 from the local machine to port 80 in a pod named my-pod
:
$ kubectl port-forward my-pod 8080:80
9. kubectl rollout
The rollout
command is used to manage rolling updates for deployments. When using the rollout
command, you can view the status of a rollout, pause and resume a rollout, and roll back to a previous version of a deployment. For example, the following command views the status of a rollout for a deployment named nginx
:
$ kubectl rollout status deployment/nginx
10. kubectl scale
The scale
command is used to scale up or down the number of replicas in a deployment. When using the scale
command, you typically provide the name of the deployment and the number of replicas you want to set. For example, the following command scales up a deployment named nginx
to 5 replicas:
$ kubectl scale deployment nginx --replicas=5
Conclusion
These are just some essential kubectl
commands that every beginner should know. Of course, there are many other useful kubectl
commands and options that you can explore. I hope this article has helped you get started with kubectl
and Kubernetes!
If you want to learn more about kubectl
and Kubernetes, be sure to check out the official Kubernetes documentation and other resources online. Happy learning!
Editor Recommended Sites
AI and Tech NewsBest Online AI Courses
Classic Writing Analysis
Tears of the Kingdom Roleplay
Prompt Catalog: Catalog of prompts for specific use cases. For chatGPT, bard / palm, llama alpaca models
Hybrid Cloud Video: Videos for deploying, monitoring, managing, IAC, across all multicloud deployments
Training Course: The best courses on programming languages, tutorials and best practice
Dev best practice - Dev Checklist & Best Practice Software Engineering: Discovery best practice for software engineers. Best Practice Checklists & Best Practice Steps
Idea Share: Share dev ideas with other developers, startup ideas, validation checking