diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a5dc6b6 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +# Use the official Node.js 14 image as the base image +FROM node:14 + +# Set the working directory inside the container +WORKDIR /app + +# Copy package.json and package-lock.json to the working directory +COPY package*.json ./ + +# Install dependencies +RUN npm install + +# Copy the rest of the application code to the working directory +COPY . . + +# Expose port 4000 (assuming your Node.js application listens on this port) +EXPOSE 4000 + +# Set the command to run when the container starts +CMD ["npm", "start"] \ No newline at end of file diff --git a/README.md b/README.md index 580ec91..0024fe2 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Simple Node Express Hello World App -![localhost:3000](/public/images/localhost_3000.png?raw=true "Node & Express") +![localhost:4000](/public/images/localhost_4000.png?raw=true "Node & Express") # Steps : ``` @@ -10,6 +10,25 @@ npm install npm start - Go to localhost:3000 + Go to localhost:4000 ``` +# Prerequisites : +## Running kubernetes cluster +make sure you have running kubernetes cluster on cloud or on hosted locally (kind - minikube ..etc) +## Install liknkerd +https://www.digitalocean.com/community/tutorials/how-to-install-and-use-linkerd-with-kubernetes +## Install Linkerd-SMI extension for canary deployment +https://linkerd.io/2.15/tasks/linkerd-smi/#install-the-linkerd-smi-extension + +# Usage : + +``` + kubectl create ns + kubectl annotate ns linkerd.io/inject=enabled + cd node-express-hello-world/k8s + kubectl apply -f version-I -n + kubectl apply -f version-II -n + kubectl apply -f traffic-split.yaml -n + +``` diff --git a/app.js b/app.js index 643cb36..d07d801 100644 --- a/app.js +++ b/app.js @@ -9,7 +9,7 @@ app.use(express.static(__dirname + '/public')); app.get('/',routes.home); -var port = process.env.PORT || 3000; +var port = process.env.PORT || 4000; var server=app.listen(port,function(req,res){ console.log("Catch the action at http://localhost:"+port); diff --git a/k8s/traffic-split.yaml b/k8s/traffic-split.yaml new file mode 100644 index 0000000..4a4e3d3 --- /dev/null +++ b/k8s/traffic-split.yaml @@ -0,0 +1,12 @@ +apiVersion: split.smi-spec.io/v1alpha1 +kind: TrafficSplit +metadata: + name: my-app-traffic-split + namespace: node +spec: + service: my-app-service + backends: + - service: my-app-service-v1 + weight: 50m + - service: my-app-service-v2 + weight: 50m \ No newline at end of file diff --git a/k8s/version-I/deploy.yaml b/k8s/version-I/deploy.yaml new file mode 100644 index 0000000..f90aeaa --- /dev/null +++ b/k8s/version-I/deploy.yaml @@ -0,0 +1,23 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: my-app-v1 +spec: + replicas: 1 + selector: + matchLabels: + app: my-app + version: v1 + template: + metadata: + labels: + app: my-app + version: v1 + annotations: + linkerd.io/inject: enabled + spec: + containers: + - name: my-app + image: ahmednageh08/node-app:v1 + ports: + - containerPort: 4000 \ No newline at end of file diff --git a/k8s/version-I/svc.yaml b/k8s/version-I/svc.yaml new file mode 100644 index 0000000..b2ec21d --- /dev/null +++ b/k8s/version-I/svc.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Service +metadata: + name: my-app-service-v1 +spec: + selector: + app: my-app + version: v1 + ports: + - protocol: TCP + port: 4000 + targetPort: 4000 \ No newline at end of file diff --git a/k8s/version-II/deploy.yaml b/k8s/version-II/deploy.yaml new file mode 100644 index 0000000..993100c --- /dev/null +++ b/k8s/version-II/deploy.yaml @@ -0,0 +1,23 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: my-app-v2 +spec: + replicas: 1 + selector: + matchLabels: + app: my-app + version: v2 + template: + metadata: + labels: + app: my-app + version: v2 + annotations: + linkerd.io/inject: enabled + spec: + containers: + - name: my-app + image: ahmednageh08/node-app:v2 + ports: + - containerPort: 4000 \ No newline at end of file diff --git a/k8s/version-II/svc.yaml b/k8s/version-II/svc.yaml new file mode 100644 index 0000000..4d7cf6f --- /dev/null +++ b/k8s/version-II/svc.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Service +metadata: + name: my-app-service-v2 +spec: + selector: + app: my-app + version: v2 + ports: + - protocol: TCP + port: 4000 + targetPort: 4000 diff --git a/playbook.yaml b/playbook.yaml new file mode 100644 index 0000000..8e51ad8 --- /dev/null +++ b/playbook.yaml @@ -0,0 +1,28 @@ +- name: Deploy and run Node.js application + hosts: all + become: true + + tasks: + - name: Clone Node.js application repository + git: + repo: https://github.com/AhmedNageh08/node-express-hello-world.git + dest: /opt/node-express-hello-world + version: master + force: yes + + - name: Install npm package + apt: + name: npm + state: present + + - name: Install App dependencies + command: npm install + args: + chdir: /opt/node-express-hello-world + + - name: Start Node.js application in detached mode + shell: > + cd /opt/node-express-hello-world && + nohup npm start &> /dev/null & + echo $! > /var/run/node-express-hello-world.pid + diff --git a/views/home.ejs b/views/home.ejs index c23582e..8827790 100644 --- a/views/home.ejs +++ b/views/home.ejs @@ -1 +1 @@ -

Heyyo :)

+

Hey Devops Team :)