-
Notifications
You must be signed in to change notification settings - Fork 2
/
devops_test.txt
808 lines (603 loc) · 26.2 KB
/
devops_test.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
Install Maven on Jenkins
Download maven packages https://maven.apache.org/download.cgi onto Jenkins server. In this case I am using /opt/maven as my installation directory - Link : https://maven.apache.org/download.cgi
# Creating maven directory under /opt
mkdir /opt/maven
cd /opt/maven
# downloading maven version 3.6.0
wget http://mirrors.fibergrid.in/apache/maven/maven-3/3.6.0/binaries/apache-maven-3.6.0-bin.zip
unzip /opt/maven/apache-maven-3.6.0-bin.zip
Setup M2_HOME and M2 paths in .bash_profile of user and add these to path variable
vi ~/.bash_profile
M2_HOME=/opt/maven/apache-maven-3.6.0
M2=$M2_HOME/bin
PAHT=<Existing_PATH>:$M2_HOME:$M2
Check point
logoff and login to check maven version Check maven version
mvn –version
So far you have completed installation of maven software to support maven plugin on jenkins console. Let's jump onto jenkins to complete remining steps.
Setup maven on jenkins console
Install maven plugin without restart
Manage Jenkins > Jenkins Plugins > available > Maven Invoker
Configure java path
Manage Jenkins > Global Tool Configuration > Maven
*****************************
Install git packages on jenkins server
yum install git -y
Setup Git on jenkins console
Install git plugin without restart
Manage Jenkins > Jenkins Plugins > available > github
Configure git path
Manage Jenkins > Global Tool Configuration > git
*****************************
Install Java
We will be using open java for our demo, Get latest version from http://openjdk.java.net/install/
yum install java-1.8*
#yum -y install java-1.8.0-openjdk
Confirm Java Version
Lets install java and set the java home
java -version
find /usr/lib/jvm/java-1.8* | head -n 3
#JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64
export JAVA_HOME
PATH=$PATH:$JAVA_HOME
# To set it permanently update your .bash_profile
source ~/.bash_profile
The output should be something like this,
[root@~]# java -version
openjdk version "1.8.0_151"
OpenJDK Runtime Environment (build 1.8.0_151-b12)
OpenJDK 64-Bit Server VM (build 25.151-b12, mixed mode)
Install Jenkins
You can install jenkins using the rpm or by setting up the repo. We will setup the repo so that we can update it easily in future. Get latest version of jenkins from https://pkg.jenkins.io/redhat-stable/
yum -y install wget
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.key
yum -y install jenkins
Start Jenkins
# Start jenkins service
systemctl start jenkins
# Setup Jenkins to start at boot,
systemctl enable jenkins
Accessing Jenkins
By default jenkins runs at port 8080, You can access jenkins at
http://YOUR-SERVER-PUBLIC-IP:8080
Configure Jenkins
The default Username is admin
Grab the default password
Password Location:/var/lib/jenkins/secrets/initialAdminPassword
Skip Plugin Installation; We can do it later
Change admin password
Admin > Configure > Password
Configure java path
Manage Jenkins > Global Tool Configuration > JDK
Create another admin user id
Test Jenkins Jobs
Create “new item”
Enter an item name – My-First-Project
Chose Freestyle project
Under Build section Execute shell : echo "Welcome to Jenkins Demo"
Save your job
Build job
Check "console output"
*********************
Install Apache Tomcat
Download tomcat packages from https://tomcat.apache.org/download-80.cgi onto /opt on EC2 instance
# create tomcat directory
cd /opt
wget http://mirrors.fibergrid.in/apache/tomcat/tomcat-8/v8.5.35/bin/apache-tomcat-8.5.35.tar.gz
tar -xvzf /opt/apache-tomcat-8.5.35.tar.gz
give executing permissions to startup.sh and shutdown.sh which are under bin.
chmod +x /opt/apache-tomcat-8.5.35/bin/startup.sh shutdown.sh
create link files for tomcat startup.sh and shutdown.sh
ln -s /opt/apache-tomcat-8.5.35/bin/startup.sh /usr/local/bin/tomcatup
ln -s /opt/apache-tomcat-8.5.35/bin/shutdown.sh /usr/local/bin/tomcatdown
tomcatup
check point :
access tomcat application from browser on prot 8080
http://<Public_IP>:8080
Using unique ports for each application is a best practice in an environment. But tomcat and Jenkins runs on ports number 8080. Hence lets change tomcat port number to 8090. Change port number in conf/server.xml file under tomcat home
cd /opt/apache-tomcat-8.5.35/conf
# update port number in the "connecter port" field in server.xml
# restart tomcat after configuration update
tomcatdown
tomcatup
check point :
access tomcat application from browser on prot 8090
http://<Public_IP>:8090
now application is accessible on port 8090. but tomcat application doesnt allow to login from browser. changing a default parameter in context.xml does address this issue
#search for context.xml
find / -name context.xml
above command gives 3 context.xml files. comment () Value ClassName field on files which are under webapp directory. After that restart tomcat services to effect these changes
tomcatdown
tomcatup
Update users information in the tomcat-users.xml file goto tomcat home directory and Add below users to conf/tomcat-user.xml file
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<user username="admin" password="admin" roles="manager-gui, manager-script, manager-jmx, manager-status"/>
<user username="deployer" password="deployer" roles="manager-script"/>
<user username="tomcat" password="s3cret" roles="manager-gui"/>
Restart serivce and try to login to tomcat application from the browser. This time it should be Successful
******************
Ansible Installation
Ansible is an open source automation platform. It is very, very simple to setup and yet powerful. Ansible can help you with configuration management, application deployment, task automation.
Follow this in **YouTube
Prerequisites
An AWS EC2 instance
Installation steps:
Add a EPEL (Extra Packages for Enterprise Linux)third party repository to get packages for Ansible
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
Install Ansible
yum install ansible -y
Check Ansible version
ansible --version
Create a new user for ansible administration & grant admin access to user (Master and Slave)
useradd ansadmin
passwd ansadmin
# below command addes ansadmin to sudoers file. But strongly recommended to use "visudo" command if you are aware vi or nano editor.
echo "ansadmin ALL=(ALL) ALL" >> /etc/sudoers
Using keybased authentication is advised. If you are still at learning stage use password based authentication (Master & Slave)
# sed command replaces "PasswordAuthentication no to yes" without editing file
sed -ie 's/PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config
Login as a ansadmin user on master and generate ssh key (Master)
ssh-keygen
Copy keys onto all ansible client nodes (Master)
ssh-copy-id <target-server>
Update target servers information on /etc/ansible/hosts file (Master)
echo "<target server IP>" > /etc/ansible/hosts
Run ansible command as ansadmin user it should be successful (Master)
ansible all -m ping
*******************
SonarQube Installation
SonarQube provides the capability to not only show health of an application but also to highlight issues newly introduced. With a Quality Gate in place, you can fix the leak and therefore improve code quality systematically.
Follow this Article in YouTube
Prerequisites
EC2 instance with Java installed
MySQL Database Server or MyQL RDS instance. Get help here
Installation
Install MySQL client version
# yum install mysql
Download stable SonarQube version from below website.
Website: https://www.sonarqube.org/downloads/
Note: This Article written for SonarQube6.0
Download & unzip SonarQube 6.0
# cd /opt
# wget https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-6.0.zip
# unzip sonarqube-6.0.zip
# mv /opt/sonarqube-6.0 /opt/sonar
Allow RDS instance security group to access SonarQube server
Connect to RDS instance with database credentials
mysql -h <RDS_Instance_endpoint>:3306 -u <DB_USER_NAME> -p <DB_PASSWORD>
Create a new sonar database
CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
Create a local and a remote user
CREATE USER sonar@localhost IDENTIFIED BY 'sonar';
CREATE USER sonar@'%' IDENTIFIED BY 'sonar';
Grant database access permissions to users
GRANT ALL ON sonar.* TO sonar@localhost;
GRANT ALL ON sonar.* TO sonar@'%';
check users and databases
use mysql
show databases;
SELECT User FROM mysql.user;
FLUSH PRIVILEGES;
QUIT
So for you have configured required database information on RDS. Let’s Jump back to your EC2 instance and enable SonarQube properties file to connect his Database.
ON EC2 Instance
Edit sonar properties file to uncomment and provide required information for below properties.
File Name: /opt/sonar/conf/sonar.properties
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
sonar.jdbc.url=jdbc:mysql://<RDS_DATABAE_ENDPOINT>:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
sonar.web.host=0.0.0.0
sonar.web.context=/sonar
Start SonarQube service
# cd /opt/sonar/bin/linux-x86-64/
# ./sonar.sh start
Run SonarQube as a default service
Implement SonarQube server as a service
Copy sonar.sh to etc/init.d/sonar and modify it according to your platform.
# sudo cp /opt/sonar/bin/linux-x86-64/sonar.sh /etc/init.d/sonar
# sudo vi /etc/init.d/sonar
Add below values to your /etc/init.d/sonar file
Insert/modify below values
SONAR_HOME=/opt/sonar
PLATFORM=linux-x86-64
WRAPPER_CMD="${SONAR_HOME}/bin/${PLATFORM}/wrapper"
WRAPPER_CONF="${SONAR_HOME}/conf/wrapper.conf"
PIDDIR="/var/run"
Start SonarQube server
# service sonar start
SonarQube application uses port 9000. access SonarQube from browser
http://<EC2_PUBLIC_IP>:9000/sonar
Troubleshooting
Check whether you enabled port 9000 in EC2 instance security group
Check whether you enabled EC2 instance IP range in RDS security group
Next Step
Integrate SonarQube with Jenkins
*****************************
SonarQube Integration with Jenkins
Integration SonarQube server with Jenkins is necessary to store your reports. Follow below steps to enable that.
Follow this in YouTube
Prerequisites
SonarQube Server Get Help here
Jenkins Server Get Help here
Implementation
Login to Jenkins server and install sonarqube scanner.
SonarQube scanner URL : https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner
Package : https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-3.2.0.1227-linux.zip
# wget https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-3.2.0.1227-linux.zip
# unzip sonar-scanner-cli-3.2.0.1227-linux.zip
# mv sonar-scanner-3.2.0.1227-linux /opt/sonar_scanner
Set SonarQube server details in sonar-scanner property file
Sonar properties file: /opt/sonar_scanner/conf/sonar-scanner.properties
sonar.host.url=http://<SONAR_SERVER_IP>:9000
Login to Jenkins GUI console and install " SonarQube scanner" plugin
Manage Jenkins > Manage Plugins > Avalable > SonarQube scanner
Configure SonarQube scanner home path
Manage Jenkins > Global Tool Configuration > SonarQube Scanner
Name : sonar_scanner
SONAR_RUNNER_HOME : /opt/sonar_scanner
Configure SonarQube server name and authentication token
Manage Jenkins > Configure Systems > SonarQube Servers
Name : SonarQube
ServerURL : http://<Sonarqube_server>:9000/sonar
Server authentication token To Get Authentication code follow below steps. Login to SonarQube server as a admin My Account > Security > Generate Token
Create a job to test SonarQube. Provide below sonar properties details in the job under build
Build:
Execute SonarQube Scanner > Analysis properties (it is mandatary).
sonar.projectKey=Valaxy
sonar.projectName=ValaxyDemo
sonar.projectVersion=1.0
sonar.sources=/var/lib/jenkins/workspace/$JOB_NAME/<PROJECT_NAME>/src
Execute job to get analysis report.
**********************
What is Docker Hub Registry
Docker Hub is a cloud-based registry service which allows you to link to code repositories, build your images and test them, stores manually pushed images, and links to Docker Cloud so you can deploy images to your hosts. It provides a centralized resource for container image discovery, distribution and change management, user and team collaboration, and workflow automation throughout the development pipeline.
Ref URL : https://docs.docker.com/docker-hub/
Create a docker hub account in https://hub.docker.com/
Pull a docker image
docker pull ubuntu
pull a docker image with old version
docker pull ubuntu:16.04
create a custom tag to docker image
docker tag ubuntu:latest valaxy/ubuntu:demo
login to your docker hub registry ``sh docker login
6. docker push valaxy/ubuntu:demo
testing
Remove all images in docker server
docker image rm -f <Image_id>
Pull your custom image from your docker account
docker pull valaxy/ubuntu:demo
*************
CMD:
how to search a docker image in hub.docker.com
docker search httpd
Download a docker image from hub.docker.com
docker image pull <image_name>:<image_version/tag>
List out docker images from your local system
docker image ls
Create/run/start a docker container from image
docker run -d --name <container_Name> <image_name>:<image_version/tag>
d - run your container in back ground (detached)
Expose your application to host server
docker run -d -p <host_port>:<container_port> --name <container_Name> <image_name>:<Image_version/tag>
docker run -d --name httpd_server -p 8080:80 httpd:2.2
List out running containers
docker ps
List out all docker container (running, stpooed, terminated, etc...)
docker ps -a
run a OS based container which interactive mode (nothing but login to container after it is up and running)
docker run -i -t --name centos_server centos:latest
i - interactive
t - Terminal
Stop a container
docker stop <container_id>
Start a container which is in stopped or exit state
docker start <container_id>
Remove a container
docker rm <container_id>
login to a docker container
docker exec -it <container_Name> /bin/bash
**************
Installing Docker on Amazon Linux server
1.Update packages
yum update -y
2.Install docker
yum install docker -y
Docker Installation on CentOS server
Referance URL : https://docs.docker.com/install/linux/docker-ce/centos/#install-using-the-repository
PREREQUISITES
Please follow below steps to install docker CE on CentoOS server instance. For RedHat only Docker EE available
1.Install required packages.
sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
2.Use the following command to set up the stable repository.
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
INSTALLING DOCKER CE
1.Install the latest version of Docker CE.
sudo yum install docker-ce
If prompted to accept the GPG key, verify that the fingerprint matches 060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35, and if so, accept it.
2.Start Docker.
sudo systemctl start docker
3.Verify that docker is installed correctly by running the hello-world image.
sudo docker run hello-world
*****************
install_apache_dockerfile.txt
FROM ubuntu:12.04
MAINTAINER Valaxy Technologies [email protected]
LABEL version="1.1.0" \
app_name="Training registration application" \
release_date="9-Sep-2018"
RUN apt-get update && apt-get install -y apache2 && apt-get clean
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
EXPOSE 80
COPY index.html /var/www/html
CMD ["/usr/sbin/apache2", "-D", "FOREGROUND"]
*************
yum install docker
service docker status
service docker start
useradd dockeradmin
passwd dockeradmin
usermod -aG docker dockeradmin
mkdir /opt/docker
vi Dockerfile.txt
#Pull Base Image
FROM tomcat:8-jre8
#copy War file on to container
copy ./webapp.war /usr/local/tomcat/webapps
-- chown R dockeradmin:dockeradmin /opt/dcoker
---
vi /etc/ssh/sshd-config
service sshd restart
---
source files - webapp/target/*.war
remove prefix - webapp/target/
remote direcoty - //opt/docker
execute cmd :docker stop demo; docker rm -f demo; docker image rm -f demo; cd /opt/docker; dcoker build -t demo
execute cmd : docker run -d --name demo -p 8090:8080 demo
******
Simple DevOps Project -01
We know how to use work with each and Git, Jenkins independently. What if you want to collaborate these two? that is where Simple DevOps project helps you. Follow below steps if you are a new guy to DevOps. You love it.
Follow this article in YouTube
Prerequisites
EC2 instance with tomcat installation **installation steps video
Jenkins server **Get Help here
Part-01 : Adding steps for Integration
Steps to create Jenkin job
Login to Jenkins console
Create Jenkins job, Fill the following details,
Source Code Management:
Repository: https://github.com/ValaxyTech/hello-world.git
Branches to build : */master
Build:
Root POM:pom.xml
Goals and options : clean install package
Part-02: Adding Deployment Steps
in this port we are going to install 'deploy to container' plugin. this is need to deploy on tomcat server which we are using.
Install maven plugin without restart
Manage Jenkins > Jenkins Plugins > available > deploy to container
To deploy our build artifacts on tomcat server our Jenkins server need access. For this we should setup credentials. This option is available in Jenkins home page
setup credentials
credentials > jenkins > Global credentials > add credentials
Username : deployer
Password : XXXXXXX
id : Tomcat_user
Description: Tomcat user to deploy on tomcat server
Modify the same job which created in part-01 and add deployment steps.
Post Steps
Deploy war/ear to container
WAR/EAR files : **/*.war
Containers : Tomcat 8.x
Credentials: Tomcat_user (which created in above step)
Tomcat URL : http://<PUBLIC_IP>:<PORT_NO>
Save and run the job now.
Port-03 : Continuous Integration & Continuous Deployment (CI/CD)
Now job is running fine but to make this as Continuous Integration and Continuous Deployment Tod do that go back and modify job as below.
Build Triggers
Poll SCM
schedule */2 * * * *
Save the job and modify the code in GitHub. Then you could see your job get trigger a build without any manual intervention.
*****************
Simple DevOps Project -02
Follow this on YouTube
Prerequisites:
Ansible server Get Help Here
Jenkins Server Get Help Here
Tocmat Server Get Help Here
Part-01 Integration Setps
Install "publish Over SSH"
Manage Jenkins > Manage Plugins > Available > Publish over SSH
Enable connection between Ansible and Jenkins
Manage Jenkins > Configure System > Publish Over SSH > SSH Servers
SSH Servers:
Hostname:<ServerIP>
username: ansadm
password: *******
Test the connection "Test Connection"
Part-02 - Execute job to connect
create a copywarfile.yml on Ansible under /opt/playbooks
# copywarfile.yml
---
- hosts: all
become: true
tasks:
- name: copy jar/war onto tomcat servers
copy:
src: /op/playbooks/wabapp/target/webapp.war
dest: /opt/apache-tomcat-8.5.32/webapps
Add tomcat server details to /etc/ansible/hosts (if you are using other hosts file update server info there)
echo "<server_IP>" >> /etc/ansible/hosts
Create Jenkins job, Fill the following details,
Source Code Management:
Repository: https://github.com/ValaxyTech/hello-world.git
Branches to build : */master
Build:
Root POM:pom.xml
Goals and options : clean install package
Add post-build steps
Send files or execute commands over SSH
SSH Server : ansible_server
Source fiels: webapp/target/*.war
Remote directory: //opt//playbooks
Add post-build steps
Send files or execute commands over ssH
SSH Server : ansible_server
Exec command ansible-playbook /opt/playbooks/copywarfile.yml
Execute job and you should be able to seen build has been deployed on Tomcat server.
*********************
Simple DevOps Project -3
Launch an EC2 instance for Docker host
Install docker on EC2 instance and start services
yum install docker
service docker start
create a new user for Docker management and add him to Docker (default) group
useradd dockeradmin
passwd dockeradmin
usermod -aG docker dockeradmin
Write a Docker file under /opt/docker
mkdir /opt/docker
### vi Dockerfile
# Pull base image
From tomcat:8-jre8
# Maintainer
MAINTAINER "valaxytech"
# copy war file on to container
COPY ./webapp.war /usr/local/tomcat/webapps
Login to Jenkins console and add Docker server to execute commands from Jenkins
Manage Jenkins --> Configure system --> Publish over SSH --> add Docker server and credentials
Create Jenkins job
A) Source Code Management
Repository : https://github.com/ValaxyTech/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[s] :
docker stop valaxy_demo;
docker rm -f valaxy_demo;
docker image rm -f valaxy_demo;
cd /opt/docker;
docker build -t valaxy_demo .
D) send files or execute commands over SSH
Name: docker_host
Exec command : docker run -d --name valaxy_demo -p 8090:8080 valaxy_demo
Login to Docker host and check images and containers. (no images and containers)
Execute Jenkins job
check images and containers again on Docker host. This time an image and container get creates through Jenkins job
Access web application from browser which is running on container
<docker_host_Public_IP>:8090
**********************
Simple DevOps Project - 4
In this project, we will be see how to use Git, Jenkins, Ansible, DockerHub, Docker to DEPLOY on a docker container.,
Follow this project in Youtube
PreRequisites
Jenkins - Get help here
Ansible - Get help here
Setup ansible client and install docker. here
Docker Hub account
Previous DevOps projects:
Simple DevOps project-1 here
Simple DevOps project-2 here
Simple DevOps project-3 here
In part-01 we create Docker image on ansible server through Jenkins job and pushed it onto DockerHub.
Part-01 : Create an docker image
Login to Jenkins console
Create Jenkins job, Fill the following details,
Source Code Management:
Repository : https://github.com/ValaxyTech/hello-world.git
Branches to build : */master
Build:
Root POM:pom.xml
Goals and options : clean install package
Post Steps
Send files or execute commands over SSH
Name: ansible_server
Source files : webapp/target/*.war
Remove prefix : webapp/target
Remote directory : //opt//docker
Send files or execute commands over SSH
Name: ansible_server
Source files : Dockerfile
Remote directory : //opt//docker
Exec Command:
cd /opt/docker
docker build -t valaxy_demo .
docker tag valaxy_demo valaxy/valaxy_demo
docker push valaxy/valaxy_demo
docker rmi valaxy_demo valaxy/valaxy_demo
Login to Docker host and check images and containers. (no images and containers)
login to docker hub and check. shouldn't find images with for valaxy_demo
Execute Jenkins job
check images in Docker hub. Now you could able to see new images pushed to Valaxy Docker_Hub
Troubleshooting:
Docker should be installed on ansible server
Should login to "docker hub" on ansible server
ansadmin user should be part of docker group
In Part-02 we create create_docker_container.yml playbook. this get intiated by jenkins job, run by ansible and exected on dokcer_host
Part-02 : Deploy Containers
Write a yml file to create a container (file name : create_docker_container.yml)
---
- hosts: web-servers
become: true
tasks:
- name: stop previous version docker
shell: docker stop valaxy_demo
- name: remove stopped container
shell: docker rm -f valaxy_demo
- name: remove docker images
shell: docker image rm -f valaxy/valaxy_demo
- name: create docker image
shell: docker run -d --name valaxy_demo -p 8090:8080 valaxy/valaxy_demo
Add this script to Jenkins job.
Chose "configure" to modify your jenkins job.
Under post build actions
Send files or execute commands over SSH
Exec Command:
cd /opt/playbooks
ansible-playbook create_docker_container.yml
Execute Jenkins job.
You could see a new container on your docker host. can able access it from browser on port 8090
Troubleshooting: Makesure you have opened required ports on AWS Security group for this server.
In Part-03 we try to improvise to store docker images previous versions
Part-03 : Deploy with Version Control Containers
So for we used latest docker image to build a container, but what happens if latest version is not working?
One easiest solution is, maintaining version for each build. This can be achieved by using environment variables.
here we use 2 variables
BUILD_ID - The current build id
JOB_NAME - Name of the project of this build. This is the name you gave your job when you first set it up.
for more info Please refer this URL
Lets modify jenkins job which was created in Part-01 as below.
Create Jenkins job
Source Code Management:
Repository : https://github.com/ValaxyTech/hello-world.git
Branches to build : */master
Build:
Root POM:pom.xml
Goals and options : clean install package
Send files or execute commands over SSH
Name: ansible_server
Source files : webapp/target/*.war
Remove prefix : webapp/target
Remote directory : //opt//docker
Send files or execute commands over SSH
Name: ansible_server
Source files : Dockerfile
Remote directory : //opt//docker
cd /opt/docker
docker build -t $JOB_NAME:v1.$BUILD_ID .
docker tag $JOB_NAME:v1.$BUILD_ID valaxy/$JOB_NAME:v1.$BUILD_ID
docker tag $JOB_NAME:v1.$BUILD_ID valaxy/$JOB_NAME:latest
docker push valaxy/$JOB_NAME:v1.$BUILD_ID
docker push valaxy/$JOB_NAME:latest
docker rmi $JOB_NAME:v1.$BUILD_ID valaxy/$JOB_NAME:v1.$BUILD_ID
valaxy/$JOB_NAME:latest
***************