到了这个章节大家可能会开始回想,刚开始听到K8S时很多人都说Kubernetes的AutoScaling很厉害,亦或者是Kubernetes能够帮使用者在Cloud上省下不少钱,但Kubernetes到底是如何这到这些事情的呢?这章节我们就是来探讨此事。
在Kubernetes cluster当中,当丛集内部触发了某些条件时,丛集会开始所谓的AutoScaling,但是这些AutoScaling又是有分层级的,分别为:
这本篇章会透过这三种层级的Scaling来探讨Kubernetes是如何办到这件事的,以及如何触发这件事。
基於CPU使用率或特定资源条件自动增减ReplicationController、ReplicaSet与Deployment中Pod的数量。特定资源条件可以为CPU、Memory、Storage或其他自订条件。另外原本就无法自动增减的Pod是不能透过HPA进行autoScaling的,像是DaemonSet中的Pod。
HorizontalPodAutoScaler也是透过Kubernetes APi资源和控制器实现,由监控中的资源来决定控制器的行为。控制器会周期性的调整replicaSet/Deployment中Pod的数量,以使得Pod的条件资源能够低於临界值而不在继续的产生新的replica。
HorizontalPodAutoScaler每个周期时间会定期去查询所监视的目标以及指定的目标资源查询率,
来判断目标是否需要Scaling。
hpa.yaml
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: ironman-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: ironman
minReplicas: 2
maxReplicas: 5
targetCPUUtilizationPercentage: 50
desiredReplicas = ceil[currentReplicas * ( currentMetricValue / desiredMetricValue )]
VerticalPodAutoscaler有两个目标:
也因此VerticalPodAutoscaler透过几个方法达成该目标:
VerticalPodAutoscaler,它包括一个标签识别器label selector(匹配Pod)、资源策略resources policy(控制VPA如何计算资源)、更新策略update policy(控制资源变化应用到Pod)和推荐资源信息。
并且VPA能够透过VPA Controller做到以下几件事:
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: ironman
labels:
name: ironman
app: ironman
spec:
minReadySeconds: 5
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
selector:
matchLabels:
app: ironman
replicas: 1
template:
metadata:
labels:
app: ironman
spec:
containers:
- name: ironman
image: ghjjhg567/ironman:latest
imagePullPolicy: Always
ports:
- containerPort: 8100
resources:
limits:
cpu: "1"
memory: "2Gi"
requests:
cpu: 500m
memory: 256Mi
envFrom:
- secretRef:
name: ironman-config
command: ["./docker-entrypoint.sh"]
- name: redis
image: redis:4.0
imagePullPolicy: Always
ports:
- containerPort: 6379
- name: nginx
image: nginx
imagePullPolicy: Always
ports:
- containerPort: 80
volumeMounts:
- mountPath: /etc/nginx/nginx.conf
name: nginx-conf-volume
subPath: nginx.conf
readOnly: true
- mountPath: /etc/nginx/conf.d/default.conf
subPath: default.conf
name: nginx-route-volume
readOnly: true
- mountPath: "/var/www/html"
name: mypd
readinessProbe:
httpGet:
path: /v1/hc
port: 80
initialDelaySeconds: 5
periodSeconds: 10
volumes:
- name: nginx-conf-volume
configMap:
name: nginx-config
- name: nginx-route-volume
configMap:
name: nginx-route-volume
- name: mypd
persistentVolumeClaim:
claimName: pvc
Cluster层级的Scaler,当物件因资源不足而无法生成启动时,或者是丛集节点使用率过低时,ClusterAutoScaler会自动地去调节节点的数量。
这边以GCP为例
$ gcloud container clusters create cluster-name --num-nodes 30 \
--enable-autoscaling --min-nodes 15 --max-nodes 50 [--zone compute-zone]
$ gcloud container node-pools create pool-name --cluster cluster-name \
--enable-autoscaling --min-nodes 1 --max-nodes 5 [--zone compute-zone]
$ gcloud container clusters update cluster-name --enable-autoscaling \
--min-nodes 1 --max-nodes 10 --zone compute-zone --node-pool default-pool
$ gcloud container clusters update cluster-name --no-enable-autoscaling \
--node-pool pool-name [--zone compute-zone --project project-id]
今天我们了解了Kubernetes cluster在三种不同的层面分别使用的资源缩放策略,这也让我们在往後遇到不同的问题,能透过最为合适的方法去进行资源调配,节省资源的浪费。
https://www.servicemesher.com/blog/kubernetes-vertical-pod-autoscaler/
https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/
https://cloud.google.com/kubernetes-engine/docs/concepts/verticalpodautoscaler
https://cloud.google.com/kubernetes-engine/docs/how-to/cluster-autoscaler?hl=zh-tw
在前十天,建立起前端专案的基本架构,接下来进入後端的世界,建立後端的基础,之後与前端专案对接。 什麽...
上次《Pyhon X 金融分析 X Azure》系列使用Azure去做简单的金融体验,这次就继续拓展...
我们的系统架构很单纯,分为托管在 Vercel 上的 Next.js 前端,以及托管在 BlueHo...
函式是 JavaScript 非常重要的特性,指的是将一或多段程序包起来,之後可以重覆使用。Java...
Plugin 可以做什麽? 将 Plugin 套用於专案後,可以扩增专案的能力,它可以做到如: 扩充...