Slide 1

Slide 1 text

Kyle bai R&D @ inwinSTACK www.inwinstack.com Extend the Kubernetes API with CRD and Custom API Server

Slide 2

Slide 2 text

Job R&D @ inwinSTACK Description 早期在校主要撰寫 java 與 objc 程式語⾔言,並專注於 iOS Mobile 應⽤用程式開發,具備四年年開發經驗。次要研讀 Hadoop 與 Spark 資料運算框架,以及 Linux 相關技術。 研所與⼯工作期間則專注於 OpenStack、Ceph 與 Kubernetes 等雲原⽣生相關技術開源專案,閒暇之餘會參參加 相關專案社區及貢獻,並利利⽤用部落落格、GitHub 與 GitBook 進⾏行行筆記記錄。 Drink Coffee Good! Phone #7727 Love Buttocks kairen([email protected]) https://kairen.github.io/ About Me Kyle Bai

Slide 3

Slide 3 text

Kubernetes 47% Containers remain the top emerging technology of interest to users. Among those currently deploying container orchestration or platform services on OpenStack, 47% are using Kubernetes. Why Kubernetes? Kubernetes API Server Extend the Kubernetes with CRD Extend the Kubernetes with apiserver-builder Agenda Today I will talk about

Slide 4

Slide 4 text

Why Kubernetes?

Slide 5

Slide 5 text

Kubernetes Kubernetes 是 Google 開源的容器(Container)分散式管理理系統,是 Google Brog ⼗十幾年年以來來⼤大規模應⽤用容器技術的經驗累積和昇華的⼀一個 重要成果,是建於 Container(OCI容器標準)之上的容器叢集排程系統, 簡稱為K8s( )。 為 CNCF(Cloud Native Computing Foundation) 專案之⼀一。 Stars 25,830+ Commits 52,905+ Contributors 1,331+

Slide 6

Slide 6 text

Kubernetes Layer

Slide 7

Slide 7 text

CNCF Members

Slide 8

Slide 8 text

CNCF Landscape

Slide 9

Slide 9 text

Container Management Platforms Preferences

Slide 10

Slide 10 text

Kubernetes Pros 資源監控 Monitoring 滾動升級 Rolling-update ⾼高可靠性 High-availability ⾃自我修復 Self-healing 雲端⽀支援 Cloud Provider 持久性儲存 Persistent Volumes 組態檔案 Configmap 安全性 Secret Kubernetes 管理理跨區域與主機的容器節點,提供基本部署、維運、管 理理,以及執⾏行行各項應⽤用程式,並具備多種特性。

Slide 11

Slide 11 text

Microservices Kubernetes 架構設計非常適合在微服務(Microservices)軟體架構,透 過多個容器(Container)與負載平衡等來來組成系統。

Slide 12

Slide 12 text

Kubernetes Distributions

Slide 13

Slide 13 text

Kubernetes API Server

Slide 14

Slide 14 text

Kubernetes Architecture(1/2) Kubernetes 屬於分散式架構系統,主要由兩兩種節點⾓角⾊色組成: • Masters – 主要提供 API、管理理⼯工作節點與排程等,為主節點。 • Nodes(Minions) – 主要執⾏行行容器應⽤用的節點,上⾯面會執⾏行行許多 容器。

Slide 15

Slide 15 text

Kubernetes Architecture(2/2)

Slide 16

Slide 16 text

Kubernetes API driven Kubernetes API 是以 JSON 作為其主要序列列化模型的 HTTP API,且能夠⽀支 援協定快取區,並⽤用於叢集內部溝通使⽤用。 • 通過 API 完成節點之間溝通進⾏行行 CRUD 操作,或是授權、認證與註冊等。 • Kubernetes 有明確定義與規範 API。⽀支援 OpenAPI。 • 使⽤用 gRPC 進⾏行行 Remote Procedure Call。 • 可擴展的 API。 • CRD(Custom Resource Definitions) • API server aggregation • Custom resources and controllers

Slide 17

Slide 17 text

