# 1. What is K8s
> [!abstract]
> K8s in an orchestrator of cloud-native microservices applications.
## Microservices
- **monolithic** applications: 一个app内包括前端、认证、中间件、日志等特性。
- 缺点1: 更新一个特性需要更新所有
- 缺点2: 扩容一个特性也需要扩容所有特性
- **microservices** applications:每个特性都独立部署,独立更新和扩展。
- 松耦合,每个暴露一个 API 给其他的使用,不更改 API 更新的话,不会影响 consumers。
## Cloud-native
- 一个 cloud-native 的 app 必须满足:
- **Self-heal**
- **Scale on demand**
- **Support rolling updates**
- **Self-healing or resiliency**
- K8s 可以自我修复(**self-heal**)应用。
- 你告诉 K8s 某个 app 应该是什么样,比如每个微服务需要多少实例,K8s 会将其记录为 desired state,并保持该 state 能够一直得到满足。
- 例如:某个微服务失败后,k8s可以启动替换。
- **Rolling updates**
- 具备部分更新应用的能力,不需要将应用下线,影响客户。
> [!warning]
> > Cloud-native has almost nothing to do with the public cloud.
> 例如部署一个 monolithic app 到 cloud 上,不会让它变成 cloud-native。
> [!note] 总结
> Cloud-native apps 满足 **resilient**、**auto scale** 和 **不宕机更新**。
## Orchestration
- 每个 cloud-native app 由做不同事情的**独立微服务**组成,需要有人将它们组织成一个有用的 app,这个角色就是 k8s,同时还响应**实时事件**,处理 self-healing、scaling 等。
<img src="https://img.jonahgao.com/oss/note/2025p1/intro_k8s_f15.jpeg" width="700" />
- 总结:K8s 是一个 application orchestrator,将不同的微服务整合成一个有用的应用。
> [!note] 总结
> - K8s 是一个 application orchestrator,将不同的微服务整合成一个有用的应用。
> - 同时它也提供和管理 cloud-native 特性如 scaling,self healing 和 updates。
## Other useful k8s things to know
- Kubernetes这个名字来源于希腊词,意为舵手。
---------------
# 2. Why k8s so important
- 本章覆盖两个领域:
- 为什么云厂商需要 k8s
- 为什么用户社区需要 k8s
## Why the cloud providers need k8s
- Kubernetes as a way to manage the explosive growth of containers.
- Kubernetes is also excellent at **abstracting** and **commoditizing** cloud and server infrastructure.
- ==The OS of the cloud==
<img src="https://img.jonahgao.com/oss/note/2025p1/intro_k8s_f21.jpeg" width="540" />
## Why the user community needs k8s
- 用户需要 vendor-neutral platforms。
- k8s 给用户带来了很强的灵活性,避免 cloud lock-in。
----
# 3. K8s architecture
一个 k8s installation 称为一个 **Kubernetes cluster**。
- 一般一个 cluster 不会跨多个云,因为节点间的延迟会很高。
- k8s可以运行在多个平台上,但容器对平台有更严格的要求。
- 对操作系统的要求:例如 windows 的容器需要 windows 的 cluster nodes。
- CPU 架构也同理,x86 的容器无法运行在 ARM 的 CPU 上。
## Control plan nodes and worker nodes
- 一个 k8s cluster 是安装了k8s 的一个或多个机器(machines)。
- 这些机器可以是物理机、虚拟机、云实例、树莓派等等。
- 在它们之上安装 k8s,并连接到一起,就创建了一个 cluster。
- k8s cluster 中的机器称为 nodes,有两种类型的 node:
- Control plane nodes
- Worker nodes
<img src="https://img.jonahgao.com/oss/note/2025p1/intro_k8s_f32.jpeg" width="700" />
### Control plane nodes
- control plane nodes 运行 k8s 内部的系统服务。
- 包括 API server,sheduler,cluster store 等。
- 每个 control plane node 运行**所有的** control plan service。
- 一个机器最好有多个 control plane node 来保证高可用。
- 通常 3 到 5 个,并且跨 failure domains 分布,比如跨机架。
- **API server** 负责用户直接跟 k8s 交互。例如 部署、管理、更新 apps。
- **Scheduler** 负责挑选 app 运行的 worker nodes。
- **Store** 保存应用和集群的状态 state。
- **Cloud controller** 集成 cloud service,如 storage 和 load balancers。
### Worker nodes
- Worker nodes 运行用户程序,可以是 Linux 和 Windows。
- Linux apps 运行在 Linux nodes 上,Win apps 运行在 windows nodes 上。
- 一个集群可以混合多种类型的 nodes(win or linux)。
<img src="https://img.jonahgao.com/oss/note/2025p1/intro_k8s_f34.jpeg" width="600" />
- Worker nodes 运行两种基本的服务:
- Kubelet
- Container runtime
- **kubelet** 是 k8s 的核心 agent。
- 负责将 workers 加入集群,以及跟 control plane 通信。
- 例如它监听 API server 来接收 work tasks,回应状态报告给 API server。
- **Container runtime** 管理 container lifecycle events,如创建、启动、停止和删除。
- 早期的运行时是 docker。
- 2016 引入 CRI,后续替换为 containerd。
## Hosted Kubernetes
- 即托管的 Kubernetes, 是一种消费模型,云服务提供商向用户出租一个生产级别的 Kubernetes 集群。
- 云服务提供商负责构建集群,拥有 control plane,负责它的性能、可用性、更新。
- 用户负责 worker nodes、应用程序、支付账单。
## Managing K8s with the kubectl command line tool
- K8s 的大部分日常管理工作都是使用 kubectl 命令行工具完成的。
- 例如部署和管理应用、检查集群和应用的健康、执行更新。