1 of 13

k8s中的核心组件

张育鑫(Taylor Zhang)

https://www.linkedin.com/in/yxzh/

2 of 13

Content

  1. Kubelet
  2. Scheduler
  3. Kubernetes API server
  4. Controller Manager
  5. An Example

3 of 13

Kubelet

An agent that runs on each node in the cluster. It makes sure that containers are running in a Pod.

The kubelet takes a set of PodSpecs that are provided through various mechanisms and ensures that the containers described in those PodSpecs are running and healthy. The kubelet doesn't manage containers which were not created by Kubernetes.

4 of 13

Kubelet

  1. 修改和维护node
    1. 一开始可以修改所有node
    2. 后被限制在所在node
  2. 自动注册 & 手动注册
    • - - api - servers
    • - - kubeconfig
    • - - cloud - provider
  3. 监听与定时报告
    • List & watch
    • LivenessProbe
    • ReadinessProbe

5 of 13

List & watch

Kubernetes uses the term list to describe returning a collection of resources to distinguish from retrieving a single resource which is usually called a get. If you sent an HTTP GET request with the ?watch query parameter, Kubernetes calls this a watch and not a get (see Efficient detection of changes for more details).

A given Kubernetes server will only preserve a historical record of changes for a limited time. Clusters using etcd 3 preserve changes in the last 5 minutes by default. When the requested watch operations fail because the historical version of that resource is not available, clients must handle the case by recognizing the status code 410 Gone, clearing their local cache, performing a new get or list operation, and starting the watch from the resourceVersion that was returned.

For subscribing to collections, Kubernetes client libraries typically offer some form of standard tool for this list-then-watch logic. (In the Go client library, this is called a Reflector and is located in the k8s.io/client-go/tools/cache package.)

6 of 13

Scheduler

A scheduler watches for newly created Pods that have no Node assigned. For every Pod that the scheduler discovers, the scheduler becomes responsible for finding the best Node for that Pod to run on.

  • 不同pod类型对资源占用需求不同
  • 对故障中断恢复及节点迁移容忍度不同
  • 业务的优先级不同
  • 节点稳定性 vs 资源使用率

7 of 13

Scheduler

  1. Node Cache
  2. Pod Cache
  3. Pod queue
    1. Active
    2. Backoff
    3. Unschedulable
  4. Filter
    • 1 pod
    • Find matched node
  5. Score
    • Score node
  6. Reserve
    • SelectHost
    • Unreserve
  7. Bind

Pod States

Initial (virtual state)

Assumed (reserved)

Added

Deleted(Virtual)

8 of 13

Scheduler

The Kubernetes scheduler is a control plane process which assigns Pods to Nodes.

https://kubernetes.io/docs/concepts/scheduling-eviction/scheduling-framework/

9 of 13

Kube API Server

The Kubernetes API server validates and configures data for the api objects which include pods, services, replicationcontrollers, and others. The API Server services REST operations and provides the frontend to the cluster's shared state through which all other components interact.

10 of 13

Kube API Server

https://github.com/kubernetes/kubernetes/tree/master/pkg/registry

The API Server keeps all known Kubernetes object kinds in a Go type registry called Scheme. In this registry, each version of kinds are defined along with how they can be converted, how new objects can be created, and how objects can be encoded and decoded to JSON or protobuf.

11 of 13

Kube API Server

接口多版本管理问题

12 of 13

Controller Manager

In robotics and automation, a control loop is a non-terminating loop that regulates the state of a system.

In Kubernetes, controllers are control loops that watch the state of your cluster, then make or request changes where needed. Each controller tries to move the current cluster state closer to the desired state.

13 of 13

Example

Create a Deployment

to create POD