Kubernetes Commands 101 📝📝
Basic kubectl commands you must know to start your k8s Journey
Introduction 🚩🚩
Hi fellow Readers 👋 :))
I have been writing about the basics of Kubernetes and it’s objects for a while now. The road ahead in the world of Kubernetes will be more advanced one. Topics will range from the concepts of Scheduling to Networking.
What I believe is before moving to those topics, we must learn about the basic Kubernetes commands first. I have discussed about some of them in my previous articles till now, but I thought it would be a good idea to collect and dump them in one single place :))
So In this article, we will try to learn about :
- What is kubectl ??
- Basic kubectl commands.
- Imperative vs Declarative way of managing a Kubernetes cluster.
It will be a very short and crisp article.
So without any further delay, lets get started with it :))
What is kubectl ?? 🤔🤔
Kubernetes provides a command line tool for communicating with a Kubernetes cluster’s control plane, using the Kubernetes API. This tool is named kubectl .
Kubectl commands adhere to syntax or a common structure, which lets administrators to read and verify every kubectl command entered in the terminal window. There are four important parameters to each kubectl call.
kubectl <command> <type> <name> <flags>
kubectl is the common CLI tool that we use to query and manage a Kubernetes cluster.
kubectl uses the API interface of Kubernetes to view, control, and manage the cluster. It is supported across different platforms and can be easily set up to manage a cluster.
Basic kubectl Commands🤓🤓
- Cluster Management
kubectl cluster-info
Display endpoint information regarding the services and master in the clusterkubectl version
Show the Kubernetes version functioning on the client and serverkubectl config view
Get the configuration of the clusterkubectl api-resources
Make a list of the available API resourceskubectl api-versions
Make a list of the available API versionskubectl get all –all-namespaces
List everything
- Node operations
kubectl get node
List one or more nodeskubectl delete node <node_name>
Delete a node or multiple nodeskubectl top node
Display Resource usage (CPU/Memory/Storage) for nodeskubectl describe nodes | grep Allocated -A 5
Resource allocation per nodekubectl get pods -o wide | grep <node_name>
Pods running on a nodekubectl annotate node <node_name>
Annotate a nodekubectl cordon node <node_name>
Mark a node as unschedulablekubectl uncordon node <node_name>
Mark node as schedulablekubectl drain node <node_name>
Drain a node in preparation for maintenancekubectl label node
Add the labels of one or more nodes
- Namespaces [Shortcode = ns]
kubectl create namespace <namespace_name>
Create namespace <name>kubectl describe namespace <namespace_name>
Show the detailed condition of one or more namespacekubectl delete namespace <namespace_name>
Delete a namespacekubectl edit namespace <namespace_name>
Edit and modify the namespace’s definitionkubectl top namespace <namespace_name>
Display Resource (CPU/Memory/Storage) usage for a namespace
- Deployments
kubectl get deployment
List one or more deploymentskubectl describe deployment <deployment_name>
Show the in-depth state of one or more deploymentskubectl edit deployment <deployment_name>
Edit and revise the definition of one or more deployment on the serverkubectl create deployment <deployment_name>
Generate one a new deploymentkubectl delete deployment <deployment_name>
Delete deploymentskubectl rollout status deployment <deployment_name>
Check the rollout status of a deployment
- Replication Controllers [Shortcode = rc]
kubectl get rc
Make a list of the replication controllerskubectl get rc –namespace=<namespace_name>
Make a list of the replication controllers by namespace
- ReplicaSets [Shortcode = rs]
kubectl get replicasets
List ReplicaSetskubectl describe replicasets <replicaset_name>
Show the detailed state of one or more ReplicaSetskubectl scale –replicas=[x]
Scale a ReplicaSet [x is a number here]
- Listing Resources
kubectl get namespaces
Create a plain-text list of all namespaceskubectl get pods
Create a plain-text list of all podskubectl get pods -o wide
Create a comprehensive plain-text list of all podskubectl get pods–field-selector=spec. nodeName=[server-name]
Create a list of all pods functioning on a certain node serverkubectl get replicationcontroller [replication-controller-name]
In plain text, make a lst a specific replication controllerkubectl get replicationcontroller, services
Generate a plain-text list of all replication services and controllers
- Logs
kubectl logs <pod_name>
Print the logs for a podkubectl logs –since=1h <pod_name>
Print the logs for a pod for the last hourkubectl logs –tail=20 <pod_name>
Get the current 20 lines of logskubectl logs -f <service_name> [-c <$container>]
Get logs from a service and choose which container optionallykubectl logs -f <pod_name>
Adhere to new logs and print the logs for a podkubectl logs -c <container_name> <pod_name>
For a container in a pod, Print the logskubectl logs <pod_name> pod.log
Output the logs for a pod into a ‘pod.log’ filekubectl logs –previous <pod_name>
View the logs for the last failed pod
In this section, we have seen the list of most basic kubectl commands which are used while administrating a Kubernetes Cluster.
Different ways of managing a Kubernetes cluster 💭 💭
Imperative Way :
In simple words, it is a way in which we tell exactly what has to be done and how.
For example if we have to create or delete anything in Kubernetes cluster, we use those commands and tell exactly we want to do this.
kubectl delete deployment/nginxkubectl create service nodeport
Declarative Way :
In simple words, it is a way in which we tell what has to be done but do not tell how. Kubernetes itself detects the things it has to do seeing the changes done in the configuration files.
For example if we have to create or update anything in Kubernetes cluster, we make or update the specific configuration files in the cluster and then use kubectl apply command on those configuration files.
create a deployment yaml file [deployment.yaml]kubectl apply -f deployment.yaml
In above steps, we don’t tell what has to be done rather we use apply command. Kubernetes itself detects the changes done in “deployment.yaml” file and carry out the things it has to do :))
Hope now we are clear about both the Imperative and Declarative approaches of managing a Kubernetes cluster 🙈🙈
What next ? 👀 👀
Thanks a lot for reaching till here! It is the end of this article.
In this article, we have just scratched the basic kubectl commands.
But whatever we have covered is enough to make everyone get started with the K8s journey and no one will face any problems in understanding the basic Kubernetes commands from now onwards.Do clap and follow me 🙈 if you like my writings and want to read more from me in the future :))
In case of any doubts around this article or for some general chit chat, feel free to reach out to me on my social media handles
Twitter — https://twitter.com/ChindaVibhor
LinkedIn — https://www.linkedin.com/in/vibhor-chinda-465927169/
Previous articles written by Me :
I will still keep on coming with new articles covering a bunch of topics I am exploring.
That’s All folks !! Doodles :))