Saturday 23 March 2019

Devops Project-3 - By using Git, Jenkins and Docker on AWS | CICD on containers by Raj Gupta


So both CI(build) and CD(deploy) part are done by using Jenkins on Docker containers





For this project we require two EC2 server

11. Jenkins server( take REHL ec2)
  2. Docker_Host server (take Amazon Linux ec2)
Do the same setup for Jenkins server as project 1.( install jenkins, git, ,maven and java on REHL Jenkins server)
Now create second Docker_Host ec2 server for this take the Amazon Linux ec2 server and do the below on Docker server.
  1. Launch an Amazon Linux  EC2 instance for Docker host
  2. Install docker on EC2 instance and start services
[root@ip-172-31-89-34 ~]# yum install docker
[root@ip-172-31-89-34 ~]# service docker start
  1. create a new user for Docker management and add him to Docker (default) group
[root@ip-172-31-89-34 ~]# useradd dockeradmin
[root@ip-172-31-89-34 ~]# passwd dockeradmin
[root@ip-172-31-89-34 ~]# usermod -aG docker dockeradmin

  1. Write a Docker file under /opt/docker
[root@ip-172-31-89-34 ~]# mkdir /opt/docker
[root@ip-172-31-89-34 ~]# cd /opt/docker
[root@ip-172-31-89-34 docker]# vi Dockerfile


# Pull base image
From tomcat:8-jre8

# Maintainer
MAINTAINER "valaxytech@gmail.com"

# copy war file on to container
COPY ./webapp.war /usr/local/tomcat/webapps






  5.  Login to Jenkins console and add Docker server to execute commands from Jenkins



Manage Jenkins-àConfigure System-à Publish over SSH-à then click on ADD and fill the below

SSH server name:- you can give any name as per your choice say here docker
Hostname:- here you give private ip address of docker server.
Username:- you give the username whatever you created on docker server like in ourcase docker admin

Then click on advance button and click on Use password authentication, or use a different key then give the password of user you have created in docker server for dockeradmin like in our case raj123456

Passphrase / Password:-    raj123456

Now logging into docker server and in the below file change the yellow part.

[root@ip-172-31-86-168 ~]# vi /etc/ssh/sshd_config


# EC2 uses keys for remote access
PasswordAuthentication yes
#PermitEmptyPasswords no



[root@ip-172-31-86-168 ~]# service sshd restart



Then in Jenkins create new project

6.    Create Jenkins job give below in the different tab of Jenkins
A) Source Code Management
Repository : 
https://github.com/rajkumargupta14/hello-world.git
Branches to build : */master
B) Build Root POM: pom.xml
Goals and options : clean install package
C) send files or execute commands over SSH Name: docker_host
Source files : webapp/target/*.war
Remove prefix : webapp/target
Remote directory : //opt//docker
Exec command : docker stop valaxy_demo; docker rm -f valaxy_demo; docker image rm -f valaxy_demo; cd /opt/docker; docker build -t valaxy_demo .  (in last  . must give)
D) send files or execute commands over SSH
Name: docker_host
Exec command : docker run -d --name valaxy_demo -p 8090:8080 valaxy_demo

  1. Login to Docker host and check images and containers. (no images and containers)
[root@ip-172-31-89-34 docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
[root@ip-172-31-89-34 docker]# docker ps    ----this give active container
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@ip-172-31-89-34 docker]# docker ps –a  ----this give active and unactive both container
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@ip-172-31-89-34 docker]#


  1. Execute Jenkins job(click on build now)

If you got the below error
ERROR: Exception when publishing, exception message [Permission denied]
Build step 'Send files or execute commands over SSH' changed build result to UNSTABLE
 
Then run the below command on docker server
[root@ip-172-31-89-34 docker]# chown -R dockeradmin:dockeradmin /opt/docker
[root@ip-172-31-89-34 docker]# docker login --username=rajguptaaws --password=aurangabad@22    (logging into dockerhub account must)

  1. check images and containers again on Docker host. This time an image and container get creates through Jenkins job
[root@ip-172-31-89-34 docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
valaxy_demo         latest              c61d83d3a824        3 minutes ago       462MB
tomcat              8-jre8              7ee26c09afb3        6 days ago          462MB
  1. Access web application from browser which is running on container <docker_host_Public_IP>:8090
http://3.88.174.154:8090/webapp/














No comments:

Post a Comment