feat: add hpa-vpa (#23)
This commit is contained in:
parent
8d30971869
commit
83a374193f
|
@ -0,0 +1,46 @@
|
||||||
|
---
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: hpa-example
|
||||||
|
spec:
|
||||||
|
replicas: 1
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app: hpa-example
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: hpa-example
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: hpa-example
|
||||||
|
image: k8s.gcr.io/hpa-example
|
||||||
|
ports:
|
||||||
|
- containerPort: 80
|
||||||
|
resources:
|
||||||
|
requests:
|
||||||
|
cpu: 200m
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: hpa-example
|
||||||
|
spec:
|
||||||
|
ports:
|
||||||
|
- port: 80
|
||||||
|
selector:
|
||||||
|
app: hpa-example
|
||||||
|
---
|
||||||
|
apiVersion: autoscaling/v1
|
||||||
|
kind: HorizontalPodAutoscaler
|
||||||
|
metadata:
|
||||||
|
name: hpa-example
|
||||||
|
spec:
|
||||||
|
maxReplicas: 10
|
||||||
|
minReplicas: 1
|
||||||
|
scaleTargetRef:
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
name: hpa-example
|
||||||
|
targetCPUUtilizationPercentage: 50
|
|
@ -1,13 +0,0 @@
|
||||||
---
|
|
||||||
apiVersion: autoscaling/v1
|
|
||||||
kind: HorizontalPodAutoscaler
|
|
||||||
metadata:
|
|
||||||
name: simple-hpa
|
|
||||||
spec:
|
|
||||||
maxReplicas: 10
|
|
||||||
minReplicas: 3
|
|
||||||
scaleTargetRef:
|
|
||||||
apiVersion: extensions/v1beta1
|
|
||||||
kind: Deployment
|
|
||||||
name: simple-deployment
|
|
||||||
targetCPUUtilizationPercentage: 80
|
|
|
@ -1,5 +1,7 @@
|
||||||
# HPA, VPA
|
# HPA, VPA
|
||||||
|
|
||||||
|
## HPA
|
||||||
|
|
||||||
HPA means `Horizontal Pod Autoscaler`. It automatically scales the number of pods in a replication controller, deployment or replica set based on observed CPU utilization (or, with custom metrics support, on some other application-provided metrics).
|
HPA means `Horizontal Pod Autoscaler`. It automatically scales the number of pods in a replication controller, deployment or replica set based on observed CPU utilization (or, with custom metrics support, on some other application-provided metrics).
|
||||||
|
|
||||||
Let's take an example with CPU usage:
|
Let's take an example with CPU usage:
|
||||||
|
@ -13,9 +15,9 @@ spec:
|
||||||
maxReplicas: 10
|
maxReplicas: 10
|
||||||
minReplicas: 3
|
minReplicas: 3
|
||||||
scaleTargetRef:
|
scaleTargetRef:
|
||||||
apiVersion: extensions/v1beta1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
name: simple-deployment
|
name: my-deployment
|
||||||
targetCPUUtilizationPercentage: 80
|
targetCPUUtilizationPercentage: 80
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -24,3 +26,71 @@ targetCPUUtilizationPercentage: 80
|
||||||
* `minReplicas`: the minimum number of pods running
|
* `minReplicas`: the minimum number of pods running
|
||||||
* `scaleTargetRef`: what this HPA targets, here a `Deployment` named `simple-deployment`
|
* `scaleTargetRef`: what this HPA targets, here a `Deployment` named `simple-deployment`
|
||||||
* `targetCPUUtilizationPercentage`: the percentage of CPU utilization to cross to activate the HPA
|
* `targetCPUUtilizationPercentage`: the percentage of CPU utilization to cross to activate the HPA
|
||||||
|
|
||||||
|
Let's see an HPA in action. This exemple is inspired by [the official Kubernetes documentation](https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/).
|
||||||
|
|
||||||
|
To see it in action, we need to have a workload that consumes some CPU. We'll use an image that is CPU intensive: https://hub.docker.com/r/googlecontainer/hpa-example.
|
||||||
|
|
||||||
|
This image computes the square root of numbers:
|
||||||
|
|
||||||
|
```php
|
||||||
|
<?php
|
||||||
|
$x = 0.0001;
|
||||||
|
for ($i = 0; $i <= 1000000; $i++) {
|
||||||
|
$x += sqrt($x);
|
||||||
|
}
|
||||||
|
echo "OK!";
|
||||||
|
?>
|
||||||
|
```
|
||||||
|
|
||||||
|
First, you need to active the [`metric-server`](https://github.com/kubernetes-incubator/metrics-server/) on minikube:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
minikube addons enable metrics-server
|
||||||
|
```
|
||||||
|
|
||||||
|
Review and apply the file [01-hpa.yml](./01-hpa.yml).
|
||||||
|
|
||||||
|
Now let's generate some load on our service:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
kubectl run -i --tty load-generator --image=busybox /bin/sh
|
||||||
|
```
|
||||||
|
|
||||||
|
Then run in this terminal:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
while true; do wget -q -O- http://hpa-example; done
|
||||||
|
```
|
||||||
|
|
||||||
|
In another terminal look at the state of the pods and the hpa, remember the `-w` flag on `kubectl`.
|
||||||
|
|
||||||
|
Wait a bit and see how it changes the pods and the hpa.
|
||||||
|
|
||||||
|
Stop the load generator, again, wait a bit and see how it changes the pods and the hpa.
|
||||||
|
|
||||||
|
## VPA
|
||||||
|
|
||||||
|
VPA means `Vertical Pod Autoscaler`. It automatically find the right resources for pods in a replication controller, deployment or replica set based on observed CPU utilization.
|
||||||
|
|
||||||
|
This feature is in beta, understand you can install it in your cluster but it's not integrate with the standard k8s source code.
|
||||||
|
|
||||||
|
If you are adventurous you can try it by:
|
||||||
|
|
||||||
|
1. Install [metrics-server](https://github.com/kubernetes-incubator/metrics-server#deployment).
|
||||||
|
1. Install the [vpa](https://github.com/kubernetes/autoscaler/tree/master/vertical-pod-autoscaler).
|
||||||
|
1. Try the exemple from the [vpa github repo](https://github.com/kubernetes/autoscaler/tree/master/vertical-pod-autoscaler#test-your-installation)
|
||||||
|
|
||||||
|
## Exercices
|
||||||
|
|
||||||
|
Nothing to see here.
|
||||||
|
|
||||||
|
## Clean up
|
||||||
|
|
||||||
|
```bash
|
||||||
|
kubectl delete service,deployment,pod,crd,vpa --all
|
||||||
|
```
|
||||||
|
|
||||||
|
## Links
|
||||||
|
|
||||||
|
* https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/
|
||||||
|
|
Loading…
Reference in New Issue