12 Kubectl command examples in Kubernetes on Windows and Linux
Hi everyone, if you are working with Kubernetes pods, like deploying your application, starting and stopping them by scaling to zero instances, and you just want to view logs or want to log into the Kubernetes pod itself, you must know how to use kubectl
the kubectl command to accomplish all these tasks. Since most of the companies, big or small, are using Kubernetes to deploy services on the cloud, it is imperative that Java developers and other software developers are familiar with the kubectl command. We deploy our Java microservices on Kubernetes mainly because Kubernetes can automatically start the services in case of downtime. We have different Kubernetes clusters for different environments like DEV, UAT, and PRODUCTION, and I often use kubectl commands to switch between these clusters to check the status of our Java services or interact with them.
Every day, I use kubectl commands like get pods to check if my service is running or get a service to find out the port it is listening on, especially when your service is using the ingress feature of Kubernetes. Since many people have asked about sharing more information about Kubernetes, we wrote this article about 10 essential kubectl commands every Java developer should learn.
The list includes kubectl
commands for retrieving nodes, finding pods, and inspecting services. It also includes kubernetes commands for viewing logs for services running on Kubernetes pods.
All these commands work not only on Linux but also on Windows if we have installed kubectl cluster
on our machine kubectl
and set it up in the configuration file
12 kubectl command examples for using Kubernetes in Windows and Linux
Have you ever had a problem understanding the kubectl command examples in Linux? You must have had a hard time when you came here. You should know the latest about these commands. This article will help you achieve that as listed below are 10 kubectl command examples in Linux. Take a closer look at them.
1. kubectl command to retrieve detailed information of the node
Nodes represent virtual or physical machines that act as worker machines in Kubernetes. They are managed by the control plane and include the pods and containers required to run services.
Understanding the status, readiness, and timing of these nodes can reveal a lot about what’s happening with your deployment. 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 is helpful to know which nodes are which. Use this command to get the overall status of the nodes:
$ kubectl get nodes
2. The kubectl command lists all running pods in the namespace
Pod is the smallest deployable computing unit that we can create and manage in Kubernetes. A pod contains a running process in the cluster, so the number of pods will increase dramatically as the workload increases.
Therefore, when the Pod is no longer needed or the process completes, the Pod will be deleted. Since Pods are very important, tracking which Pods are running can help us better understand active processes and potentially gain insight into active services. Enter this command:
$ kubectl get pods --field-selector=status.phase=Running
3. Use kubectl to understand your cluster services
Our clusters consist of nodes, pods, and containers — ultimately services running on top of our infrastructure. It’s not uncommon to have a large number of services in a cluster, which can become harder to keep track of over time.
Kubernetes is also inherently network-based, as service instances are assigned IP addresses throughout their lifecycle. The same is true for other resources. To display the endpoint information (name, resource type, etc.) of our master node and services, type the following simple command:
$ kubectl cluster-info
In general, it's hard to remember kubectl
commands, at least for me since I've been using kubectl regularly for over a year now, but I can't type them without looking at my notes. It's getting better, but I think there's still a long way to go.
Also, most of the kubectl commands follow a pattern, so if you remember that pattern, it will be slightly easier to remember and use those kubectl commands. Here are the kubectl command patterns you should know.
4. The kubectl command uses your files to configure Kubernetes
While some changes and settings can be easily applied in Kubernetes with just commands, others require external configuration files to reference. While it is possible to use the config method instead, Kubernetes prefers to recommend that we use JSON or YAMLSTDIN
files for these configurations . To utilize the configuration files and make changes to our resources, use the following command:
$ kubectl apply -f config.yaml
5. kubectl command to request or view our application or service logs
Logs are filled with information that tells us how our services are running, what notable events have occurred, and when those events occurred. This list of human-readable details can help us retrospectively investigate (and later fix) any unresolved deployment issues.
Services often generate large log files; what if a service is down or behaving erratically? Use this command to help troubleshoot:
$ kubectl logs -f <service_name>
6. Use the kubectl command to view Kuberentes secrets
secrets
Secrets are passwords, credentials, keys, etc. that help our services (and Kubernetes) run efficiently at any given time. Without this data, proper authentication and authorization would be impossible. Managing these secrets is essential - it would be difficult if we didn't know about them. Get a list of all Kubernetes secrets with the following command:
$ kubectl get secrets
7. kubectl command is used to track events
Kubernetes 事件会告诉我们资源何时发生状态更改、遇到错误或出于任何原因需要广播系统范围的消息。 这些对象提供了在 Kubernetes 中引起注意的任何显着活动的记录。 使用此快速命令调用所有基于资源的事件的列表:
$ kubectl get events
8. kubectl 命令使用新的 DaemonSets
Kubernetes DaemonSets 确保所有特定节点至少运行一个 pod 副本。 因此,DaemonSets 可以帮助控制整个系统的分布式进程。 然后可以在这些节点内运行边车服务(存储、日志收集、监控)以提高可观察性。
DaemonSet 非常高效。 我们可以利用 DaemonSets 来提供我们的 Kubernetes 本机监控解决方案。 使用此命令创建一个新的、命名为 DaemonSet:
$ kubectl create daemonset <daemonset_name>
9. 用于显示资源状态的 kubectl 命令
要详细显示任意数量资源的状态,请使用 kubectl describe
命令。 默认情况下,输出还列出未初始化的资源。
查看有关特定节点的详细信息:
$ kubectl describe nodes [node-name]
查看有关特定 pod 的详细信息:
$ kubectl describe pods [pod-name]
10. 用于应用和更新资源的 kubectl 命令
要应用或更新资源,请使用 kubectl apply
命令。 此操作中的源可以是文件或标准输入 (stdin)。
使用 [service-name].yaml
文件中包含的定义创建新服务:
$ kubectl apply -f [service-name].yaml
使用 [controller-name].yaml
文件中包含的定义创建一个新的复制控制器:
$ kubectl apply -f [controller-name].yaml
在目录中的任何 .yaml
、.yml
或 .json
文件中创建定义的对象:
$ kubectl apply -f [directory-name
11. kubectl 命令创建具有唯一名称的新命名空间
在组织 Kubernetes 资源时,我们已经谈到了名称空间的重要性。 在大规模管理 Kubernetes 时,资源成倍增加是很常见的——无论是通过日常活动,还是出于更好地维护系统的需要。
我们可能需要创建新的命名空间以保持 Kubernetes 整洁和配置良好。 使用此命令创建一个新的命名空间。 随意命名(只要该名称尚未被使用):
$ kubectl create ns hello-there
12. kubectl 命令在现有 pod 中运行命令
如果你想在 pod 中运行一个命令而不实际登录到 pod,那么你需要使用下面的 kubectl exec <pod_name> -- <commands>
命令。 在这里,我尝试使用 du -sh
命令检查 test-pod-0 pod 的 /u01/test 目录的大小,而无需登录到 test-pod-0 pod,如下所示。
$ kubectl exec test-pod-0 -- du -sh /u01/test/
935M /u01/test/
这就是 Linux 和 Kubernetes 中 kubectl
命令的 12 个示例。 了解这些命令肯定会让你在 Linux 和 Kubernetes 方面处于另一个层次,但不要担心,如果你安装了 kubectl
实用程序,所有这些命令也适用于 Windows。 你现在一定比其他不知道上述命令的高手高了一个档次。 一切尽在掌握,一切顺利。 好好利用它。
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Restart PostgreSQL in Ubuntu 18.04
Publish Date:2025/04/09 Views:72 Category:PostgreSQL
-
This short article shows how to restart PostgreSQL in Ubuntu. Restart PostgreSQL Server in Ubuntu You can restart Postgres server in Ubuntu using the following command. Order: sudo service postgres restart Sometimes the above command does n
Issues to note when installing Apache on Linux
Publish Date:2025/04/08 Views:78 Category:OPERATING SYSTEM
-
As the most commonly used web server, Apache can be used in most computer operating systems. As a free and open source Unix-like operating system, Linux and Apache are a golden pair. This article will introduce the installation and use of A
How to decompress x.tar.xz format files under Linux
Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM
-
A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr
Summary of vim common commands
Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM
-
In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme
Detailed explanation of command return value $? in Linux
Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM
-
? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re
Common judgment formulas for Linux script shell
Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM
-
In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –
Shell script programming practice - specify a directory to delete files
Publish Date:2025/04/08 Views:98 Category:OPERATING SYSTEM
-
Usually, in Linux system we need to frequently delete some temporary files or junk files. If we delete them one by one manually, it will be quite troublesome. I have also been learning shell script programming recently, so I tried to write
Use of Linux command at - set time to execute command only once
Publish Date:2025/04/08 Views:158 Category:OPERATING SYSTEM
-
This article mainly involves a knowledge point, which is the atd service. Similar to this service is the crond service. The functions of these two services can be similar to the two functional functions of javascript. Those who have learned
Use of Linux command crontab - loop execution of set commands
Publish Date:2025/04/08 Views:170 Category:OPERATING SYSTEM
-
Compared with at , which executes a command only once, crontab, which we are going to talk about in this article, executes the set commands in a loop. Similarly, the use of crontab requires the support of the crond service. The service is s