手动扩缩容机制
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment-v2
spec:
selector:
matchLabels:
app: nginx
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
查看现在的副本数量
[root@scms01 day9]# kubectl get pods |grep v2
nginx-deployment-v2-57c68fcd95-7w67j 1/1 Running 0 5m41s
nginx-deployment-v2-57c68fcd95-hq5zt 1/1 Running 0 5m41s
nginx-deployment-v2-57c68fcd95-zbwvw 1/1 Running
把副本数量更新到5个
kubectl scale deployment nginx-deployment-v2 --replicas 5
deployment.apps/nginx-deployment-v2 scaled
查看现在的副本数量
[root@scms01 day9]# kubectl get pods |grep v2
nginx-deployment-v2-57c68fcd95-75phl 1/1 Running 0 3s
nginx-deployment-v2-57c68fcd95-7w67j 1/1 Running 0 6m36s
nginx-deployment-v2-57c68fcd95-8vvhg 1/1 Running 0 3s
nginx-deployment-v2-57c68fcd95-hq5zt 1/1 Running 0 6m36s
nginx-deployment-v2-57c68fcd95-zbwvw 1/1 Running 0 6m36s
把副本数减少到1个
[root@scms01 day9]# kubectl scale deployment nginx-deployment-v2 --replicas 1
查看现在的副本数量
[root@scms01 day9]# kubectl get pods |grep v2
nginx-deployment-v2-57c68fcd95-zbwvw 1/1 Running 0 6m53s
自动扩缩容机制
HorizontalPodAutoScaler配置
HorizontalPodAutoScaler资源对象来提供资源的扩缩容操作
- 基于autoccaling/v1版本的HorizontalPod/Autoscaler配置
#资源对象HorizontalPodAutoscalar处于 autoscaling/v1中
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metedata:
name: php-apche
spec:
# 目标作用对象可以是Deployment,ReplicationController或ReplicaSet
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: php-apache
#pod副本数量的最大值和最小值,系统在这个范围内扩缩容
minReplicas: 1
maxReplicas: 10
#期望每个pod的CPU使用率都为50%,改使用率基于pod设置的CPU Request值计算,例如设置的200m
#实际cpu的使用值为100m
targetCPUUtilizationPercentage: 50
- 基于autoscaling/v2beta2版本的HorizontalPodAutoScaler配置
-
#资源对象HorizontalPodAutoscalar处于 autoscaling/v1中 apiVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscaler metedata: name: php-apche spec: # 目标作用对象可以是Deployment,ReplicationController或ReplicaSet scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: php-apache #pod副本数量的最大值和最小值,系统在这个范围内扩缩容 minReplicas: 1 maxReplicas: 10 #目至少值,在metrics 中通过type定义指标的类型 metrics: - type: Resource resource: name: cpu #通过target定义相应的指标目标值,系统将在指标到达目标值时触发扩缩容操作 target: type: Utilization averageUtilization: 50
metrics中type设置为一下四种类型:
-
Resource: 当前对象下pod的CPU和Memory指标,只支持Utilization和AverageValue类型的目标,对于CPU使用率,在target中的设置averageUtilization 定义目标平均CPU使用率。对于内存资源,在target中通过设置AverageValue定义目标平均内存使用值
-
Pods,指的是对象Pods的指标,需要需要第三方Adapter提供,支持Value和AverageValue类型的目标值
-
Object,k8s内部对象指标,也由第三方Adapter提供,只支持Value和AverageValue类型的目标值
-
External, 指的是k8s外部的指标,同样需要第三方Adapter提供,只支持Value和AverageValue类型的目标值