Now it is normal for enterprises to go abroad, and we are faced with more work of server installation in countries and regions. While cloud services have become ubiquitous and SaaS is available for quick use, the need for customization will always exist and a rapid deployment approach will be necessary.
What rapid deployment methods are currently working with?
- Server image – the fastest, but must be in the same supplier, the sea may be different regions with different cloud service providers, can not be achieved.
- Docker — can be quickly deployed by mirroring for complex environments;
- Shell scripts – Can simplify the installation of most complex applications and are suitable for situations where the system environment does not change much, such as using the same version of the system.
Shell scripts don’t work as well as Docker and images, but if you haven’t tried them yet, you can, as a tool, greatly improve your productivity, as we’ve also seen that many Linux installers offer similar one-click install scripts.
Before writing the script, I need to explain the contents of my server software:
- Gitlab-runner integrated deployment
- NodeJS environment
- NodeJS standard runs under the gitlab-runner user
- Use NVM to manage nodeJS versions
- Use pM2 to manage processes
The following is a shell script I used when the game went to sea, and a simple explanation of how to use shell script to deploy quickly:
#! /bin/sh # The first line, which is basically the standard start of all sh scripts
Centos 7.8 # Centos 7.8 #
Hard disk directory: variable declaration
DISK_PATH="/data" # Path to the previously mounted hard disk
#hostname:
HOSTNAME="my_server_name" # server name, easy to distinguish after login
#gitlab-runner:
GITLAB_RUNNER="https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh"
#nodejs version
NODE_VERSION="v8.17.0"
#NVM #NVM installation script
NVM_SCRIPT="https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh"
# # PM2 PM2 version
PM2="pm2"
#pm2 data path: #pm2 log path, we do not want to put on the system disk
PM2_DATA_DIR="pm2Data" DISK_PATH+/PM2_DATA_DIR
echo "Start installing SDK Channel suite ============"
if [ -n "$HOSTNAME" ] -n is not empty
then
echo "Change hostname to:$HOSTNAME"
hostnamectl set-hostname "$HOSTNAME"
else
echo "You can use 'hostnamectl set-hostname newHostname' # to set the current hostname, with immediate effect and restart."
fi
echo "Ready to start, current hostname:"
hostname
# Below is a series of installation and processing
echo "Basic preparation:"
yum install epel-release -y
yum update -y
echo "Install HTOP:"
yum install htop -y
echo "Install ngniX:"rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm yum install nginx -y systemctlenable nginx
systemctl start nginx
touch "/etc/nginx/conf.d/virtual.conf"
echo "Install gitlab - runner."
# For RHEL/CentOS/Fedora
curl -L "$GITLAB_RUNNER" | sudo bash
yum install gitlab-runner -y
echo "alias lg='su -l gitlab-runner'" >> "$HOME/.bashrc"
source "$HOME/.bashrc"
echo Create pm2Data directory:
PM2_FINALDIR="$DISK_PATH/$PM2_DATA_DIR"
if [ ! -d "$PM2_FINALDIR" ]; then #-d specifies whether to create a directory
mkdir "$PM2_FINALDIR"
fi
chown gitlab-runner:gitlab-runner "$PM2_FINALDIR" # change directory owner
# I will generate a new script to the gitlab-runner directory, why do I do that?
Because the user needs to run the service in gitlab-ruuner, the environment is expected to be there for the target user.
The other reason is that installing PM2 as another user will cause problems, so I will run the script twice
echo "Switch user to Install Node suite"
######## nodejs.sh Start########
NODE_SHELL='/home/gitlab-runner/nodejs.sh'
echo "#! /bin/sh # Centos 7.8 # disk directory: #nodejs version NODE_VERSION=\"$NODE_VERSION\"
PM2_FINALDIR=\"$PM2_FINALDIR\"
#NVM
NVM_SCRIPT=\"$NVM_SCRIPT\"
#PM2
PM2=\"$PM2\" echo \" Install NVM \" curl -o-$NVM_SCRIPT| bash echo \ "installation node$NODE_VERSION:\"
source \"\$HOME/.bashrc\"
nvm install $NODE_VERSION
nvm use $NODE_VERSIONEcho \" Pm2:$PM2\"
npm i $PM2-g NPM install --global coffeescript NPM install --global gulp # pm2 PM2_HOME= "$HOME/.pm2" if [-d $PM2_HOME ]; Pm2 directory 'PWD ll mv \$PM2_HOME$PM2_FINALDIR
rm \$PM2_HOME -r
ln -s $PM2_FINALDIR/ \$PM2_HOME else echo -e '. Pm2 directory does not exist 'ln -s$PM2_FINALDIR/ $PM2_HOME fi source \"\$HOME/.bashrc\" echo \"NodeJS package end \" > "$NODE_SHELL"
chown gitlab-runner:gitlab-runner "$NODE_SHELL"
chmod +x "$NODE_SHELL" # let the script run
# sudo -h -u gitlab-runner bash -c "$NODE_SHELL" #
echo "Switch to user gitlab-runner and run./nodejs.sh"
######## nodejs.sh End ########
su -l gitlab-runner Run./nodejs.sh to install the nodejs environment
sudo gitlab-runner register # registered gitlab - runner
echo "Complete"
exit 0
Copy the code
The biggest problem with this script is that we are currently unable to install PM2 in root and will report an error. Of course, it can be solved by research, but what if pM2 is later updated? Is the method not working? So just script to script and run again. Remember to escape strings when coding.
In this way, I was able to quickly install two more service clusters of three. In the middle of the installation, I found that the supplier gave the wrong server, only 3 servers need to be re-installed! Good thing there’s a script, otherwise it’s a disaster!
If you found this article helpful, please like it and retweet it.