Jenkins and docker’s automated CI / CD combat

Time:2021-2-26

Jenkins and docker's automated CI / CD combat

Original text: https://www.toutiao.com/a6602…

1、 Release process design

Jenkins and docker's automated CI / CD combat

Jenkins and docker's automated CI / CD combat

Workflow:

Developers submit code to git version repository;
Jenkins manual / timed trigger project construction;
Jenkins pulls the code, codes, packages the image, and pushes it to the image warehouse;
Jenkins creates a container on the docker host and publishes it.

2、 Environmental Planning:
Jenkins and docker's automated CI / CD combat

3、 Deployment process

1. Deploy Git

If there’s direct cloning within the company, that’s fine

git clone [email protected]:/home/git/solo.git  

2. Deploy Jenkins environment

Deployment portal:Jenkins + Maven + SVN to realize automatic code packaging and publishing

Jenkins and docker's automated CI / CD combat

3. Deploy private image warehouse

Note: due to the HTTPS authentication of docker warehouse, all clients that need to pull need to modify the configuration file

[[email protected] ~]# vim /etc/sysconfig/docker
# Modify these options if you want to change the way the docker daemon runs
 OPTIONS='--selinux-enabled --insecure-registry 192.168.56.11:5000'

4. Install docker on all hosts

1) Install dependency package

 yum install -y yum-utils device-mapper-persistent-data lvm2 

2) Add docker package source:

yum-config-manager   
--add-repo   
https://download.docker.com/linux/centos/docker-ce.repo  

3) Install docker CE

 yum install docker-ce -y 

4) Configure accelerator

curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://bc437cce.m.daocloud.io
 
 #Because the default source will go abroad to obtain data, it will be slow and can time out. This is why we need to configure the accelerator to point to the domestic source https://www.daocloud.io/

5) Start and start

# systemctl start docker  
# systemctl enable docker

4、 Building basic image

【Apache、Nginx、Tomcat、LNMP、LAMP、LNTP】

Java program must have JDK environment to run. In order to reduce the image size and improve performance, JDK is directly put on the host computer, and the container is used in the form of mount.

1. Install JDK

#RZ uploads the tar package, decompresses it and places it in the specified directory  
rz.......   
tar -zxvf jdk-8u60-linux-x64.tar.gz  
mv jdk1.8.0_60 /usr/local/jdk1.8

2. Write dockerfile

# cat Dockerfile  
FROM centos:7  
#Who is his mother  
MAINTAINER www.aliangedu.com  
#Who is his father  
ENV VERSION=8.5.33  
#Tomcat version  
ENV JAVA\_HOME /usr/local/jdk  
#JDK absolute path  
RUN yum install wget -y  
#Command to run  
RUN wget http://mirrors.shu.edu.cn/apache/tomcat/tomcat-8/v${VERSION}/bin/apache-tomcat-${VERSION}.tar.gz &&   
 tar zxf apache-tomcat-${VERSION}.tar.gz &&   
 mv apache-tomcat-${VERSION} /usr/local/tomcat &&   
 rm -rf apache-tomcat-${VERSION}.tar.gz /usr/local/tomcat/webapps/\* &&   
 mkdir /usr/local/tomcat/webapps/ROOT  
EXPOSE 8080  
#Port used by the program  
CMD /usr/local/tomcat/bin/catalina.sh run  
#When I execute the startup script in Tomcat directory, I encounter a hole, that is, when - V hangs the host JDK directory to the container / usr / local / JDK, because the image is typed according to the dockerfile, the path can not be found when I execute the command, so I temporarily delete the two lines of expose and CMD, and then repackage them with - P Specify the port, and then enter the container, and manually start Tomcat

3. Build a mirror image

docker build -t 192.168.56.11:5000/tomcat-85:latest -f dockerfile .   
#The last point. Represents the current path. When making the image, the context content will be recorded

Jenkins and docker's automated CI / CD combat

4. Upload to the docker image warehouse

[[email protected] scripts]# docker push 192.168.56.11:5000/tomcat-85:latest   

5. Start the image test

[[email protected] scripts]# docker run -it -d -p 8080:8080 -v /usr/local/jdk1.8:/usr/local/jdk 192.168.56.11:5000/tomcat-8:latest  
[[email protected] ROOT]# echo "123" >index.jsp  

Jenkins and docker's automated CI / CD combat

5、 Jenkins configuration

1. Main page > System Management > global tool configuration

Specify JDK and Maven paths. Git remains the default

Jenkins and docker's automated CI / CD combat

2. Jenkins install necessary plug-ins

Main page > System Management > management plug in:

Install SSH and git parameter plug-ins.
Plug in Description:
》SSH: used for SSH Remote docker host to execute shell command
》Git parameter: dynamically obtain branch and tag of GIT warehouse

Jenkins and docker's automated CI / CD combat

3. Configure SSH plug-in

Step 1: first, create a certificate (authorized user) for connecting to the docker host

Main page > credentials > system > right click global credentials > Add credentials:

