Pod 自动扩容:可以根据 CPU 使用率或自定义指标(metrics)自动对 Pod 进行扩/缩容。
控制管理器每隔30s(可以通过–horizontal-pod-autoscaler-sync-period修改)查询metrics的资源使用情况
- 支持三种metrics类型
- 预定义metrics(比如Pod的CPU)以利用率的方式计算
- 自定义的Pod metrics,以原始值(raw value)的方式计算
- 自定义的object metrics
- 支持两种metrics查询方式:Heapster和自定义的REST API
- 支持多metrics
通常用于 Deployment,不适用于无法扩/缩容的对象,如 DaemonSet
实现 cpu 或内存的监控,首先有个前提条件是该对象必须配置了 resources.requests.cpu 或 resources.requests.memory 才可以,可以配置当 cpu/memory 达到上述配置的百分比后进行扩容或缩容
1.创建一个 HPA:
#修改配置文件 #加入资源限制 [root@k8s-master deployments]# vi nginx-deploy.yaml
apiVersion: apps/v1 kind: Deployment metadata: labels: app: nginx-deploy name: nginx-deploy namespace: default spec: replicas: 1 revisionHistoryLimit: 10 selector: matchLabels: app: nginx-deploy strategy: rollingUpdate: maxSurge: 25% maxUnavailable: 25% type: RollingUpdate template: metadata: labels: app: nginx-deploy spec: containers: - image: nginx:1.7.9 imagePullPolicy: IfNotPresent name: nginx #加入资源限制 resources: limits: cpu: 200m memory: 128Mi requests: cpu: 10m memory: 128Mi restartPolicy: Always terminationGracePeriodSeconds: 30
# 更新操作 [root@k8s-master deployments]# kubectl replace -f nginx-deploy.yaml deployment.apps/nginx-deploy replaced
# 查看 只有一个副本 [root@k8s-master deployments]# kubectl get deploy nginx-deploy NAME READY UP-TO-DATE AVAILABLE AGE nginx-deploy 1/1 1 1 18h
创建一个 HPA:
- 先准备一个好一个有做资源限制的 deployment
- 执行命令 kubectl autoscale deploy nginx-deploy --cpu-percent=20 --min=2 --max=5
- 通过 kubectl get hpa 可以获取 HPA 信息
[root@k8s-master deployments]# kubectl autoscale deploy nginx-deploy --cpu-percent=20 --min=2 --max=5 horizontalpodautoscaler.autoscaling/nginx-deploy autoscaled #现在再次查看 副本自动扩容到了2个 [root@k8s-master deployments]# kubectl get deploy nginx-deploy NAME READY UP-TO-DATE AVAILABLE AGE nginx-deploy 2/2 2 2 18h
未安装 Metrics Server 查看 pod 内存、CPU 将报错
[root@k8s ~]# kubectl top pods --all-namespaces error: Metrics API not available
Pod 的水平自动缩放(前提需要安装插件metrics-server )
2.Metrics Server安装
一、使用阿里云的components.yaml配置文件
aliyun-components.yaml
[root@k8s-master ~]# rz [root@k8s-master ~]# ls aliyun-components.yaml anaconda-ks.cfg #查看文件 [root@k8s-master ~]# cat aliyun-components.yaml
二、执行安装命令
[root@k8s-master ~]# kubectl apply -f aliyun-components.yaml
执行完输出内容:
serviceaccount/metrics-server created clusterrole.rbac.authorization.k8s.io/system:aggregated-metrics-reader created clusterrole.rbac.authorization.k8s.io/system:metrics-server created rolebinding.rbac.authorization.k8s.io/metrics-server-auth-reader created clusterrolebinding.rbac.authorization.k8s.io/metrics-server:system:auth-delegator created clusterrolebinding.rbac.authorization.k8s.io/system:metrics-server created service/metrics-server created deployment.apps/metrics-server created apiservice.apiregistration.k8s.io/v1beta1.metrics.k8s.io created
三、查看效果,验证是否安装成功
[root@k8s-master ~]# kubectl get svc metrics-server -n kube-system NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE metrics-server ClusterIP 10.105.89.80 <none> 443/TCP 152m # 使用kubectl top node 查看资源使用情况 [root@k8s-master ~]# kubectl top node NAME CPU(cores) CPU% MEMORY(bytes) MEMORY% k8s-master 91m 4% 912Mi 24% k8s-node1 31m 1% 756Mi 20% [root@k8s-master ~]# kubectl top pods --all-namespaces NAMESPACE NAME CPU(cores) MEMORY(bytes) kube-flannel kube-flannel-ds-8fxmx 3m 18Mi kube-flannel kube-flannel-ds-xfmpr 4m 20Mi kube-system coredns-65c54cc984-7vd4t 1m 17Mi kube-system coredns-65c54cc984-qqv8m 1m 16Mi kube-system etcd-k8s-master 9m 67Mi kube-system kube-apiserver-k8s-master 34m 228Mi kube-system kube-controller-manager-k8s-master 12m 49Mi kube-system kube-proxy-l99bw 1m 17Mi kube-system kube-proxy-x8tdf 1m 16Mi kube-system kube-scheduler-k8s-master 2m 20Mi kube-system metrics-server-b9f7b695f-bljhg 3m 20Mi
至此,metrics-server安装完成
[root@k8s-master deployments]# kubectl top pods NAME CPU(cores) MEMORY(bytes) nginx-deploy-56696fbb5-4ksdj 0m 1Mi nginx-deploy-56696fbb5-wtjtq 0m 1Mi
3.创建一个servie
[root@k8s-master deployments]# kubectl create -f nginx-svc.yaml service/nginx-svc created
apiVersion: v1 kind: Service metadata: name: nginx-svc labels: app: nginx spec: selector: app: nginx-deploy ports: - port: 80 targetPort: 80 name: web type: NodePort
[root@k8s-master deployments]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE nginx-svc NodePort 10.100.220.235 <none> 80:31590/TCP 55s
[root@k8s-master deployments]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx-deploy-56696fbb5-4ksdj 1/1 Running 0 37m 10.244.1.46 k8s-node1 <none> <none> nginx-deploy-56696fbb5-wtjtq 1/1 Running 0 40m 10.244.1.45 k8s-node1 <none> <none>
4.测试
找到对应服务的 service,编写循环测试脚本提升内存与 cpu 负载
[root@k8s-node1 ~]# while true; do wget -q -O- http://10.100.220.235 > /dev/null ; done
[root@k8s-master deployments]# kubectl top pods NAME CPU(cores) MEMORY(bytes) nginx-deploy-56696fbb5-4ksdj 0m 1Mi nginx-deploy-56696fbb5-bdkx2 2m 1Mi nginx-deploy-56696fbb5-wtjtq 0m 1Mi nginx-deploy-56696fbb5-zrdb7 0m 1Mi
#400%/20% [root@k8s-master deployments]# kubectl get hpa NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE nginx-deploy Deployment/nginx-deploy 400%/20% 2 5 2 67m #查看已经扩容到5个副本了 [root@k8s-master deployments]# kubectl get deploy NAME READY UP-TO-DATE AVAILABLE AGE nginx-deploy 5/5 5 5 19h #暂停之前死循环的命令 等一会再次查看 [root@k8s-master deployments]# kubectl get hpa NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE nginx-deploy Deployment/nginx-deploy 0%/20% 2 5 5 69m #pod变化 [root@k8s-master deployments]# kubectl get pods NAME READY STATUS RESTARTS AGE nginx-deploy-c4986b7f-8tplt 1/1 Running 0 6m24s nginx-deploy-c4986b7f-qltv6 1/1 Running 0 7m2s #deployment变化 [root@k8s-master deployments]# kubectl get deploy nginx-deploy NAME READY UP-TO-DATE AVAILABLE AGE nginx-deploy 2/2 2 2 19h #发现已经自动完整了扩容缩容