diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 000000000..9fe4451b5 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,13 @@ +Dockerfile +.gitignore +.dockerignore +.git +README.md +README_es.md +mongodb +mongodb/* +docker-compose.yml +license +aws-k8s-replication.yml +k8s-node-todo.yml +Jenkinsfile diff --git a/.gitignore b/.gitignore index a72b52ebe..54efcc1ef 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ results npm-debug.log node_modules +mongodb diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..469749ae3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,24 @@ +FROM node:alpine +MAINTAINER uzzal, uzzal2k5@gmail.com +WORKDIR /node-todo +COPY . . +RUN npm install + +# CONFIG STANDARD ERROR LOG +RUN ln -sf /dev/stdout /var/log/access.log \ + && ln -sf /dev/stderr /var/log/error.log + +# SET HEALTH CHECK +HEALTHCHECK --interval=5s \ + --timeout=5s \ + CMD curl -f http://127.0.0.1:8000 || exit 1 + +RUN echo 'http://dl-cdn.alpinelinux.org/alpine/v3.6/main' >> /etc/apk/repositories +RUN echo 'http://dl-cdn.alpinelinux.org/alpine/v3.6/community' >> /etc/apk/repositories +RUN apk update +#RUN apk add mongodb=3.4.4-r0 +#RUN mongo --version +EXPOSE 8080 + + +ENTRYPOINT ["node", "server.js"] \ No newline at end of file diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 000000000..bac83bffc --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,134 @@ +#!groovy +import groovy.json.JsonSlurperClassic +def DOCKER_BUILD_SERVER = "unix:///var/run/docker.sock" +def DOCKER_IMAGE_REPOSITORY = "uzzal2k5" +def GIT_REPOSITORY_NAME = "https://github.com/uzzal2k5/node-todo.git" +def version, revision +def BRANCH = 'master' + + +//Version & Release Specified Here +def getVersion(def projectJson){ + def slurper = new JsonSlurperClassic() + project = slurper.parseText(projectJson) + slurper = null + return project.version.split('-')[0] +} + + +// REPOSITORY CLONE FROM GIT +def CloneFromGit( REPOSITORY_NAME,BRANCH ){ + def version, revision + try { + git(branch: "${BRANCH}", + changelog: true, + credentialsId: 'github_credentials', + poll: true, + url: "${REPOSITORY_NAME }" + ) + } + catch (Exception e) { + println 'Some Exception happened here ' + throw e + + } + finally { + revision = version + "-" + sprintf("%04d", env.BUILD_NUMBER.toInteger()) + println "Start building revision $revision" + + } + return this +} + + +// DOCKER IMAGE BUILD & PUSH TO REGISTRY +// def DockerImageBuild( DOCKER_BUILD_SERVER, IMAGE_REPOSITORY, IMAGE_NAME ){ +// +// // DOCKER IMAGE BUILD +// withDockerServer([uri: "${DOCKER_BUILD_SERVER}"]) { +// stage('IMAGE BUILD'){ +// todoImages = docker.build("${IMAGE_REPOSITORY}/${IMAGE_NAME}") +// } + +// +// //PUSH TO REGISTRY +// stage('PUSH IMAGE'){ +// withDockerRegistry(credentialsId: 'dockerhub_credentials', url: '') { +// todoImages.push("${env.BUILD_NUMBER}") +// todoImages.push("latest") +// } +// +// } +// +// } +// return this +// } +// def ScanWithSynk(){ +// snykSecurity( +// organisation: 'uzzal2k5', +// projectName: 'uzzal2k5/node-todo', +// severity: 'medium', +// snykInstallation: 'snyk-latest', +// snykTokenId: 'synk_api_token', +// targetFile: '/Users/uzzal/.jenkins/workspace/synk-project/Dockerfile', +// failOnIssues: false +// ) +// +// return this +// } + +// BUILD NODE +pipeline { +environment { + IMAGE_NAME = "node-todo" + todoImages = '' + IMAGE_REPOSITORY = "uzzal2k5" + } + + agent any + stages { + + stage('GIT CLONE') { + steps { + CloneFromGit(GIT_REPOSITORY_NAME, BRANCH) + } + } + stage('IMAGE BUILD'){ + steps { + script { + todoImages = docker.build("${IMAGE_REPOSITORY}/${IMAGE_NAME}") + } + } + } + stage('SCAN'){ + steps{ + script{ + snykSecurity( + organisation: 'uzzal2k5', + projectName: 'uzzal2k5/node-todo', + severity: 'medium', + snykInstallation: 'snyk-latest', + snykTokenId: 'synk_api_token', + failOnIssues: false + ) + def variable = sh( + script: 'snyk container test uzzal2k5/node-todo:latest --severity-threshold=medium', + returnStatus: true + ) + echo "Error Code = ${variable}" + if (variable !=0){ + echo "Alert for Vulnerability Found" + } + } + } + } + + + } + +// DockerImageBuild(DOCKER_BUILD_SERVER,DOCKER_IMAGE_REPOSITORY, IMAGE_NAME) + + + +//NODE END +} diff --git a/aws-k8s-replication.yml b/aws-k8s-replication.yml new file mode 100644 index 000000000..2c8d5754e --- /dev/null +++ b/aws-k8s-replication.yml @@ -0,0 +1,76 @@ +--- +apiVersion: v1 +kind: Service +metadata: + name: todo-service + labels: + name: todo-service + app: nodeapp +spec: + selector: + name: node-todo + app: todoapp + ports: + - port: 80 + name: node-port + targetPort: 8080 + protocol: TCP + type: LoadBalancer + +--- +apiVersion: v1 +kind: ReplicationController +metadata: + name: todo-replicas + labels: + name: node-todo + app: todoapp +spec: + replicas: 2 + template: + metadata: + name: todo-pod + labels: + name: node-todo + app: todoapp + spec: + containers: + - name: todo-container + image: index.docker.io/uzzal2k5/node-todo:latest + env: + - name: MONGODB_HOST + value: mongodb + ports: + - containerPort: 8080 + #imagePullPolicy: Always +--- +apiVersion: v1 +kind: Service +metadata: + name: mongodb + labels: + name: mongodb + app: database +spec: + selector: + name: mongodb + app: database + ports: + - port: 27017 + name: db-port + targetPort: 27017 + +--- +apiVersion: v1 +kind: Pod +metadata: + name: mongodb + labels: + name: mongodb + app: database +spec: + containers: + - name: mongodb + image: mongo:4.0.2 + ports: + - containerPort: 27017 \ No newline at end of file diff --git a/config/database.js b/config/database.js index 1f4cfc593..d0a28bffe 100644 --- a/config/database.js +++ b/config/database.js @@ -1,4 +1,4 @@ module.exports = { - remoteUrl : 'mongodb://node:nodeuser@mongo.onmodulus.net:27017/uwO3mypu', + remoteUrl : 'mongodb://mongodb:27017/test', localUrl: 'mongodb://localhost/meanstacktutorials' -}; +}; \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 000000000..3360e24c4 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,31 @@ +version: '2.0' +services: + # mongoDB + mongodb: + image: mongo:4.0.2 + hostname: mongodb + container_name: mongodb + #environment: + # MONGO_INITDB_ROOT_USERNAME: "uzzal" + # MONGO_INITDB_ROOT_PASSWORD: "password" + ports: + - "27017:27017" + restart: always + + # node-todo App + node-todo: + build: . + container_name: todo-container + hostname: node-todo + ports: + - 80:8080/tcp + tty: true + links: + - mongodb + depends_on: + - mongodb + restart: always + + + + diff --git a/docs/DeployingProcedure.md b/docs/DeployingProcedure.md new file mode 100644 index 000000000..a8f931fef --- /dev/null +++ b/docs/DeployingProcedure.md @@ -0,0 +1,355 @@ +1 : Write Dockerfile for node-todo nodeJs app to build docker images + + FROM node:alpine + MAINTAINER uzzal, uzzal2k5@gmail.com + WORKDIR /node-todo + COPY . . + RUN npm install + + # CONFIG STANDARD ERROR LOG + RUN ln -sf /dev/stdout /var/log/access.log \ + && ln -sf /dev/stderr /var/log/error.log + + # SET HEALTH CHECK + HEALTHCHECK --interval=5s \ + --timeout=5s \ + CMD curl -f http://127.0.0.1:8000 || exit 1 + + RUN apk add mongodb + EXPOSE 8080 + + + ENTRYPOINT ["node", "server.js"] + +2 : Test Build with manual build command + + docker build -t node-todo . + +3 : Write docker-compose file to deploy Container Service + + version: '2.0' + services: + # mongoDB + mongodb: + image: mongo:4.0.2 + hostname: mongodb + container_name: mongodb + #environment: + # MONGO_INITDB_ROOT_USERNAME: "uzzal" + # MONGO_INITDB_ROOT_PASSWORD: "password" + ports: + - "27017:27017" + restart: always + + # node-todo App + node-todo: + build: . + container_name: todo-container + hostname: node-todo + ports: + - 80:8080/tcp + tty: true + links: + - mongodb + depends_on: + - mongodb + restart: always + + + Note: Official mongoDB docker image are used here. I found some issue with USERNAME & PASSWORD + For that reasion, I leave USERNAEM & PASSWORD + +4 : Found Some Issue with Database Connection [Not Connecting Remote Database], So that I modified server.js Looking Database connection on 127.0.0.1:27017 + + //mongoose.connect(database.localUrl); + + mongoose.connect(database.remoteUrl); + +5 : Inthis point change DATABASE Connection as bellow + + config/database.js + --- + + module.exports = { + remoteUrl : 'mongodb://username:password@mongodb:27017/tododb', + localUrl: 'mongodb://localhost/meanstacktutorials' + }; + +6 : In step 5, I fall in authentication issue with database. After digging into depp on MongoDB Container, I found there was Issue with that official Image Creation, USERS are getting permission into test DB by default + +If I create user into MongoDB Container, That user get permission by default for test DB + + db.createUser({ + user: 'username', + pwd: 'password', + roles: [ { role: "dbOwner", db: "tododb" }, + { role: "dbAdmin", db: "tododb" }, + { role: "readWrite", db: "tododb" } + ]}) + +In this stage I build a new MongoDB image using Ubuntu:16.04 and and Test everything working fine with - + + module.exports = { + remoteUrl : 'mongodb://username:password@mongodb:27017/tododb', + localUrl: 'mongodb://localhost/meanstacktutorials' + }; + +After that I decided to use database connection without USERNAME & PASSWORD, as this is a test project. + + module.exports = { + remoteUrl : 'mongodb://mongodb:27017/tododb', + localUrl: 'mongodb://localhost/meanstacktutorials' + }; + +