This project is about learning Kubernetes.
- Prerequisite
- Getting started
- Postman Collection
- Localhost server
- API endpoints
- Docker Image
- Docker Container in localhost
- Tests
- Kubernetes files
This project depends on the following.
- NodeJS (v14 or higher)
- Docker (Optional. Needed only if you want to create docker image and run the project in a container.)
This is a NodeJS project. To get started, first install all the packages by running the following command in the terminal.
npm i
Check the docs
folder.
To start the localhost server.
npm run start
Check out the docs
folder. It has the Postman collection.
curl --location 'http://0.0.0.0:3000'
Response:
Status: 200 Content-Type: application/json
Body:
{
"data": {
"message": "Hello, World!",
"hostname": "Yusufs-MBP-2"
}
}
Use this for livenessProbe in Kubernetes.
When the app is live.
curl --location 'http://0.0.0.0:3000/liveness'
Response:
Status: 200 Content-Type: application/json
Body:
{
"data": {
"message": "I am live!"
}
}
When the app is not live.
curl --location 'http://0.0.0.0:3000/liveness'
Response:
Status: 500 Content-Type: application/json
Body:
{
"data": {
"message": "I am not live!"
}
}
This API will make the app liveness unhealthy. Pass the TTL in seconds.
Request:
{
"data": {
"ttl": 100
}
}
Default ttl: 60 seconds.
curl --location --request PUT 'http://0.0.0.0:3000/liveness/make/unhealthy' \
--header 'Content-Type: application/json' \
--data '{"data":{"ttl": 10}}'
Response:
Status: 200 Content-Type: application/json
Body:
{
"data": {
"message": "I am unhealthy!"
}
}
Use this for readinessProbe in Kubernetes.
When the app is ready.
curl --location 'http://0.0.0.0:3000/readiness'
Response:
Status: 200 Content-Type: application/json
Body:
{
"data": {
"message": "I am ready!"
}
}
When the app is not ready.
curl --location 'http://0.0.0.0:3000/readiness'
Response:
Status: 500 Content-Type: application/json
Body:
{
"data": {
"message": "I am not ready!"
}
}
This API will make the app readiness unhealthy. Pass the TTL in seconds.
Request:
{
"data": {
"ttl": 100
}
}
Default ttl: 60 seconds.
curl --location --request PUT 'http://0.0.0.0:3000/readiness/make/unhealthy' \
--header 'Content-Type: application/json' \
--data '{"data":{"ttl": 10}}'
Response:
Status: 200 Content-Type: application/json
Body:
{
"data": {
"message": "I am unhealthy!"
}
}
curl --location 'http://0.0.0.0:3000/version'
Response:
Status: 200 Content-Type: application/json
Body:
{
"data": {
"version": "0.1.0"
}
}
To change the version set the environment variable APPLICATION_VERSION=semver
.
Example: APPLICATION_VERSION=1.0.0
curl --location 'http://0.0.0.0:3000/metadata'
Response:
Status: 200 Content-Type: application/json
Body:
{
"data": {
"os": "darwin",
"hostname": "Yusufs-MacBook-Pro-2.local",
"uptime": {
"unit": "sec",
"quantity": "891910"
},
"memory": {
"unit": "bytes",
"quantity": "17179869184"
},
"cpus": [
{
"model": "Apple M1 Pro",
"speed": 24,
"times": {
"user": 99240520,
"nice": 0,
"sys": 57942390,
"idle": 219834960,
"irq": 0
}
}
]
}
}
This will kill the server, and it will not work anymore.
curl --location --request POST 'http://0.0.0.0:3000/die'
Response:
Status: 200 Content-Type: application/json
Body:
{
"data": {
"message": "Goodbye..."
}
}
curl --location 'http://0.0.0.0:3000/metrics'
Response:
Status: 200 Content-Type: text/plain
Body:
# HELP process_cpu_user_seconds_total Total user CPU time spent in seconds.
# TYPE process_cpu_user_seconds_total counter
process_cpu_user_seconds_total 0.22259
more line follows...
To run the test suite.
npm run test
DockerHub link of this project yusufshakeel/learn-k8s
To pull the image on your localhost.
docker pull yusufshakeel/learn-k8s
To run the docker container in localhost.
docker run -d -p 3000:3000 yusufshakeel/learn-k8s
This will start the server on port 3000.
Check the k8s
folder for Kubernetes files.
It's free 😃
MIT License Copyright (c) 2023 Yusuf Shakeel
Feeling generous 😃 Donate via PayPal