美文网首页Kubernetes
Pod扩容和缩容

Pod扩容和缩容

作者: 王勇1024 | 来源:发表于2019-10-05 19:39 被阅读0次

在实际生产系统中,我们经常会遇到某个服务需要扩容的场景,也可能会遇到由于资源紧张或者工作负载降低而需要减少服务实例数量的场景。此时我们可以利用 Deployment/RC 的Scale机制来完成这些工作。

Kubernetes对Pod的扩容和缩容操作提供了手动自动两种模式,手动模式通过执行kubectl scale命令对一个 Deployment/RC 进行Pod副本数量的设置,即可一键完成。自动模式则需要用户根据某个性能指标或者自定义业务指标并指定Pod副本数量的范围,系统将自动在这个范围内根据性能指标的变化进行调整。

1. 手动扩容和缩容模式

以 Deployment nginx为例:

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80

已运行的Pod副本数量为3个:

$ kubectl get pods | grep nginx
nginx-deployment-76bf4969df-2zgwr       1/1     Running            0          4m15s
nginx-deployment-76bf4969df-fmcz2       1/1     Running            0          4m15s
nginx-deployment-76bf4969df-t7zrs       1/1     Running            0          4m15s

通过kubectl scale命令可以将Pod副本数量从初始的3个更新为5个:

$ kubectl scale deployment nginx-deployment --replicas 5
deployment.extensions/nginx-deployment scaled

--replicas设置为比当前Pod副本数量更小的数字,系统将会“杀掉”一些运行中的Pod,以实现应用集群缩容:

$ kubectl scale deployment nginx-deployment --replicas=1
deployment.extensions/nginx-deployment scaled

2. 自动扩容和缩容模式

从Kubernetes v1.1 版本开始,新增了名为Horizontal Pod Autoscaler(HPA)的控制器,用于实现基于CPU使用率进行自动Pod扩容和缩容的功能。

HPA控制器基于Master的kube-controller-manager服务启动参数--horizontal-pod-autoscaler-sync-period定义的时长(默认30s),周期性地检测目标Pod的CPU使用率,并在满足条件时对 Deployment/RC 或 Deployment 中的Pod副本数量进行调整,以符合用户定义的平均Pod CPU使用率。

Pod CPU使用率来源于HeapsterMetric-Server组件,所以需要预先安装好HeapsterMetric-Server,安装过程参考:
Kubernetes heapster监控插件安装
Kubernetes Metrics Server安装

创建HPA时可以使用kubectl autoscale命令进行快速创建或者使用yaml配置文件进行创建。

在创建HPA之前,需要已经存在一个 Deployment/RC 对象,并且该 Deployment/RC 中的Pod必须定义 resources.requests.cpu的资源请求值,如果不设置该值,则Heapster将无法收集到该Pod的CPU使用情况,会导致HPA无法正常工作。

下面通过为一个Deployment设置HPA,然后使用一个客户端对其进行压力测试,对HPA的用法进行示例。
以php-apache的Deployment为例,设置cpu request为200m,未设置limit上限的值:

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: php-apache
spec:
  replicas: 1
  template:
    metadata:
      name: php-apache
      labels:
        app: php-apache
    spec:
      containers:
      - name: php-apache
        image: siriuszg/hpa-example
        resources:
          requests:
            cpu: 200m
        ports:
        - containerPort: 80

在创建一个php-apache的Service,供客户端访问:

apiVersion: v1
kind: Service
metadata:
  name: php-apache
spec:
  ports:
  - port: 80
  selector:
    app: php-apache

接下来为Deployment “php-apache” 创建一个HPA控制器,在1和10之间调整Pod的副本数量,以使得平均Pod CPU使用率维持在50%。
使用kubectl autoscale命令进行创建:

kubectl autoscale deployment php-apache --min=1 --max=10 --cpu-percent=50

或者通过yaml配置文件来创建HPA,需要在scaleTargetRef字段指定需要管理的 Deployment/RC 的名字,然后设置minReplicasmaxReplicastargetCPUUtilizationPercentage参数:

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: php-apache
spec:
  scaleTargetRef:
    apiVersion: apps/v1beta1
    kind: Deployment
    name: php-apache
  minReplicas: 1
  maxReplicas: 10
  targetCPUUtilizationPercentage: 50

查看已创建的HPA:

$ kubectl get hpa
NAME         REFERENCE               TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
php-apache   Deployment/php-apache   <unknown>/50%   1         10        1          47s

然后,创建一个busybox Pod,用于对php-apache服务发起压力测试的请求:

