Kubernetes學習之路(十一)之資源清單定義

來源:互聯網
上載者:User

標籤:clu   schema   git   and   one   kubectl   ring   ecif   請求   

一、Kubernetes常用資源

以下列舉的內容都是 kubernetes 中的 Object,這些對象都可以在 yaml 檔案中作為一種 API 類型來配置。

類別 名稱
工作負載型資來源物件 Pod  Replicaset  ReplicationController  Deployments StatefulSets Daemonset Job CronJob
服務發現及負載平衡  Service  Ingress
配置與儲存 Volume、Persistent Volume、CSl 、 configmap、  secret
叢集資源 Namespace Node Role ClusterRole  RoleBinding  ClusterRoleBinding
中繼資料資源 HPA PodTemplate LimitRang
二、理解Kubernetes中的對象

在 Kubernetes 系統中,Kubernetes 對象 是持久化的條目。Kubernetes 使用這些條目去表示整個叢集的狀態。特別地,它們描述了如下資訊:

  • 什麼容器化應用在運行(以及在哪個 Node 上)
  • 可以被應用使用的資源
  • 關於應用如何表現的策略,比如重啟策略、升級策略,以及容錯策略

Kubernetes 對象是 “目標性記錄” —— 一旦建立對象,Kubernetes 系統將持續工作以確保對象存在。通過建立對象,可以有效地告知 Kubernetes 系統,所需要的叢集工作負載看起來是什麼樣子的,這就是 Kubernetes 叢集的 期望狀態。

與 Kubernetes 對象工作 —— 是否建立、修改,或者刪除 —— 需要使用 Kubernetes API。當使用 kubectl 命令列介面時,比如,CLI 會使用必要的 Kubernetes API 呼叫,也可以在程式中直接使用 Kubernetes API。

三、對象的Spec和狀態

每個 Kubernetes 對象包含兩個嵌套的對象欄位,它們負責管理對象的配置:對象 spec 和 對象 statusspec 必須提供,它描述了對象的 期望狀態—— 希望對象所具有的特徵。status 描述了對象的 實際狀態,它是由 Kubernetes 系統提供和更新。在任何時刻,Kubernetes 控制平面一直處於活躍狀態,管理著對象的實際狀態以與我們所期望的狀態相匹配。

例如,Kubernetes Deployment 對象能夠表示運行在叢集中的應用。當建立 Deployment 時,可能需要設定 Deployment 的 spec,以指定該應用需要有 3 個副本在運行。Kubernetes 系統讀取 Deployment spec,啟動我們所期望的該應用的 3 個執行個體 —— 更新狀態以與 spec 相匹配。如果那些執行個體中有失敗的(一種狀態變更),Kubernetes 系統通過修正來響應 spec 和狀態之間的不一致 —— 這種情況,啟動一個新的執行個體來替換。

四、使用YAML定義資源

當建立 Kubernetes 對象時,必須提供對象的 spec,用來描述該對象的期望狀態,以及關於對象的一些基本資料(例如,名稱)。當使用 Kubernetes API 建立對象時(或者直接建立,或者基於kubectl),API 請求必須在請求體中包含 JSON 格式的資訊。更常用的是,需要在 .yaml 檔案中為 kubectl 提供這些資訊。 kubectl 在執行 API 請求時,將這些資訊轉換成 JSON 格式。舉個例子:

[[email protected] ~]# vim my-demo.yaml apiVersion: v1kind: Podmetadata:  name: my-demo  namespace: default  labels:    name: myapp    tier: appfrontspec:  containers:  - name: myapp    image: ikubernetes/myapp:v1  - name: busybox    image: busybox    command:    - "/bin/sh"    - "-c"    - "sleep 3600"[[email protected]-master ~]# kubectl apply -f my-demo.yaml

在想要建立的 Kubernetes 對象對應的 .yaml 檔案中,需要配置如下的欄位:

  • apiVersion - 建立該對象所使用的 Kubernetes API 的版本
  • kind - 想要建立的對象的類型
  • metadata - 協助識別對象唯一性的資料,包括一個 name 字串、UID 和可選的 namespace

可以通過kubectl explain的方式進行查看Deployment進行資源定義的欄位

[[email protected] ~]# kubectl explain deployment  #查看deployment的定義欄位,包含require標記的是必須欄位KIND:     DeploymentVERSION:  extensions/v1beta1DESCRIPTION:     DEPRECATED - This group version of Deployment is deprecated by     apps/v1beta2/Deployment. See the release notes for more information.     Deployment enables declarative updates for Pods and ReplicaSets.FIELDS:   apiVersion    <string>     APIVersion defines the versioned schema of this representation of an     object. Servers should convert recognized schemas to the latest internal     value, and may reject unrecognized values. More info:     https://git.k8s.io/community/contributors/devel/api-conventions.md#resources   kind    <string>     Kind is a string value representing the REST resource this object     represents. Servers may infer this from the endpoint the client submits     requests to. Cannot be updated. In CamelCase. More info:     https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds   metadata    <Object>     Standard object metadata.   spec    <Object>     Specification of the desired behavior of the Deployment.   status    <Object>     Most recently observed status of the Deployment.

 

Kubernetes學習之路(十一)之資源清單定義

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.