DevOps application solution based on Docker containers

Table of Contents

Experimental requirements:

1. Preparation work

2.gitlab host operation

3. harbor host operation

4. jenkins host operation

Write jenkins pipeline script


Experimental requirements:

1. Deploy gitlab and upload the code of the above project to your own gitlab warehouse (the first host)
2. Deploy jenkins based on tomcat (second host)
3. Deploy harbor warehouse (3rd host)
4. Make a tomcat image and upload it to the harbor warehouse (the 4th host)
5. Use jenkins to package the above projects into a war package (the second host)
6. Use the tomcat image you made to launch the war package of the project (completed with the jenkins pipeline) (second host)

Project path:

https://gitee.com/forgotten/tomcat-java-demo

Download the zip package locally, transfer it to the gitlab host and decompress it, and upload the project directory to the warehouse on the gitlab host

[root@git ~]# ls
anaconda-ks.cfg
tomcat-java-demo-master

[root@git ~]# yum -y install unzip
[root@git ~]# unzip tomcat-java-demo-master.zip
[root@git ~]# ls
[root@git ~]# ls
anaconda-ks.cfg
tomcat-java-demo-master
tomcat-java-demo-master.zip
[root@git ~]# ls tomcat-java-demo-master
db deploy.yaml Dockerfile jenkinsfile LICENSE pom.xml README.md src

# This is all the content in the project directory

1. Preparation work

Host Description IP System
gitlab.example.com gitlab warehouse 192.168.187.129 centos8
jenkins.example.com jenkins integration 192.168 .187.131 centos8
harbor.example.com harbor private warehouse 192.168.187.140 centos8
docker.example.com Application host 192.168.187.150 centos8

Deploy services on gitlab, jenkins, and harbor hosts and see the web interface

gitlab warehouse service deployment

jenkins integration service deployment

Harbor private warehouse deployment

II.gitlab host operation

Created a project named beauty on the web page

Upload on the command line page

# 1. Global settings
# Set the user and email address. The email address is not used temporarily, so fill it in casually.
[root@git ~]# git config --global user.name "root"
[root@git ~]# git config --global user.email "[email protected]"


# 2. Create a repository
# The name of the memory card here must be preceded by the name of the project
[root@git ~]# git clone http://192.168.187.129/root/beauty.git
Cloning into 'beauty'...
Username for 'http://192.168.187.129': root
Password for 'http://[email protected]':
warning: You appear to have cloned an empty repository.
[root@git ~]# ls
anaconda-ks.cfg
beauty
tomcat-java-demo-master
tomcat-java-demo-master.zip

# An empty directory named beauty appears

# 3. Create file and upload
[root@git ~]# cd beauty

#Create a new branch named beautiful, or you can use the default branch main
[root@git beauty]# git switch --create beautiful
Switched to a new branch 'beautifil'
 
# Move everything in the project to this directory
[root@git beauty]# mv ../tomcat-java-demo-master/* .
[root@git beauty]# ls
db deploy.yaml Dockerfile jenkinsfile LICENSE pom.xml README.md src

#Add all contents in the directory
[root@git beauty]# git add *
 
# illustrate
[root@git beauty]# git commit -m "add beautiful"
[beautiful (root-commit) b84f797] add beautiful
[beautiful ad000d1] add beautiful
 151 files changed, 2190 insertions( + ), 92 deletions(-)
 create mode 100644 Dockerfile
 create mode 100644 LICENSE
 rewrite README.md (100%)
...omitted
 
#upload
[root@git beauty]# git push --set-upstream origin beautiful
Username for 'http://192.168.187.129': root
Password for 'http://[email protected]':
Enumerating objects: 183, done.
Counting objects: 100% (183/183), done.
Delta compression using up to 8 threads
Compressing objects: 100% (168/168), done.
Writing objects: 100% (181/181), 1.12 MiB | 6.75 MiB/s, done.
Total 181 (delta 4), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (4/4), done.
remote:
remote: To create a merge request for beautiful, visit:
remote: http://192.168.187.129/root/beauty/-/merge_requests/new?merge_request[source_branch]=beautiful
remote:
To http://192.168.187.129/root/beauty.git
 * [new branch] beautiful -> beautiful
Branch 'beautiful' set up to track remote branch 'beautiful' from 'origin'.
 
# Upload successful
# You can see the newly uploaded beautiful on the project
# Set the default branch to beautiful in the settings, so that when we pull the project code, we will directly pull the content under the beautiful branch

Three.harbor host operation

# 1. Make a container based on the centos8 system on the harbor host and deploy the tomcat service in it. Here we can use dockerfile to create the image. The contents of the dockerfile are as follows:
[root@harbor ~]# vim docker/dockerfile
FROM centos:8
  
