1. 程式人生 > >Kubernetes學習之路(十一)之資源清單定義

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

map latest dem kubectl 服務發現 bject 均衡 ima limit

一、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 格式。舉個例子:

[root@k8s-master ~]# vim my-demo.yaml 

apiVersion: v1
kind: Pod
metadata:
  name: my-demo
  namespace: default
  labels:
    name: myapp
    tier: appfront
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
  - name: busybox
    image: busybox
    command:
    - "/bin/sh"
    - "-c"
    - "sleep 3600"
[root@k8s-master ~]# kubectl apply -f my-demo.yaml

在想要創建的 Kubernetes 對象對應的 .yaml 文件中,需要配置如下的字段:

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

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

[root@k8s-master ~]# kubectl explain deployment  #查看deployment的定義字段,包含require標記的是必須字段
KIND:     Deployment
VERSION:  extensions/v1beta1

DESCRIPTION:
     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學習之路(十一)之資源清單定義