Continuous Integration and Continuous Deployment Using Ansible , AWS EC2 and Docker

CI/CD POC

Tools Used:
1. Git
2. Jenkins
3. Ansible
4. Docker
5. AWS EC2

How it works:
Git commit -> Git Push -> Automatic trigger Jenkins Job -> Run Ansible Playbook -> Launch EC2 instance -> Launch Docker in EC2 -> Setup web server inside docker -> Run webserver

On ansible
Yum install epel-release ansible
Yum install python-pip
Pip install –upgrade pip
Pip install boto

Setup GIT server (make ssh password less authentication between Jenkins user (Jenkins) and git user(git). i.e., Jenkins user from Jenkins server should be able to connect git repository server with out any password.
————————
yum install git
su – git
ssh-keygen
exit
cat /root/.ssh/id_rsa.pub >> /home/git/.ssh/authorized_keys

(on git server)
mkdir /git
mkdir /git/project.git

cd /git/project.git
git init –bare
chmod -R 777 /git/project.git/objects
chmod -R 777 /git/project.git/refs

(on git client)
mkdir /project
cd /project
git init
git config –global user.name “git”
git config –global user.email “madhu.ravipati@gmail.com”

touch sample.txt
git add .
git commit -m ‘some comment’
git remote add origin git@hdpvm1.hadoop.com:/git/project.git
git push origin master

git add .
git commit -m ‘my change’
git push origin master

1. Install Ansible, Jenkins. Configure Jenkins to use SSH for trigger and Ansible for build

mkdir /etc/ansible/playbooks
mkdir /etc/ansible/playbooks/vars

2. Use the below url for Ansible script to launch dockers in EC2

http://www.techhawk.in/2017/06/ansible-playbook…r-and-launch-ec2/

Verification:

cp /etc/ansible/playbooks/ec2_docker.yml /myproject/ (do some change in the script)
cd /myproject/
git add .
git commit -m ‘change’
git push origin master

You can see in Jenkins UI that the job will trigger automatically

Cat /dockerfiles/dockerfile.java
#Download centos base image
FROM centos:7

#Update software repository
RUN yum -y install net-tools

ENV container docker
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == \
systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/multi-user.target.wants/*;\
rm -f /etc/systemd/system/*.wants/*;\
rm -f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f /lib/systemd/system/basic.target.wants/*;\
rm -f /lib/systemd/system/anaconda.target.wants/*;
VOLUME [ “/sys/fs/cgroup” ]
CMD [“/usr/sbin/init”]

#print hello world

RUN mkdir /mydir

COPY jdk.tar.gz /mydir
RUN cd /mydir
RUN tar -xvf /mydir/jdk.tar.gz

ENV JAVA_HOME /jdk1.8.0_77
ENV PATH $PATH:$JAVA_HOME/bin

Cat /dockerfiles/dockerfile.web

FROM rawdocker

RUN yum -y install httpd
RUN rm -rf /etc/httpd/logs
RUN mkdir /etc/httpd/logs
RUN systemctl enable httpd.service
EXPOSE 80
CMD [“/usr/sbin/init”]

For remote ssh, ansible use “execute remote ssh scripts” . so create shell script with ansible-playbook command and run the script
(make sure the key pem file permissions are 0400)

execute a script or command remotely from jenkins.

                                                                                        By Madhu R

 

1Shares

One Reply to “Continuous Integration and Continuous Deployment Using Ansible , AWS EC2 and Docker”

Leave a Reply

Your email address will not be published. Required fields are marked *