Docker + Jenkins + GIT + Tomcat hands-on continuous integration, dockerjenkins

Source: Internet
Author: User
Tags custom name nginx host

Docker + Jenkins + GIT + Tomcat hands-on continuous integration, dockerjenkins

I. Nginx application Overview

Nginx is a high-performance http server/reverse proxy server and email (IMAP/POP3) proxy server.

1. http Server

Nginx is an http service that provides http services independently.

2. VM
Multiple websites can be virtualized on one server. For example, the virtual host used by a personal website.

3. reverse proxy and Server Load balancer
When a website's access volume reaches a certain level and a single server cannot meet users' requests, you need to use multiple Server clusters to use nginx for reverse proxy. In addition, multiple servers can carry the load evenly, so that a server is not idle due to the high load of a server.

2. What is a VM?

Virtual Host Technology is a technology used by Internet servers to save server hardware costs. Virtual Host technology is mainly used in HTTP (Hypertext Transfer Protocol, Hypertext Transfer Protocol) services, the logic of a certain or all service content of a server is divided into multiple service units, and multiple servers are displayed externally, so as to make full use of the server hardware resources.


Virtual Hosts use special hardware and software technologies to divide a real physical server host into multiple logical storage units. Each logical unit has no physical entity, but each logical unit can work on the network like a real physical host, with a separate IP address (or shared IP address) independent domain names and complete Internet servers (including WWW, FTP, and email.

The key technology of a VM is that it does not interfere with each other even if different server programs run for multiple users on the same hardware and operating system. Each user has a part of his/her system resources (such as IP addresses, file storage space, memory, and CPU ). Each virtual host is completely independent. in the outside world, each virtual host performs exactly the same as a single host. Therefore, this virtualized logical host is visually called a "virtual host ".

Iii. Nginx host-based Virtual Host Configuration

Default configuration parameters of Nginx. conf

#user  nobody;worker_processes  1;#error_log  logs/error.log;#error_log  logs/error.log  notice;#error_log  logs/error.log  info;#pid        logs/nginx.pid;events {    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '    #                  '$status $body_bytes_sent "$http_referer" '    #                  '"$http_user_agent" "$http_x_forwarded_for"';    #access_log  logs/access.log  main;    sendfile        on;    #tcp_nopush     on;    #keepalive_timeout  0;    keepalive_timeout  65;    #gzip  on;    server {        listen       80;        server_name  localhost;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {            root   html;            index  index.html index.htm;        }        #error_page  404              /404.html;        # redirect server error pages to the static page /50x.html        #        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }        # proxy the PHP scripts to Apache listening on 127.0.0.1:80        #        #location ~ \.php$ {        #    proxy_pass   http://127.0.0.1;        #}        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000        #        #location ~ \.php$ {        #    root           html;        #    fastcgi_pass   127.0.0.1:9000;        #    fastcgi_index  index.php;        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;        #    include        fastcgi_params;        #}        # deny access to .htaccess files, if Apache's document root        # concurs with nginx's one        #        #location ~ /\.ht {        #    deny  all;        #}    }    # another virtual host using mix of IP-, name-, and port-based configuration    #    #server {    #    listen       8000;    #    listen       somename:8080;    #    server_name  somename  alias  another.alias;    #    location / {    #        root   html;    #        index  index.html index.htm;    #    }    #}    # HTTPS server    #    #server {    #    listen       443 ssl;    #    server_name  localhost;    #    ssl_certificate      cert.pem;    #    ssl_certificate_key  cert.key;    #    ssl_session_cache    shared:SSL:1m;    #    ssl_session_timeout  5m;    #    ssl_ciphers  HIGH:!aNULL:!MD5;    #    ssl_prefer_server_ciphers  on;    #    location / {    #        root   html;    #        index  index.html index.htm;    #    }    #}}
Virtual Host based on different domain names
server {        listen       80;        server_name www.aaa.com;        location / {                root  html/aaa;                index index.html index.htm;        }    }    server {        listen       80;        server_name  www.bbb.com;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {            root   html;            index  index.html index.htm;        }

After the modification is complete, create the aaa directory in the html directory and write the test page as follows:

The access test is as follows:

I. Summary

The developer uploads the source code to the github repository, uses the jenkins continuous integration engine and the git tool to pull the source code to the jenkins server, and uses the maven tool to compile and package the source code locally into a war package, on ssh to the Docker host machine, run the script to generate a custom Dockerfile, and finally run the generated images and start the container. That is, tomcat containing the war package is started, to provide external web services.

2. Environment deployment
Host Name IP address Function
Docker-server 172.20.6.20 Docker host machine
Jenkins-server 172.20.6.22 Jenkins 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 note (1) Related tool installation and configuration.

2.2 Docker Host Configuration

For details about Docker basic commands, refer to the container Docker explanation. You need to log on to the Docker host machine as a basic tomcat environment and jenkins server under the docker host machine pull, and publish the war package to the specified directory, execute a specific script to generate the Dockerfile, generate the images based on the Dockerfile, and start the container Based on the war package to provide external WEB services.

Create an ssh publish user and Directory

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

Upload script

Upload the script to/data/dockerfiles/scripts and name it devops. The name can be customized. However, the script name must be consistent when jenkins is configured to execute the ssh command. Because the distribution path inside the script is the absolute write path, the directory should be fixed and can be modified according to the actual situation. This script author: junsansi, which can be modified by reference.

cat >/data/dockerfiles/scripts/devops.sh<
 
   ${DOCKER_FILE}echo 'MAINTAINER junsansi "junsansi@sina.com"' >> ${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
 

Get tomcat image from Docker host machine

Docker pull docker. io/tomcat
Use docker images to view tomcat images

Now the Docker host has been configured.

3. Jenkins Configuration

Log on to the Jenkins WEB page

3.1 configure ssh information for the Docker host

System Management --- system settings --- Publish over SSH --- add an SSH Server
To add an SSH Server, you can add it as a key or directly use the user name and password. Here, you can use the user name and password, select
Use password authentication, or use a different key, that is, the password of a docker user, to log on and release the product. The owner group of the/data/dockerfiles directory has been changed to docker, check whether the Jenkins server uses docker to perform subsequent operations in this directory.

3.2 General settings for building a maven Project


To ensure sufficient disk space for the Jenkins server, select discard old build> keep the maximum number of build as 10

In a real development environment, members directly interact with each other. GIT is multi-branch. Considering branch management, you need to select parametric build here-> select Git Parameter-> define name as release_branch, therefore, select Branch as the parameter type. If you find the devops script, you can see that this parameter is called to select the build Branch.
In addition, considering the name of the created Docker, you must name the subsequently created docker images with the project name.

Source code management

Source code management select git, fill in repository url: git@github.com: redhatxl/zrlog. git

In this case, an error message is displayed. Because this warehouse is my private warehouse, you need to configure Credentials, click add, and select SSH Username with private key for Kind of Add Credentials, username: select the Username of the public Key user uploaded on github. The Username is root, and the Private Key is the local Private Key of the jenkins server to log on to github. view the Private Key cat/root /. copy and paste ssh/id_rsa to the Key area. At this time, we can see that the error has disappeared.

In the Branches to build module, we need to fill in the variables in our parameter terms, and reference $ release_branch here.

Environment Construction

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

Add post-build operations

Add the post-build step --- Select Send build artifacts over SSH
SSH-Server name select Docker host machine
Set Source files: target/*. war in Transfers.

Remove prefix: target/
Remote directory:/war/ Projectname, that is, the war directory (/data/dockerfiles/war/docker01? Tomcat? Zrlog) Execcommand:/data/dockerfiles/scripts/cmdp. sh Projectname, that is, the war directory (/data/dockerfiles/war/docker01? Tomcat? Zrlog) Execcommand:/data/dockerfiles/scripts/devop. shproject_name zrlog $ release_branch 8888 8080
That is, the script command executed after the jenkins server ssh to the docker host machine, and the/data/dockerfiles/scripts/cmdp. sh script. This script requires five references,
Project_name: Project name, that is, the name of the images generated by docker
Zrlog: Project name
Release_branch: branch submitted on git. Here we only have master Branch
The two ports, the former port and the host listening port, and the latter port mapped to the internal port of the docker container. tomcat is used here, and port 8080 is used by default.

Note: once again, you can add multiple servers to implement the cluster. The web server is stateless and the log files are mounted to the local physical disk to implement log monitoring and persistent data storage.

After adding a build, select Editable Email Notification and use plug-in mail to send notifications.
You can customize the subject and content as needed. Here, add the information recipient. Because always is configured globally, enter the recipient's email address in advanced settings.

After the application is created, click and save.

3.3 execute build

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

View Console Output


View email notifications

Log on to the docker host machine to view images and container.

Webpage Testing

At this point, jenkins is used to pull the source code on github to the local jenkins server, generate war packages using maven compilation, distribute them to the docker host, execute scripts to generate dockerfile, and start the container, final email notification.

4. Start Multiple containers

After configuring jenkins ssh commands, we can add multiple servers to achieve distributed processing, at the same time, we can also generate multiple images by distinguishing port numbers on a host and start the container with multiple ports not connected.
The third parameter proxy_port can be added to the parameterization construction process of General. It can be set to the local listening port of the docker host to implement multi-port and multi-instance

Call

Different images ports can be defined at the start of the build.

View started containers

Note: If you have special requirements, such as releasing php code or others, you can modify the script by yourself. You can define multiple parameters as needed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.