You may have heard the buzz around Kubernetes and noticed that many companies have been rapidly adopting it. Due to its many components and vast ecosystem it can be quite confusing to find where the path starts to learn it.
In this session, you will learn the basics of containers and Kubernetes. Step by step, we will go through the entire process of packaging a Node.js application into a Docker container image and then deploying it on Kubernetes. We will demonstrate scaling to multiple replicas for better performance. The end result will be a resilient and scalable Node.js deployment.
You will leave this session with sufficient knowledge of containerization, Kubernetes basics, and the ability to deploy highly available, performant, and scalable Node.js applications on Kubernetes.
💰 Use this free $200 credit to try out Kubernetes on DigitalOcean for free!
Kamal Nasser is a Developer Advocate at DigitalOcean. If not automating and playing with modern software and technologies, you’ll likely find him penning early 17th-century calligraphy. You can find Kamal on Twitter at @kamaln7 or on GitHub at @kamaln7.
View the slides for this talk, or watch the recording on YouTube.
Be sure to follow along with the recording for an explanation and replace kamaln7
with your own DockerHub username.
Create an empty node package: npm init -y
Install express as a dependency: npm install express
index.js
const express = require('express')
const os = require('os')
const app = express()
app.get('/', (req, res) => {
res.send(`Hi from ${os.hostname()}!`)
})
const port = 3000
app.listen(port, () => console.log(`listening on port ${port}`))
Dockerfile
FROM node:13-alpine
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm install --production
COPY . .
EXPOSE 3000
CMD node index.js
Build the image: docker build -t kamaln7/node-hello-app .
Edit index.js
and replace the word Hi
with Hello
.
Re-build the image and notice Docker re-using previous layers: docker build -t kamaln7/node-hello-app .
Run a container to test it: docker run --rm -d -p 3000:3000 kamaln7/node-hello-app
Look at the running containers: docker ps
Stop the container: docker stop CONTAINER_ID
Push the image to DockerHub: docker push kamaln7/node-hello-app
kubectl get nodes
kubectl create deployment --image kamaln7/node-hello-app node-app
kubectl scale deployment node-app --replicas 3
kubectl expose deployment node-app --type=NodePort --port 3000
kubectl get services
kubectl get nodes -o wide
IP:port
to test the servicekubectl edit service node-app
port: 3000
with port: 80
type: NodePort
with type: LoadBalancer
kubectl get service
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
very good tutorial!
How do we apply SSL / lets encrypt in kubernetes? Do you have any tutorial about this?
thanks
This is a really good tutorial. Most tutorials focus too much on the .yml config which is not easy to understand when just getting started. Also, the delivery is high quality and engaging.