Deploy an image consisting of Tomcat, Java and MySQL using Ansible and Jenkins

In this tutorial, you will use ** Ansible ** and ** Jenkins ** to deploy an image composed of Tomcat, ** Java **, and MySQL on ** Alibaba Cloud **.

Prerequisites

  1. At least one [Jenkins server](https://www.alibabacloud.com/blog/ci%2Fcd-with-jenkins --- part-1% 3A-install-jenkins-on-ubuntu_593717? Spm = a2c65.11461447.0.0.53a46c64aNElRK) is required (for master only or master / slave settings).

Then, after setting up the master / slave (agent) environment of the Jenkins server, check the server status to make sure that the agent is running normally. Especially in my setup, I'm using the master node as the Jenkins server and the agent machine for Ansible. If you wish, you can have only one machine by installing Ansible on the master itself.

image.png

  1. Then install Ansible Tool on the master and agent machines.

  2. Introduce Ansible. Ansible is a powerful tool for IT automation that can be used in the CI / CD process to provision a target environment and deploy applications on it. However, be aware that Ansible is cumbersome to maintain and will reuse scripts in the long run. This is useful if you want to perform the same task across different servers / environments from a centralized location where Ansible is installed. Ansible is completely agentless, so Chef and Puppet It has an advantage over other IaC (Infrastructure-as-Code) tools such as a2c65.11461447.0.0.53a46c64aNElRK). With Ansible, there is no need to install an agent on the client system and automation is done by SSH communication between the client and server. With hundreds of instances in a given region, all automation is easy. Ansible playbooks are written in YAML / YML languages.

When it comes to Ansible, the following knowledge is also required.

--Control node: The machine in charge of the server that Ansible is installed and manages. --Inventory: A file that defines the hosts and host groups on which commands, modules, and tasks in a playbook run. This file can be in one of many formats, depending on your Ansible environment and plugins. Used to create a project-specific inventory file in another location. --Playbook: Part of Ansible's configuration, deployment, orchestration language. You can describe the policy you want to apply to the remote system and the sequence of steps.

Installation of Ansible and Ansible Alicloud modules

First, install the Ansible plugin on Jenkins. To do this, follow the steps below.

  1. Click Manage Jenkins on the dashboard.

image.png

  1. Click Manage Plugins and find the Ansible plugin in the search bar at the top right of the page.

image.png

  1. Select Ansible, click Download now, and install after reboot.

Note: Do not install Ansible Tower, which is not required for the current setup used in this tutorial.

image.png

After installing the Ansible plugin on Jenkins, the next step is to install Ansible on the Client / Agent machine.

Install Ansible on client / agent machine

