In this Article we are going to learn How to Deploy to Kubernetes using GitHub Actions | Deploy Node.js app to Kubernetes using GitHub Actions.Prerequisite:
- GitHub Repo or You can use our Existing GitHub Repo
Step #1:Create Dockerfile for Node.js App
We need to create Dockerfile for Node.js App using below code.
FROM node:14
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
RUN npm install express
COPY . .
EXPOSE 3000
CMD [ "node", "server.js" ]
Step #2:Create package.json file
After the first step we need to create package.json file using below code.
{
"name": "docker_web_app",
"version": "1.0.0",
"description": "Node.js on Docker",
"author": "First Last <first.last@example.com>",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.16.1"
}
}
Step #3:Create Server.js file
Now lets create server.js file
'use strict';
const express = require('express');
// Constants
const PORT = 3000;
const HOST = '0.0.0.0';
// App
const app = express();
app.get('/', (req, res) => {
res.send('Hello World');
});
app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);
Step #4:Create Kubernetes Deployment and Service file
Here lets create k8s-node-app.yaml file using below code.
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: nodejs-app
namespace: default
labels:
app: nodejs-app
spec:
replicas: 1
selector:
matchLabels:
app: nodejs-app
template:
metadata:
labels:
app: nodejs-app
spec:
containers:
- name: nodejs-app
image: "devopshint/node-app:latest"
ports:
- containerPort: 3000
---
apiVersion: v1
kind: Service
metadata:
name: nodejs-app
namespace: default
spec:
selector:
app: nodejs-app
type: NodePort
ports:
- name: http
targetPort: 3000
port: 80
Step #5:GitHub Actions Workflow to Deploy to Kubernetes using GitHub Actions
Now its time to create workflow so firstly create .github>>Workflows>>deploy-to-minikube-github-actions.yaml
name: Deploy to Minikube using GitHub Actions
on: [push]
jobs:
job1:
runs-on: ubuntu-latest
name: build Node.js Docker Image and deploy to minikube
steps:
- uses: actions/checkout@v2
- name: Start minikube
uses: medyagh/setup-minikube@master
- name: Try the cluster !
run: kubectl get pods -A
- name: Build image
run: |
export SHELL=/bin/bash
eval $(minikube -p minikube docker-env)
docker build -f ./Dockerfile -t devopshint/node-app:latest .
echo -n "verifying images:"
docker images
- name: Deploy to minikube
run:
kubectl apply -f k8s-node-app.yaml
- name: Test service URLs
run: |
minikube service list
minikube service nodejs-app --url
After that go to the actions and check your job is success or not
![](https://rushiinfotech.in/wp-content/uploads/2024/03/image-1-1024x398.png)
So you can see my job status is success
![](https://rushiinfotech.in/wp-content/uploads/2024/03/Screenshot-2023-02-21-at-4.06.10-PM-1024x540.png)
You can see the final output
We have covered Deploy to Kubernetes using GitHub Actions.
Conclusion:
In this article we have covered How to Deploy to Kubernetes using GitHub Actions | Deploy Node.js app to Kubernetes using GitHub Actions
Related Articles:
Push Docker Image to DockerHub Using GitHub Actions
Reference: