Monday, April 24, 2023

12 Examples of kubectl command in Linux

Hello guys, if you are working with Kubernetes pods like deploying your application, starting and stopping them by scaling to zero instances, while you just want to see logs or want to login into the Kubernetes pods itself, you must know how to use kubectl commands to do all these task. Since most of the companies, both big and small are using Kubernetes to deploy services on cloud, its imperative for Java developers and other Software developers to get familiar with the kubectl command. We deploy our Java Microservices on Kubernetes mainly because Kubernetes can start the services automatically when it goes down. We have different Kubernetes clusters for different environments like DEV, UAT, and PRODUCTION and I often switch between those cluster using kubectl commands to check the status of our Java services or interact with them.

Everyday, I use kubectl commands like get pods to check if my service is running or get services to find out the port its listening, especially if your service is using ingress feature of Kubernetes. Since a lot of you ask about sharing more information on Kubernetes, I decided to write this article about 10 essential kubectl commands every Java developer should learn. 

The list includes kubectl commands to retrieve nodes, find out pods and check services. It also includes kubernetes command to view logs of services running on Kubernetes pods. While I have not shared many more commands like how to edit configMap or how to switch between different Kubernetes cluster, I have shared a nice Kubernetes cheat sheet which contains all those commands, but if you need examples, just ask in commands and I will include them into this article. 

By the way, if you have used Kubernetes then most likely you will be familiar with all these kuberentes commands but if you are new then I also suggest you to start with a nice, updated and comprehensive Kubernetes course as it will save a lot of your time when it comes to learning both kubectl and Kubernetes. If you need recommendations, this kubernetes course article has a couple of nice courses for beginners. 

All these commands not just work in Linux but also on Windows provided you have installed kubectl on your Machine and setup kubectl cluster in your config file, if you face any issue while running these kubectl commands, do ask in comments. 

12 kubectl command examples to work with Kubernetes in Windows and Linux

Have you been having problems knowing examples of kubectl command in Linux? By the time you come here, you must have had a hard time. As a Linux expert, you are supposed to be up to date with such commands. This article is going to help you achieve that because 10 examples of kubectl command in Linux are listed below. Take a close look at them.

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.

Understanding the status, readiness, and age of those nodes can shed light on your deployment’s age. Because Kubernetes can support up to 5,000 nodes per cluster, it’s not uncommon for these nodes to come and go. 

Additionally, using different combinations of worker and master nodes can help optimize system performance. It’s helpful to know which nodes are which. Use this command to grab a node’s overall status:
$ 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

kubectl command cheat sheet

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

10 Examples of Kubectl command in Kubernetes in Windows and Linux

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. 

Services typically generate plenty of log files; what if a service shuts down, or behaves erratically? Use this command to help troubleshoot:

$ 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. 

DaemonSets are extremely efficient. At ContainIQ, we leverage DaemonSets to deliver our Kubernetes native monitoring solution. Use this command to create a new, named DaemonSet:

$ kubectl create daemonset <daemonset_name>

kubectl command to use new DaemonSets

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

. kubectl command for Applying and Updating a Resource

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