Alibaba Cloud provides a comprehensive set of global cloud computing services to power Alibaba’s eCommerce ecosystem and other online businesses. Since 2009, Alibaba Cloud has been one of the leading providers in Artificial Intelligence (AI) and cloud computing. Alibaba Cloud offers cloud computing resources and services to thousands of government organizations, developers, and enterprises in more than 200 regions.
Elastic Compute Service (ECS) is a high-performance, reliable, stable, and scalable Infrastructure as a Service (IaaS) level service offered by Alibaba Cloud. In Alibaba Cloud, Elastic Compute Service eliminates the need to invest in IT hardware upfront and allows the user to quickly scale computing resources on demand. This feature makes ECS more efficient and convenient than physical servers. ECS offers a variety of instance types that suit various business needs and help boost their growth. ECS supports multiple billing methods such as pay-as-you-go, subscription, and purchasing options, such as preemptible instances and reserved instances. This tutorial teaches the steps to automatically deploy applications on an ECS CentOS instance created by AutoScaling.
We can boot CentOS into the following run levels:
- Runlevel 0: the halt runlevel.
- Runlevel 1: causes the system to boot up in a single user mode under which only the root user can log in.
- Runlevel 2: boots the system into the multi-user mode with text-based console login capability. This run level does not start the network.
- Runlevel 3: starts up the system into the multi-user mode with text-based console login capability. This run level will start the network.
- Runlevel 4: This is an undefined run level. Users can configure this run level to provide a custom boot state.
- Runlevel 5: boots the system into the multi-user mode. This run level boots the graphical desktop environment at the end of the process.
- Runlevel 6: reboots the system.
Users can use a Shell script to automatically update or install applications on ECS instances created by Auto Scaling. Users can also use shell scripting to run specific codes on ECS instances. To automate:
- First, add the Shell script to a custom image and configure a command to execute the script when the operating system boots.
- Next, select the custom image in a scaling configuration.
- After an Elastic Compute Service (ECS) instance is created based on the scaling configuration; the Shell script will automatically execute on the ECS instance.
Earlier versions of CentOS and CentOS 6 uses System V init as the initialization system, whereas CentOS 7 and later versions use Systemd as the initialization system. System V init and Systemd are quite diverse in the ways that they operate.
Automatically Deploy Applications on ECS Instance
CentOS 6 and Earlier Version
To configure a Shell script in CentOS 6 and earlier versions to automatically deploy applications on ECS instances, follow the below steps:
1. First, open a terminal and create a Shell script for testing by executing the following commands.
#! /bin/sh # chkconfig: 6 10 90 # description: Test Service echo "hello!"
The above script’s chkconfig command will set the run level and priorities for running the script when the operating system shuts down and boots. The value 6 in the above command indicates run level 6, which means the Shell script will run when the operating system reboots. The value 10 in the chkconfig command indicates the priority for executing the script when the operating system reboots. The value 90 in the above command shows the priority for running the script when the operating system shuts down. Users can use the priority ranges from 0 to 100, where a lower value indicates a higher priority.
> Note: To ensure that the ECS instance is only released after executing the Shell script, replace run level 6 to run level 0 in the above script.
2. Next, move the test script into the /etc/rc.d/init.d/ directory and run the following command. After executing the below command, the script is run each time the operating system reboots.
chkconfig --level 6 test on
For example, users can use the following sample script to install PHPWind automatically. Even after executing the script, users still need to enter the password for logging on to the database. It is recommended to modify the below script as per the user requirements.
cd /tmp echo "phpwind" yum install -y \ unzip \ wget \ httpd \ php \ php-fpm \ php-mysql \ php-mbstring \ php-xml \ php-gd \ php-pear \ php-devel chkconfig php-fpm on \ chkconfig httpd on wget http://pwfiles.oss-cn-hangzhou.aliyuncs.com/com/soft/phpwind_v9.0_utf8.zip \ unzip -d pw phpwind_v9.0_utf8.zip \ mv pw/phpwind_v9.0_utf8/upload/* /var/www/html \ wget http://ess.oss-cn-hangzhou.aliyuncs.com/ossupload_utf8.zip -O ossupload_utf8.zip \ unzip -d ossupload ossupload_utf8.zip \ /bin/cp -rf ossupload/ossupload_utf8/* /var/www/html/src/extensions/ \ chown -R apache:apache /var/www/html service httpd start service php-fpm start echo "Install CloudMonitor" wget http://update2.aegis.aliyun.com/download/quartz_install.sh chmod +x quartz_install.sh bash quartz_install.sh echo "CloudMonitor installed"
CentOS 7 and Later Version
CentOS 7 and later versions use Systemd as the initialization system. After the configuration of the Shell script, it can be run when the system is shut down. To configure a Shell script in CentOS 7 and later versions to automatically deploy applications on ECS instances, follow the below steps:
1. First, open a terminal.
2. Create the run-script-when-shutdown.service file in the /etc/systemd/system directory. Replace run-script-when-shutdown.service in the below command with the desired file name.
3. Copy and paste the following content to the script file. In the below content, replace the ExecStop variable value in the below content with the absolute path of the script to run.
[Unit] Description=service to run script when shutdown After=syslog.target network.target [Service] Type=simple ExecStart=/bin/true ExecStop=/path/to/script/to/run RemainAfterExit=yes [Install] WantedBy=default.target
4. Then, save and exit the file.
5. Finally, start the script by executing the following command:
systemctl enable run-script-when-shutdown systemctl start run-script-when-shutdown
6. If the run-script-when-shutdown service is no longer needed, run the following command to disable the service.
systemctl disable run-script-when-shutdown
This tutorial presents the steps to automatically deploy applications on ECS CentOS instances created by AutoScaling using Shell scripting. Hope this tutorial was helpful, and do reach out to us if you have any query or suggestions.