The Alibaba Ansible module is updated frequently, so please refer to the [link] on GitHub (https://github.com/alibaba/alibaba.alicloud?spm=a2c65.11461447.0.0.53a46c64aNElRK) for the latest version. I recommend it.

  1. Open a terminal window on the agent machine and run the following command.

--If you are using CentOS 7.4, this command is a good choice.

sudo yum check-update; sudo yum install -y gcc libffi-devel python-devel openssl-devel epel-release
sudo yum install -y python-pip python-wheel

--If you are using Ubuntu 16.04 LTS, use this command.

sudo apt-get update && sudo apt-get install -y libssl-dev libffi-dev python-dev python-pip
  1. Enter the following command to install the required package Ansible for Alibaba.
sudo pip install ansible

sudo pip install ansible_alicloud

sudo pip install ansible_alicloud_module_utils

Set up Ansible playbook

I created an Ansible playbook script for provisioning Alibaba ECS here. You can fork the source code and change the value of the parameter to suit your environment.

    alicloud_access_key: <Alibaba Access Key>
    alicloud_secret_key: <Alibaba Secret Key>
    alicloud_region: <Alibaba Region for your resource> e.g. - ap-south-1
    alicloud_zone: <Alibaba Zone for your resource> e.g. ap-south-1a
    password: <New VM Password>
    image: "m-a2d4qmk8v2w9s5wmh0rw"  

Note: I am importing images from Alibaba Application Stacks provided by Zhuyun. Please check here. It consists of Linux, Nginx, MySQL and Jdk-Tomcat (Nginx1.6-jdk1.7-tomcat7-mysql5.5-vsFTPd2.2.2).

Access key and secret generation

Now it's time to generate the access key and secret. To do this, follow the steps below.

  1. To get the access key and secret key, go to Alibaba Cloud Console (https://account.alibabacloud.com/login/login.htm?spm=a2c65.11461447.0.0.53a46c64aNElRK) Then select Resource Access Management (RAM) from the product menu.

image.png

  1. Click the User option in the left navigation pane and select Create User.

image.png

  1. Create a new user and enter your login and display name. Next, in [Access Mode], check [Program Access]. You do not need to provide this user with Console Password Logon access.

image.png

  1. Copy ʻAccessKeyId and ʻAccessKeySecret, which are the values of ʻalicloud_access_key and ʻalicloud_secret_key in the playbook.

Note: If you close the dialog box, the AccessKey information will no longer be available. Therefore, it is important to copy and save this information before closing the dialog box.

image.png

  1. Provide the appropriate permissions to provision resources such as VPCs, V-Switches, security groups, or ECS instances. I provide Administrator Access, which happens to provide full access to Alibaba Cloud services and resources.

image.png

image.png

Alibaba Cloud Regions and Zones

image.png

image.png

image.png

image.png

Note) 1.

  1. To get the complete list, you can use Alibaba Cloud CLI tools. It spits out the JSON wall, so it's also convenient to be on * nix and use the jq tool. Given that, all you need is this short shell script.
#!/usr/bin/env bash

for region in $( aliyun ecs DescribeRegions | jq '.Regions.Region[].RegionId' )
do
    echo $region
    reg=$( echo $region | sed s/\"//g )
    echo '---'
    for zone in $( aliyun ecs DescribeZones --RegionId $reg | jq '.Zones.Zone[].ZoneId' | sort )
    do
        echo $zone
    done
    echo ''
done
  1. If you would like to provide resources to the following regions / zones, please register your real name from this link. ..
alicloud_region: cn-beijing
alicloud_zone: cn-beijing-a

The image ID of each region is shown below (for Linux, Nginx, MySQL, Jdk-Tomcat images). Of course, you need to change the image ID depending on the region.

image.png

Create and deploy Alibaba VM using Jenkins job

To create and deploy an Alibaba VM using a Jenkins job, follow these steps:

  1. From the Jenkins dashboard, select New Item.

image.png

  1. Enter a name, select Freestyle project and click OK.

image.png

  1. (Optional) Below General, please provide a brief description for reference.

image.png

  1. Select the Source Code Control (SCM) tab at the top or scroll down and enter the following information:

SCM: - Git Repository URL:-Changed parameters to GitHub link Example: https://github.com/nadaraj15/alibaba_ansible/ Credentials:-None (public repository, so store credentials in Jenkins if private) Branch specifier (blank for'any'):-* / master

image.png

  1. Next, skip the build trigger for the time being and move to the build environment. There are several settings available when creating a new project. This settings page also gives you the option to add build steps to perform extra actions such as running scripts. Rather, run a shell script to download the Ansible Playbook file from GitHub. Use the following command.
sudo apt-get update  (Optional)
sudo apt-get install -y libssl-dev libffi-dev python-dev python-pip (Optional)
sudo pip install ansible[azure]==2.7.0rc2 (Optional)
sudo apt-get install -y maven (Optional)
cd /usr/bin
sudo wget https://raw.githubusercontent.com/nadaraj15/alibaba_ansible/master/AliVM.yml

image.png

image.png

  1. I would like to add a build step and launch ansible playbook. This step runs ansible playbook. Enter the following information.

--Playbook path: --AliVM.yml (playbook name) --Inventory: --Select "Do not specify Inventory" --Credentials:-Select None (I've embedded the credentials in the deployment file so I can pass them as environment variables)

image.png

image.png

  1. Clean up the post-deployment workspace and add a shell script execution step with the following command.

Note: If jenkins_home is your location, the default path is set to / var / jenkins_home. However, if you have a custom location, you can use that location instead.

sudo rm -rf /var/jenkins_home/workspaces/<workspace_name>

image.png

  1. Add post-built actions as needed. Finally, when all the steps are set, click Save.

image.png

  1. Go to the Jenkins project dashboard and click Build now to manually trigger the build.

image.png

  1. Go to the console output to see the triggered build status. If all resources are provisioned successfully, the output will show a success status.

image.png

image.png

  1. Go to Alibaba Console to see the provisioned Alibaba ECS and configuration.

image.png

image.png

You have now deployed an instance with Tomcat, Java, and MySQL installed. If you enter the public IP address that corresponds to your browser, you can see that the Apache Tomcat page is running because the web server is exposed through port 80 of the VM's public IP.

image.png

References

1、https://www.alibabacloud.com/blog/ci%2Fcd-with-jenkins---part-1%3A-install-jenkins-on-ubuntu_593717 2、https://www.alibabacloud.com/blog/continuous-integration-with-jenkins-on-alibaba-cloud_594512 3、https://www.alibabacloud.com/blog/594449 4、https://github.com/alibaba/ansible-provider 5、https://mohitgoyal.co/2017/02/14/add-linux-slave-node-in-the-jenkins/ 6、https://marketplace.alibabacloud.com/products/56728001/Tomcat_Nginx_My_SQL_Stack_Package_on_Ubuntu-cmjj011399.html?innerSource=search#product-details

Recommended Posts

Deploy an image consisting of Tomcat, Java and MySQL using Ansible and Jenkins
Install Java and Tomcat with Ansible
Automatically deploy Web applications developed in Java using Jenkins [Tomcat application]
Simple installation of nginx and Docker using ansible
Try similar search of Image Search using Java SDK [Search]
[Java10] Be careful of using var and generics together
[Java] Comparison method of character strings and comparison method using regular expressions
A Simple CRUD Sample Using Java Servlet / JSP and MySQL
About an instance of java
Advantages and disadvantages of Java
Automatically deploy a Web application developed in Java using Jenkins [Preparation]