Recently I found that the company’s services are too scattered. Each deployment is a bunch of jar packages. It makes my hands numb to package, upload and restart, so I decided to study automated deployment.
One of the currently commonly used automation tools is K8S and the other is Jenkins. I looked at the K8S installation and it seems a bit complicated, so I chose a simple Jenkins to try.
2023-11-1 Update:
There were other things that delayed me for a while, but it finally took about two days to finally succeed. Okay, let’s continue.
Before installing Jenkins, be sure to install jdk on the server and configure the environment variables. If you don’t know how, search it yourself.
What I use here is still jdk1.8
The first is to download the installation package. This is still the old rule to download from the official website. It is safe and secure. All historical installation package paths on the Jenkins official website are
The latest version is as follows, but I heard that the new version requires a higher version of jdk support. I still use jdk8, so I still use the previous version. Here I choose jenkins-2.346
In fact, this version will also prompt to use Java11, but in the end it can be used so there is no need to worry about this issue.
After uploading the package to the server, decompress it and place it in the /usr/local directory.
rpm -ivh jenkins-2.346-1.1.noarch.rpm
I have a problem here that keeps prompting NOKEY. At first, the latest version I selected prompts this, so after changing to this version, it still prompts, but regardless of it, it can be used normally.
Of course, who knows how to solve it? I hope you can tell me.
After decompression, we need to modify the Jenkins startup file and configure the jdk path for it.
vi /etc/init.d/jenkins
Please take a look at the installation path of the server jdk. There is an additional /bin/java at the end. Remember to save it.
/usr/java/jdk1.8.0-x64/bin/java
Let’s adjust the user permissions of Jenkins to prevent insufficient permissions when operating files later.
vi /etc/sysconfig/jenkins
By the way, change the port. 8080 is prone to conflict.
After making the changes, save them. Then we need to open the port or close the firewall directly.
firewall-cmd --add-port=1010/tcp --permanent // Open port 1010 firewall-cmd --reload // Restart the port configuration
Let’s find the configuration file
find / -name *.UpdateCenter.xml
Go into this file
vi /var/lib/jenkins/hudson.model.UpdateCenter.xml
This is also a pitfall of Jenkins, that is, no matter which version you install, the download component will always download the latest version, and then various prompts will appear on the plug-in version exception. This problem wasted my whole day.
We go to Tsinghua Mirror Library to find the corresponding version Tsinghua Mirror Library
Click in and select update-center.json, right-click and copy the link address
Fill in this address into the above file and save it
https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/dynamic-2.346/update-center.json
The basic configuration of Jenkins is now complete. Let’s start it in the /etc/init.d/ directory.
cd /etc/init.d/ ./jenkins start
You can see this when starting up
Here is its initial password. If you don’t remember the password later, you can use the following command to find it.
cat /var/lib/jenkins/secrets/initialAdminPassword
Okay, let’s enter the address http://192.168.43.128:1010/ directly into the browser
When the following page appears, it means success (sometimes the page keeps prompting “Please wait while Jenkins is getting ready to work…”, don’t worry, it is downloading components and waiting for a while)
Just enter the password you just entered to log in.
Don’t install the plug-in first, select the plug-in to install.
Select None and click Install in the lower right corner.
Then create a user
Continue and click save
Those who are careful will find that there is something wrong with my address here. Don’t worry about it. This is because the document was written in two times. The first time was on my local machine, and the second time was installed on the company’s server.
This is the default, no need to change it
Click to start using
You can see that there is still an exception prompt here, regardless of it
Let’s install a Chinese plug-in for it
Choose this to install the plug-in
After installation, you are prompted to restart.
Let’s go to the server and restart Jenkins.
cd /etc/init.d ./jenkins restart # Restart the service ./jenkins stop # Stop the service ./jenkins start # Start the service
Sometimes an error will be reported during startup because not all shutdowns are completed during shutdown. We use the ps -ef|grep jenkins command to take a look. If there is any error, just kill -9
After restarting, we log in again, and when we come in, we can see that the page has changed to Chinese.
If there are any problems with the installation, uninstall and reinstall.
sudo rpm -e jenkins # Uninstall sudo rpm -ql jenkins # Check whether the uninstallation is successful sudo find / -iname jenkins | xargs -n 1000 rm -rf # Completely delete residual files
Let’s wait for a while before this step of Jenkins. In order to facilitate later use, we also need to install Maven and Git on the server where Jenkins is installed.
Maven download address maven3.6.1 download address
Upload the downloaded package to the server
Then unzip
tar -zxvf apache-maven-3.6.1-bin.tar.gz
After decompression, you still need to configure environment variables.
vim /etc/profile
Add these two lines at the end, which is the path you just decompressed.
export MAVEN_HOME=/usr/local/apache-maven-3.6.1 export PATH=${<!-- -->PATH}:${MAVEN_HOME}/bin
After saving, refresh the configuration file.
source /etc/profile
Then verify whether the installation is complete
mvn -version
That’s it
We also need to configure a warehouse for Maven
Create a folder first
mkdir repository
Then we go to the maven package we just decompressed and find the configuration file settings.xml
cd apache-maven-3.6.1/conf
Open this file, find the content of the first arrow, copy it and put it below into the folder you just created.
<localRepository>/usr/local/repository</localRepository>
Continue to scroll down and find the label. Let’s configure the Alibaba Cloud image here.
Pay attention to the position, don’t put it in the comment tag
<!--Alibaba Cloud Image--> <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror>
Continue to scroll down to find the profiles tag and configure the default jdk information.
<profile> <id>jdk-1.8</id> <activation> <activeByDefault>true</activeByDefault> <jdk>1.8</jdk> </activation> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion> </properties> </profile>
Remember to save. The Maven installation is complete here.
Git installation is relatively simple, just run the command directly
yum -y install git
Okay, after Maven and Git are installed, let’s go back to Jenkins and see how this implements automated deployment.
Because I am a Maven project and the code is placed in Git, I also need to install these two plug-ins on Jenkins.
Wait until it is installed
Then we install the Git plug-in. I don’t know the difference between Git and GitHub plug-ins here, so I installed them both.
After the same installation is completed, you need to restart Jenkins to take effect. Let’s restart Jenkins.
After restarting, we log in to Jenkins again
Then continue to configure the Maven and jdk paths installed on our server in System Management-Global Tool Configuration
Configure the Maven configuration file path of the server (note where you install it)
/usr/local/apache-maven-3.6.1/conf/settings.xml
Then configure jdk, your own installation path, and choose any alias
/usr/java/jdk1.8.0-x64
Scroll down, there is another Maven to configure, first uncheck the automatic installation, fill in the installation path of Maven, and the name can be whatever you want.
/usr/local/apache-maven-3.6.1
Click save
Because my Jenkins server and my project server are not together, I also need a plug-in Publish over SSH to push the jar package created by Jenkins to the designated server.
Let’s install the Publish over SSH plugin
After installation, remember to restart Jenkins and then log in to Jenkins again.
This time we choose System Management-System Configuration
Find the Publish over SSH configuration and click New
Fill in the server where we want to deploy the service
Pay attention to this /usr/local/webapp, we will push the jar to this location of this service later.
Click Advanced and fill in the server password.
Check this to use a password. Of course, you can also use other methods, as long as you can finally connect to the server.
Just need a password
Test it. If it prompts success, it means that the configuration is OK and Jenkins can connect to the specified server.
If you have multiple servers, continue to add them below.
All preparations are completed and we can start deploying the project
Create a new task
This is a Maven project here, just use Maven directly.
Select Git and fill in your project address.
Here is how to configure the Git account password
Click here to enter
Come out of this page and fill in your account and password.
After clicking Add, look at this place and you can select the Git account you just created.
Specify the branch to deploy to
Scroll down and select Send files or execute commands over SSH
Fill in these, and look down. I will explain what each one means one by one.
The first 104 is the server we configured above to deploy the service.
The second sakura-service/sakura-order/target/sakura-order-1.0.0.jar path is the path of the jar package after our project is packaged, but we must pay attention to the project structure. If the path here is wrong, the jar will not be pushed later. Of course, if you only have one jar package, you can write **/target/*.jar
Below is my project directory
The third sakura-service/sakura-order/target/ path is the path to be removed after we push the jar package to the target server. The path we configured above is /usr/local/webapp. If we do not add this, the final jar package will be The pushed directory is /usr/local/webapp/sakura-service/sakura-order/target/. Adding this will remove sakura-service/sakura-order/target/
The third /sakura-order is a supplementary directory, that is, I want to put my jars under /usr/local/webapp/sakura-order, because this is a microservice with many modules, so I don’t want to put them together.
The last sh /usr/local/webapp/sh_folder/start.sh is the script to start the jar package. This needs to be placed in the specified location first.
The content of the script is as follows. You can customize the entire script according to your own needs. I am here just for example.
pkill -f kills the previous process according to the startup command, nohup java -jar is nothing to say about starting the jar package
In fact, you can also directly write nohup java -jar /usr/local/webapp/sakura-order/sakura-order-1.0.0.jar >/dev/null 2> & amp;1 & amp; in the Exec command.
pkill -f 'java -jar /usr/local/webapp/sakura-order/sakura-order-1.0.0.jar' sleep 5 nohup java -jar /usr/local/webapp/sakura-order/sakura-order-1.0.0.jar >/dev/null 2> & amp;1 & amp;
Note that the script must be put in place first, otherwise an error will be reported.
Click save
Multiple modules like mine can continue to be added below, but this is just a demonstration and will not continue. In fact, just follow the above configuration and just change the directory.
Next is the moment to witness the miracle. Success or failure lies here. If you fail, you will have a headache and go back and look again to see if you made a mistake.
Click Build Now
Click here to see the details
Here is a console output
It will take a long time the first time because you need to download the jar package, but it will be very quick after that.
You can also check to see if there are any errors reported. Generally, if there are exceptions, they will be prompted here.
The normal one is as follows, which shows that a file is pushed successfully and the final status is SUCCESS.
Let’s go to the target server and take a look. We can see that the jar package has been pushed up.
Look at the process again and find that it has started normally.
One more updated question. When I deployed a server, I found that the push was successful, but the script was not executed in the end. After two hours of troubleshooting, I finally found that there was a problem with the environment variables of that server. I would like to condemn those who have already done so. Former colleagues who have resigned, can you please be more reliable?
Okay, let’s start solving the problem. Let’s go to the target server to check the environment variable path.
echo $PATH
The following is the path to this server
Then we take this out and add export PATH= in front of it
export PATH=/usr/local/java/jdk1.8.0_221/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
Then put it in front of our script and save it.
Let me remind everyone again, remember to delete the unused build history. This thing really takes up memory. Suddenly it prompts that the server has insufficient memory. I am confused. When I look again, it is occupied by Jenkins.
Click to delete
You can also configure it when configuring the task.
At this point, the installation and automated deployment of Jenkins are complete. If you have any questions, you can ask me at any time. If you need to install packages, you can also contact me.