10 kubectl command examples to work with Kubernetes in Windows and Linux
1. kubectl command to Retrieve details on your nodes
Nodes represent virtual or physical machines that act as worker machines within Kubernetes. They’re governed by the control panel and include the pods and containers needed to run your services.$ kubectl get nodes
Btw, if you are visual learner, here is a nice cheat sheet of all essential kubectl command from AcloudGuru and Linux Academy, something worth printing and sticking to your desk. I always look this kubectl commands while working with Kubernetes pods like deploying services, starting or stopping services like scaling down to zero instance as well as while changing the config values on ConfigMap.
2. kubectl command to List all running pods in a namespace
Pods are the smallest deployable units of computing that you can create and manage in Kubernetes. One pod contains one running process in your cluster, so pod counts can increase dramatically as workloads increase.
Accordingly, pods are deleted when they’re no longer needed or when a
process is completed. Because pods are so crucial, tracking which ones are
running can help us better understand active processes and perhaps dig into
active services. Enter this command:
$ kubectl get pods --field-selector=status.phase=Running
3. kubectl command to understand your cluster services
Your cluster contains nodes, pods, and containers—and ultimately the services running atop your infrastructure. It’s not uncommon for numerous services to exist in a cluster, and these may become harder to track over time.
Kubernetes is also inherently network-based, since service instances are
assigned IP addresses throughout their life cycles. The same goes for other
resources. To display endpoint information (name, resource type, etc.) for
your masters and services, type this simple command:
$ kubectl cluster-info
In general, its hard to remember the kubectl commands, at least for me, as Its been more than an year I have been using kubectl on regular basis but I can not type them without looking at my notes. It's getting better but I think still a long way to go.
Also, most of kubectl commands follow a pattern so, if you remember that pattern, its slightly easier to remember and use those kubectl commands, here is the kubectl command pattern which you should know
4. kubectl command to Leverage your files to configure Kubernetes
While some changes and settings are easily applicable within Kubernetes through commands alone, others require external configuration files for reference. While it’s possible to use the STDIN method instead, Kubernetes tends to recommend that you use either a JSON or YAML file for these configurations. To leverage a config file and alter your resources, use the following command:$ kubectl apply -f config.yaml
5. kubectl command to Request or view your application or service logs
Logs are chock full of information that tell you how your services are running, which notable events occurred, and at what times these events took place. These human-readable lists of details can help you retrospectively investigate (and later fix) any outstanding deployment issues.$ kubectl logs -f <service_name>
6. kubectl command to see Kuberentes secrets
Secrets are the passwords, credentials, keys, and more that help your services (and Kubernetes) run effectively at any given time. Without this data, proper authentication and authorization are impossible. Managing these secrets is essential—which is tough if you don’t know them. Use the following command to fetch a list of all Kubernetes secrets:$ kubectl get secrets
7. kubectl command for keeping track of events
Kubernetes events tell you when resources undergo state changes, encounter errors, or need to broadcast system-wide messages for whatever reason. These objects provide a record of any notable activity that raises eyebrows in Kubernetes. Summon a list of all resource-based events with this quick command:$ kubectl get events
8. kubectl command to use new DaemonSets
Kubernetes DaemonSets ensure that all specific nodes run at least one copy of a pod. Because of this, DaemonSets can help control distributed processes across your system. It’s possible to then run sidecar services (storage, logs collection, monitoring) within those nodes to boost observability.$ kubectl create daemonset <daemonset_name>
9. kubectl command for Displaying the State of Resources
To display the state of any number of resources in detail, use the kubectl describe command. By default, the output also lists uninitialized resources.View details about a particular node:
$ kubectl describe nodes [node-name]
View details about a particular pod:
$ kubectl describe pods [pod-name]
10. kubectl command for Applying and Updating a Resource
To apply or update a resource use the kubectl apply command. The source in this operation can be either a file or the standard input (stdin).Create a new service with the definition contained in a [service-name].yaml file:
$ kubectl apply -f [service-name].yaml
Create a new replication controller with the definition contained in a
[controller-name].yaml file:
$ kubectl apply -f [controller-name].yaml
Create the objects defined in any .yaml, .yml, or .json file in a
directory:
$ kubectl apply -f [directory-name
11. kubectl command to create a new namespace with a unique name
We’ve touched on the importance of namespaces when it comes to organizing Kubernetes resources. While managing Kubernetes at scale, it’s common for resources to multiply—either through daily activity, or out of necessity to better maintain the system.
You might need to create new namespaces to keep Kubernetes tidy and
well-configured. Use this command to create a new namespace. Name it
whatever you’d like (as long as that name isn’t already taken):
$ kubectl create ns hello-there
12. kubectl command to run a command in existing pod
If
you want to run a command inside a pod without actually logging in to the
pod, then you need to use below
kubectl exec <pod_name> -- <commands>
command. Here I am trying to check the size of /u01/test directory of
test-pod-0 pod using du -sh command without logging in to the test-pod-0 pod
as shown below.
$ kubectl exec test-pod-0 -- du -sh /u01/test/
935M /u01/test/
That's all about the
12 examples of kubectl command in Linux and Kubernetes. Knowing these
commands will surely put you on another level as far as Linux and Kubernetes
is concerned but don't worry, all these commands also works in Windows if
you have installed kubectl utility. You must be a notch higher now
compared to other experts who are not aware of the above-mentioned commands.
With everything in the palm of your hands, you are good to go. Make good use
of it.
Other Linux, Docker, and Kubernetes articles you may like to explore
- 10 Best Linux Courses for Programmers and Developers (Courses)
- My favorite tips to work fast in Linux (tips)
- How to get an IP address from the hostname and vice-versa in Linux (command)
- 5 Best courses to learn Bash Scripting (courses)
- 10 examples of the xargs command in Linux (examples)
- 5 Free Courses to learn Linux for Beginners (Free courses)
- 10 examples of date command in Linux (examples)
- How to create, update and delete soft link in UNIX (command)
- 10 examples of Vim in UNIX (examples)
- My Favorite Courses to learn VIM Editor in-depth (courses)
- 10 examples of cut command in Linux (examples)
- 5 examples of sort command in Linux (examples)
- 6 Free Courses to learn Bash scripting in-depth (free courses)
- 5 examples of kill command in Linux (examples)
- 10 Books every Linux Power user should read (books)
Thanks for reading this article so far. If you like these
essential kubectl commands for both developers and DevOps to effectively
work with Kubernetes pods then please share them with your friends and colleagues. If you have
any questions or feedback then please drop a note.
P. S. - If you are new to Kubernetes and looking for a best online
course to learn essential Kubernetes commands and concepts then I also
suggest you check out these best Docker and Kubernetes courses for experienced developers. It contains nice collection of kubernetes courses, and tutorial to learn
not just kubectl commands but also essential Kubernetes architecture and
concepts.
No comments :
Post a Comment