API Server

API Server 是唯一连接 etcd 的组件:

                    ┌─────────────────┐
                    │      etcd       │
                    │   (数据存储)     │
                    └────────┬────────┘
                             │
                      唯一连接 ▲
                             │
                    ┌────────┴────────┐
                    │  kube-apiserver │
                    └────────┬────────┘
                             │
          ┌──────────────────┼──────────────────┐
          │                  │                  │
          ▼                  ▼                  ▼
   ┌─────────────┐   ┌─────────────┐   ┌─────────────┐
   │  scheduler  │   │ controller  │   │   kubelet   │
   └─────────────┘   └─────────────┘   └─────────────┘
   
   所有组件都必须通过 API Server 访问集群状态

另外API Server 是集群的"唯一大门”,所有组件必须通过它访问 etcd,同时负责验证"你是谁”(认证)和"你能做什么”(授权),确保集群安全:

kubectl get pods
        │
        ▼
┌───────────────────────────────────────────────────────┐
│                    API Server                         │
├───────────────────────────────────────────────────────┤
│  1. 认证 (Authentication)                              │
│     └── 证书/Token 验证 → 确认是 user: admin            │
│                                                       │
│  2. 授权 (Authorization)                              │
│     └── RBAC 检查 → admin 有权 get pods ✓              │
│                                                       │
│  3. 准入控制 (Admission Control)                       │
│     └── 请求是否符合策略(如资源配额)                     │
│                                                       │
│  4. 验证通过,查询 etcd                                 │
│     └── 返回 pods 列表                                 │
└───────────────────────────────────────────────────────┘
        │
        ▼
    返回结果

当用户创建一个Pod时,时序图如下:

image-20200118104356063

  1. Kubectl writes to the API server(kubectl run mywebserver --image=nginx)
  2. API server will authenticate and authorize. Upon validation, it will write it to etcd.
  3. Upon write to etcd, API Server will invoke the scheduler.
  4. Scheduler decides which node the pod should run and return data to API Server. API will in-turn write it back to etcd.
  5. API Server will invoke the kubelet in the node decided by the scheduler.
  6. Kubelet communicates to the docker daemon via Docker socket to create a container.
  7. Kubelet will update the status of the POD back to the API Server.
  8. API Server will write the status details back to etcd.