HTTP API spaces 由於其可擴展性, Kubernetes ⽀支援不同 API 路路徑版本,比如 /api/v1或/apis/ extensions/v1beta1。

Slide 18

Slide 18 text

API Levels 在 Kubernetes 中,不同版本的 API 意味著不同層級穩定度與⽀支援度: • Alpha level:在預設下是⼤大多情況禁⽌止使⽤用狀狀態,這些功能有可能隨時在下⼀一 版本被遺棄,因此只適⽤用於測試⽤用,Example: v1alpha1。 • Beta level: 在這級別⼀一般預設會啟⽤用,這表⽰示該功能已經過很好的測試項 ⽬目,但是物件內容可能會在後續版本或穩定版本發⽣生變化。Example: v1beta2。 • Stable level:在這級別表⽰示該功能已經穩定,會很長的時間⼀一直存在。 Example: v1。

Slide 19

Slide 19 text

API Terminology(1/2) • Kind:是⼀一個實體(Entity)的類型,每個 Kubernetes 物件都會有⼀一個欄欄位 Kind。 1. 物件表⽰示系統中的⼀一個持久實體,如 Pod 與 Namespace。 2. ⼀一個多種實體的資源集合列列表,如 PodLists, NodeLists。 3. 特殊⽤用途類型⽤用於物件與非持久實體的特 定操作,如 /binding, /status 這些⽤用來來取得 API Group 與 Resource 的錯誤狀狀態。

Slide 20

Slide 20 text

API Terminology(2/2) • API Group:是邏輯上相關的種類集合,如 Job 與 CronJob 都屬於批次處理理功能相 關。 • Version: 每個 API Group 存在多個版本,這些版本區分不同穩定度層級,⼀一般功能會 從 v1alpha1 升級到 v1beta1,然後在 v1 成為穩定版本。 • Resource: 資源是透過 HTTP 發送與檢索的系統實體,其以 JSON 來來表⽰示。可以是 單⼀一或者多個資源。

Slide 21

Slide 21 text

Request Flow and Processing(1/4) https://github.com/kubernetes/apiserver/blob/master/pkg/server/config.go • 在 DefaultBuildHandlerChain() 做 操作過濾處理理。 • 透過 Multiplexer 來來依據 HTTP 路路 徑將請求路路由到對應 Handlers。 • 利利⽤用 Routes 來來將 HTTP 路路徑與 Handlers 進⾏行行連接。 • Handlers 獲取 HTTP 請求與上下⽂文 ( User, Auth 等),並從儲存中取得 物件實體。

Slide 22

Slide 22 text

Request Flow and Processing(2/4) https://www.slideshare.net/sttts/kubernetes-api-deep-dive-into-the-kubeapiserver

Slide 23

Slide 23 text

Request Flow and Processing(3/4) https://www.slideshare.net/sttts/kubernetes-api-deep-dive-into-the-kubeapiserver

Slide 24

Slide 24 text

Request Flow and Processing(4/4) https://www.slideshare.net/sttts/kubernetes-api-deep-dive-into-the-kubeapiserver

Slide 25

Slide 25 text

Cluster state in Etcd

Slide 26

Slide 26 text

Validation and Admission • Admission:透過驗證叢集的全域約束來來檢查是否建立或更更新 API 物件,在 Kubernetes 中有很多這樣功能。幾個約束範例例: • NamespaceLifecycle: 如果命名空間不存在,則拒絕該所有請求。 • ResourceQuota:為叢集的當前使⽤用者強制執⾏行行額度限制。 • Validation:檢查傳入的物件(建立與更更新過程)的格式是否符合。例例如: • 檢查所有的字串串是否是有效的格式。 • 檢查是否有設定⽭矛盾欄欄位。

Slide 27

Slide 27 text

YAML file to define resource Example resource:

Slide 28

Slide 28 text

Extend the Kubernetes with CRD

Slide 29

Slide 29 text

