---
reviewers:
- eparis
- mikedanese
title: Kubernetes 101
content_template: templates/tutorial
---

{{% capture overview %}}

<!--
For Kubernetes 101, we will cover kubectl, Pods, Volumes, and multiple containers.
-->
关于 Kubernetes 101，我们将涵盖 kubectl, Pods, Volumes 和多个容器这些内容。

{{% /capture %}}

{{% capture objectives %}}

<!--
* What is `kubectl`.
* Manage a Pod.
* Create and mount a volume.
* Create multiple containers in a Pod.
-->
* `kubectl` 是什么。
* 管理 Pod。
* 创建和挂载卷。
* 在一个 Pod 中创建多个容器。

{{% /capture %}}

{{% capture prerequisites %}}

<!--
* In order for the kubectl usage examples to work, make sure you have an example directory locally, either from [a release](https://github.com/kubernetes/kubernetes/releases) or the latest `.yaml` files located [here](https://github.com/kubernetes/website/tree/master/content/en/docs/tutorials).
-->
* {{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
* 为了 kubectl 使用示例能正常工作，请确保您在本地有一个示例目录，可以从 [一个发行版本](https://github.com/kubernetes/kubernetes/releases) 或者最新的 `.yaml` 文件 [这里](https://github.com/kubernetes/website/tree/master/content/en/docs/tutorials) 获取。

{{% /capture %}}

{{% capture lessoncontent %}}

<!--
## Kubectl CLI

The easiest way to interact with Kubernetes is through the kubectl command-line interface.

For more info about kubectl, including its usage, commands, and parameters, see [Overview of kubectl](/docs/reference/kubectl/overview/).

For more information about installing and configuring kubectl, see [Install and Set Up kubectl](/docs/tasks/tools/install-kubectl/).
-->
## Kubectl 命令行

与 Kubernetes 进行交互最简单的方法就是通过 kubectl 命令行。

想要了解更多关于 kubectl，包括它的使用、命令和参数，查看 [kubectl 概览](/docs/reference/kubectl/overview/)。

想要了解更多关于 kubectl 的安装和配置，查看 [安装和设置 kubectl](/docs/tasks/tools/install-kubectl/)。

<!--
## Pods

In Kubernetes, a group of one or more containers is called a _Pod_. Containers in a Pod are deployed together, and are started, stopped, and replicated as a group.

For more information, see [Pods](/docs/concepts/workloads/pods/pod/).
-->
## Pods

在 Kubernetes 中，一组一个或者多个容器被称为  _Pod_ 。在一个 Pod 里的容器是在一起部署的，它们作为一组一起启动、停止和伸缩。

想要了解更多, 查看 [Pods](/docs/concepts/workloads/pods/pod/)。

<!--
#### Pod Definition

The simplest Pod definition describes the deployment of a single container.  For example, an nginx web server Pod might be defined as:

{{< codenew file="pods/simple-pod.yaml" >}}

A Pod definition is a declaration of a _desired state_.  Desired state is a very important concept in the Kubernetes model.  Many things present a desired state to the system, and Kubernetes' ensures that the current state matches the desired state.  For example, when you create a Pod and declare that the containers in it to be running. If the containers happen not to be running because of a program failure, Kubernetes continues to (re-)create the Pod in order to drive the pod to the desired state. This process continues until you delete the Pod.

For more information, see [Kubernetes Design Documents and Proposals](https://github.com/kubernetes/community/blob/master/contributors/design-proposals/README.md).
-->
#### Pod 定义

最简单的 Pod 定义描述了 Deployment 中的单个容器。例如，一个 nginx web 服务器 Pod 可能被定义为：

{{< codenew file="pods/simple-pod.yaml" >}}

Pod 是对预期状态的声明的定义。预期状态在 Kubernetes 模型中是一个非常重要的概念。在这个系统中的很多东西都呈现为预期的状态，Kubernetes 保证现在的状态和预期的状态保持一致。例如，当您创建了一个 Pod 并且声明了将要在里面运行的容器。如果这些容器因为程序故障没有运行，Kubernetes 将继续创建或者重新创建这个 Pod 以使这个 Pod 达到期望状态。这个过程会一直持续直到您删除这个 Pod。

想了解更多信息，请查看 [Kubernetes Design Documents and Proposals](https://github.com/kubernetes/community/blob/master/contributors/design-proposals/README.md)。


<!--
#### Pod Management

Create a Pod containing an nginx server ([simple-pod.yaml](/examples/pods/simple-pod.yaml)):

```shell
kubectl create -f https://k8s.io/examples/pods/simple-pod.yaml
```

List all Pods:

```shell
kubectl get pods
```
-->
#### Pod 管理

创建一个包含 nginx 服务器的 Pod ([simple-pod.yaml](/examples/pods/simple-pod.yaml)):

```shell
kubectl create -f https://k8s.io/examples/pods/simple-pod.yaml
```

 列出所有的 Pod:

```shell
kubectl get pods
```

<!--
On most providers, the Pod IPs are not externally accessible. The easiest way to test that the pod is working is to create a busybox Pod and exec commands on it remotely. For more information, see [Get a Shell to a Running Container](/docs/tasks/debug-application-cluster/get-shell-running-container/).

If the IP of the Pod is accessible, you can access its http endpoint with wget on port 80:
-->

对于大多数服务提供商，Pod IP 无法从外部访问。测试 Pod 是否工作的最简单方法是创建一个 busybox Pod 并且远程执行 exec 命令。有关更多信息，请参阅 [获取正在运行的容器的Shell](/docs/tasks/debug-application-cluster/get-shell-running-container/).

如果可以访问 Pod 的 IP，则可以使用端口 80 上的 wget 访问其 http 端点：

```shell
kubectl run busybox --image=busybox --restart=Never --tty -i --generator=run-pod/v1 --env "POD_IP=$(kubectl get pod nginx -o go-template='{{.status.podIP}}')"
u@busybox$ wget -qO- http://$POD_IP # Run in the busybox container
u@busybox$ exit # Exit the busybox container
```

```shell
kubectl delete pod busybox # Clean up the pod we created with "kubectl run"
```

<!--
To delete a Pod named nginx:
-->
删除一个名为 nginx 的 Pod:

```shell
kubectl delete pod nginx
```


<!--
#### Volumes

That's great for a simple static web server, but what about persistent storage?

The container file system only lives as long as the container does. So if your app's state needs to survive relocation, reboots, and crashes, you'll need to configure some persistent storage.

In this example you can create a Redis Pod with a named volume, and a volume mount that defines the path to mount the Volume.
-->
#### 卷

这对于简单的静态 Web 服务器来说非常棒，但是持久存储呢？

容器文件系统只在容器中存在。因此，如果您的应用程序的状态需要在重定位，重新启动和崩溃中存活，您将需要配置一些持久存储。

在此示例中，您将创建一个具有数据卷的 Redis Pod,并将数据卷挂载到指定的路径。

<!--
1. Define a Volume:
-->
1. 定义一个卷:


      ```yaml
      volumes:
        - name: redis-storage
          emptyDir: {}
      ```

<!--
1. Define a Volume mount within a container definition:
-->
1. 在容器定义中定义卷挂载：

      ```yaml
      volumeMounts:
        # name must match the volume name defined in volumes
        - name: redis-storage
          # mount path within the container
          mountPath: /data/redis
      ```

<!--
Here is an example of Redis Pod definition with a persistent storage volume ([redis.yaml](/examples/pods/storage/redis.yaml)):
-->
以下是具有持久存储卷的 Redis Pod 定义的示例 ([redis.yaml](/examples/pods/storage/redis.yaml)):

{{< codenew file="pods/storage/redis.yaml" >}}

<!--
Where:

- The `volumeMounts` `name` is a reference to a specific  `volumes` `name`.
- The `volumeMounts` `mountPath` is the path to mount the volume within the container.
-->

其中：

- `volumeMounts` `name` 是特定 `volumes` `name` 的引用。
- `volumeMounts` `mountPath` 是在容器中挂载卷的路径。

<!--
##### Volume Types

- **EmptyDir**: Creates a new directory that exists as long as the Pod is running on the node, but it can persist across container failures and restarts.
- **HostPath**: Mounts an existing directory on the node's file system. For example (`/var/logs`).
-->
##### 卷类型

- **EmptyDir**: 只要 Pod 在节点上运行，就会创建一个新目录，但它可以在容器故障和重新启动之间保持不变。
- **HostPath**: 在节点的文件系统上安装现有目录。例如 (`/var/logs`).

<!--
For more information, see [Volumes](/docs/concepts/storage/volumes/).
-->
有关更多信息，请参阅 [卷](/docs/concepts/storage/volumes/).

<!--
#### Multiple Containers

{{< note >}}
**Note:** The examples below are syntactically correct, but some of the images (e.g. kubernetes/git-monitor) don't exist yet.  We're working on turning these into working examples.
{{< /note >}}
-->
#### 多个容器

{{< note >}}
**注意:** 以下示例在语法上是正确的，但某些镜像（例如 kubernetes/git-monitor）尚不存在。我们正在努力将这些变成可以工作的示例。
{{< /note >}}

<!--
However, often you want to have two different containers that work together.  An example of this would be a web server, and a helper job that polls a git repository for new updates:
-->
但是，通常您希望有两个不同的容器一起工作。这方面的一个例子是 Web 服务器和一个帮助程序作业，它可以轮询 git 仓库以获取更新：

```yaml
apiVersion: v1
kind: Pod
metadata:
  name: www
spec:
  containers:
  - name: nginx
    image: nginx
    volumeMounts:
    - mountPath: /srv/www
      name: www-data
      readOnly: true
  - name: git-monitor
    image: kubernetes/git-monitor
    env:
    - name: GIT_REPO
      value: http://github.com/some/repo.git
    volumeMounts:
    - mountPath: /data
      name: www-data
  volumes:
  - name: www-data
    emptyDir: {}
```

<!--
Note that we have also added a Volume here.  In this case, the Volume is mounted into both containers.  It is marked `readOnly` in the web server's case, since it doesn't need to write to the directory.

Finally, we have also introduced an environment variable to the `git-monitor` container, which allows us to parameterize that container with the particular git repository that we want to track.
-->
请注意，我们在此处还添加了一个卷。在这种情况下，卷安装在两个容器中。它在 Web 服务器容器的情况下标记为 `readOnly`，因为它不需要写入目录。

最后，我们还向 `git-monitor` 容器引入了一个环境变量，它允许我们使用我们想要跟踪的特定 git 仓库来参数化该容器。

{{% /capture %}}

{{% capture whatsnext %}}

<!--
Continue on to [Kubernetes 201](/docs/tutorials/k8s201/) or
for a complete application see the [guestbook example](https://github.com/kubernetes/examples/tree/{{< param "githubbranch" >}}/guestbook/)
-->
继续阅读 [Kubernetes 201](/docs/tutorials/k8s201/) 或者有关完整的应用程序，请参阅 [guestbook 示例](https://github.com/kubernetes/examples/tree/{{< param "githubbranch" >}}/guestbook/)

{{% /capture %}}
