Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

rutikdevops/DevOps-Project-2

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DevOps-Project-2

  • In this project, I build and deploy my application on Docker Container with the help of Ansible. Commit Code

Once the latest code is available on the Git repository, Jenkins is capable of fetching the updated code, initiating a build using Maven, creating Docker images, and subsequently deploying these Docker containers onto the Docker host.

In that case, we used Jenkins as a tool for building and used docker for deploying applications.

So in this case Jenkins is going to take the code from GitHub and build artifacts and copy those artifacts onto Ansible server.

Now it is Ansible’s task to create images and deploy the containers.Ansible is going to take the artifact and with the help of Docker file it creates a Docker image.

This Docker image, we can commit it into the Docker hub because Docker Hub is a repository to store Docker images.

Now whenever we execute any Ansible playbook to deploy a container, this Docker host communicates with the Docker hub and pull the image whatever we mentioned in our playbook and create a container out of it.

Project Blog link :-

https://medium.com/@rutikdevops/build-and-deploy-application-on-docker-container-with-the-help-of-ansible-a54061ef7d3

Project Steps :-

  • Create 4 ec2 instance name as:- (AWS Linux-2, t2 micro)
  1. Jenkins-Server
  2. Ansible-Server
  3. Docker-Server
  4. Tomcat-Server

1. Install and Configure the Jenkins :-

ec2-user
sudo su
yum update -y
hostnamectl set-hostname jenkins
bash
  • Java installation on Jenkins server:-
yum install java* -y
java --version
alternatives --config java               ## Using this command you can choose any version of Java
  • Jenkins installation on Jenkins server:-
wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
yum install jenkins -y
systemctl enable jenkins.service
systemctl start jenkins.service
systemctl status jenkins.service
  • Copy public ip of jenkins server and paste it in new tab with port no.8080
image
  • copy this path and paste in terminal with "cat" command
cat /var/lib/jenkins/secrets/initialAdminPassword
  • Now copy & paste this passwd in jenkins. so, jenkins is ready.

2. Install and Configure the Maven :-

sudo su
cd ~
pwd
cd /opt
wget https://dlcdn.apache.org/maven/maven-3/3.9.4/binaries/apache-maven-3.9.4-bin.tar.gz
tar -xvzf apache-maven-3.9.4-bin.tar.gz
mv apache-maven-3.9.4 maven
cd maven/
cd bin/
./mvn -v
cd ~
ll -a
vim .bash_profile
  • In the vi editor add this path :-
image
M2_HOME=/opt/maven
M2=/opt/maven/bin
JAVA_HOME=/usr/lib/jvm/java-11-amazon-corretto.x86_64
PATH=$PATH:$HOME/bin:$JAVA_HOME:$M2_HOME:$M2
  • Find java path using this command:-
find / -name java-11*
  • Showing maven & java path :-
source .bash_profile
echo $PATH



3. Install Maven Plugin and Configure Jenkins for Maven :-

  • Dashboard-> Manage Jenkins-> Pluins-> Available Plugins-> Maven Integration(Install this)

  • Manage Jenkins-> Tools-> JDK image

  • Manage Jenkins-> Tools-> Maven image

  • Dashboard-> Manage Jenkins-> Pluins-> Installed Plugins->github

  • GitHub Branch Source Plugin = Disable

  • GitHub plugin = Enable

image

  • git installation on Jenkins server:-
yum install git -y
  • Jenkins-> New Item-> Maven Project
image

  • In this job do this:-
image

image

image

  • Maven Build is successful:-
image

4. Ansible Server setup and Ansible Installation:-

ec2-user
sudo su
yum update -y
hostnamectl set-hostname jenkins
bash
  • add user
useradd ansadmin
passwd ansadmin      ## enter passwd 2 times
visudo               ## In vi editor go to end of the file = press shift+g without press i
ansadmin  ALL=(ALL)       NOPASSWD: ALL      ## add this in editor
image
vi /etc/ssh/sshd_config      ##Do this changes in vi editor:- ** #PermitRootLogin yes (remove #) ** PasswordAuthentication no (replace no to yes)
  • Now run this command:-
systemctl restart sshd
  • Now switch to created user:-
sudo su - ansadmin
yum install ansible -y
amazon-linux-extras install ansible2 -y
ansible --version
  • Go to ansible server and type command:-
ssh-keygen      ##(and press enter 2 to 3 times)



5. Integrate Ansible with Jenkins:-

  • Jenkins-> Manage Jenkins-> System-> Publish over SSH

image

  • Go to Ansible
cd /opt
ls
mkdir docker
chown ansadmin:ansadmin docker    ## Using this ansadmin is owner of docker directory
ls
cd docker/
ls
  • Jenkins-> New Project-> Do this mentioned in Img-> Build Now image

  • Now, webapp.war file is involved in docker directory

6. Install and Configure Docker on Ansible Server

yum install docker -y
id ansadmin
usermod -aG docker ansadmin     ## provide all the access to ansadmin on the docker
service docker start
service docker status
su - ansadmin
cd /opt/docker/
vi dockerfile     ## add below commande in dockerfile
FROM tomcat:latest
RUN cp -R /usr/local/tomcat/webapps.dist/* /usr/local/tomcat/webapps
COPY ./*.war /usr/local/tomcat/webapps



7. Create Ansible Playbook to create Docker Image and Push that Image to DockerHub

  • In Ansivle Open host file
vi /etc/ansible/hosts

[Ansible]                       ## type this in editor
(paste public Ip here)
ssh-copy-id (paste here ansible private ip)
## enter passwd 2 times
  • Create Ansible playbook to create the docker image and push image to dockerhub
vi regapp.yml   ## paste below cmnds in editor
---
- hosts: ansible
  tasks:
  - name: create docker image
    command: docker build -t regapp:latest .
    args:
      chdir: /opt/docker

  - name: create tag to push image onto dockerhub
    command: docker tag regapp:latest rutikdevops/regapp:latest

  - name: push docker image
    command: docker push rutikdevops/regapp:latest
  • check indentation of playbook
ansible-playbook regapp.yml --check
docker images
docker login



8. Update Jenkins Job to use the Ansible Playbook

  • Jenkins-> Project-> Configure
image

- Now, your image is pushed in DockerHub

9. Webapp deploy to container

  • Do passwordless authentication between Ansible & Docker
  • Install apache on docker server
yum install httpd -y
systemctl enable httpd
systemctl start httpd
systemctl status httpd
  • In Ansible Open host file
vi /etc/ansible/hosts

[docker]                       ## type this in editor
(paste public Ip here)
ssh-copy-id (paste here docker private ip)
## enter passwd 2 times
  • Create Ansible playbook to deploy webapp on server:-
vi deploy_regapp.yml   ## paste below cmnds in editor
---
- hosts: docker
  tasks:
    - name: stop existing container
      command: docker stop regapp-server
      ignore_errors: yes
    - name: remove the container
      command: docker rm regapp-server
      ignore_errors: yes
    - name: remove image
      command: docker rmi rutikdevops/regapp:latest
      ignore_errors: yes
    - name: create container
      command: docker run -d --name regapp-server -p 8082:8080 rutikdevops/regapp:latest
  • Now, regapp sussessfully host on server

Project Reference :-

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Morty Proxy This is a proxified and sanitized view of the page, visit original site.