Using rpmbuild to package erlang and rabbitmq for service deployment

Keywords: Linux

Using rpmbuild to package erlang and rabbitmq for service deployment

Background description

1. rabbitmq Is based on erlang Developed message queue, itself rabbitmq I don't distinguish between architectures. 
2. however erlang It is architecture specific, You need to compile for different architectures
3. rabbitmq Command needs to get erlang Binary path of. 
4. There are two ways to deal with it, The first is to erlang Binary addition of/usr/bin etc..
5. Another method can be modified rabbitmq Environment variable for the command. Can be processed. 

Get installation package

Sign in rabbitmq Official website of, Download from the official website erlang Binary
 Can from github Download above rabbitmq The executable file is easy to use.
otp_src_24.0.tar.gz 
rabbitmq-server-generic-unix-3.9.8.tar.xz

Compile erlang in arm and x86 respectively

Note that it's best to find a machine that can surf the Internet, It's hard to compile, otherwise. 
To be installed rpm The main packages are as follows:
yum install build-essential openssl openssl-devel unixODBC unixODBC-devel make gcc gcc-c++ kernel-devel m4 ncurses-devel tk tc xz
 Note that some environments may require --without-javac To compile
 Galaxy Kirin ARM Take version as an example. 
decompression  erlang Source code, cd reach otp Go to the directory of
./configure --prefix /opt/rabbitmq/erlang --without-javac
 be careful,This can save time and distinguish between source code and compiled code.
Enter after compilation
/opt/rabbitmq/erlang/bin
 implement
./erl
 Verify version. 
Erlang/OTP 24 [erts-12.0] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]

Eshell V12.0  (abort with ^G)
1> 
Under normal circumstances, this is OK.
  • Here are some unusual cases
domestic linux Inside openssl1.1 About the same version erlang 24 The version of can be used normally
 quite a lot centos7 The system will report missing libcrypto.so.1.1
 The simplest solution is from an existing environment, Copy a binary file of the same architecture to the job level
/usr/lib Directory can avoid this problem. 
  • Rabbitmq is relatively simple and can be directly decompressed into / opt/rabbitmq

Processing of rabbitmq

be careful rabbitmq If you follow the above directory, all executable files are in the
/opt/rabbitmq/sbin Under the directory
[root@Kylin-arm-vm sbin]# ll
 Total consumption 44
-rwxrwxrwx 1 root root  897 10 June 28-19:19 rabbitmqctl
-rwxrwxrwx 1 root root  651 10 June 28-19:19 rabbitmq-defaults
-rwxrwxrwx 1 root root  906 10 June 28-19:19 rabbitmq-diagnostics
-rwxrwxrwx 1 root root 6929 10 June 28-19:19 rabbitmq-env
-rwxrwxrwx 1 root root  902 10 June 28-19:19 rabbitmq-plugins
-rwxrwxrwx 1 root root  859 10 June 28-19:19 rabbitmq-queues
-rwxrwxrwx 1 root root 6044 10 June 28-19:19 rabbitmq-server
-rwxrwxrwx 1 root root  899 10 June 28-19:19 rabbitmq-streams
-rwxrwxrwx 1 root root  902 10 June 28-19:19 rabbitmq-upgrade
  • Note that it needs to be edited and modified. Ensure that the current system does not have a version below erlang 23 installed, otherwise there will be exceptions
vim Edit all executables
 Add the following content to the header of each file
export PATH=$PATH:/opt/rabbitmq/erlang/bin
 In that case, The command can be executed directly, No need to install in environment variables erlang Yes. 
The execution effect is
[root@Kylin-arm-vm sbin]# ./rabbitmqctl version
3.9.8
  • If there is no other pursuit, it is also possible to directly use rabbitmq server to run the service

Higher pursuit

Trying to run as a service, In this way, automatic startup can be realized, 
as well as rabbitmq It can automatically keep alive in case of abnormality.
The method is also relatively simple, Edit a rabbitmq.service that will do
 Note that this name is deliberately associated with rabbitmq-server.service Distinguish between official documents.
To quickly install without a network rabbitmq And meet the installation directory requirements of safe and reliable machines. 
Edit a rabbitmq.service The main contents are as follows:
  • Note: this rabbitmq.service does not conflict with the previous environment variables, mainly because the processing of environment variables is not very elegant when systemd manages services