apiVersion: v1
kind: Pod
metadata:
  name: busybox
spec:
  containers:
  - name: busybox
    image: busybox
    command: ["sleep", "3600"]

登录busybox容器,执行一个无限循环的wget命令来访问php-apache服务:

while true; do wget -q -O- http://php-apache > /dev/null; done

3. 遇到的问题

通过kubectl get hpa命令查看HPA状态时,TARGETS状态为unknown。
通过kubectl describe hpa查看HPA详细信息:

# kubectl describe hpa
Name:                                                  php-apache
Namespace:                                             default
Labels:                                                <none>
Annotations:                                           <none>
CreationTimestamp:                                     Sat, 05 Oct 2019 18:50:34 +0800
Reference:                                             Deployment/php-apache
Metrics:                                               ( current / target )
  resource cpu on pods  (as a percentage of request):  <unknown> / 50%
Min replicas:                                          1
Max replicas:                                          10
Deployment pods:                                       1 current / 0 desired
Conditions:
  Type           Status  Reason                   Message
  ----           ------  ------                   -------
  AbleToScale    True    SucceededGetScale        the HPA controller was able to get the target's current scale
  ScalingActive  False   FailedGetResourceMetric  the HPA was unable to compute the replica count: unable to get metrics for resource cpu: unable to fetch metrics from resource metrics API: the server could not find the requested resource (get pods.metrics.k8s.io)
Events:
  Type     Reason                        Age                  From                       Message
  ----     ------                        ----                 ----                       -------
  Warning  FailedGetResourceMetric       4s (x11 over 2m35s)  horizontal-pod-autoscaler  unable to get metrics for resource cpu: unable to fetch metrics from resource metrics API: the server could not find the requested resource (get pods.metrics.k8s.io)
  Warning  FailedComputeMetricsReplicas  4s (x11 over 2m35s)  horizontal-pod-autoscaler  failed to get cpu utilization: unable to get metrics for resource cpu: unable to fetch metrics from resource metrics API: the server could not find the requested resource (get pods.metrics.k8s.io)

可以看到failed to get cpu utilization: unable to get metrics for resource cpu: unable to fetch metrics from resource metrics API: the server could not find the requested resource (get pods.metrics.k8s.io)
原因可能是:

  1. 配置资源限额时拼写错误,致使资源限额添加失败。
  2. Metrics Server未安装。

解决方案:

  1. 修正资源限额配置。
  2. Kubernetes Metrics Server安装

等待一段时间后,观察HPA控制器收集到的Pod CPU使用率:

$ kubectl get hpa
NAME         REFERENCE               TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
php-apache   Deployment/php-apache   2156%/50%   1         10        1          47s

4. 参考文档

kubectl get hpa targets:unknow
K8S集群基于heapster的HPA测试

相关文章

  • Pod扩容和缩容

    在实际生产系统中,我们经常会遇到某个服务需要扩容的场景,也可能会遇到由于资源紧张或者工作负载降低而需要减少服务实例...

  • k8s集群相关对象

    1.ReplicationController扩容和缩容: 2.获取pod所对应的IP和容器内部所需要监听的端口,...

  • Pod自动扩容与缩容

    Pod自动扩容/缩容 HPA介绍 HPA基本工作原理 使用HPA前提条件 API聚合层 启用聚合层 基于资源指标 ...

  • k8s滚动升级和扩缩容

    一、扩缩容 手动扩容 k8s使用过kubectl scale命令进行扩容 假设原本的pod有3个,这个时候由于业务...

  • 2.8、深入pod-扩容与缩容

    1、 手动扩容 我理解是对通过ReplicationController控制的pod,通过kubectl scal...

  • Redis集群扩容和缩容

    https://blog.csdn.net/zsj777/article/details/80235568

  • Redis 集群扩容和缩容

    集群的伸缩 这里也是使用的docker部署的扩容就是在之前集群的compose中加入了两个容器作为一主和一从 下面...

  • 59 Arraylist

    ArrayList源码解读 扩容(1.5倍扩容)和缩容存放元素: 有序线程是否安全 不安全get add 方法 数...

  • 3 深入掌握Pod(1)

    本章主要讲Pod和容器的使用、应用配置管理、Pod的控制和调度管理、Pod的升级和回滚,以及Pod的扩缩容机制等内...

  • 实现自己的动态数组

    GenericArray 属性与构造函数 关键点 动态扩容resize 缩容

网友评论

    本文标题:Pod扩容和缩容

    本文链接:https://www.haomeiwen.com/subject/zndepctx.html