Kubernetes技术分析之资源管理

原文:http://www.open-open.com/lib/view/open1439386169661.html


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

现在在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
2. 一个Pod的所有容器的CPU使用必须在250m ~ 2 cores
3. 一个容器的内存使用必须在6Mi ~ 1Gi, 默认是100Mi
4. 一个容器的CPU使用必须在250m ~ 2 cores, 默认是250m
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值