CustomResourceDefinitions(CRD) CustomResourceDefinition(CRD) 是 v1.7+ 版本新加入的 Kubernetes API 擴 展機制,⽬目標是無需修改核⼼心程式碼就能對 Kubernetes 進⾏行行擴展,⽽而 CRD 更更 是 ThirdPartyResources(TPR) 的升級版。 • 使⽤用⾃自定義物件進⾏行行擴展 Kubernetes API. • CRDs 能夠沿⽤用熟係的 UX ⼯工具,ex: kubectl. • 能夠與 Controllers 進⾏行行結合. • ⽀支援 SubResources(v1.9). P.S. TPR 在 v1.8 版本已被遺棄。

Slide 30

Slide 30 text

How CRD works internally(1/2) Creating a CRD

Slide 31

Slide 31 text

How CRD works internally(2/2) Creating a CustomResource

Slide 32

Slide 32 text

CRD Example(1/2)

Slide 33

Slide 33 text

CRD Example(2/2)

Slide 34

Slide 34 text

Add Controller to Handle events

Slide 35

Slide 35 text

Operators Operator 是 CoreOS 開發的框架,⽬目標是簡化複雜有狀狀態應⽤用的管理理,它能 夠達到應⽤用程式的狀狀態事件變化,並利利⽤用控制器來來透過擴展的 Kubernetes API 進⾏行行⾃自動建立、管理理與配置應⽤用程式容器實例例。

Slide 36

Slide 36 text

Use case - rook.io(1/3) rook.io: ⼀一款⽀支援檔案系統、區塊儲存與物件儲存服務的雲原⽣生儲存系統。(Ceph 改良版 on Kubenretes)。

Slide 37

Slide 37 text

Use case - rook.io(2/3)

Slide 38

Slide 38 text

Use case - rook.io(3/3) Rook 使⽤用 CRD 來來定義建立 Pool 與 Filesystem 資源,以提供 Rook operator 存取並建立對應服務。

Slide 39

Slide 39 text

Use case - TensorFlow(1/3) https://github.com/tensorflow/k8s Kubernetes Custom Resource and Operator For TensorFlow jobs.

Slide 40

Slide 40 text

Use case - TensorFlow(2/3)

Slide 41

Slide 41 text

Use case - TensorFlow(3/3)

Slide 42

Slide 42 text

Demo https://github.com/kairen/k8s-operator-examples

Slide 43

Slide 43 text

Refer projects • code-generator • crd-code-generation • apiextensions-apiserver • sample-apiserver • sample-controller • apimachinery

Slide 44

Slide 44 text

Extend the Kubernetes API with apiserver-builder

Slide 45

Slide 45 text

apiserver-builder apiserver-builder 是⼀一個實作快速建構 Kubernetes apiserver 與⾃自定義資源類 型的⼯工具與函式庫,該⼯工具具備了了以下幾個特性: • 能夠⼀一次建立新資源類型、控制器、測試函式與⽂文件。 • 能夠建構與執⾏行行於 Standalone, Minikube 與遠端叢集。 • 能夠簡單地從控制器(Controller)來來觀看與更更新 Kubernetes API 資源類型。 • 很簡單的加入新資源與⼦子資源。 • 為⼤大多數欄欄位提供預設值,且能夠被覆蓋。

Slide 46

Slide 46 text

apiserver-builder cmd # 初始化 $ cd GOPATH/src/github.com/my-org/my-project $ apiserver-boot init repo --domain # 建立資源 $ apiserver-boot create group version resource --group --version --kind # 編譯執⾏行行檔 $ apiserver-boot build executables $ apiserver-boot build docs # 在 local 執⾏行行 $ apiserver-boot run local # 在叢集執⾏行行 $ apiserver-boot run in-cluster --name nameofservicetorun --namespace default --image gcr.io/myrepo/ myimage:mytag $ kubectl create -f sample/.yaml

Slide 47

Slide 47 text

apiserver-builder storage operations

Slide 48

Slide 48 text

Demo https://github.com/kairen/k8s-operator-examples

Slide 49

Slide 49 text

Refer projects • apiserver-builder/example • kube-openapi • service-catalog

Slide 50

Slide 50 text

50 有任何問題與想法,可以⼀一起討論唷。 Thank You!!