RUN rm -rf /etc/yum.repos.d/* & amp; & amp; \
    curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo & amp; & amp; \
    sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo & amp; & amp; \
    yum clean all & amp; & amp; \
    yum makecache & amp; & amp; \
    yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel & amp; & amp; \
    yum -y install wget & amp; & amp; \
    wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.79/bin/apache-tomcat-9.0.79.tar.gz & amp; & amp; \
    tar -xf apache-tomcat-9.0.79.tar.gz & amp; & amp; \
    mv apache-tomcat-9.0.79 /usr/local/tomcat & amp; & amp; \
    /usr/local/tomcat/bin/startup.sh

CMD ["/usr/local/tomcat/bin/catalina.sh","run"]

# Make an image
# The container image name here must be consistent with the harbor warehouse information: ip/storage directory/custom name:label
docker build -t 192.168.187.140/library/tomcat:v50.0 /root/docker/

[root@harbor ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.187.140/library/tomcat v50.0 905023dca678 2 days ago 539MB

# Log in to the harbor warehouse and upload the image
[root@harbor ~]# docker login 192.168.187.140
Authenticating with existing credentials...
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[root@harbor ~]# docker push 192.168.187.140/library/tomcat:v50.0

#View on the web page

Four.jenkins host operation

Preparation: Try to pull the project directory and mirror operations

# Because the image needs to be made on the jenkins host, the docker service must also be deployed on the jenkins host.
# If there is no git command, install it with yum first.
# In this experiment, login verification is required in many places, so make a password-free login on the Jenkins host for other hosts.
[root@jenks ~]# ssh-keygen -t rsa and then press Enter, the creation is successful
# Send the public key to several other hosts
[root@jenks ~]# ssh-copy-id [email protected]
[root@jenks ~]# ssh-copy-id [email protected]
[root@jenks ~]# ssh-copy-id [email protected]

# 1. Try to pull the project directory
[root@jenks ~]# git clone [email protected]:root/beauty.git
Cloning into 'beauty'...
remote: Enumerating objects: 184, done.
remote: Counting objects: 100% (184/184), done.
remote: Compressing objects: 100% (166/166), done.
remote: Total 184 (delta 4), reused 181 (delta 4), pack-reused 0
Receiving objects: 100% (184/184), 1.12 MiB | 31.10 MiB/s, done.
Resolving deltas: 100% (4/4), done.
[root@jenks ~]# ls
anaconda-ks.cfg beauty docker files
[root@jenks ~]# cd beauty/
[root@jenks beauty]#ls
db deploy.yaml Dockerfile jenkinsfile LICENSE pom.xml README.md src
# The pull was successful and the content is also there

# 2. Try to pull the tomcat image on the harbor host
# Add the harbor host’s IP and domain name to the /etc/hosts file for resolution
192.168.187.140 harbor.example.com

# Edit the /etc/docker/daemon.json file
{
 "insecure-registries": ["192.168.187.140"], # Add this line to allow pulling images from insecure image warehouses
 "registry-mirrors" : [ # The ip in square brackets is the ip of the harbor host
    "https://registry.docker-cn.com",
    "http://hub-mirror.c.163.com"
 ]
}

# Pull the image
[root@jenks ~]# docker pull 192.168.187.140/library/tomcat:v50.0
v50.0: Pulling from library/tomcat
a1d0c7532777: Already exists
a06464f817fa: Already exists
7231e5739c7c: Already exists
109b05d3272e: Pull complete
Digest: sha256:4ab5e8b0e514fcc8c4f1396e2237e9f376c4b7e08bd19f99b3b5895ee7eaaa81
Status: Downloaded newer image for 192.168.187.140/library/tomcat:v50.0
192.168.187.140/library/tomcat:v50.0
[root@jenks ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.187.140/library/tomcat v50.0 4d65b426ccad 2 days ago 621MB
# Pull successfully, no problem

Write jenkins pipeline script

Create a task, select the pipeline, click OK and check Do not allow concurrent builds in the configuration

Then start editing the script content

# Pipeline script description
# 1. First pull the project directory on the gitlab host. After the pull is successful, it will be saved in the /root/.jenkins/workspace/tomcat/ directory by default. Enter the beauty (project name) directory and use the mvn command (yum to install maven ) is compiled into a war package. After compilation, the war package is in the target directory under the same level directory and is named ly-simple-tomcat-0.0.1-SNAPSHOT.war.
# 2. Pull the tomcat image on the harbor host, then use the image to run a container named tomcat, delete ROOT in the /usr/local/tomcat/webapps directory, move the war package to the webapps directory and rename it ROOT .war, which becomes ROOT after decompression. The picture can only be seen when this war package is named ROOT.
# 3. Restart the tomcat service, and the war package will be automatically decompressed. Since we did port mapping when running the image to generate the container, we can see the contents of the war package on the 9000 port of the Jenkins host at this time.
# 4. Make the container into a mirror and upload it (provided that the jenkins host is logged in to the harbor warehouse)


pipeline {
    agent any
    { stages
        stage("pull code and mvn"){
            {steps
                sh """
                git clone [email protected]:root/beauty.git
                cd /root/.jenkins/workspace/tomcat/beauty
                mvn clean package -Dmaven.test.skip=true
                """
            }
        }
        stage("pull images and run and deploy tomcat"){
            {steps
                sh """
                docker pull 192.168.187.140/library/tomcat:latest
                docker run -d -p 9000:8080 --name tomcat 192.168.187.140/library/tomcat
                docker exec tomcat rm -rf /usr/local/tomcat/webapps/ROOT
                docker cp /root/.jenkins/workspace/tomcat/beauty/target/ly-simple-tomcat-0.0.1-SNAPSHOT.war tomcat:/usr/local/tomcat/webapps/
                docker exec tomcat /usr/local/tomcat/bin/catalina.sh stop
                docker exec tomcat /usr/local/tomcat/bin/catalina.sh start
                docker exec tomcat mv /usr/local/tomcat/webapps/ly-simple-tomcat-0.0.1-SNAPSHOT /usr/local/tomcat/webapps/ROOT
                """
            }
        }
        stage("build image for tomcat and push"){
            {steps
                sh """
                docker commit -a "zmq" tomcat 192.168.187.140/library/tomcat:v100.0
                docker push 192.168.187.140/library/tomcat:v100.0
                """
            }
        }
    }
}

Access the ip on the web page and add the mapped port number, and you can see the web page

And the image has been uploaded to harbor