Please enable Javascript to view the contents

Kubernetes extends concept

 ·  ☕ 3 分钟

前言

最近由于工作需要,重新系统回顾 Kubernetes 的编程。发现《Programming Kubernetes》这书写得比较系统。于是边学,边记录一些重点。

Controller

Controller Loop

image-20210325162149792

来源:[Programming Kubernetes]

  • Informers:CRD 资源变化通知者。通过 watch API 监听
  • Work queues: 记录失败/推后的操作,以方便后面重试

CRD and CR

image-20210325165417585

Kubernetes API Basics

image-20210325164044430

Kubernetes architecture overview —— 来源:[Programming Kubernetes]

HTTP Interface of the API Server

HTTP verbs:

  • The HTTP GET verb is used for retrieving the data with a specific resource (such as a certain pod) or a collection or list of resources (for example, all pods in a namespace).
  • The HTTP POST verb is used for creating a resource, such as a service or a deployment.
  • The HTTP PUT verb is used for updating an existing resource—for example, changing the container image of a pod.
  • The HTTP PATCH verb is used for partial updates of existing resources

API 术语

  • Kind - 基本对象。一个 Kind 对应一个 GO 对象定义
  • List - 集合对象,如 kubectl get pods 时获取的对象
  • API group : 相关的 Kind 的集合。如 JobScheduledJob均在batch API group
  • Version:每个 API Group在开发迭代过程中会产生多个版本,如 v1alpha1 -> v1beta1 -> v1。API Server 需要兼容这多版本。对于一个对象,返回什么版本由 API 调用者决定(即实时转换)
  • Resource:表现为一个 URL Path,如 xxx/podsxxx/pods/nginx

Resource

Resource支持CRUD操作,在它之下,还有Subresources。如.../pod/nginx/port-forward,.../pod/nginx/logs,它们一般走 WebSockets

Resource 的URL Pattern:

image-20210325170412704

Resource 可以以 GroupVersionResource (or GVR) 作为标识。另外有一种标识是用 GroupVersionKind (GVK)。他们之间可转换,由 Golang 中的 RESTMappers完成。

全局看,API 可以用以下树表示:

image-20210325171231478

由于历史原因,k8s核心的api 在 api/v1下,而不是用 api group 分类的 /apis/core/v1下。

声明式的状态管理

大部分 API 对象的定义(specification)分为两部分:

  • 期望的状态(desired state)
  • 现实的状态(status)

期望的状态一般会被持久化,一般是保存在 etcd 中。

在命令行中调用 API

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
$ kubectl proxy --port=8080
Starting to serve on 127.0.0.1:8080
$ curl http://127.0.0.1:8080/apis/batch/v1 #如果不想用 kubectl proxy,可以直接 kubectl get --raw /apis/batch/v1.
{
  "kind": "APIResourceList",
  "apiVersion": "v1",
  "groupVersion": "batch/v1",
  "resources": [
    {
      "name": "jobs",
      "singularName": "",
      "namespaced": true,
      "kind": "Job",
      "verbs":[
        "create",
        "delete",
        "deletecollection",
        "get",
        "list",
        "patch",
        "update",
        "watch"
      ],
      ....
}

# 你可以以 v2beta1 版本来返回同一个对象:
$ curl http://127.0.0.1:8080/apis/batch/v1beta1
{
  "kind": "APIResourceList",
  "apiVersion": "v1",
  "groupVersion": "batch/v1beta1",
  "resources": [
    {
      "name": "cronjobs",
      "singularName": "",
      "namespaced": true,
      "kind": "CronJob",
      "verbs": [
        "create",
        "delete",
        "deletecollection",
        "get",
        "list",
        "patch",
        "update",
        "watch"
      ],
      "shortNames": [
        "cj"
      ],
      "categories": [
        "all"
      ]
    },
    {
      "name": "cronjobs/status",
      "singularName": "",
      "namespaced": true,
      "kind": "CronJob",
      "verbs": [
        "get",
        ...

正如你看到的,v1beta1 有一个未放入正式版本中的 cronjobs

如果你想列出所有资源:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ kubectl api-resources
NAME                   SHORTNAMES APIGROUP NAMESPACED   KIND
bindings                                   true         Binding
componentstatuses      cs                  false        ComponentStatus
configmaps             cm                  true         ConfigMap
endpoints              ep                  true         Endpoints
events                 ev                  true         Event
limitranges            limits              true         LimitRange
namespaces             ns                  false        Namespace
nodes                  no                  false        Node
persistentvolumeclaims pvc                 true         PersistentVolumeClaim
persistentvolumes      pv                  false        PersistentVolume
pods                   po                  true         Pod
podtemplates                               true         PodTemplate
replicationcontrollers rc                  true         ReplicationController
resourcequotas         quota               true         ResourceQuota
secrets                                    true         Secret
serviceaccounts        sa                  true         ServiceAccount
services               svc                 true         Service
controllerrevisions               apps     true         ControllerRevision
daemonsets             ds         apps     true         DaemonSet
deployments            deploy     apps     true         Deployment

如果想知道系统支持的 api group:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
$ kubectl api-versions
admissionregistration.k8s.io/v1beta1
apiextensions.k8s.io/v1beta1
apiregistration.k8s.io/v1
apiregistration.k8s.io/v1beta1
appmesh.k8s.aws/v1alpha1
appmesh.k8s.aws/v1beta1
apps/v1
apps/v1beta1
apps/v1beta2
authentication.k8s.io/v1

API Server 处理请求的流程

image-20210325175234705

这个调用链定义在源码(link),

参考

[Programming Kubernetes]

分享

Mark Zhu
作者
Mark Zhu
Old Developer