# 架构概览

> `Kubernetes` (通常称为 K8s) 是用于自动部署、扩展和管理容器化（containerized）应用程序的开源系统。Google 设计并捐赠给 Cloud Native Computing Foundation（CNCF，今属 Linux 基金会）来使用的。它旨在提供 “跨主机集群的自动部署、扩展以及运行应用程序容器的平台”。它支持一系列容器工具, 包括 Docker 等。 -- 摘自维基百科 [Kubernetes](https://zh.wikipedia.org/wiki/Kubernetes) 词条

## Kubernetes 架构

![kubernetes-architecuture](/files/-LoAvkcKB3-iD9OHtdvF)

> [Kubernetes Design and Architecture](https://github.com/kubernetes/community/blob/master/contributors/design-proposals/architecture/architecture.md#controller-manager-server)

### 集群控制平面（Cluster control plane）即 Master

Kubernetes 控制平面由一系列组件组成，可以运行在一个单独的主节点上，也可以分布部署以支持高可用集群，或者运行在 Kubernetes 之上。

#### API 服务（API Server）

* kube-apiserver

API server 提供 [Kubernetes API](https://kubernetes.io/docs/concepts/overview/kubernetes-api/)。API server 扮演集群网关的角色，它主要处理 REST 操作，验证并更新到 `etcd` 存储。

#### 集群状态存储（Cluster state store）

* etcd

`etcd` 是一个分布式 key-value 数据库，Kubernetes 用 `etcd` 作为后端数据存储。集群所有的持久性状态都存储在 `etcd` 实例中。`etcd` 提供了可靠配置数据存储。通过 `watch` 的支持，可以非常快速地通知协调组件变更。

#### 控制管理服务（Controller-Manager Server）

* kube-controller-manager

集群内部的管理控制中心，如 Node、Volume、Deployment 、Service 等资源管理，以及空间生命周期，Pod GC、节点 GC 等。

#### 调度器（Scheduler）

* kube-scheduler

执行 pod 的相关调度。调度程序监视未调度的 pod，并根据所请求资源的可用性，服务质量要求、亲和性和反亲和性设置以及其它约束，通过 `/binding` pod 子资源 API 绑定到相应节点。

### Kubernetes 节点（The Kubernetes Node）

#### Kubelet

Kubelet 是 Kubernetes 中最重要和突出的控制器，它是驱动容器执行层的 Pod 和 Node API 的主要实现者。没有这些 API，Kubernetes 只是一个后端由键值存储支持的面向 CRUD 的 REST 应用程序框架。

Kubelet 决定 Pod 是否可以运行在给定的节点上的最终决策者，不是调度器也或者 DaemonSets。此外，Kubelet 还集成了 [cAdvisor](https://github.com/google/cadvisor) 资源监控 agent。

#### 容器运行时（Container runtime）

每一个节点运行一个容器运行时，负责下载镜像和运行容器。Kubelet 不集成容器运行时。作为替代，定义了一个 [Container Runtime Interface](https://github.com/kubernetes/community/blob/master/contributors/devel/container-runtime-interface.md) 控制底层运行时并促进该层的可插拔性。当前支持的有 docker、[rkt](https://github.com/rkt/rkt)、[cri-o](https://github.com/kubernetes-incubator/cri-o)、[frakti](https://github.com/kubernetes/frakti)。

#### Kube Proxy

service 的抽象提供了一种在公共访问策略（如负载均衡）下对 pod 进行分组的方式。Service 通过创建 VIP，提供给客户端访问，再透明代理到 Service 中的 pods。每个节点都运行一个 kube-proxy 进程，该进程维护一套 iptables 规则，以捕获对服务 IPs 的访问，并重定向到正确的后端（1.12.x ipvs 正式 GA，性能相对 iptables 有很大的提升）。

附上华为关于 Service 性能这块的介绍，主要是对比 ipvs 和 iptables：

* [华为云在 K8S 大规模场景下的 Service 性能优化实践](https://zhuanlan.zhihu.com/p/37230013)

### 附加组件和依赖

* [DNS](https://github.com/kubernetes/kubernetes/tree/master/cluster/addons/dns) 提供集群内部解析和服务发现
* [Ingress controller](https://github.com/kubernetes/ingress-nginx) 提供内部服务七层代理到外部
* [Kubernetes Metrics Server](https://github.com/kubernetes-incubator/metrics-server) 替换 Heapster 监控
* [Dashboard](https://github.com/kubernetes/dashboard/) Kubernetes GUI

以及包括 [kube-state-metrics](https://github.com/kubernetes/kube-state-metrics) 等其它 [add-ons ](https://github.com/kubernetes/kubernetes/tree/master/cluster/addons) 组件。

#### 一些基于 Kubernetes 的开源平台

Kubernetes GUI 从体验上来说还是相对比较差的，可以使用一些开源的替代版本：

* [Multi-Cluster Kubernetes Management -- Rancher](https://rancher.com/)
  * Rancher 团队是做的比较早的，现在已经发布 2.x 版本了，支持中文，具体可以参考文档 [Rancher 2.0-CN](https://www.cnrancher.com/docs/rancher/v2.x/cn/overview/)
* [Web UI for Kubernetes multi-clusters -- Wayne](https://github.com/Qihoo360/wayne)
  * 360 开源的 Kubernetes 多集群 Web UI


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wiki.opskumu.com/kubernetes/arch.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
