Kubernetes技術分析之資源管理

jopen 9年前發布 | 69K 次閱讀 Kubernetes

Docker的流行激活了一直不溫不火的PaaS,隨著而來的是各類Micro-PaaS的出現,Kubernetes是其中最具代表性的一員,它是 Google多年大規模容器管理技術的開源版本。本系列文章將逐一分析Kubernetes,本文主要通過一個例子介紹Kubernetes的資源管理機制(Limit Range和Resource Quota)。

Kubernetes資源管理

作為一個容器管理平臺,難免地會部署多套應用,如果沒有合理的資源管理機制,應用對資源的需求是不受限的,那么就很快會耗盡所有資源,影響到其他應用。所以需要對資源進行合理的分配,這是一項需要積累的課題。

資源隔離和限制,這是PaaS的基礎能力,Kubernetes對此也有初步的設計,有3 個層次的資源限制方式,分別在Container、Pod、Namespace 層次。Container層次主要利用容器本身的支持,比如Docker 對CPU、內存等的支持;Pod方面可以限制系統內創建Pod的資源范圍,比如最大或者最小的CPU、memory需求;Namespace層次就是對用戶級別的資源限額了,包括CPU、內存,還可以限定Pod、rc、service的數量。

Kubernetes中有2個元素Limit Range和Resource Quota用來進行資源管理,下面將采用一個例子進行介紹。
注意:kube-apiserver啟動參數需要設置“--admission_control=LimitRanger,ResourceQuota...”

示例

首先創建一個namespace,
namespace.yaml:

apiVersion: v1
kind: Namespace
metadata:
name: quota-example

$ kubectl create -f docs/user-guide/resourcequota/namespace.yaml
$ kubectl get namespaces
NAME             LABELS    STATUS
default          <none>    Active
quota-example    <none>    Active

默認情況下namespace是沒有資源配額的,現在給namespace設置配額,
quota.yaml:

apiVersion: v1
kind: ResourceQuota
metadata:
name: quota
spec:
hard:
cpu: "20"
memory: 1Gi
persistentvolumeclaims: "10"
pods: "10"
replicationcontrollers: "20"
resourcequotas: "1"
secrets: "10"
services: "5"

$ kubectl create -f docs/user-guide/resourcequota/quota.yaml --namespace=quota-example
$ kubectl describe quota quota --namespace=quota-example
Name:             quota
Namespace:        quota-example
Resource                   Used        Hard
--------                   ----        ----
cpu                        100m        20
memory                     536870912   1Gi
persistentvolumeclaims     0           10
pods                       1           10
replicationcontrollers     1           20
resourcequotas             1           1
secrets                    1           10
services                   0           5


可以看出資源配額包括2方面:

  • 計算資源配額
    cpu Total cpu limits of containers
    memory Total memory limits of containers
  • Kubernetes元素數量限制
    pods Total number of pods
    services Total number of services
    replicationcontrollers Total number of replication controllers
    resourcequotas Total number of resource quotas
    secrets Total number of secrets
    persistentvolumeclaims Total number of persistent volume claims
  • </ul>
    現在在namespace下創建Pod,
    nginx-rc.yaml:

    apiVersion: v1
    kind: ReplicationController
    metadata:
    name: nginx
    namespace: quota-example
    labels:
    name: nginx
    spec:
    replicas: 1
    selector:
    name: nginx
    template:
    metadata:
      labels:
        name: nginx
    spec:
      containers:
      - name: nginx
        image: nginx

    $ kubectl create -f ./niginx-rc.yaml
    $ kubectl describe rc nginx --namespace=quota-example
    ... Error creating: Pod "nginx-" is forbidden: Limited to 1Gi memory, but pod has no specified memory limit


    因為Pod沒有設置資源限制,Kubeneters會拒絕創建Pod。有2種方法可以解決,一是給Pod配置資源限制,
    nginx-rc.yaml:

    apiVersion: v1
    kind: ReplicationController
    metadata:
    name: nginx
    namespace: quota-example
    labels:
    name: nginx
    spec:
    replicas: 1
    selector:
    name: nginx
    template:
    metadata:
      labels:
        name: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        resources:
          limits:
            cpu: 100m
            memory: 100Mi

    另一種方法是可以設置Pod的默認資源限制:
    limits.yaml:

    apiVersion: v1
    kind: LimitRange
    metadata:
    name: limits
    spec:
    limits:
    - default:
      cpu: 100m
      memory: 100Mi
    type: Container

    $ kubectl create -f docs/user-guide/resourcequota/limits.yaml --namespace=quota-example
    $ kubectl describe limits limits --namespace=quota-example
    Name:       limits
    Namespace:  quota-example
    Type        Resource     Min    Max    Default
    ----        --------     ---    ---    ---
    Container    cpu         -      -      100m
    Container    memory      -      -      100Mi


    那么Pod就能創建成功了,那么相應的資源也消耗了:

    $ kubectl describe quota quota --namespace=quota-example
    Name:            quota
    Namespace:        quota-example
    Resource                 Used        Hard
    --------                 ----        ----
    cpu                      100m        20
    memory                   104857600   1Gi
    persistentvolumeclaims   0           10
    pods                     1           10
    replicationcontrollers   1           20
    resourcequotas           1           1
    secrets                  1           10
    services                 0           5


    Limit Range除了可設置Container之外,也可以設置Pod,

    limits.yaml:
    apiVersion: v1
    kind: LimitRange
    metadata:
    name: mylimits
    spec:
    limits:
    - max:
      cpu: "2"
      memory: 1Gi
    min:
      cpu: 250m
      memory: 6Mi
    type: Pod
    - default:
      cpu: 250m
      memory: 100Mi
    max:
      cpu: "2"
      memory: 1Gi
    min:
      cpu: 250m
      memory: 6Mi
    type: Container

    $ kubectl create -f limits.yaml --namespace=quota-example
    $ kubectl describe limits mylimits --namespace=quota-example
    Name:   mylimits
    Type      Resource  Min  Max Default
    ----      --------  ---  --- ---
    Pod       memory    6Mi  1Gi -
    Pod       cpu       250m   2 -
    Container memory    6Mi  1Gi 100Mi
    Container cpu       250m   2 250m


    這個設置為:
    1.一個Pod的所有容器內存使用必須在6Mi ~ 1Gi

    1. 一個Pod的所有容器的CPU使用必須在250m ~ 2 cores
    2. 一個容器的內存使用必須在6Mi ~ 1Gi, 默認是100Mi
    3. 一個容器的CPU使用必須在250m ~ 2 cores, 默認是250m

      參考


      ==========================================================
      作者簡介
      吳龍輝,現任網宿科技高級運營工程師,致力于云計算PaaS的研究和實踐,活躍于CloudFoundry,Docker,Kubernetes等開源社區,貢獻代碼和撰寫技術文檔。 郵箱:wulh@chinanetcenter.com/wlh6666@qq.com

      來自:http://dockone.io/article/581
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!