|
| 1 | +# 08-scaling-an-app-on-kubernetes |
| 2 | + |
| 3 | +This examples demonstrates how to deploy and scale a simple Node.js webserver on Kubernetes (using minikube) |
| 4 | + |
| 5 | +## Requirements |
| 6 | + |
| 7 | +Install [docker](https://docs.docker.com/get-docker/) and [minikube](https://kubernetes.io/docs/tasks/tools/install-minikube/) |
| 8 | + |
| 9 | + |
| 10 | +Once minikube is initialized (`minikube start`), on mac and linux, make sure to run `eval $(minikube docker-env)` to connect the docker daemon to the minikube virtual machine. |
| 11 | + |
| 12 | +## Run |
| 13 | + |
| 14 | +### 1. Build the image |
| 15 | + |
| 16 | +Build the docker image with: |
| 17 | + |
| 18 | +```bash |
| 19 | +docker build -t hello-web:v1 . |
| 20 | +``` |
| 21 | + |
| 22 | +### 2. Add the app to the kubernetes cluster |
| 23 | + |
| 24 | +Start the minikube kubernetes dashboard to see how you local cluster changes with the various interactions we are going to perform: |
| 25 | + |
| 26 | +```bash |
| 27 | +minikube dashboard |
| 28 | +``` |
| 29 | + |
| 30 | +Now, add the app to the kubernetes cluster with: |
| 31 | + |
| 32 | +```bash |
| 33 | +kubectl create deployment hello-web --image=hello-web:v1 |
| 34 | +``` |
| 35 | + |
| 36 | +To be able to access the application from your laptop you need to expose it: |
| 37 | + |
| 38 | +```bash |
| 39 | +kubectl expose deployment hello-web --type=LoadBalancer --port=8080 |
| 40 | +minikube service hello-web |
| 41 | +``` |
| 42 | + |
| 43 | +### 3. Scale the number of instances |
| 44 | + |
| 45 | +Now let's scale the application to 5 instances: |
| 46 | + |
| 47 | +```bash |
| 48 | +kubectl scale --replicas=5 deployment hello-web |
| 49 | +``` |
| 50 | + |
| 51 | +Try to make few requests, you should see that the response will provide different hostnames. |
| 52 | + |
| 53 | +### 4. Update the image |
| 54 | + |
| 55 | +Now let's make some changes to our app and deploy a new version. |
| 56 | + |
| 57 | +Change `version` to `2` in `app.js` and rebuild a new image version with: |
| 58 | + |
| 59 | +```bash |
| 60 | +docker build -t hello-web:v2 . |
| 61 | +``` |
| 62 | + |
| 63 | +update the deployment: |
| 64 | + |
| 65 | +```bash |
| 66 | +kubectl set image deployment/hello-web hello-web=hello-web:v2 --record |
| 67 | +``` |
| 68 | + |
| 69 | +Wait few seconds and make some new requests. You should now see v2. |
| 70 | + |
| 71 | + |
| 72 | +### Cleanup |
| 73 | + |
| 74 | +Reduce all the pods to 0 (stop the app): |
| 75 | + |
| 76 | +```bash |
| 77 | +kubectl scale --replicas=0 deployment hello-web |
| 78 | +``` |
| 79 | + |
| 80 | +Delete the deployment: |
| 81 | + |
| 82 | +```bash |
| 83 | +kubectl delete -n default deployment hello-web |
| 84 | +``` |
| 85 | + |
| 86 | +Delete the service: |
| 87 | + |
| 88 | +```bash |
| 89 | +kubectl delete -n default service hello-web |
| 90 | +``` |
| 91 | + |
| 92 | +You can finally stop minikube by running: |
| 93 | + |
| 94 | +```bash |
| 95 | +minikube stop |
| 96 | +``` |
0 commit comments