Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Continuous integration of Docker+Jenkins+GIT+Tomcat in actual combat

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

For basic Docker tutorials, please refer to: container Docker details

The basics of Jenkins can be referred to: jenkins Notes (1)

The basics of Git can be found in GIT Notes.

File script content reference: junsansi, you can modify the script according to the actual project.

I. Summary

The developer uploads the source code to the github repository, pulls the source code to the jenkins server through the jenkins continuous integration engine combined with the git tool, compiles and packages the source code locally into a war package through the tool maven, creates a custom Dockerfile from the ssh to the Docker host, and finally executes the images, and starts the container, that is, starts the tomcat containing the war package, and then provides the web service to the outside world.

Environment deployment hostname IP address function Docker-server172.20.6.20Docker host Jenkins-server172.20.6.22Jenkins server 2.1 Jenkins server configuration

Jenkins installation and configuration of Jenkins server related tools (maven, git, ssh) configuration, detailed steps can refer to jenkins Notes (1) related tools installation and configuration.

2.2 Docker Host configuration

The basic commands of Docker can be explained in detail by referring to Container Docker. You need to log in to the tomcat host under the Docker host pull and the jenkins server through docker users to log in to the Docker host, publish the war package to the established directory, execute specific scripts to generate Dockerfile, and finally generate images based on the Dockerfile, and launch the container containing the war package, so as to provide WEB services to the public.

Create ssh publishing users and directories

Useradd dockerecho "docker:docker" | chpasswd # set the password for docker users mkdir-pv / data/dockerfiles/scripts

Upload script

Upload the script to / data/dockerfiles/scripts and name it devops, which is customizable, but the script name needs to be consistent later when you configure jenkins to execute the ssh command. Since the distribution path within the script is an absolute path to write, the directory needs to be fixed or can be modified according to the actual situation. This script author:junsansi can be modified with reference to it.

Cat > / data/dockerfiles/scripts/devops.sh ${DOCKER_FILE} echo "ADD *. War / usr/local/tomcat/webapps/$ {PROJECT_NAME} .war" > > ${DOCKER_FILE} echo "EXPOSE 8080" > > ${DOCKER_FILE} echo "CMD / usr/local/tomcat/bin/startup.sh & & tail-f / usr/local/tomcat/logs/catalina.out" > > ${DOCKER_FILE} cat ${DOCKER_FILE} echo "* Init dockerfile end." # Build dockerfilecd ${DOCKER_FILE_DIR} rm * .war-rfmv / data/dockerfiles/war/$ {DOCKER_NAME} / * .war. / echo "echo" # # Build dockerfile for "${DOCKER_NAME} / usr/bin/docker build-t ${DOCKER_NAME}: ${PROJ_VERSION}. # Run docker containerecho "echo" # # Running docker container: "${DOCKER_NAME} / usr/bin/docker run-- name ${DOCKER_NAME} _ D1-d-p ${SPORT}: ${DPORT} ${DOCKER_NAME}: ${PROJ_VERSION} EOFchmod + x / data/dockerfiles/scripts/devops.shchown docker.docker / data/dockerfiles-R

Docker host acquires tomcat image

Docker pull docker.io/tomcat

Use docker images to view tomcat images

At this point, the Docker host is configured.

III. Jenkins configuration

Log in to Jenkins's WEB interface

3.1Configuring ssh information of Docker host

System management-system settings-Publish over SSH--- adds SSH Server

To add a SSH Server, you can add it in the form of a key, or you can use the username password directly. Use the username password here, select

Use password authentication, or use a different key, that is, the password of the docker user, to log in and publish. The master group of the / data/dockerfiles directory has been changed to docker. Make sure that the Jenkins server uses docker users to perform subsequent operations in this directory.

3.2 General settings for building a maven project

To ensure sufficient disk space on the Jenkins server, select discard old builds-> keep the maximum number of builds to 10

In a real development environment, members assist each other directly, and GIT is multi-branch. For branch management, you need to select Parametric Construction-> Select Git Parameter- > define the name as release_branch, so select the parameter type as Branch: if you find the devops script, you can see that there are branches that call this parameter to select and build.

At the same time, in order to create the name of the Docker, you need to name the docker images created later with the name of the project.

Source code management

Select git for source code management and enter url:git@github.com:redhatxl/zrlog.git in the warehouse.

An error message will appear at this time, because this warehouse is my private warehouse, and you need to configure Credentials, click add, and the Kind of Add Credentials selects the user name of the public key user previously uploaded in github. This time, root,Private Key logs in to the local private key of github for the jenkins server. Check the private key cat / root/.ssh/id_rsa and copy and paste it into the Key area to complete the addition. At this point, it can be found that the error report has disappeared.

Before the Branches to build module needs to fill in the variables of our parameter words, refer to $release_branch here.

Environment construction

Add maven build reference and command to build's Goals and options: clean install-D maven.test.skip=true

Add post-build operations

Add post-build steps-Select Send build artifacts over SSH

SSH-Server name selects Docker host

Fill in Source files:target/*.war in Transfers

Remove prefix:target/

Remote directory:/war/$project_name, that is, the war directory under remote / data/dockerfiles (/ data/dockerfiles/war/docker01-tomcat-zrlog)

Exec command:/data/dockerfiles/scripts/devop.sh $project_name zrlog $release_branch 8888 8080

That is, the script command executed after the jenkins server ssh to the docker host, executes the / data/dockerfiles/scripts/devop.sh script. This script needs to pass in 5 references.

Project_name: project name, that is, the name of the docker build images

Zrlog: that is, the project name

Release_branch: branches submitted on git, where we only have master branches

The two ports, the first port and the listening port of the host, the latter port and the port mapped to the inside of the docker container, the tomcat is used here, and the default is port 8080.

Note: once again, you can add multiple server, that is, to implement a cluster. You can say that the web server is stateless and mount the log files to the local physical disk to achieve log monitoring, that is, persistent data storage.

Add add post-build actions, select Editable Email Notification, and use plug-in email to send notifications.

You can customize the subject and content of the email according to your needs. Add the recipient here. Because always is configured globally, fill in the recipient mailbox in the advanced settings.

Click apply after creation and save.

3.3 perform build

Select Build with Parameters, and then select the branch on the github to be executed in the release_branch on the right. Here, select master. Similarly, select project_name. For name of docker images, you can choose to add your own custom name, and click to start building.

View Console Output

View email notifications

Log in to the docker host to view images and container

Web page testing

So far, we simply use jenkins to pull the source code on github to the local jenkins server, use maven to compile and generate war packages and distribute them to docker hosts, execute scripts to generate dockerfile, start the container, and finally notify by mail.

4. Start multiple containers

After configuring the ssh of jenkins, we can add multiple server to achieve distribution. at the same time, we can also generate multiple images and start container with multiple impassable ports by distinguishing port numbers on one host.

Adding the third parameter proxy_port to the parameterized construction of General can be set to the local listening port of the docker host to achieve multi-port and multi-instance.

Called in the ssh command

Ports that correspond to different images can be defined at the beginning of the build.

View the container after startup

Note: if there are special needs, such as publishing php code or others can modify the script on their own, you can define multiple parameters to use flexibly according to the requirements.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report