This is an automated tutorial instructing hot to use Kubernetes to achieve horizontal auto-scaling. The technology stack heavily involves Ansible, Kubernetes, Docker etc. For simplicity, we will first use Minikube to demonstrate the power of Kubernetes on local machine.
For the best user experience, please prepare a physical / virtual (not VirtualBox powered, please refer to reasons below) machine with:
- Ubuntu
- Xenial - v16.04
- Trusty - v14.04
- Python2 / Python3
- Git
- Ansible (v2.3+)
There are some packages that will be automatically installed, some with a fixed stable version.
- docker-ce (for reference v17.06 )
- WARNING Ansible would purge old apt repository for lxc-docker / docker.io
- virtualbox (fixed v5.1)
- WARNING Ansible would remove old package for v4.x / v5.0
- minikube (fixed v0.24.1)
- kubernetes (fixed v1.8.5)
There is a operate
script provided to operate over the Ansible playbook. Here is the example usage
>$./operate
Usage: ./operate {play|check} {playbook-file}
This always run on localhost
- Execute a playbook
>$./operate play {playbook-file}
...
SUDO password: <enter-your-sudo-password>
- Clean up a kubernetes cluster
./operate play shutdown-playbook.yml
...
SUDO password: <enter-your-sudo-password>
- Syntactic check a playbook
>$./operate check {playbook-file}
Note Running Ansible in check mode will not make any changes to the target systems as it only check for playbook and roles syntax error if any.
Here are some services that Kubernetes assists to scale horizontally to a predefined replication definition based on observed resource utilization.
Service Name | Container Repository | Resource Type | Resource Threshold | Replication (From -> To) | Note |
---|---|---|---|---|---|
Official PHP Apache | Google Container Repo | CPU | 50% | 1 -> 2 | |
Apache (Httpd) | Docker Hub Repository | CPU | 50% | 1 -> 2 |
This is the official example to demonstrate Horizontal Pod Autoscaler. The example uses an PHP server image which performs some CPU intensive computations so that it is easier to reach our goal.
- Start Official PHP Apache Playbook :
./operate play official-php-apache-autoscaler-playbook.yml`
After a few minutes, we will see Official PHP Apache service / pod launched as below
- Start Official PHP Apache DDOS Playbook:
./operate play official-php-apache-ddos-playbook.yml
Within seconds, we will observe increase in CPU consumption. As a results, the deployment will resize to 2 replicas as predefined.
-
Stop the load by pressing
Ctrl-C
After a short while, CPU utilization drops to 0. HPA auto-scaler removes one replica. -
Grafana as Monitor Server Grafana shows the resource trend as a observability tool.
This example is (a lot) less CPU-intensive than to the 'Official PHP Apache' example, thus it may takes longer (minutes) for Kubernetes to trigger the pod replication. Further, this example leverages Docker Hub repository.
- Start Apache Playbook :
./operate play httpd-autoscaler-playbook.yml
- Start Apache DDOS Playbook:
./operate play httpd-ddos-playbook.yml
- Stop the load by pressing
Ctrl-C
>$ minikube start
========================================
Starting local Kubernetes v1.8.0 cluster...
Starting VM...
E1215 13:47:28.650742 2593 start.go:156] Error starting host: Error creating host: Error executing step: Running precreate checks.
: This computer doesn't have VT-X/AMD-v enabled. Enabling it in the BIOS is mandatory
Please check the enable hardware virtualization support in the host OS.
As a side note, VirtualBox does not support VT-X/AMD-v in nested virtualization. For details, see the ticket
We should check minikube logs by
minikube logs
Here are some possible scenarios:
We should check pod status by
kubectl describe pod <pod-name>
Here are some possible scenarios: