The fourth stage of improvement
Time: August 22, 2023
Participants: All members of the class
Contents:
Automatically package and deploy Tomcat environment based on Jenkins
Table of contents
1. Traditional website deployment process
(1) The process of traditional code going online
(2) Current mainstream website deployment process
(1) Experimental environment and server settings
(2) Turn off the firewall and selinux on all hosts, and modify the host name
(4) Configure the jenkins host
1. Traditional website deployment process
In the operation and maintenance process, website deployment is one of the operation and maintenance tasks.
The traditional website deployment process is roughly divided into:
Requirements analysis-->Prototype design-->Develop code-->Submit code-->Intranet deployment-->Intranet test-->Confirm online-->Backup data-->External network update-->External network Testing-->Release completed.
If an abnormality is found in the code during the internal network test, the name of the code developer will be returned and the code will be adjusted; if an abnormality is found in the code deployed on the external network during the external network test, the website can be rolled back in time.
(1) The process of traditional code going online
The developer initiates the request to go online (the email contains the developed WAR package) --> the operation and maintenance personnel connect to the online load scheduler (Nginx) --> isolate a group of servers (Tomcat) --> connect to the server (Tomcat) ) --> Back up the old code (tar packaging) --> Delete the old code directory --> Upload the new WAR package --> External network test --> If the test fails, roll back the code through backup --> If the test passes, Use the rsync script to push the code to other servers --> Unify the external network test --> Connect the scheduler to restore the isolation mechanism --> Isolate another group of servers to implement the online steps --> Online completion.
(2) Current mainstream website deployment process
The current mainstream website deployment method: fully automatic deployment + testing through the Hudson/Jenkins tool platform. It is an scalable continuous integration engine and an open source software project. It aims to provide an open and easy-to-use software platform to make continuous integration of software easier. possible. Jenkins is very easy to install, configure, and easy to use.
Developers: After writing the code, you don’t need to compile and package the source code yourself. You can just store the code branch directly in the SVN or Git warehouse.
Operation and maintenance personnel: Reduce the error rate of manual intervention, while freeing operation and maintenance personnel from complicated operations such as uploading code, manual backup, and updates.
Testers: You can perform simple code and website testing through Jenkins.
(3) How Jenkins works :
First, copy the source code from the SVN/Git version control system to the local, and then call Maven to build according to the set script. The key to the entire system is the build script, which tells Jenkins the tasks that need to be performed in an integration.
2. Installation and deployment. Automatically package and deploy the Tomcat environment based on Jenkins.
(1) Experimental environment and server settings
IP address |
CPU name |
Role |
192.168.100.131 |
git |
git server |
192.168.100.132 |
Jenkins git client |
jenkins server |
192.168.100.133 |
tomcat |
web server |
(2) Turn off the firewall and selinux on all hosts , and modify the host name
git host
Jenkins host
web host
(3) Configure git host
1. # rpm installation installation dependencies: both 1 and 2 are installed
[root@gitclient ~]# yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
2. # Compile and install—server and client, visit https://mirrors.edge.kernel.org/pub/software/scm/git/ to download the required version
[root@gitclient ~]# tar xf git-2.22.0.tar.gz -C /usr/src/
[root@gitclient ~]# cd /usr/src/git-2.22.0/
[root@git git-2.22.0]# make configure
[root@git git-2.22.0]# ./configure --prefix=/usr/local/git && make && make install
Ps Note: As shown in the picture above, /bin/sh: autoconf: command not found make: *** [configure] error 127, what should I do?
Follow the prompts to install autoconf
[root@git git-2.22.0]# yum -y install autoconf
Then reconfigure, compile and install!
[root@git git-2.22.0]# ln -sf /usr/local/git/bin/git /usr/bin/
[root@git git-2.22.0]# git --version
3. #Configure users on the git server
[root@git ~]# useradd git
[root@git ~]# echo "123456" | passwd --stdin git
4. Create a local warehouse probe
[root@git ~]# su - git
[git@git ~]$ mkdir probe.git
[git@git ~]$ cd probe.git
[git@git probe.git]$ git --bare init
[git@git probe.git]$ exit
5. Clone the project code and synchronize it to the warehouse you created.
[root@git ~]# rz #Upload psi-probe.tar.gz
[root@git ~]# tar xf psi-probe.tar.gz
[root@git ~]# git clone [email protected]:/home/git/probe.git
[root@git ~]# cp -rf psi-probe/* probe/
[root@git ~]# cd probe/
[root@git probe]# git add .
[root@git probe]# git config --global user.email "[email protected]"
[root@git probe]# git config --global user.name "huyang"
[root@git probe]# git config --list
[root@git probe]# git commit -m "all probe"
[root@git probe]# git push origin master
(4) Configure the jenkins host
1. Install jenkins from source code
[root@jenkins ~]#rm -rf /usr/bin/java
[ root@jenkins ~]#tar xf jdk-17_linux-x64_bin.tar.gz
[ root@jenkins ~]#mv jdk-17.0.8 /usr/local/java
[root@jenkins ~]#vim /etc/profile
[root@jenkins ~]#source /etc/profile
[root@jenkins ~]#java -version
[root@jenkins ~]# rpm -ivh jenkins-2.181-1.1.noarch.rpm
2. Start jenkins
[root@jenkins ~]# jenkins start (keep it open)
The password is as shown in the picture:
[root@jenkins ~]# netstat -lnpt | grep :8080
3. Web configuration
Browser access test: http://192.168. 1 00.1 32 :8080
Enter the secret key:
Unlock Jenkins, password obtained from command line
Enter the authorization password and click Next
Choose to install recommended plug-ins
Automatically download plug-ins (status is all green, next step)
The status is red, please restart and install again.
The status is red, and you can enter after restarting. However, there are plug-in installation errors, which will affect
After turning green, do the following:
Create an administrator user and choose to use the admin account to continue!
Instance configuration, save and done!
Click to get started
Ready to use!
(5) Jenkins configuration
1. Install the necessary plugins for Jenkins
On the Jenkins homepage, click Manage Jenkins>>Manage Plugins>>Optional plug-ins on the left, enter the Publish Over SSH and Maven Integration plug-ins to be installed in the filter search box, and check the check box on the left, and click "Direct installation" can start the plug-in installation operation.
Check:
Bottom of the page:
2. Add verification credentials
You can add credentials here or not. This test does not involve credentials.
Operation as shown:
Operation as shown:
Operation as shown:
Operation as shown:
Operation as shown:
In the credentials page, click "system" to jump to the "System" page. Click "Add Domain" in the left navigation bar.
Jump to the "Add Domain" page. Create a domain name of "web" on this page and click "ok" to complete the configuration.
"Add Credentials"
Click "+Add Credentials"
After filling in the above data, click "OK" to view the newly added remote web host account, as shown below:
3. Add Publish Over SSH remote host
Click "Manage Jenkins" -> "Configure System" -> "Publish over SSH" -> "SSH Servers" -> "Add" option button on the Jenkins homepage to add an SSH remote host:
As shown in the figure, after entering necessary information such as Name, Hostname, Username, etc., click the " Advanced" option button -> Check the "Use Password authentication, or use a different key" option -> Enter the "Remote host login password" -> " Test Configuration" to test the remote host configuration. After successfully testing the remote host configuration, click the "Save" button.
4. Configure Maven, JDK, and Git environments
On the Jenkins homepage, click "Manage Jenkins" -> "Global Tool Configuration" -> "JDK" -> Add "JDK" and set the JDK alias to "JDK17" . Remove the "Install automatically" option and set "JAVA_HOME" to the actual installation path of the JDK in this case.
/usr/local/java
5. Install maven in Jenkins
[root@jenkins ~]# tar xf apache-maven-3.5.0-bin.tar.gz
[root@jenkins ~]# mv apache-maven-3.5.0 /usr/local/maven-3.5.0
6. Change the Alibaba Cloud mirror station for maven
[root@jenkins ~]# vim /usr/local/maven-3.5.0/conf/settings.xml
#删除两个<mirrors>中的所有东西,大概12行,把下面的阿里云镜像站添加到两个<mirrors>中
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
final effect
Find the Maven configuration option in the "Global Tool Configuration" configuration interface, then click "Add Maven" and set the alias to " maven3.5.0 " .
7. Git configuration
[root@jenkins ~]# which git
/usr/bin/git
After the above global basic configuration is completed, click Save to complete.
(6) Configure web host
1. Install tomcat
[root@tomcat ~]# tar xf apache-tomcat-8.5.40.tar.gz
[root@tomcat ~]# mv apache-tomcat-8.5.40 /usr/local/tomcat
2. Publish the public key to the jenkins host
[root@tomcat ~]# ssh-keygen
[root@tomcat ~]# ssh-copy-id 192.168.100.132
3. The host Jenkins uses the jenkins user to connect to git by default, so use the jenkins user to generate a key pair and send it to git.
[root@jenkins ~]# id jenkins
[root@jenkins ~]# su -s /bin/bash jenkins
bash-4.2$ ssh-keygen
bash-4.2$ ssh-copy-id [email protected]
bash-4.2$ ssh [email protected] #Login test
bash-4.2$ exit
[root@jenkins ~]# ssh-keygen
[root@jenkins ~]# ssh-copy-id [email protected]
[root@jenkins ~]# ssh [email protected] #Login test
4. Create a new Maven project
After the above configuration is completed, return to the Jenkins homepage, select "New Task", then enter a task name "probe", select "Maven project" and click the "OK" button at the bottom of the current page.
Select "New item "
Select "Maven project" and click "OK"
After clicking the " OK " button, select " Source Code Management ", select " Gi t", and configure " RepositoriesURL " as
[email protected]:/home/git/probe.git
Select "Build"
Enter the second line: clean package -Dmaven.test.skip=true
Select " send build artifacts over SSH " in " Post-Build Operations ". The meaning of executing the command in " Exec command " is: kill the Tomcat process before automatic deployment, then delete the war package, and use the scp remote copy command to automatically package Jenkins. Copy the good project war package to the current Tomcat application directory. Then restart Tomcat.
After selecting, enter the following in Exec command :
scp 192.168.100.132:/root/.jenkins/workspace/probe/psi-probe-web/target/probe.war /usr/local/tomcat/webapps/
/usr/local/tomcat/bin/startup.sh
After all the above configurations are completed, click Save. Then click the " probe" -> "Build Now " you just created until the project is built. The build process can be viewed in the "Console Output".
Select Build Now and #1 appears
Click on the history and a link will appear
Click the link to go to #1
Select console output to view the build process
The wait is over
5. Verify Jenkins automatic packaging and deployment results
Check on the web host whether the probe directory is copied to the /usr/local/tomcat/webapps directory
[root@tomcat ~]# ls /usr/local/tomcat/webapps/
[root@tomcat ~]# ls /usr/local/tomcat/webapps/probe -l
Judging from the above results, Jenkins has copied the prepared probe war package.
(7) Web page access test:
It can be seen from the execution command after the build that Tomcat has been restarted and the test probe monitoring system is accessed through the browser. http://192.168.100.133:8080/probe
Unable to access, need to configure permissions!
[root@tomcat ~]# vim /usr/local/tomcat/conf/tomcat-users.xml
[root@tomcat ~]# vim /usr/local/tomcat/webapps/manager/META-INF/context.xml
[root@tomcat ~]# /usr/local/tomcat/bin/shutdown.sh
[root@tomcat ~]# /usr/local/tomcat/bin/startup.sh
Visit the test again:
http://192.168.100.133:8080/probe , enter username: tomcat, password: tomcat
After logging in, it displays:
At this point, Jenkins automatically packages and deploys.