Skip to content

Commit df5a876

Browse files
committed
chore: chapter 12
1 parent dd0ca7a commit df5a876

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM node:24-slim
2+
EXPOSE 8080
3+
COPY app.js package.json /app/
4+
WORKDIR /app
5+
CMD ["npm", "start"]
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
```
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { createServer } from 'node:http'
2+
import { hostname } from 'node:os'
3+
4+
const version = 1
5+
6+
const server = createServer((_req, res) => {
7+
res.end(`Hello from ${hostname()} (v${version})`)
8+
})
9+
10+
server.listen(8080)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "08-scaling-an-app-on-kubernetes",
3+
"version": "1.0.0",
4+
"description": "This example demonstrates how to implement peer-to-peer load balancing",
5+
"type": "module",
6+
"scripts": {
7+
"start": "node app.js"
8+
},
9+
"engines": {
10+
"node": ">=24"
11+
},
12+
"engineStrict": true,
13+
"keywords": [],
14+
"author": "Luciano Mammino and Mario Casciaro",
15+
"license": "MIT",
16+
"devDependencies": {},
17+
"dependencies": {}
18+
}

0 commit comments

Comments
 (0)