k8s的資源分配

限制每一個實例

在建立一個replicationcontroller(如下簡稱rc)時,咱們能夠在建立文件中指定pod的資源配額,以下面的json:


 {
    "kind": "ReplicationController",
    "apiVersion": "v1",
    "metadata": {
        "name": "eatcpu",
        "creationTimestamp": null
    },
    "spec": {
        "replicas": 2,
        "selector": {
            "name": "huang"
        },
        "template": {
            "metadata": {
                "name": "cpu",
                "labels": {
                    "name": "huang"
                }
            },
            "spec": {
                "containers": [
                    {
                        "name": "eatcpucontainer",
                        "image": "registry.hub.abc.com.cn/eatcpu:v1.1",
                        "resources": {
                        "request": {
                                "cpu": "1.0",
                                "memory": "1.0Gi"
                            },
                            "limits": {
                                "cpu": "1.2",
                                "memory": "1.1Gi"
                            }
                        },
                        "command": [
                            "/deadloop",
                            "-max_procs=4"
                        ]
                    }
                ]
            }
        }
    },
    "status": {
        "replicas": 0
    }
}

固然實際上json不用寫這麼複雜,關鍵是:node

"resources": {
             "limits": {
                      "cpu": "1.0",
                      "memory": "1.0Gi"
            },
            "limits": {
                    "cpu": "1.2",
                    "memory": "1.1Gi"
              }
         },

這句的意思是給這個rc的每一個pod分配cpu額度的最低要求是1.0(即1個CPU核心),內存的最低要求是1.0Gi,對CPU的限制是不能超過1.2個核心,內存則是1.1Gi。
當咱們執行create命令的時候,若scheduler組件檢查各個nodes發現沒有知足1個空閒cpu核心和1Gi空閒內存的機器,那麼這個pod就不能被建立,若rc要建立3個pod,整個集羣只能知足建立2個,那麼第三個pod會被放入隊列中,等待集羣有足夠資源時再建立出來。git

另外,若pod在運行過程當中持續地消耗內存,超過了1.1G,pod會被銷燬並重啓,但當cpu消耗超過配額時,k8s不會作出相應的措施。github

k8s1.3左右的版本增長了horizontalAutoScale特性,當CPU在必定時間內高於一個閾值時,會出發控制器對其進行水平擴容。json

整個名稱空間下的資源配額

利用k8s搭建部署服務,並在正式的場合使用時,幾乎是確定要引入一個「用戶」概念的。可使用namespace來實現用戶的隔離。並使用quota爲每一個用戶指定配額。
注:這裏差很少是翻譯github上原文,引用的幾個yaml也出自於彼處,有興趣的能夠直接去看官文。api

k8s下默認的namespace是default,咱們能夠手動添加一個namespacetcp

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

接着咱們建立一個quota,quota能夠指定某個namespace有多少的資源配額,包括cpu,內存,persistentvolumeclaims(聽說是內存),pod數量,rc數量等等。
以下建立一個quotaide

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

建立了quota後咱們每次建立一個rc(也即pods)都要指定它的資源配額,不然即使建立成功,容器也不能run起來。
指定配額的方法見標題1,可是那是對一個集羣中的pod統一制定,咱們也能夠統一指定該namespace下的全部pods的配額,即建立一個limitsoop

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

如今咱們能夠正常地run一個rc了,而且,在容器成功跑起來後,咱們能夠統一地看到該namespace下的資源使用狀況:ui

kubectl describe quota quota --namespace=quota-example
Name:                   quota
Namespace:              default
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
相關文章
相關標籤/搜索