Kubernetes quick tutorial
A step-by-step process to create your first Kubernetes example
π Introduction
Let's make the simplest but understandable Kubernetes example ever.
Video version:
We will create our first example with Kubernetes starting from scratch, using: JavaScript Node.js Docker Docker Compose Kubernetes ## π Requirements Node.js Docker (with Kubernetes enabled) --- ## π Getting started Let's start in a new VS Code window (or any other editor of your choice). ### 1. Create a new projectbash
npm init -y
### 2. Install the dependencies
bash
npm i express
### 3. Create a new file called index.js
js
touch index.js

js
//simple express server
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(3000, () => {
console.log('Example app listening on port 3000!');
});
Run this by typing:
plaintext
node index.js

plaintext
touch .dockignore Dockerfile docker-compose.yml
### β .dockignore
Add node_modules
to the file

node_modules
folder when building the image.
### π³ Dockerfile
Open the Dockerfile
and add the following:
plaintext
FROM node:19-alpine
# Create app directory
WORKDIR /app
# Copy package.json and package-lock.json
COPY package*.json ./
# Install dependencies
RUN npm install
# Copy source code
COPY . .
# Expose port 3000
EXPOSE 3000
# Run the app
CMD [ "node", "index.js" ]
To build the app, let's populate the docker-compose.yml
file with the following:
yaml
version: '3.9'
services:
myapp:
container_name: myapp
image: myapp:1.0.0
build: .
ports:
- 3000:3000
Explanation of the docker-compose.yml
file:
version: '3.9'
- the version of the docker-compose file
services:
- the services (containers) we want to run. In this case, just one.
myapp:
- the name of the service
container_name: myapp
- the name of the container
image: myapp:1.0.0
- the name of the image
build: .
- the path to the Dockerfile. In this case, the current directory.
ports:
- the ports that we want to expose. In this case, port 3000 of the container will be exposed on port 3000 of the host machine.
Before we test it with Docker, let's modify the index.js file by changing Hello World!
to Hello Docker!
js
//simple express server
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello Docker!');
});
app.listen(3000, () => {
console.log('Example app listening on port 3000!');
});
Now let's run the container by typing this in the terminal:
bash
docker compose up

Hello Docker!
to Hello Kubernetes!
js
//simple express server
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello Kubernetes!');
});
app.listen(3000, () => {
console.log('Example app listening on port 3000!');
});
Then rebuild the image by typing:
plaintext
docker compose build
βΉοΈ This is not really necessary, but good to understand how it works.
---
## βΈοΈ Kubernetes
Before we start, be sure Kubernetes is enabled in Docker Desktop.

deployment.yaml
and add the following:
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:1.0
ports:
- containerPort: 3000
β οΈ You might see a warning in the last five lines, but it's only because this configuration might eat a lot of resources. But let's keep it simple for now.
Explanation of the deployment.yaml
file:
apiVersion: apps/v1
- the version of the Kubernetes API
kind: Deployment
- the kind of object we want to create. In this case, a deployment.
name: myapp
- the name of the deployment
replicas: 3
- the number of replicas of the deployment
image: myapp:1.0
- the image of the container. We defined this in the docker-compose.yml file
ports:
- the ports of the container. 3000 in this case.
Before we create the deployment, let's check if there are any deployments in the cluster:
bash
kubectl get deployments

bash
kubectl apply -f deployment.yaml
Then let's try to get the deployments again:
bash
kubectl get deployments
We can also get a detail of the pods:
bash
kubectl get pods

service.yaml
and add the following:
yaml
apiVersion: v1
kind: Service
metadata:
name: myapp
spec:
selector:
app: myapp
ports:
- port: 80
targetPort: 3000
type: LoadBalancer
This is enough to expose the deployment to the outside world (port 80)
Before we apply this configuration, let's check if there are any services in the cluster:
bash
kubectl get services

Kubernetes
but that's the default service that is created when you install Kubernetes.
Let's create the service by typing:
bash
kubectl apply -f service.yaml
Then let's try to get the services again:
bash
kubectl get services

localhost:80
in your browser and you should see Hello Kubernetes!

bash
kubectl delete -f deployment.yaml
kubectl delete -f service.yaml

All the code is available in the GitHub repository (link in the video description): youtu.be/noJmOirhHKM
That's all.
If you have any questions, drop a comment below.
Β