[Unit]
Description=RabbitMQ broker
After=syslog.target network.target

[Service]
Environment="PATH=/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/rabbitmq/erlang/bin"
Environment="HOME=/opt/rabbitmq/"
Environment="LIB=/opt/rabbitmq/erlang/lib:/usr/lib"

Type=notify
LimitNOFILE=65536
LimitNPROC=65535
WorkingDirectory=/opt/rabbitmq
ExecStart=/opt/rabbitmq/sbin/rabbitmq-server
ExecStop=/opt/rabbitmq/sbin/rabbitmqctl stop
ExecStop=/bin/sh -c "while ps -p $MAINPID >/dev/null 2>&1; do sleep 1; done"
Restart=always
NotifyAccess=all
TimeoutStartSec=3600

[Install]
WantedBy=multi-user.target
  • Note that it imitates the settings of some other official documents. However, I don't seem to have finished the web port access of 15672
  • Note that the environment variables need to be given enough to avoid program exceptions
  • Note that the Restart setting is actually the same as -- restart=always added when docker run s
systemctl enable rabbitmq  && systemctl restart rabbitmq 
Service startup and simple maintenance can be realized

Higher pursuit

  • Many environments require rpm package to be installed, and in order to solve the problem of too many files and disorderly manual processing, it is easy to make mistakes
  • It is recommended to package all the above files into rpm packages for distribution and use
  • The prerequisite is to build the rpmbuild environment using Yum install RPM build
yum install rpm-build -y
mkdir -p /root/rpmbuild/{SPECS,SOURCES,RPMS}
Note: first place the resource file in the following directory
/root/rpmbuild/SOURCES
 The content is: 
[root@Kylin-arm-vm SOURCES]# ll
 Total consumption 12
drwx------ 9 root root 4096 10 July 28-17:33 rabbitmq
-rw------- 1 root root  603 10 June 28-19:18 rabbitmq.service
drwx------ 2 root root   61 10 June 28-20:30 redis
-rw------- 1 root root  163 10 June 28-20:19 redis.service
[root@Kylin-arm-vm SOURCES]# pwd
/root/rpmbuild/SOURCES
 Look, I packed it here redis and rabbitmq I feel redis Relatively simple. rabbitmq There's more to say.
 So I don't say how to pack redis Yes. 
  • After the file and location are packaged, you need to create a spec file. Here, you still imitate the official spec file for processing
Location is /root/rpmbuild/SPECS/
Add a simple content vim rabbitmq.service
#DEFINES
%define _binaries_in_noarch_packages_terminate_build   0
%global __os_install_post %{nil}

Name:       rabbitmq
Version:    3.9.8
Release:    1%{?dist}
Summary:    Rpm package for Rabbitmq ...

Group:      gscloud-rabbitmq
License:    Copyright © Inspur Inspur General Software Co., Ltd
URL:        https://www.inspur.com/
Source0:    rabbitmq/
Source1:    rabbitmq.service
BuildArch: noarch
Autoreq:    no

%changelog
* Sun Apr 18 17:26:47 CST 2021 gscloud-author:zhaobsh
Initial Version ...
%description    
Rpm package for Rabbitmq

%install
app_dir=%{buildroot}/opt
%{__install} -p -D %{SOURCE1} $RPM_BUILD_ROOT/etc/systemd/system/rabbitmq.service
mkdir -p $app_dir                               
echo pwd
echo %{SOURCE0}/rabbitmq/
cp -r %{SOURCE0}/rabbitmq/ $app_dir/
cp -r %{SOURCE1} /etc/systemd/system/rabbitmq.service
%files
%defattr(777,root,root)
/opt/rabbitmq
/etc/systemd/system/rabbitmq.service
%dir    
/opt/rabbitmq
  • Note that the core is the script at install and files, which must be available normally
  • Use rpmbuild -bb rabbitmq.spec to compile
  • Note that I compiled it before rpm packaging. Many tutorials can compile it during rpm packaging (for example, upgrading openssh before)
  • I don't think it's good or bad. The advantage of self packaging is that if there's a problem, you can directly read and modify it. If rpm, the waiting time is longer
  • The packaged files are stored under / root/rpmbuild/RPMS according to the schema
  • Other machines can execute rpm -ivh xxxx.rpm

Posted by DEVILofDARKNESS on Thu, 28 Oct 2021 10:57:13 -0700