Jenkins and docker's automated CI / CD combat

Enter the user name and password to connect to the docker host:

Jenkins and docker's automated CI / CD combat

Step 2: add SSH Remote Host

Main page > System Management > System Settings > SSH remote hosts:

Jenkins and docker's automated CI / CD combat

Problem: when using docker images as an ordinary user, the following error occurs:

Jenkins and docker's automated CI / CD combat

6、 Upload the Java project downloaded from GitHub to your own gitlab repository

# git clone https://github.com/b3log/solo  
# cd solo  
Remove the old push address and add a new one:  
# git remote remove origin   
# git remote add origin [email protected]:qqq/solo.git  
Submit code to git warehouse and create tag:  
# touch src/main/webapp/a.html  
# git add .  
# git commit -m “a”  
To create a label:  
# git tag 1.0.0  
Push to git server:  
# git push origin 1.0.0

Log in to gitlab to view solo project:

Jenkins and docker's automated CI / CD combat

7、 Jenkins creates projects and publishes tests

1. Main page > new task > input task name to build a maven project

Jenkins and docker's automated CI / CD combat

Note: if the “build a maven project” option is not displayed, you need to install the “Maven integration plugin” plug-in in the management plug-in.

Configure git parametric Construction:

Jenkins and docker's automated CI / CD combat

2. Dynamically obtain git warehouse tag and interact with users to select tag Publishing: [branch can also be set]

Jenkins and docker's automated CI / CD combat

3. Designated project git warehouse address:

Jenkins and docker's automated CI / CD combat

Modify the * / master to $tag. Tag is the variable name obtained dynamically above, which means to print the code version according to the user’s choice.

4. Set Maven build command options:

clean package -Dmaven.test.skip=ture  

Jenkins and docker's automated CI / CD combat

utilize pom.xml File building project.

In Jenkins, the local image is built and pushed to the image warehouse, and SSH is used to remotely connect to the docker host to create the container with the pushed image

Jenkins and docker's automated CI / CD combat

The command in the figure above is as follows:

REPOSITORY=192.168.56.11:5000/solo:${Tag}  
#Building a mirror image  

cat > Dockerfile << EOF  
FROM 192.168.56.11:5000/tomcat-8:latest  
RUN rm -rf /usr/local/tomcat/webapps/ROOT  
COPY target/*.war /usr/local/tomcat/webapps/ROOT.war  
CMD ["/usr/local/tomcat/bin/catalina.sh", "run"]  
EOF  

docker build -t $REPOSITORY .  
#Upload image  

docker push $REPOSITORY

Jenkins and docker's automated CI / CD combat

The contents in the figure above are as follows:

REPOSITORY=192.168.56.11:5000/solo:${Tag}  
#Deployment  

sudo docker rm -f blog-solo |true  
sudo docker image rm $REPOSITORY |true  
sudo docker container run -d --name blog-solo -v /usr/local/jdk1.8:/usr/local/jdk -p 8080:8080 $REPOSITORY  
#- d running in the background, - V hanging in the directory, - P mapping port, followed by the image  

#Note: the container name is blog solo, and the host port 8080 is exposed, that is, the host IP 192.168.56.12:8080 is used to access the blog solo project.

The blog solo project has been configured and started to build

Select tag and start building:

Jenkins and docker's automated CI / CD combat

Jenkins and docker's automated CI / CD combat

Click the construction history in the lower left corner, and right-click the first one to view the console output

Jenkins and docker's automated CI / CD combat

Build details

Jenkins and docker's automated CI / CD combat

Build successfully

Visit: 192.168.56.12:8080 to view the deployment results

Jenkins and docker's automated CI / CD combat

Adjust project access address

Enter the container and switch to the project directory

vi WEB-INF/classes/latke.properties   
#### Server ####  
# Browser visit protocol  
serverScheme=http  
# Browser visit domain name  
serverHost=192.168.56.12  
# Browser visit port, 80 as usual, THIS IS NOT SERVER LISTEN PORT!  
serverPort=8080  

After adjustment, restart tomcat, verify again, OK, the results are as follows:

Jenkins and docker's automated CI / CD combat

So far, the automated CI environment has been built. You can simulate the code submission and tag to test the automated release process.

8、 Problem summary:

see docker.sock jurisdiction

[[email protected] ~]# ll /var/run/docker.sock   
SRW RW --- 1 root docker 0 September 4 21:55 / var / run/ docker.sock

Jenkins and docker's automated CI / CD combat

Solution: [using docker images without sudo]

[[email protected] ~]# sudo groupadd docker  
 ##Groupadd: docker group already exists  
[[email protected] ~]# sudo gpasswd -a jenkins docker  
 ##Adding user 'Jenkins' to' docker 'group  
[[email protected] ~]# sudo service docker restart  
##Restart service  
[[email protected] ~]# newgrp - docker  
##To reload the group information, you must enter this command, otherwise the latest group content cannot be loaded because there is a cache