Create EC2 instance by referring to the below link
taraps.hashnode.dev/project-1-django-applic..
Install Jenkins referring to the below link
taraps.hashnode.dev/project-1-django-applic..
Add DockerHub Credential by referring to the below link
Installing prerequisite for EKS and connecting to AWS
https://taraps.hashnode.dev/setting-up-eks-cluster-via-cliterminal
Creating Namespace in EKS
After establishing the connection with aws, you can create a namespace in the cluster
kubectl create ns app
Now to check the namespaces and pods inside the namespace
kubectl get ns # It will show all the namespaces prest in EKS kubectl get pods -n <namespace name> # It will show all the pods inside the perticular namespace
Storing dockerhub credential in jenkins
Go to Manage Jenkins
Then select credentials > system (global) and store the credential
Installing required plugins
Go to Manage Jenkins from the Jenkin's Dashboard > Plugins > Available plugins and install Kubernetes CLI plugins
.
Create a job
Go to Jenkin's dashboard > Click on New Item
Give the project name "node-todo", select Pipeline and then select ok
In the general section select github project and enter the URL.
In the pipeline section choose Pipeline scrip from SCM by providing the github URL where we have jenkinsfile and click on SAVE.
For more detailed about SCM Polling and webhook click on the below link
https://taraps.hashnode.dev/creating-webhook-and-scm-polling-in-jenkins
Run the Build
Below is the Log of the build
In the EC2 instance where jenkins is running run the below commands to connect to EKS.
sudo snap install kubectl --classic sudo snap install aws-cli --classic aws configure aws eks update-kubeconfig --name demo-eks --region us-east-1
Write the Dockerfile to push the application to the dockerhub
FROM node:12.2.0-alpine WORKDIR app COPY . . RUN npm install RUN npm run test EXPOSE 8000 CMD ["node","app.js"]
Write the Deployment.yaml to deploy the application into the EKS
apiVersion: apps/v1 kind: Deployment metadata: name: todo spec: selector: matchLabels: app: todo template: metadata: labels: app: todo spec: containers: - name: myapp image: tara0674/node-app-test-new:latest ports: - containerPort: 8000 --- apiVersion: v1 kind: Service metadata: name: todo spec: selector: app: todo ports: - protocol: TCP port: 8000 targetPort: 8000 type: LoadBalancer
Write the Jenkinsfile to create the pipeline
pipeline { agent any environment { KUBE_NAMESPACE = 'app' } stages{ stage("Clone Code"){ steps{ git url: "https://github.com/prasadtara09/node-todo-cicd.git", branch: "master" } } stage("Build and Test"){ steps{ sh "docker build . -t node-app-test-new" } } stage("Push to Docker Hub"){ steps{ // fetch the dockerhub login credentials from jenkins withCredentials([usernamePassword(credentialsId:"dockerHub",passwordVariable:"dockerHubPass",usernameVariable:"dockerHubUser")]) { sh "docker tag node-app-test-new ${env.dockerHubUser}/node-app-test-new:latest" sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPass}" sh "docker push ${env.dockerHubUser}/node-app-test-new:latest" } } } stage ('EKS Deploy') { steps{ // Deploy the application to EKS sh "kubectl apply -f Deployment.yaml -n $KUBE_NAMESPACE" } } } }
Output
Repository created in DockerHub
Loadbalacer is created in EKS
Pods are running