用 kubectl 管理 Kubernetes 的 5 种有用方法
kubectl

> 
> 学习 kubectl，提升你与 Kubernetes 的互动方式。
> 
> 
> 


![Ship captain sailing the Kubernetes seas](/data/attachment/album/202108/13/105348oxt824fszcyyzj2j.png "Ship captain sailing the Kubernetes seas")


Kubernetes 可以帮你编排运行有大量容器的软件。Kubernetes 不仅提供工具来管理（或者说 [编排](https://opensource.com/article/20/11/orchestration-vs-automation)）运行的容器，还帮助这些容器根据需要进行扩展。有了 Kubernetes 作为你的中央<ruby> 控制面板 <rt>  control panel </rt></ruby>（或称 <ruby> 控制平面 <rt>  control plane </rt></ruby>），你需要一种方式来管理 Kubernetes，而这项工作的工具就是 kubectl。`kubectl` 命令让你控制、维护、分析和排查 Kubernetes 集群的故障。与许多使用 `ctl`（“控制”的缩写）后缀的工具一样，如 `systemctl` 和 `sysctl`，`kubectl` 拥有大量的功能和任务权限，所以如果你正在运行 Kubernetes，你肯定会经常使用它。它是一个拥有众多选项的命令，所以下面是 `kubectl` 中简单易用的五个常见任务。


### 1、列出并描述资源


按照设计，容器往往会成倍增加。在某些条件下，它们可以快速增加。如果你只能通过 `podman ps`或 `docker ps` 来查看正在运行的容器，这可能会让你不知所措。通过 `kubectl get` 和 `kubectl describe`，你可以列出正在运行的<ruby> 吊舱 <rt>  pod </rt></ruby>以及它们正在处理的容器信息。更重要的是，你可以通过使用 `--namespace` 或 `name` 或 `--selector`等选项，只列出所需信息。


`get` 子命令不仅仅对吊舱和容器有用。它也有关于节点、命名空间、发布、服务和副本的信息。


### 2、创建资源


如果你只通过类似 OpenShift、OKD 或 Kubernetes 提供的 Web 用户界面（UI）创建过发布，但你想从 Linux 终端控制你的集群，那么可以使用 `kubectl create`。`kubectl create` 命令并不只是实例化一个新的应用发布。Kubernetes 中还有很多其他组件可以创建，比如服务、配额和 [计划任务](https://opensource.com/article/20/11/kubernetes-jobs-cronjobs)。


Kubernetes 中的计划任务可以创建一个临时的吊舱，用来在你选择的时间表上执行一些任务。它们并不难设置。下面是一个计划任务，让一个 BusyBox 镜像每分钟打印 “hello world”。



```
$ kubectl create cronjob \
    hello-world \
    --image=busybox \
    --schedule="*/1 * * * *" -- echo "hello world"

```

### 3、编辑文件


Kubernetes 中的对象都有相应的配置文件，但在文件系统中查找相应的文件较为麻烦。有了 `kubectl edit`，你可以把注意力放在对象上，而不是定义文件上。你可以通过 `kubectl` 找到并打开文件（通过 `KUBE_EDITOR` 环境变量，你可以设置成你喜欢的编辑器）。



```
$ KUBE_EDITOR=emacs \
    kubectl edit cronjob/hello-world

```

### 4、容器之间的传输文件


初次接触容器的人往往对无法直接访问的共享系统的概念感到困惑。他们可能会在容器引擎或 `kubectl` 中了解到 `exec` 选项，但当他们不能从容器中提取文件或将文件放入容器中时，容器仍然会显得不透明。使用 `kubectl cp` 命令，你可以把容器当做远程服务器，使主机和容器之间文件传输如 SSH 命令一样简单：



```
$ kubectl cp foo my-pod:/tmp

```

### 5、应用变更


对 Kubernetes 对象进行修改，可以通过 `kubectl apply` 命令完成。你所要做的就是将该命令指向一个配置文件：



```
$ kubectl apply -f ./mypod.json

```

类似于运行 Ansible 剧本或 Bash 脚本，`apply` 使得快速“导入”设置到运行中的 Kubernetes 实例很容易。例如，GitOps 工具 [ArgoCD](https://argoproj.github.io/argo-cd/) 由于 `apply` 子命令，安装起来出奇地简单：



```
$ kubectl create namespace argocd
$ kubectl apply -n argocd \
    -f https://raw.githubusercontent.com/argoproj/argo-cd/vx.y.z/manifests/install.yaml

```

### 使用 kubectl


Kubectl 是一个强大的工具，由于它是一个终端命令，它可以写成脚本，并能实现用众多 Web UI 无法实现的功能。学习 `kubectl` 是进一步了解 Kubernetes、容器、吊舱以及围绕这些重要的云计算创新技术的一个好方法。[下载我们的 kubectl 速查表](https://opensource.com/downloads/kubectl-cheat-sheet)，以获得快速参考，其中包括命令示例，以帮助你学习，并在为你提供注意细节。




---


via: <https://opensource.com/article/21/7/kubectl>


作者：[Alan Smithee](https://opensource.com/users/alansmithee) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[turbokernel](https://github.com/turbokernel)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
Kubernetes is software to help you run lots of containers in an organized way. Aside from providing tools to manage (or [orchestrate](https://opensource.com/article/20/11/orchestration-vs-automation)) the containers you run, Kubernetes also helps those containers scale out as needed. With Kubernetes as your central control panel (or *control plane*), you need a way to manage Kubernetes, and the tool for that job is kubectl. The `kubectl`

command lets you control, maintain, analyze, and troubleshoot Kubernetes clusters. As with many tools using the `ctl`

(short for "control") suffix, such as systemctl and sysctl, kubectl has purview over a broad array of functions and tasks, so you end up using it a lot if you're running Kubernetes. It's a big command with lots of options, so here are five common tasks that kubectl makes easy.

1. List and describe resources


Containers, by design, tend to multiply. Under certain conditions, they can multiply rapidly. This can get overwhelming if the only way you have to see running containers is `podman ps`

or `docker ps`

. With `kubectl get`

and `kubectl describe`

, you can get information about what pods are running and the containers they're handling. What's more is that you can get just the information you need by using options like `--namespace`

or `name`

or `--selector`

.

The `get`

subcommand is useful for a lot more than just pods and containers. It has information about nodes, namespaces, deployments, services, and replicas.

2. Create resources

If you've only ever created deployments through a web user interface (UI) like one provided by OpenShift, OKD, or Kubernetes, but you're looking to take control of your cluster from your Linux terminal instead, then get ready to use `kubectl create`

. The `kubectl create`

command doesn't just instantiate a new app deployment, though. There are lots of other components available in Kubernetes that you can create, such as services, quotas, and [CronJobs](https://opensource.com/article/20/11/kubernetes-jobs-cronjobs).

A CronJob in Kubernetes can create a transient pod meant to perform some task on a schedule of your choice. They're not difficult to set up. Here's a CronJob to have a BusyBox image echo "hello world" every minute:

```
$ kubectl create cronjob \
hello-world \
--image=busybox \
--schedule="*/1 * * * *" -- echo "hello world"
```


3. Edit files

You may have an understanding that objects in Kubernetes have accompanying configuration files, but rummaging through your filesystem to find the appropriate file can be troublesome. With `kubectl edit`

, you can keep your mind on the objects and not on the files that define them. You can have `kubectl`

find and open the file for you (it respects the `KUBE_EDITOR`

environment variable, so you can set your editor to whatever you prefer):

```
$ KUBE_EDITOR=emacs \
kubectl edit cronjob/hello-world
```


4. Trade files between containers

Newcomers to containers are often baffled by the concept of a shared system that they can't apparently access. They may learn about `exec`

options in their container engine or in kubectl itself, but containers still can seem impervious when they can't just grab a file from or place a file into a container. Using the `kubectl cp`

command, you can treat containers as if they were remote servers, making copying files to and from containers no more complex than an SSH command:

`$ kubectl cp foo my-pod:/tmp`


5. Apply changes

Making changes to Kubernetes objects can be done at any time with the `kubectl apply`

command. All you have to do is point the command to a configuration file:

`$ kubectl apply -f ./mypod.json`


Akin to running an Ansible playbook or a Bash script, `apply`

makes it easy to "import" settings quickly into a running Kubernetes instance. For instance, the GitOps tool [ArgoCD](https://argoproj.github.io/argo-cd/) is surprisingly simple to install thanks to the `apply` subcommand:

```
$ kubectl create namespace argocd
$ kubectl apply -n argocd \
-f https://raw.githubusercontent.com/argoproj/argo-cd/vx.y.z/manifests/install.yaml
```


Use kubectl

Kubectl is a powerful tool, and because it's a terminal command it can be scripted and used in many ways a web UI cannot. Learning kubectl is a great way to further your understanding of Kubernetes, containers, pods, and all the technologies that surround these important cloud innovations. [Download our kubectl cheat sheet](https://opensource.com/downloads/kubectl-cheat-sheet) for a quick reference, complete with sample commands, to help you as you learn and remind you of the details once you're a pro.

## Comments are closed.